-
Notifications
You must be signed in to change notification settings - Fork 55
/
resource_spotinst_organization_user.go
executable file
·201 lines (165 loc) · 5.85 KB
/
resource_spotinst_organization_user.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
package spotinst
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spotinst/spotinst-sdk-go/service/organization"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/terraform-provider-spotinst/spotinst/commons"
organizationPackage "github.com/spotinst/terraform-provider-spotinst/spotinst/organization_user"
)
func resourceOrgUser() *schema.Resource {
setupOrgUser()
return &schema.Resource{
CreateContext: resourceOrgUserCreate,
UpdateContext: resourceOrgUserUpdate,
ReadContext: resourceOrgUserRead,
DeleteContext: resourceOrgUserDelete,
Schema: commons.OrgUserResource.GetSchemaMap(),
}
}
func setupOrgUser() {
fieldsMap := make(map[commons.FieldName]*commons.GenericField)
organizationPackage.Setup(fieldsMap)
commons.OrgUserResource = commons.NewOrgUserResource(fieldsMap)
}
func resourceOrgUserDelete(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
id := resourceData.Id()
log.Printf(string(commons.ResourceOnDelete),
commons.OrgUserResource.GetName(), id)
input := &organization.DeleteUserInput{UserID: spotinst.String(id)}
if _, err := meta.(*Client).organization.DeleteUser(context.Background(), input); err != nil {
return diag.Errorf("[ERROR] Failed to delete user: %s", err)
}
resourceData.SetId("")
return nil
}
func resourceOrgUserRead(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
id := resourceData.Id()
log.Printf(string(commons.ResourceOnRead),
commons.OrgUserResource.GetName(), id)
client := meta.(*Client)
input := &organization.ReadUserInput{UserID: spotinst.String(resourceData.Id())}
userResponse, err := client.organization.ReadUser(context.Background(), input)
if err != nil {
return diag.Errorf("[ERROR] Failed to read user: %s", err)
}
// If nothing was found, then return no state.
user := userResponse.User
if user == nil {
resourceData.SetId("")
return nil
}
if err := commons.OrgUserResource.OnRead(user, resourceData, meta); err != nil {
return diag.FromErr(err)
}
log.Printf("===> User read successfully: %s <===", id)
return nil
}
func resourceOrgUserCreate(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf(string(commons.ResourceOnCreate),
commons.OrgUserResource.GetName())
user, err := commons.OrgUserResource.OnCreate(resourceData, meta)
var policies = user.Policies
var userGroupIds = user.UserGroupIds
user.Policies = nil
user.UserGroupIds = nil
if err != nil {
return diag.FromErr(err)
}
userId, err := createUser(user, meta.(*Client))
if err != nil {
return diag.FromErr(err)
}
var updateErr error = nil
if policies != nil {
updateErr = updatePolicyMapping(policies, userId, meta.(*Client))
}
if updateErr != nil {
return diag.FromErr(updateErr)
}
if userGroupIds != nil {
updateErr = updateUserGroupMapping(userGroupIds, userId, meta.(*Client))
}
if updateErr != nil {
return diag.FromErr(updateErr)
}
resourceData.SetId(spotinst.StringValue(userId))
log.Printf("===> User created successfully: %s <===", resourceData.Id())
return resourceOrgUserRead(ctx, resourceData, meta)
}
func createUser(userObj *organization.User, spotinstClient *Client) (*string, error) {
input := userObj
resp, err := spotinstClient.organization.CreateUser(context.Background(), input, spotinst.Bool(true))
if err != nil {
return nil, fmt.Errorf("[ERROR] failed to create user: %s", err)
}
return resp.User.UserID, nil
}
func updatePolicyMapping(policies []*organization.UserPolicy, userId *string, spotinstClient *Client) error {
err := spotinstClient.organization.UpdatePolicyMappingOfUser(context.Background(), &organization.UpdatePolicyMappingOfUserInput{
UserID: userId,
Policies: policies,
})
if err != nil {
return fmt.Errorf("[ERROR] failed to update policy mapping for user: %s", err)
}
return nil
}
func updateUserGroupMapping(userGroupIds []string, userId *string, spotinstClient *Client) error {
err := spotinstClient.organization.UpdateUserGroupMappingOfUser(context.Background(), &organization.UpdateUserGroupMappingOfUserInput{
UserID: userId,
UserGroupIds: userGroupIds,
})
if err != nil {
return fmt.Errorf("[ERROR] failed to update policy mapping for user: %s", err)
}
return nil
}
func resourceOrgUserUpdate(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
id := resourceData.Id()
log.Printf(string(commons.ResourceOnUpdate),
commons.OrgUserResource.GetName(), id)
shouldUpdate, user, err := commons.OrgUserResource.OnUpdate(resourceData, meta)
if err != nil {
return diag.FromErr(err)
}
var policies []*organization.UserPolicy = user.Policies
var userGroupIds []string = user.UserGroupIds
if shouldUpdate {
user.UserID = spotinst.String(id)
var accountIds []string
if policies != nil {
for _, policy := range policies {
for _, actId := range policy.AccountIds {
if spotinst.StringValue(policy.PolicyId) != "3" {
for i := 0; i < len(policy.AccountIds); i++ {
if accountIds != nil {
if accountIds[i] == actId {
break
}
}
accountIds = append(accountIds, actId)
}
}
}
}
var accountViewerPolicy organization.UserPolicy
accountViewerPolicy.PolicyId = spotinst.String("3")
accountViewerPolicy.AccountIds = accountIds
policies = append(policies, &accountViewerPolicy)
if err := updatePolicyMapping(policies, &id, meta.(*Client)); err != nil {
return diag.FromErr(err)
}
}
if userGroupIds != nil {
if err := updateUserGroupMapping(userGroupIds, &id, meta.(*Client)); err != nil {
return diag.FromErr(err)
}
}
}
log.Printf("===> User mapping updated successfully: %s <===", id)
return resourceOrgUserRead(ctx, resourceData, meta)
}