forked from hashicorp/terraform-provider-aws
/
resource_aws_cognito_user_pool_domain.go
194 lines (168 loc) · 5.09 KB
/
resource_aws_cognito_user_pool_domain.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsCognitoUserPoolDomain() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCognitoUserPoolDomainCreate,
Read: resourceAwsCognitoUserPoolDomainRead,
Delete: resourceAwsCognitoUserPoolDomainDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 63),
},
"certificate_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateArn,
},
"user_pool_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"aws_account_id": {
Type: schema.TypeString,
Computed: true,
},
"cloudfront_distribution_arn": {
Type: schema.TypeString,
Computed: true,
},
"s3_bucket": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsCognitoUserPoolDomainCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoidpconn
domain := d.Get("domain").(string)
timeout := 1 * time.Minute //Default timeout for a basic domain
params := &cognitoidentityprovider.CreateUserPoolDomainInput{
Domain: aws.String(domain),
UserPoolId: aws.String(d.Get("user_pool_id").(string)),
}
if v, ok := d.GetOk("certificate_arn"); ok {
customDomainConfig := &cognitoidentityprovider.CustomDomainConfigType{
CertificateArn: aws.String(v.(string)),
}
params.CustomDomainConfig = customDomainConfig
timeout = 60 * time.Minute //Custom domains take more time to become active
}
log.Printf("[DEBUG] Creating Cognito User Pool Domain: %s", params)
_, err := conn.CreateUserPoolDomain(params)
if err != nil {
return fmt.Errorf("Error creating Cognito User Pool Domain: %s", err)
}
d.SetId(domain)
stateConf := resource.StateChangeConf{
Pending: []string{
cognitoidentityprovider.DomainStatusTypeCreating,
cognitoidentityprovider.DomainStatusTypeUpdating,
},
Target: []string{
cognitoidentityprovider.DomainStatusTypeActive,
},
MinTimeout: 1 * time.Minute,
Timeout: timeout,
Refresh: func() (interface{}, string, error) {
domain, err := conn.DescribeUserPoolDomain(&cognitoidentityprovider.DescribeUserPoolDomainInput{
Domain: aws.String(d.Get("domain").(string)),
})
if err != nil {
return 42, "", err
}
desc := domain.DomainDescription
return domain, *desc.Status, nil
},
}
_, err = stateConf.WaitForState()
if err != nil {
return err
}
return resourceAwsCognitoUserPoolDomainRead(d, meta)
}
func resourceAwsCognitoUserPoolDomainRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoidpconn
log.Printf("[DEBUG] Reading Cognito User Pool Domain: %s", d.Id())
domain, err := conn.DescribeUserPoolDomain(&cognitoidentityprovider.DescribeUserPoolDomainInput{
Domain: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, "ResourceNotFoundException", "") {
log.Printf("[WARN] Cognito User Pool Domain %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
desc := domain.DomainDescription
d.Set("domain", d.Id())
d.Set("certificate_arn", "")
if desc.CustomDomainConfig != nil {
d.Set("certificate_arn", desc.CustomDomainConfig.CertificateArn)
}
d.Set("aws_account_id", desc.AWSAccountId)
d.Set("cloudfront_distribution_arn", desc.CloudFrontDistribution)
d.Set("s3_bucket", desc.S3Bucket)
d.Set("user_pool_id", desc.UserPoolId)
d.Set("version", desc.Version)
return nil
}
func resourceAwsCognitoUserPoolDomainDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoidpconn
log.Printf("[DEBUG] Deleting Cognito User Pool Domain: %s", d.Id())
_, err := conn.DeleteUserPoolDomain(&cognitoidentityprovider.DeleteUserPoolDomainInput{
Domain: aws.String(d.Id()),
UserPoolId: aws.String(d.Get("user_pool_id").(string)),
})
if err != nil {
return err
}
stateConf := resource.StateChangeConf{
Pending: []string{
cognitoidentityprovider.DomainStatusTypeUpdating,
cognitoidentityprovider.DomainStatusTypeDeleting,
},
Target: []string{""},
Timeout: 1 * time.Minute,
Refresh: func() (interface{}, string, error) {
domain, err := conn.DescribeUserPoolDomain(&cognitoidentityprovider.DescribeUserPoolDomainInput{
Domain: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, "ResourceNotFoundException", "") {
return 42, "", nil
}
return 42, "", err
}
desc := domain.DomainDescription
if desc.Status == nil {
return 42, "", nil
}
return domain, *desc.Status, nil
},
}
_, err = stateConf.WaitForState()
return err
}