Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add namespace delete protection #404

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions app/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const (
codecPassAccessTokenFlagName = "pass-access-token"
codecIncludeCredentialsFlagName = "include-credentials"
sinkRegionFlagName = "region"
enableDeleteProtectionFlagName = "enable-delete-protection"
disableDeleteProtectionFlagName = "disable-delete-protection"
)

const (
Expand Down Expand Up @@ -514,6 +516,11 @@ func NewNamespaceCommand(getNamespaceClientFn GetNamespaceClientFn) (CommandOut,
Usage: fmt.Sprintf("Flag can be used multiple times; value must be \"email=permission\"; valid permissions are: %v", getNamespacePermissionTypes()),
Aliases: []string{"p"},
},
&cli.BoolFlag{
Name: enableDeleteProtectionFlagName,
Usage: "Enable delete protection on the namespace",
Aliases: []string{"edp"},
},
codecEndpointFlag,
codecPassAccessTokenFlag,
codecIncludeCredentialsFlag,
Expand Down Expand Up @@ -622,6 +629,10 @@ func NewNamespaceCommand(getNamespaceClientFn GetNamespaceClientFn) (CommandOut,
}
}

n.Spec.DeleteProtection = &namespace.DeleteProtectionSpec{
EnableDeleteProtection: ctx.Bool(enableDeleteProtectionFlagName),
}

return c.createNamespace(n, unp)
},
},
Expand Down Expand Up @@ -652,6 +663,65 @@ func NewNamespaceCommand(getNamespaceClientFn GetNamespaceClientFn) (CommandOut,
return c.addRegion(ctx)
},
},
{
Name: "delete-protection",
Usage: "Enable delete protection on a temporal namespace",
Aliases: []string{"dp"},
Flags: []cli.Flag{
RequestIDFlag,
ResourceVersionFlag,
&cli.StringFlag{
Name: NamespaceFlagName,
Usage: "The namespace hosted on temporal cloud",
Aliases: []string{"n"},
Required: true,
},
&cli.BoolFlag{
Name: enableDeleteProtectionFlagName,
Usage: "Enable delete protection on the namespace",
Aliases: []string{"edp"},
},
&cli.BoolFlag{
Name: disableDeleteProtectionFlagName,
Usage: "Enable delete protection on the namespace",
Aliases: []string{"ddp"},
},
},
Action: func(ctx *cli.Context) error {
enable := ctx.Bool(enableDeleteProtectionFlagName)
disable := ctx.Bool(disableDeleteProtectionFlagName)
if !enable && !disable {
return errors.New("either enable or disable delete protection is required")
}
if enable && disable {
return errors.New("cannot set both enable and disable delete protection flags")
}

namespaceName := ctx.String(NamespaceFlagName)
n, err := c.getNamespace(namespaceName)
if err != nil {
return err
}

if enable {
if n.Spec.DeleteProtection != nil && n.Spec.DeleteProtection.EnableDeleteProtection {
return errors.New("delete protection is already enabled")
}
n.Spec.DeleteProtection = &namespace.DeleteProtectionSpec{
EnableDeleteProtection: true,
}
} else if disable {
if n.Spec.DeleteProtection == nil || !n.Spec.DeleteProtection.EnableDeleteProtection {
return errors.New("delete protection is already disabled")
}
n.Spec.DeleteProtection = &namespace.DeleteProtectionSpec{
EnableDeleteProtection: false,
}
}

return c.updateNamespace(ctx, n)
},
},
{
Name: "delete",
Usage: "Delete a temporal namespace",
Expand Down
110 changes: 110 additions & 0 deletions app/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,116 @@ func (s *NamespaceTestSuite) TestList() {
s.NoError(s.RunCmd("namespace", "list"))
}

func (s *NamespaceTestSuite) TestDeleteProtection() {
ns := "ns1"
type morphGetResp func(*namespaceservice.GetNamespaceResponse)
type morphUpdateReq func(*namespaceservice.UpdateNamespaceRequest)

tests := []struct {
name string
args []string
expectGet morphGetResp
expectErr bool
expectUpdate morphUpdateReq
}{
{
name: "help",
args: []string{"namespace", "delete-protection"},
expectErr: true,
},
{
name: "no args",
args: []string{"namespace", "delete-protection", "enable-delete-protection"},
expectErr: true,
},
{
name: "alias with no args",
args: []string{"n", "dp"},
expectErr: true,
},
{
name: "missing bool flag",
args: []string{"n", "dp", "-n", ns},
expectErr: true,
},
{
name: "both bool flags incorrectly set",
args: []string{"n", "dp", "-n", ns, "--edp", "--ddp"},
expectErr: true,
},
{
name: "success enable",
args: []string{"n", "dp", "-n", ns, "--edp"},
expectGet: func(g *namespaceservice.GetNamespaceResponse) {},
expectUpdate: func(r *namespaceservice.UpdateNamespaceRequest) {
r.Spec.DeleteProtection = &namespace.DeleteProtectionSpec{
EnableDeleteProtection: true,
}
},
},
{
name: "no change already disabled",
args: []string{"n", "dp", "-n", ns, "--ddp"},
expectGet: func(g *namespaceservice.GetNamespaceResponse) {},
expectErr: true,
},
{
name: "missing namespace",
args: []string{"n", "dp", "-n", ns, "--edp"},
expectGet: func(g *namespaceservice.GetNamespaceResponse) {
g.Namespace = nil
},
expectErr: true,
},
}

for _, tc := range tests {
s.Run(tc.name, func() {
getResp := namespaceservice.GetNamespaceResponse{
Namespace: &namespace.Namespace{
Namespace: ns,
Spec: &namespace.NamespaceSpec{
SearchAttributes: map[string]namespace.SearchAttributeType{
"attr1": namespace.SEARCH_ATTRIBUTE_TYPE_BOOL,
},
RetentionDays: 7,
AuthMethod: namespace.AUTH_METHOD_RESTRICTED,
},
State: namespace.STATE_ACTIVE,
ResourceVersion: "ver1",
},
}
if tc.expectGet != nil {
tc.expectGet(&getResp)
s.mockService.EXPECT().GetNamespace(gomock.Any(), &namespaceservice.GetNamespaceRequest{
Namespace: ns,
}).Return(&getResp, nil).Times(1)
}

if tc.expectUpdate != nil {
spec := *(getResp.Namespace.Spec)
req := namespaceservice.UpdateNamespaceRequest{
Namespace: ns,
Spec: &spec,
ResourceVersion: getResp.Namespace.ResourceVersion,
}
tc.expectUpdate(&req)
s.mockService.EXPECT().UpdateNamespace(gomock.Any(), &req).
Return(&namespaceservice.UpdateNamespaceResponse{
RequestStatus: &request.RequestStatus{},
}, nil).Times(1)
}

err := s.RunCmd(tc.args...)
if tc.expectErr {
s.Error(err)
} else {
s.NoError(err)
}
})
}
}

func (s *NamespaceTestSuite) TestUpdateAuthMethod() {
ns := "ns1"
type morphGetResp func(*namespaceservice.GetNamespaceResponse)
Expand Down
Loading
Loading