/
get.go
108 lines (86 loc) · 2.7 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package client
import (
"fmt"
"os"
"strings"
awssdk "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider/types"
"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(name string, output printer.Output, o resource.Options) error {
options, ok := o.(*Options)
if !ok {
return fmt.Errorf("internal error, unable to cast options to client.Options")
}
var appClient *types.UserPoolClientType
var appClients []*types.UserPoolClientType
var err error
switch {
case name != "":
appClient, err = g.GetAppClientByName(name, options.UserPoolID)
appClients = []*types.UserPoolClientType{appClient}
case options.AppClientID != "":
appClient, err = g.GetAppClientByID(options.AppClientID, options.UserPoolID)
appClients = []*types.UserPoolClientType{appClient}
default:
appClients, err = g.GetAllAppClients(options.UserPoolID)
}
if err != nil {
return err
}
return output.Print(os.Stdout, NewPrinter(appClients))
}
func (g *Getter) GetAllAppClients(userPoolID string) ([]*types.UserPoolClientType, error) {
appClientDescriptions, err := g.cognitoClient.ListUserPoolClients(userPoolID)
if err != nil {
return nil, err
}
appClients := make([]*types.UserPoolClientType, 0, len(appClientDescriptions))
for _, acd := range appClientDescriptions {
result, err := g.cognitoClient.DescribeUserPoolClient(awssdk.ToString(acd.ClientId), userPoolID)
if err != nil {
return nil, err
}
appClients = append(appClients, result)
}
return appClients, nil
}
func (g *Getter) GetAppClientByID(appClientID, userPoolID string) (*types.UserPoolClientType, error) {
appClient, err := g.cognitoClient.DescribeUserPoolClient(appClientID, userPoolID)
if err != nil {
return nil, err
}
return appClient, nil
}
func (g *Getter) GetAppClientByName(name, userPoolID string) (*types.UserPoolClientType, error) {
appClients, err := g.GetAllAppClients(userPoolID)
if err != nil {
return nil, err
}
found := []*types.UserPoolClientType{}
for _, ac := range appClients {
if strings.EqualFold(name, awssdk.ToString(ac.ClientName)) {
found = append(found, ac)
}
}
if len(found) == 0 {
return nil, &resource.NotFoundByNameError{Type: "cognito-app-client", Name: name}
}
if len(found) > 1 {
return nil, fmt.Errorf("multiple cognito app clients found with name: %s", name)
}
return found[0], nil
}