Skip to content

Commit 289d5c4

Browse files
committed
feat: NS and user group idempotent flag
1 parent 1eeab2b commit 289d5c4

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

app/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func isNothingChangedErr(ctx *cli.Context, e error) bool {
4848
if !ok {
4949
return false
5050
}
51-
return s.Code() == codes.InvalidArgument && s.Message() == "nothing to change"
51+
return s.Code() == codes.InvalidArgument && strings.Contains(s.Message(), "nothing to change")
5252
}
5353

5454
func parseAssumedRole(assumedRole string) (string, string, error) {

app/namespace.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ func (c *NamespaceClient) updateNamespace(ctx *cli.Context, n *namespace.Namespa
344344
Spec: n.Spec,
345345
})
346346
if err != nil {
347+
if isNothingChangedErr(ctx, err) {
348+
return nil
349+
}
347350
return err
348351
}
349352

@@ -802,13 +805,20 @@ func NewNamespaceCommand(getNamespaceClientFn GetNamespaceClientFn) (CommandOut,
802805

803806
if enable {
804807
if n.Spec.Lifecycle != nil && n.Spec.Lifecycle.EnableDeleteProtection {
808+
if ctx.Bool(IdempotentFlagName) {
809+
return nil
810+
}
811+
805812
return errors.New("delete protection is already enabled")
806813
}
807814
n.Spec.Lifecycle = &namespace.LifecycleSpec{
808815
EnableDeleteProtection: true,
809816
}
810817
} else {
811818
if n.Spec.Lifecycle == nil || !n.Spec.Lifecycle.EnableDeleteProtection {
819+
if ctx.Bool(IdempotentFlagName) {
820+
return nil
821+
}
812822
return errors.New("delete protection is already disabled")
813823
}
814824
y, err := ConfirmPrompt(ctx, "disabling namespace delete protection may be prone to accidental deletion. confirm?")
@@ -936,6 +946,9 @@ func NewNamespaceCommand(getNamespaceClientFn GetNamespaceClientFn) (CommandOut,
936946
return err
937947
}
938948
if n.Spec.AcceptedClientCa == bundle {
949+
if ctx.Bool(IdempotentFlagName) {
950+
return nil
951+
}
939952
return errors.New("nothing to change")
940953
}
941954
n.Spec.AcceptedClientCa = bundle
@@ -983,6 +996,9 @@ func NewNamespaceCommand(getNamespaceClientFn GetNamespaceClientFn) (CommandOut,
983996
return err
984997
}
985998
if n.Spec.AcceptedClientCa == bundle {
999+
if ctx.Bool(IdempotentFlagName) {
1000+
return nil
1001+
}
9861002
return errors.New("nothing to change")
9871003
}
9881004
n.Spec.AcceptedClientCa = bundle
@@ -1014,6 +1030,9 @@ func NewNamespaceCommand(getNamespaceClientFn GetNamespaceClientFn) (CommandOut,
10141030
return err
10151031
}
10161032
if n.Spec.AcceptedClientCa == cert {
1033+
if ctx.Bool(IdempotentFlagName) {
1034+
return nil
1035+
}
10171036
return errors.New("nothing to change")
10181037
}
10191038
n.Spec.AcceptedClientCa = cert
@@ -1051,6 +1070,9 @@ func NewNamespaceCommand(getNamespaceClientFn GetNamespaceClientFn) (CommandOut,
10511070
return err
10521071
}
10531072
if n.Spec.AuthMethod == authMethod {
1073+
if ctx.Bool(IdempotentFlagName) {
1074+
return nil
1075+
}
10541076
return errors.New("nothing to change")
10551077
}
10561078
if disruptiveChange(n.Spec.AuthMethod, authMethod) {

app/namespace_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (s *NamespaceTestSuite) SetupTest() {
6565
}
6666
flags := []cli.Flag{
6767
AutoConfirmFlag,
68+
IdempotentFlag,
6869
}
6970

7071
s.cliApp, _ = NewTestApp(s.T(), cmds, flags)
@@ -224,6 +225,16 @@ func (s *NamespaceTestSuite) TestDeleteProtection() {
224225
},
225226
expectErr: true,
226227
},
228+
{
229+
name: "no change already enabled, idempotent",
230+
args: []string{"--idempotent", "n", "lc", "set", "-n", ns, "--edp", "true"},
231+
expectGet: func(g *namespaceservice.GetNamespaceResponse) {
232+
g.Namespace.Spec.Lifecycle = &namespace.LifecycleSpec{
233+
EnableDeleteProtection: true,
234+
}
235+
},
236+
expectErr: false,
237+
},
227238
{
228239
name: "no change already disabled",
229240
args: []string{"n", "lc", "set", "-n", ns, "--edp", "false"},

app/user_group.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func nsRoleToAccess(role string) (string, *identity.NamespaceAccess) {
127127
}
128128

129129
// setAccess sets the access for a group using the cloud services API
130-
func (c *UserGroupClient) setAccess(_ *cli.Context, groupID string, accountRole string, nsRoles []string) error {
130+
func (c *UserGroupClient) setAccess(ctx *cli.Context, groupID string, accountRole string, nsRoles []string) error {
131131
group, err := c.client.GetUserGroup(c.ctx, &cloudsvc.GetUserGroupRequest{
132132
GroupId: groupID,
133133
})
@@ -162,6 +162,9 @@ func (c *UserGroupClient) setAccess(_ *cli.Context, groupID string, accountRole
162162

163163
resp, err := c.client.UpdateUserGroup(c.ctx, req)
164164
if err != nil {
165+
if isNothingChangedErr(ctx, err) {
166+
return nil
167+
}
165168
return err
166169
}
167170

0 commit comments

Comments
 (0)