-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathresource_aws_cognito_user_group.go
174 lines (140 loc) · 4.6 KB
/
resource_aws_cognito_user_group.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
package aws
import (
"errors"
"fmt"
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsCognitoUserGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCognitoUserGroupCreate,
Read: resourceAwsCognitoUserGroupRead,
Update: resourceAwsCognitoUserGroupUpdate,
Delete: resourceAwsCognitoUserGroupDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsCognitoUserGroupImport,
},
// https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_CreateGroup.html
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateMaxLength(2048),
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateCognitoUserGroupName,
},
"precedence": {
Type: schema.TypeInt,
Optional: true,
},
"role_arn": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"user_pool_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateCognitoUserPoolId,
},
},
}
}
func resourceAwsCognitoUserGroupCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoidpconn
params := &cognitoidentityprovider.CreateGroupInput{
GroupName: aws.String(d.Get("name").(string)),
UserPoolId: aws.String(d.Get("user_pool_id").(string)),
}
if v, ok := d.GetOk("description"); ok {
params.Description = aws.String(v.(string))
}
if v, ok := d.GetOk("precedence"); ok {
params.Precedence = aws.Int64(int64(v.(int)))
}
if v, ok := d.GetOk("role_arn"); ok {
params.RoleArn = aws.String(v.(string))
}
log.Print("[DEBUG] Creating Cognito User Group")
resp, err := conn.CreateGroup(params)
if err != nil {
return fmt.Errorf("Error creating Cognito User Group: %s", err)
}
d.SetId(fmt.Sprintf("%s/%s", *resp.Group.UserPoolId, *resp.Group.GroupName))
return resourceAwsCognitoUserGroupRead(d, meta)
}
func resourceAwsCognitoUserGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoidpconn
params := &cognitoidentityprovider.GetGroupInput{
GroupName: aws.String(d.Get("name").(string)),
UserPoolId: aws.String(d.Get("user_pool_id").(string)),
}
log.Print("[DEBUG] Reading Cognito User Group")
resp, err := conn.GetGroup(params)
if err != nil {
if isAWSErr(err, "ResourceNotFoundException", "") {
log.Printf("[WARN] Cognito User Group %s is already gone", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Cognito User Group: %s", err)
}
d.Set("description", resp.Group.Description)
d.Set("precedence", resp.Group.Precedence)
d.Set("role_arn", resp.Group.RoleArn)
return nil
}
func resourceAwsCognitoUserGroupUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoidpconn
params := &cognitoidentityprovider.UpdateGroupInput{
GroupName: aws.String(d.Get("name").(string)),
UserPoolId: aws.String(d.Get("user_pool_id").(string)),
}
if d.HasChange("description") {
params.Description = aws.String(d.Get("description").(string))
}
if d.HasChange("precedence") {
params.Precedence = aws.Int64(int64(d.Get("precedence").(int)))
}
if d.HasChange("role_arn") {
params.RoleArn = aws.String(d.Get("description").(string))
}
log.Print("[DEBUG] Updating Cognito User Group")
_, err := conn.UpdateGroup(params)
if err != nil {
return fmt.Errorf("Error updating Cognito User Group: %s", err)
}
return resourceAwsCognitoUserGroupRead(d, meta)
}
func resourceAwsCognitoUserGroupDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cognitoidpconn
params := &cognitoidentityprovider.DeleteGroupInput{
GroupName: aws.String(d.Get("name").(string)),
UserPoolId: aws.String(d.Get("user_pool_id").(string)),
}
log.Print("[DEBUG] Deleting Cognito User Group")
_, err := conn.DeleteGroup(params)
if err != nil {
return fmt.Errorf("Error deleting Cognito User Group: %s", err)
}
return nil
}
func resourceAwsCognitoUserGroupImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idSplit := strings.Split(d.Id(), "/")
if len(idSplit) != 2 {
return nil, errors.New("Error importing Cognito User Group. Must specify user_pool_id/group_name")
}
userPoolId := idSplit[0]
name := idSplit[1]
d.Set("user_pool_id", userPoolId)
d.Set("name", name)
return []*schema.ResourceData{d}, nil
}