generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 44
/
resource_user.go
275 lines (233 loc) · 8.3 KB
/
resource_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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
package user
import (
"context"
"net/http"
"github.com/antihax/optional"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/harness/terraform-provider-harness/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceUser() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a Harness User. This requires your authentication mechanism to be set to SAML, LDAP, or OAuth, and the feature flag AUTO_ACCEPT_SAML_ACCOUNT_INVITES to be enabled.",
ReadContext: resourceUserRead,
UpdateContext: resourceUserCreateOrUpdate,
DeleteContext: resourceUserDelete,
CreateContext: resourceUserCreateOrUpdate,
Importer: helpers.UserResourceImporter,
Schema: map[string]*schema.Schema{
"identifier": {
Description: "Unique identifier of the user.",
Type: schema.TypeString,
Computed: true,
},
"org_id": {
Description: "Organization identifier of the user.",
Type: schema.TypeString,
Optional: true,
},
"project_id": {
Description: "Project identifier of the user.",
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"org_id"},
},
"name": {
Description: "Name of the user.",
Type: schema.TypeString,
Computed: true,
},
"email": {
Description: "The email of the user.",
Type: schema.TypeString,
Required: true,
},
"disabled": {
Description: "Whether or not the user account is disabled.",
Type: schema.TypeBool,
Computed: true,
},
"locked": {
Description: "Whether or not the user account is locked.",
Type: schema.TypeBool,
Computed: true,
},
"externally_managed": {
Description: "Whether or not the user account is externally managed.",
Type: schema.TypeBool,
Computed: true,
},
"user_groups": {
Description: "The user group of the user. Cannot be updated.",
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"role_bindings": {
Description: "Role Bindings of the user. Cannot be updated.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"resource_group_identifier": {
Description: "Resource Group Identifier of the user.",
Type: schema.TypeString,
Optional: true,
},
"role_identifier": {
Description: "Role Identifier of the user.",
Type: schema.TypeString,
Optional: true,
},
"role_name": {
Description: "Role Name Identifier of the user.",
Type: schema.TypeString,
Optional: true,
},
"resource_group_name": {
Description: "Resource Group Name of the user.",
Type: schema.TypeString,
Optional: true,
},
"managed_role": {
Description: "Managed Role of the user.",
Type: schema.TypeBool,
Optional: true,
},
}},
},
},
}
return resource
}
func resourceUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var email = ""
if attr, ok := d.GetOk("email"); ok {
email = attr.(string)
}
resp, httpResp, err := c.UserApi.GetAggregatedUsers(ctx, c.AccountId, &nextgen.UserApiGetAggregatedUsersOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
SearchTerm: optional.NewString(email),
})
if &resp == nil || resp.Data == nil || resp.Data.Empty {
d.SetId("")
d.MarkNewResource()
return nil
}
if err != nil {
return helpers.HandleReadApiError(err, d, httpResp)
}
readUserList(d, resp.Data)
return nil
}
func resourceUserCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
id := d.Id()
var err error
var httpResp *http.Response
if id == "" {
addUserBody := createAddUserBody(d)
_, httpResp, err = c.UserApi.AddUsers(ctx, *addUserBody, c.AccountId, &nextgen.UserApiAddUsersOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
} else {
return diag.Errorf("Update operation is not allowed for User resource.")
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
var email = ""
if attr, ok := d.GetOk("email"); ok {
email = attr.(string)
}
resp, httpResp, err := c.UserApi.GetAggregatedUsers(ctx, c.AccountId, &nextgen.UserApiGetAggregatedUsersOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
SearchTerm: optional.NewString(email),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readUserList(d, resp.Data)
return nil
}
func resourceUserDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
uuid := d.Get("identifier").(string)
orgIdentifier := optional.NewString(d.Get("org_id").(string))
projectIdentifier := optional.NewString(d.Get("project_id").(string))
var removeUserOpts = &nextgen.UserApiRemoveUserOpts{}
if orgIdentifier.IsSet() && len(orgIdentifier.Value()) > 0 && projectIdentifier.IsSet() && len(projectIdentifier.Value()) > 0 {
removeUserOpts = &nextgen.UserApiRemoveUserOpts{
OrgIdentifier: orgIdentifier,
ProjectIdentifier: projectIdentifier,
}
} else if orgIdentifier.IsSet() && len(orgIdentifier.Value()) > 0 {
removeUserOpts = &nextgen.UserApiRemoveUserOpts{
OrgIdentifier: orgIdentifier,
}
}
_, httpResp, err := c.UserApi.RemoveUser(ctx, uuid, c.AccountId, removeUserOpts)
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
func createAddUserBody(d *schema.ResourceData) *nextgen.AddUsersDto {
var addUsersDto nextgen.AddUsersDto
if attr, ok := d.GetOk("email"); ok {
addUsersDto.Emails = []string{attr.(string)}
}
if attr, ok := d.GetOk("user_groups"); ok {
addUsersDto.UserGroups = utils.InterfaceSliceToStringSlice(attr.(*schema.Set).List())
}
var ipRoleBindings map[string]interface{}
if attr, ok := d.GetOk("role_bindings"); ok {
var roleBindings []nextgen.RoleBinding
ipRoleBindings = attr.([]interface{})[0].(map[string]interface{})
var roleBindingObj nextgen.RoleBinding
if ipRoleBindings["resource_group_identifier"] != nil && len(ipRoleBindings["resource_group_identifier"].(string)) > 0 {
roleBindingObj.ResourceGroupIdentifier = ipRoleBindings["resource_group_identifier"].(string)
}
if ipRoleBindings["role_identifier"] != nil && len(ipRoleBindings["role_identifier"].(string)) > 0 {
roleBindingObj.RoleIdentifier = ipRoleBindings["role_identifier"].(string)
}
if ipRoleBindings["role_name"] != nil && len(ipRoleBindings["role_name"].(string)) > 0 {
roleBindingObj.RoleName = ipRoleBindings["role_name"].(string)
}
if ipRoleBindings["resource_group_name"] != nil && len(ipRoleBindings["resource_group_name"].(string)) > 0 {
roleBindingObj.ResourceGroupName = ipRoleBindings["resource_group_name"].(string)
}
if ipRoleBindings["managed_role"] != nil {
roleBindingObj.ManagedRole = ipRoleBindings["managed_role"].(bool)
}
roleBindings = append(roleBindings, roleBindingObj)
addUsersDto.RoleBindings = roleBindings
} else {
addUsersDto.RoleBindings = []nextgen.RoleBinding{}
}
return &addUsersDto
}
func readUserList(d *schema.ResourceData, userInfo *nextgen.PageResponseUserAggregate) {
userInfoList := userInfo.Content
for _, value := range userInfoList {
readUser(d, &value)
}
}
func readUser(d *schema.ResourceData, UserAggregate *nextgen.UserAggregate) {
d.SetId(UserAggregate.User.Email)
d.Set("identifier", UserAggregate.User.Uuid)
d.Set("name", UserAggregate.User.Name)
d.Set("email", UserAggregate.User.Email)
d.Set("locked", UserAggregate.User.Locked)
d.Set("disabled", UserAggregate.User.Disabled)
d.Set("externally_managed", UserAggregate.User.ExternallyManaged)
}