Skip to content

Commit

Permalink
add: get cognito-domain (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroniscode committed Jul 12, 2023
1 parent bfe58c2 commit 403dc49
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/get/cognito.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -26,6 +27,7 @@ func NewGetCognitoCmd() *cobra.Command {

func init() {
cognitoResources = []func() *resource.Resource{
domain.New,
userpool.New,
}
}
13 changes: 13 additions & 0 deletions pkg/aws/cognito_userpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
29 changes: 29 additions & 0 deletions pkg/resource/cognito/domain/cognito_domain.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
33 changes: 33 additions & 0 deletions pkg/resource/cognito/domain/get.go
Original file line number Diff line number Diff line change
@@ -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))
}
31 changes: 31 additions & 0 deletions pkg/resource/cognito/domain/options.go
Original file line number Diff line number Diff line change
@@ -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
}
44 changes: 44 additions & 0 deletions pkg/resource/cognito/domain/printer.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 403dc49

Please sign in to comment.