Skip to content

Commit

Permalink
add: delete cognito-user-pool
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroniscode committed Jul 11, 2023
1 parent 8670e27 commit c9d6a93
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 7 deletions.
31 changes: 31 additions & 0 deletions cmd/delete/cognito.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package delete

import (
"github.com/awslabs/eksdemo/pkg/resource"
"github.com/awslabs/eksdemo/pkg/resource/cognito/userpool"
"github.com/spf13/cobra"
)

var cognitoResources []func() *resource.Resource

func NewCognitoCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cognito",
Short: "Amazon Cognito Resources",
}

// Don't show flag errors for `delete cognito` without a subcommand
cmd.DisableFlagParsing = true

for _, r := range cognitoResources {
cmd.AddCommand(r().NewDeleteCmd())
}

return cmd
}

func init() {
cognitoResources = []func() *resource.Resource{
userpool.New,
}
}
24 changes: 22 additions & 2 deletions cmd/delete.go → cmd/delete/delete.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd
package delete

import (
"github.com/awslabs/eksdemo/pkg/resource"
"github.com/awslabs/eksdemo/pkg/resource/acm_certificate"
"github.com/awslabs/eksdemo/pkg/resource/addon"
"github.com/awslabs/eksdemo/pkg/resource/amg_workspace"
Expand All @@ -21,7 +22,7 @@ import (
"github.com/spf13/cobra"
)

func newCmdDelete() *cobra.Command {
func NewDeleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "delete resource(s)",
Expand All @@ -36,6 +37,8 @@ func newCmdDelete() *cobra.Command {
cmd.AddCommand(amp_workspace.NewResource().NewDeleteCmd())
cmd.AddCommand(cloudformation_stack.NewResource().NewDeleteCmd())
cmd.AddCommand(cluster.NewResource().NewDeleteCmd())
cmd.AddCommand(NewCognitoCmd())
cmd.AddCommand(NewDeleteAliasCmds(cognitoResources, "cognito-")...)
cmd.AddCommand(dns_record.NewResource().NewDeleteCmd())
cmd.AddCommand(ec2_instance.NewResource().NewDeleteCmd())
cmd.AddCommand(fargate_profile.NewResource().NewDeleteCmd())
Expand All @@ -50,3 +53,20 @@ func newCmdDelete() *cobra.Command {

return cmd
}

// This creates alias commands for subcommands under DELETE
func NewDeleteAliasCmds(resList []func() *resource.Resource, prefix string) []*cobra.Command {
cmds := make([]*cobra.Command, 0, len(resList))

for _, res := range resList {
r := res()
r.Command.Name = prefix + r.Command.Name
r.Command.Hidden = true
for i, alias := range r.Command.Aliases {
r.Command.Aliases[i] = prefix + alias
}
cmds = append(cmds, r.NewDeleteCmd())
}

return cmds
}
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/awslabs/eksdemo/cmd/create"
del "github.com/awslabs/eksdemo/cmd/delete"
"github.com/awslabs/eksdemo/cmd/get"
"github.com/awslabs/eksdemo/cmd/install"
"github.com/awslabs/eksdemo/pkg/aws"
Expand Down Expand Up @@ -44,7 +45,7 @@ func init() {

rootCmd.AddCommand(
create.NewCreateCmd(),
newCmdDelete(),
del.NewDeleteCmd(),
get.NewGetCmd(),
install.NewInstallCmd(),
install.NewUninstallCmd(),
Expand Down
12 changes: 12 additions & 0 deletions pkg/aws/cognito_userpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func NewCognitoUserPoolClient() *CognitoUserPoolClient {
return &CognitoUserPoolClient{cognitoidp.NewFromConfig(GetConfig())}
}

// Creates a new Amazon Cognito user pool and sets the password policy for the pool.
func (c *CognitoUserPoolClient) CreateUserPool(name string) (*types.UserPoolType, error) {
input := cognitoidp.CreateUserPoolInput{
PoolName: aws.String(name),
Expand All @@ -29,6 +30,16 @@ func (c *CognitoUserPoolClient) CreateUserPool(name string) (*types.UserPoolType
return result.UserPool, err
}

// Deletes the specified Amazon Cognito user pool.
func (c *CognitoUserPoolClient) DeleteUserPool(id string) error {
_, err := c.Client.DeleteUserPool(context.Background(), &cognitoidp.DeleteUserPoolInput{
UserPoolId: aws.String(id),
})

return err
}

// Returns the configuration information and metadata of the specified user pool.
func (c *CognitoUserPoolClient) DescribeUserPool(id string) (*types.UserPoolType, error) {
result, err := c.Client.DescribeUserPool(context.Background(), &cognitoidp.DescribeUserPoolInput{
UserPoolId: aws.String(id),
Expand All @@ -41,6 +52,7 @@ func (c *CognitoUserPoolClient) DescribeUserPool(id string) (*types.UserPoolType
return result.UserPool, nil
}

// Lists the user pools associated with an AWS account.
func (c *CognitoUserPoolClient) ListUserPools() ([]types.UserPoolDescriptionType, error) {
pools := []types.UserPoolDescriptionType{}
pageNum := 0
Expand Down
3 changes: 2 additions & 1 deletion pkg/resource/cognito/userpool/cognito_userpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

func New() *resource.Resource {
options, createFlags, _ := NewOptions()
options, createFlags, deleteFlags := NewOptions()
res := NewWithOptions(options)
res.CreateFlags = createFlags
res.DeleteFlags = deleteFlags

return res
}
Expand Down
31 changes: 29 additions & 2 deletions pkg/resource/cognito/userpool/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,35 @@ func (m *Manager) Create(options resource.Options) error {
return nil
}

func (m *Manager) Delete(_ resource.Options) error {
return fmt.Errorf("feature not supported")
func (m *Manager) Delete(o resource.Options) error {
options, ok := o.(*Options)
if !ok {
return fmt.Errorf("internal error, unable to cast options to userpool.Options")
}

id := options.Common().Id

if id == "" {
up, err := m.userPoolGetter.GetUserPoolByName(options.UserPoolName)

if err != nil {
var rnfe *resource.NotFoundByNameError
if errors.As(err, &rnfe) {
fmt.Printf("Cognito User Pool with name %q does not exist\n", options.UserPoolName)
return nil
}
return err
}
id = awssdk.ToString(up.Id)
}

err := m.cognitoClient.DeleteUserPool(id)
if err != nil {
return aws.FormatErrorAsMessageOnly(err)
}
fmt.Printf("Cognito User Pool Id %q deleted\n", id)

return nil
}

func (m *Manager) SetDryRun() {
Expand Down
19 changes: 18 additions & 1 deletion pkg/resource/cognito/userpool/options.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package userpool

import (
"fmt"

"github.com/awslabs/eksdemo/pkg/cmd"
"github.com/awslabs/eksdemo/pkg/resource"
"github.com/spf13/cobra"
)

type Options struct {
Expand All @@ -21,7 +24,21 @@ func NewOptions() (options *Options, createFlags, deleteFlags cmd.Flags) {

createFlags = cmd.Flags{}

deleteFlags = cmd.Flags{}
deleteFlags = cmd.Flags{
&cmd.StringFlag{
CommandFlag: cmd.CommandFlag{
Name: "id",
Description: "delete by ID instead",
Validate: func(cmd *cobra.Command, args []string) error {
if options.Id == "" && len(args) == 0 {
return fmt.Errorf("must include either %q argument or %q flag", "NAME", "--id")
}
return nil
},
},
Option: &options.Id,
},
}

return
}
Expand Down

0 comments on commit c9d6a93

Please sign in to comment.