-
Notifications
You must be signed in to change notification settings - Fork 65
/
schema_user.go
131 lines (110 loc) · 3.16 KB
/
schema_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
package octopusdeploy
import (
"context"
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func expandUser(d *schema.ResourceData) *octopusdeploy.User {
username := d.Get("username").(string)
displayName := d.Get("display_name").(string)
user := octopusdeploy.NewUser(username, displayName)
user.ID = d.Id()
if v, ok := d.GetOk("email_address"); ok {
user.EmailAddress = v.(string)
}
if v, ok := d.GetOk("identity"); ok {
user.Identities = expandIdentities(v.(*schema.Set).List())
}
if v, ok := d.GetOk("is_active"); ok {
user.IsActive = v.(bool)
}
if v, ok := d.GetOk("is_requestor"); ok {
user.IsRequestor = v.(bool)
}
if v, ok := d.GetOk("is_service"); ok {
user.IsService = v.(bool)
}
if v, ok := d.GetOk("password"); ok {
user.Password = v.(string)
}
return user
}
func flattenUser(user *octopusdeploy.User) map[string]interface{} {
if user == nil {
return nil
}
return map[string]interface{}{
"can_password_be_edited": user.CanPasswordBeEdited,
"display_name": user.DisplayName,
"email_address": user.EmailAddress,
"id": user.GetID(),
"identity": flattenIdentities(user.Identities),
"is_active": user.IsActive,
"is_service": user.IsService,
"username": user.Username,
}
}
func getUserDataSchema() map[string]*schema.Schema {
dataSchema := getUserSchema()
setDataSchema(&dataSchema)
return map[string]*schema.Schema{
"filter": getQueryFilter(),
"id": getDataSchemaID(),
"ids": getQueryIDs(),
"skip": getQuerySkip(),
"take": getQueryTake(),
"users": {
Computed: true,
Description: "A list of users that match the filter(s).",
Elem: &schema.Resource{Schema: dataSchema},
Optional: true,
Type: schema.TypeList,
},
}
}
func getUserSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"can_password_be_edited": {
Computed: true,
Type: schema.TypeBool,
},
"display_name": getDisplayNameSchema(true),
"email_address": getEmailAddressSchema(false),
"id": getIDSchema(),
"identity": {
Optional: true,
Elem: &schema.Resource{Schema: getIdentitySchema()},
Type: schema.TypeSet,
},
"is_active": {
Optional: true,
Type: schema.TypeBool,
},
"is_requestor": {
Computed: true,
Type: schema.TypeBool,
},
"is_service": {
Optional: true,
Type: schema.TypeBool,
},
"username": getUsernameSchema(true),
"password": getPasswordSchema(false),
}
}
func setUser(ctx context.Context, d *schema.ResourceData, user *octopusdeploy.User) error {
d.Set("can_password_be_edited", user.CanPasswordBeEdited)
d.Set("display_name", user.DisplayName)
d.Set("email_address", user.EmailAddress)
d.Set("id", user.GetID())
if err := d.Set("identity", flattenIdentities(user.Identities)); err != nil {
return fmt.Errorf("error setting identity: %s", err)
}
d.Set("is_active", user.IsActive)
d.Set("is_requestor", user.IsRequestor)
d.Set("is_service", user.IsService)
d.Set("username", user.Username)
d.SetId(user.GetID())
return nil
}