Skip to content

Commit 194741b

Browse files
Add update-codec-server (#216)
* update codec server * nit fix * update not import * https fix
1 parent 5652a92 commit 194741b

2 files changed

Lines changed: 176 additions & 2 deletions

File tree

app/namespace.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,63 @@ func NewNamespaceCommand(getNamespaceClientFn GetNamespaceClientFn) (CommandOut,
829829
},
830830
},
831831
},
832+
{
833+
Name: "update-codec-server",
834+
Usage: "Update codec server config used to decode encoded payloads through remote endpoint",
835+
Aliases: []string{"ucs"},
836+
Flags: []cli.Flag{
837+
NamespaceFlag,
838+
&cli.StringFlag{
839+
Name: "endpoint",
840+
Usage: "The codec server endpoint to decode payloads for all users interacting with this Namespace, must be https",
841+
Aliases: []string{"e"},
842+
Required: true,
843+
},
844+
&cli.BoolFlag{
845+
Name: "pass-access-token",
846+
Usage: "Pass the user access token with the remote endpoint",
847+
Aliases: []string{"pat"},
848+
},
849+
&cli.BoolFlag{
850+
Name: "include-credentials",
851+
Usage: "Include cross-origin credentials",
852+
Aliases: []string{"ic"},
853+
},
854+
},
855+
Action: func(ctx *cli.Context) error {
856+
n, err := c.getNamespace(ctx.String(NamespaceFlagName))
857+
if err != nil {
858+
return err
859+
}
860+
861+
replacement := &namespace.CodecServerPropertySpec{
862+
Endpoint: ctx.String("endpoint"),
863+
PassAccessToken: ctx.Bool("pass-access-token"),
864+
IncludeCredentials: ctx.Bool("include-credentials"),
865+
}
866+
867+
difference, err := compareCodecSpec(n.Spec.CodecSpec, replacement)
868+
if err != nil {
869+
return err
870+
}
871+
872+
fmt.Println("this update will result in the following changes to the codec server config:")
873+
fmt.Println(difference)
874+
875+
confirmed, err := ConfirmPrompt(ctx, "confirm codec server update operation")
876+
if err != nil {
877+
return err
878+
}
879+
880+
if confirmed {
881+
n.Spec.CodecSpec = replacement
882+
return c.updateNamespace(ctx, n)
883+
}
884+
885+
fmt.Println("operation canceled")
886+
return nil
887+
},
888+
},
832889
{
833890
Name: "retention",
834891
Usage: "Manages configuration of the length of time (in days) a closed workflow will be preserved before deletion",
@@ -1071,6 +1128,20 @@ func compareCertificateFilters(existing, replacement certificateFiltersConfig) (
10711128
return diff.Diff(string(existingBytes), string(replacementBytes)), nil
10721129
}
10731130

1131+
func compareCodecSpec(existing, replacement *namespace.CodecServerPropertySpec) (string, error) {
1132+
existingBytes, err := FormatJson(existing)
1133+
if err != nil {
1134+
return "", err
1135+
}
1136+
1137+
replacementBytes, err := FormatJson(replacement)
1138+
if err != nil {
1139+
return "", err
1140+
}
1141+
1142+
return diff.Diff(string(existingBytes), string(replacementBytes)), nil
1143+
}
1144+
10741145
func validateNamespaceRegion(region string) error {
10751146
for _, r := range namespaceRegions {
10761147
if r == region {

app/namespace_test.go

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"context"
55
"encoding/base64"
66
"errors"
7-
"github.com/temporalio/tcld/protogen/api/auth/v1"
8-
"github.com/temporalio/tcld/protogen/api/authservice/v1"
97
"os"
108
"strings"
119
"testing"
1210

11+
"github.com/temporalio/tcld/protogen/api/auth/v1"
12+
"github.com/temporalio/tcld/protogen/api/authservice/v1"
13+
1314
"github.com/golang/mock/gomock"
1415
"github.com/stretchr/testify/suite"
1516
"github.com/temporalio/tcld/protogen/api/namespace/v1"
@@ -1063,6 +1064,108 @@ func (s *NamespaceTestSuite) TestClearCertificateFilters() {
10631064
}
10641065
}
10651066

1067+
func (s *NamespaceTestSuite) TestUpdateCodecServer() {
1068+
ns := "ns1"
1069+
type morphGetResp func(*namespaceservice.GetNamespaceResponse)
1070+
type morphUpdateReq func(*namespaceservice.UpdateNamespaceRequest)
1071+
1072+
tests := []struct {
1073+
args []string
1074+
expectGet morphGetResp
1075+
expectErr bool
1076+
expectUpdate morphUpdateReq
1077+
}{
1078+
{
1079+
1080+
args: []string{"namespace", "update-codec-server"},
1081+
expectErr: true,
1082+
},
1083+
{
1084+
args: []string{"namespace", "update-codec-server", "--namespace", ns},
1085+
expectErr: true,
1086+
},
1087+
{
1088+
args: []string{"n", "ucs", "-n", ns, "-endpoint", "fakehost:9999"},
1089+
expectGet: func(g *namespaceservice.GetNamespaceResponse) {},
1090+
expectUpdate: func(r *namespaceservice.UpdateNamespaceRequest) {
1091+
r.Spec.CodecSpec = &namespace.CodecServerPropertySpec{Endpoint: "fakehost:9999"}
1092+
},
1093+
},
1094+
{
1095+
args: []string{"n", "ucs", "-n", ns, "-e", "fakehost:9999", "--pass-access-token"},
1096+
expectGet: func(g *namespaceservice.GetNamespaceResponse) {},
1097+
expectUpdate: func(r *namespaceservice.UpdateNamespaceRequest) {
1098+
r.Spec.CodecSpec = &namespace.CodecServerPropertySpec{
1099+
Endpoint: "fakehost:9999",
1100+
PassAccessToken: true,
1101+
}
1102+
},
1103+
},
1104+
{
1105+
args: []string{"n", "ucs", "-n", ns, "-e", "fakehost:9999", "--pat", "--include-credentials"},
1106+
expectGet: func(g *namespaceservice.GetNamespaceResponse) {},
1107+
expectUpdate: func(r *namespaceservice.UpdateNamespaceRequest) {
1108+
r.Spec.CodecSpec = &namespace.CodecServerPropertySpec{
1109+
Endpoint: "fakehost:9999",
1110+
PassAccessToken: true,
1111+
IncludeCredentials: true,
1112+
}
1113+
},
1114+
},
1115+
}
1116+
1117+
for _, tc := range tests {
1118+
s.Run(strings.Join(tc.args, " "), func() {
1119+
getResp := namespaceservice.GetNamespaceResponse{
1120+
Namespace: &namespace.Namespace{
1121+
Namespace: ns,
1122+
Spec: &namespace.NamespaceSpec{
1123+
AcceptedClientCa: "cert1",
1124+
SearchAttributes: map[string]namespace.SearchAttributeType{
1125+
"attr1": namespace.SEARCH_ATTRIBUTE_TYPE_BOOL,
1126+
},
1127+
RetentionDays: 7,
1128+
CertificateFilters: []*namespace.CertificateFilterSpec{
1129+
{
1130+
CommonName: "test0",
1131+
},
1132+
},
1133+
},
1134+
State: namespace.STATE_ACTIVE,
1135+
ResourceVersion: "ver1",
1136+
},
1137+
}
1138+
if tc.expectGet != nil {
1139+
tc.expectGet(&getResp)
1140+
s.mockService.EXPECT().GetNamespace(gomock.Any(), &namespaceservice.GetNamespaceRequest{
1141+
Namespace: ns,
1142+
}).Return(&getResp, nil).Times(1)
1143+
}
1144+
1145+
if tc.expectUpdate != nil {
1146+
spec := *(getResp.Namespace.Spec)
1147+
req := namespaceservice.UpdateNamespaceRequest{
1148+
Namespace: ns,
1149+
Spec: &spec,
1150+
ResourceVersion: getResp.Namespace.ResourceVersion,
1151+
}
1152+
tc.expectUpdate(&req)
1153+
s.mockService.EXPECT().UpdateNamespace(gomock.Any(), &req).
1154+
Return(&namespaceservice.UpdateNamespaceResponse{
1155+
RequestStatus: &request.RequestStatus{},
1156+
}, nil).Times(1)
1157+
}
1158+
1159+
err := s.RunCmd(tc.args...)
1160+
if tc.expectErr {
1161+
s.Error(err)
1162+
} else {
1163+
s.NoError(err)
1164+
}
1165+
})
1166+
}
1167+
}
1168+
10661169
func (s *NamespaceTestSuite) TestUpdateNamespaceRetention() {
10671170

10681171
ns := "ns1"

0 commit comments

Comments
 (0)