forked from hashicorp/terraform-provider-aws
/
data_source_aws_cognito_user_pools.go
96 lines (83 loc) · 2.26 KB
/
data_source_aws_cognito_user_pools.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
package aws
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsCognitoUserPools() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsCognitoUserPoolsRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"arns": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceAwsCognitoUserPoolsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoidpconn
name := d.Get("name").(string)
var ids []string
var arns []string
pools, err := getAllCognitoUserPools(conn)
if err != nil {
return fmt.Errorf("Error listing cognito user pools: %s", err)
}
for _, pool := range pools {
if name == aws.StringValue(pool.Name) {
id := aws.StringValue(pool.Id)
arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "cognito-idp",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("userpool/%s", id),
}.String()
ids = append(ids, id)
arns = append(arns, arn)
}
}
if len(ids) == 0 {
return fmt.Errorf("No cognito user pool found with name: %s", name)
}
d.SetId(name)
d.Set("ids", ids)
d.Set("arns", arns)
return nil
}
func getAllCognitoUserPools(conn *cognitoidentityprovider.CognitoIdentityProvider) ([]*cognitoidentityprovider.UserPoolDescriptionType, error) {
var pools []*cognitoidentityprovider.UserPoolDescriptionType
var nextToken string
for {
input := &cognitoidentityprovider.ListUserPoolsInput{
// MaxResults Valid Range: Minimum value of 1. Maximum value of 60
MaxResults: aws.Int64(int64(60)),
}
if nextToken != "" {
input.NextToken = aws.String(nextToken)
}
out, err := conn.ListUserPools(input)
if err != nil {
return pools, err
}
pools = append(pools, out.UserPools...)
if out.NextToken == nil {
break
}
nextToken = aws.StringValue(out.NextToken)
}
return pools, nil
}