forked from hashicorp/terraform-provider-aws
/
resource_aws_cognito_identity_pool_roles_attachment.go
262 lines (223 loc) · 8.55 KB
/
resource_aws_cognito_identity_pool_roles_attachment.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/cognitoidentity"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsCognitoIdentityPoolRolesAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCognitoIdentityPoolRolesAttachmentCreate,
Read: resourceAwsCognitoIdentityPoolRolesAttachmentRead,
Update: resourceAwsCognitoIdentityPoolRolesAttachmentUpdate,
Delete: resourceAwsCognitoIdentityPoolRolesAttachmentDelete,
Schema: map[string]*schema.Schema{
"identity_pool_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"role_mapping": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"identity_provider": {
Type: schema.TypeString,
Required: true,
},
"ambiguous_role_resolution": {
Type: schema.TypeString,
Optional: true, // Required if Type equals Token or Rules.
ValidateFunc: validation.StringInSlice([]string{
cognitoidentity.AmbiguousRoleResolutionTypeAuthenticatedRole,
cognitoidentity.AmbiguousRoleResolutionTypeDeny,
}, false),
},
"mapping_rule": {
Type: schema.TypeList,
Optional: true,
MaxItems: 25,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"claim": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateCognitoRoleMappingsRulesClaim,
},
"match_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
cognitoidentity.MappingRuleMatchTypeEquals,
cognitoidentity.MappingRuleMatchTypeContains,
cognitoidentity.MappingRuleMatchTypeStartsWith,
cognitoidentity.MappingRuleMatchTypeNotEqual,
}, false),
},
"role_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
"value": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 128),
},
},
},
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
cognitoidentity.RoleMappingTypeToken,
cognitoidentity.RoleMappingTypeRules,
}, false),
},
},
},
},
"roles": {
Type: schema.TypeMap,
Required: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"authenticated": {
Type: schema.TypeString,
ValidateFunc: validateArn,
Optional: true, // Required if unauthenticated isn't defined.
},
"unauthenticated": {
Type: schema.TypeString,
ValidateFunc: validateArn,
Optional: true, // Required if authenticated isn't defined.
},
},
},
},
},
}
}
func resourceAwsCognitoIdentityPoolRolesAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoconn
// Validates role keys to be either authenticated or unauthenticated,
// since ValidateFunc validates only the value not the key.
if errors := validateCognitoRoles(d.Get("roles").(map[string]interface{})); len(errors) > 0 {
return fmt.Errorf("Error validating Roles: %v", errors)
}
params := &cognitoidentity.SetIdentityPoolRolesInput{
IdentityPoolId: aws.String(d.Get("identity_pool_id").(string)),
Roles: expandCognitoIdentityPoolRoles(d.Get("roles").(map[string]interface{})),
}
if v, ok := d.GetOk("role_mapping"); ok {
errors := validateRoleMappings(v.(*schema.Set).List())
if len(errors) > 0 {
return fmt.Errorf("Error validating ambiguous role resolution: %v", errors)
}
params.RoleMappings = expandCognitoIdentityPoolRoleMappingsAttachment(v.(*schema.Set).List())
}
log.Printf("[DEBUG] Creating Cognito Identity Pool Roles Association: %#v", params)
_, err := conn.SetIdentityPoolRoles(params)
if err != nil {
return fmt.Errorf("Error creating Cognito Identity Pool Roles Association: %s", err)
}
d.SetId(d.Get("identity_pool_id").(string))
return resourceAwsCognitoIdentityPoolRolesAttachmentRead(d, meta)
}
func resourceAwsCognitoIdentityPoolRolesAttachmentRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoconn
log.Printf("[DEBUG] Reading Cognito Identity Pool Roles Association: %s", d.Id())
ip, err := conn.GetIdentityPoolRoles(&cognitoidentity.GetIdentityPoolRolesInput{
IdentityPoolId: aws.String(d.Get("identity_pool_id").(string)),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "ResourceNotFoundException" {
log.Printf("[WARN] Cognito Identity Pool Roles Association %s not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
if err := d.Set("roles", flattenCognitoIdentityPoolRoles(ip.Roles)); err != nil {
return fmt.Errorf("Error setting roles error: %#v", err)
}
if err := d.Set("role_mapping", flattenCognitoIdentityPoolRoleMappingsAttachment(ip.RoleMappings)); err != nil {
return fmt.Errorf("Error setting role mappings error: %#v", err)
}
return nil
}
func resourceAwsCognitoIdentityPoolRolesAttachmentUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoconn
// Validates role keys to be either authenticated or unauthenticated,
// since ValidateFunc validates only the value not the key.
if errors := validateCognitoRoles(d.Get("roles").(map[string]interface{})); len(errors) > 0 {
return fmt.Errorf("Error validating Roles: %v", errors)
}
params := &cognitoidentity.SetIdentityPoolRolesInput{
IdentityPoolId: aws.String(d.Get("identity_pool_id").(string)),
Roles: expandCognitoIdentityPoolRoles(d.Get("roles").(map[string]interface{})),
}
if d.HasChange("role_mapping") {
v, ok := d.GetOk("role_mapping")
var mappings []interface{}
if ok {
errors := validateRoleMappings(v.(*schema.Set).List())
if len(errors) > 0 {
return fmt.Errorf("Error validating ambiguous role resolution: %v", errors)
}
mappings = v.(*schema.Set).List()
} else {
mappings = []interface{}{}
}
params.RoleMappings = expandCognitoIdentityPoolRoleMappingsAttachment(mappings)
}
log.Printf("[DEBUG] Updating Cognito Identity Pool Roles Association: %#v", params)
_, err := conn.SetIdentityPoolRoles(params)
if err != nil {
return fmt.Errorf("Error updating Cognito Identity Pool Roles Association: %s", err)
}
d.SetId(d.Get("identity_pool_id").(string))
return resourceAwsCognitoIdentityPoolRolesAttachmentRead(d, meta)
}
func resourceAwsCognitoIdentityPoolRolesAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoconn
log.Printf("[DEBUG] Deleting Cognito Identity Pool Roles Association: %s", d.Id())
return resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := conn.SetIdentityPoolRoles(&cognitoidentity.SetIdentityPoolRolesInput{
IdentityPoolId: aws.String(d.Get("identity_pool_id").(string)),
Roles: expandCognitoIdentityPoolRoles(make(map[string]interface{})),
RoleMappings: expandCognitoIdentityPoolRoleMappingsAttachment([]interface{}{}),
})
if err == nil {
return nil
}
return resource.NonRetryableError(err)
})
}
// Validating that each role_mapping ambiguous_role_resolution
// is defined when "type" equals Token or Rules.
func validateRoleMappings(roleMappings []interface{}) []error {
errors := make([]error, 0)
for _, r := range roleMappings {
rm := r.(map[string]interface{})
// If Type equals "Token" or "Rules", ambiguous_role_resolution must be defined.
// This should be removed as soon as we can have a ValidateFuncAgainst callable on the schema.
if err := validateCognitoRoleMappingsAmbiguousRoleResolutionAgainstType(rm); len(err) > 0 {
errors = append(errors, fmt.Errorf("Role Mapping %q: %v", rm["identity_provider"].(string), err))
}
// Validating that Rules Configuration is defined when Type equals Rules
// but not defined when Type equals Token.
if err := validateCognitoRoleMappingsRulesConfiguration(rm); len(err) > 0 {
errors = append(errors, fmt.Errorf("Role Mapping %q: %v", rm["identity_provider"].(string), err))
}
}
return errors
}