diff --git a/cmd/get/cognito.go b/cmd/get/cognito.go index 0495cd6..dfb9246 100644 --- a/cmd/get/cognito.go +++ b/cmd/get/cognito.go @@ -2,6 +2,7 @@ package get import ( "github.com/awslabs/eksdemo/pkg/resource" + "github.com/awslabs/eksdemo/pkg/resource/cognito/domain" "github.com/awslabs/eksdemo/pkg/resource/cognito/userpool" "github.com/spf13/cobra" ) @@ -26,6 +27,7 @@ func NewGetCognitoCmd() *cobra.Command { func init() { cognitoResources = []func() *resource.Resource{ + domain.New, userpool.New, } } diff --git a/pkg/aws/cognito_userpool.go b/pkg/aws/cognito_userpool.go index 958a5aa..b0e4e1c 100644 --- a/pkg/aws/cognito_userpool.go +++ b/pkg/aws/cognito_userpool.go @@ -52,6 +52,19 @@ func (c *CognitoUserPoolClient) DescribeUserPool(id string) (*types.UserPoolType return result.UserPool, nil } +// Gets information about a domain. +func (c *CognitoUserPoolClient) DescribeUserPooDomainl(domain string) (*types.DomainDescriptionType, error) { + result, err := c.Client.DescribeUserPoolDomain(context.Background(), &cognitoidp.DescribeUserPoolDomainInput{ + Domain: aws.String(domain), + }) + + if err != nil { + return nil, err + } + + return result.DomainDescription, nil +} + // Lists the user pools associated with an AWS account. func (c *CognitoUserPoolClient) ListUserPools() ([]types.UserPoolDescriptionType, error) { pools := []types.UserPoolDescriptionType{} diff --git a/pkg/resource/cognito/domain/cognito_domain.go b/pkg/resource/cognito/domain/cognito_domain.go new file mode 100644 index 0000000..5bf0012 --- /dev/null +++ b/pkg/resource/cognito/domain/cognito_domain.go @@ -0,0 +1,29 @@ +package domain + +import ( + "github.com/awslabs/eksdemo/pkg/cmd" + "github.com/awslabs/eksdemo/pkg/resource" +) + +func New() *resource.Resource { + options, createFlags, deleteFlags := NewOptions() + res := NewWithOptions(options) + res.CreateFlags = createFlags + res.DeleteFlags = deleteFlags + + return res +} + +func NewWithOptions(options *Options) *resource.Resource { + return &resource.Resource{ + Command: cmd.Command{ + Name: "domain", + Description: "Cognito User Pool Domain", + Args: []string{"DOMAIN"}, + }, + + Getter: &Getter{}, + + Options: options, + } +} diff --git a/pkg/resource/cognito/domain/get.go b/pkg/resource/cognito/domain/get.go new file mode 100644 index 0000000..00309b9 --- /dev/null +++ b/pkg/resource/cognito/domain/get.go @@ -0,0 +1,33 @@ +package domain + +import ( + "os" + + "github.com/awslabs/eksdemo/pkg/aws" + "github.com/awslabs/eksdemo/pkg/printer" + "github.com/awslabs/eksdemo/pkg/resource" +) + +type Getter struct { + cognitoClient *aws.CognitoUserPoolClient +} + +func NewGetter(cognitoClient *aws.CognitoUserPoolClient) *Getter { + return &Getter{cognitoClient} +} + +func (g *Getter) Init() { + if g.cognitoClient == nil { + g.cognitoClient = aws.NewCognitoUserPoolClient() + } +} + +func (g *Getter) Get(domain string, output printer.Output, _ resource.Options) error { + userPoolDomain, err := g.cognitoClient.DescribeUserPooDomainl(domain) + + if err != nil { + return err + } + + return output.Print(os.Stdout, NewPrinter(userPoolDomain)) +} diff --git a/pkg/resource/cognito/domain/options.go b/pkg/resource/cognito/domain/options.go new file mode 100644 index 0000000..bf45985 --- /dev/null +++ b/pkg/resource/cognito/domain/options.go @@ -0,0 +1,31 @@ +package domain + +import ( + "github.com/awslabs/eksdemo/pkg/cmd" + "github.com/awslabs/eksdemo/pkg/resource" +) + +type Options struct { + resource.CommonOptions + DomainName string +} + +func NewOptions() (options *Options, createFlags, deleteFlags cmd.Flags) { + options = &Options{ + CommonOptions: resource.CommonOptions{ + Name: "cognito-domain", + ClusterFlagDisabled: true, + GetArgumentRequired: true, + }, + } + + createFlags = cmd.Flags{} + + deleteFlags = cmd.Flags{} + + return +} + +func (o *Options) SetName(name string) { + o.DomainName = name +} diff --git a/pkg/resource/cognito/domain/printer.go b/pkg/resource/cognito/domain/printer.go new file mode 100644 index 0000000..74230d4 --- /dev/null +++ b/pkg/resource/cognito/domain/printer.go @@ -0,0 +1,44 @@ +package domain + +import ( + "io" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider/types" + "github.com/awslabs/eksdemo/pkg/printer" +) + +type Printer struct { + domain *types.DomainDescriptionType +} + +func NewPrinter(domain *types.DomainDescriptionType) *Printer { + return &Printer{domain} +} + +func (p *Printer) PrintTable(writer io.Writer) error { + table := printer.NewTablePrinter() + table.SetHeader([]string{"Status", "Domain", "User Pool Id"}) + + // DescribeUserPoolDomain doesn't appear to return a ResourceNotFoundException + // So we only print out the result if the domain is not nil + if p.domain.Domain != nil { + table.AppendRow([]string{ + string(p.domain.Status), + aws.ToString(p.domain.Domain), + aws.ToString(p.domain.UserPoolId), + }) + } + + table.Print(writer) + + return nil +} + +func (p *Printer) PrintJSON(writer io.Writer) error { + return printer.EncodeJSON(writer, p.domain) +} + +func (p *Printer) PrintYAML(writer io.Writer) error { + return printer.EncodeYAML(writer, p.domain) +}