-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathschema_user_role.go
158 lines (132 loc) · 4.62 KB
/
schema_user_role.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
package octopusdeploy
import (
"context"
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func expandUserRole(d *schema.ResourceData) *octopusdeploy.UserRole {
userRole := &octopusdeploy.UserRole{}
userRole.ID = d.Id()
if v, ok := d.GetOk("can_be_deleted"); ok {
userRole.CanBeDeleted = v.(bool)
}
if v, ok := d.GetOk("description"); ok {
userRole.Description = v.(string)
}
if v, ok := d.GetOk("granted_space_permissions"); ok {
userRole.GrantedSpacePermissions = getSliceFromTerraformTypeList(v)
}
if v, ok := d.GetOk("granted_system_permissions"); ok {
userRole.GrantedSystemPermissions = getSliceFromTerraformTypeList(v)
}
if v, ok := d.GetOk("name"); ok {
userRole.Name = v.(string)
}
if v, ok := d.GetOk("space_permission_descriptions"); ok {
userRole.SpacePermissionDescriptions = getSliceFromTerraformTypeList(v)
}
if v, ok := d.GetOk("supported_restrictions"); ok {
userRole.SupportedRestrictions = getSliceFromTerraformTypeList(v)
}
if v, ok := d.GetOk("system_permission_descriptions"); ok {
userRole.SystemPermissionDescriptions = getSliceFromTerraformTypeList(v)
}
return userRole
}
func flattenUserRole(userRole *octopusdeploy.UserRole) map[string]interface{} {
if userRole == nil {
return nil
}
return map[string]interface{}{
"can_be_deleted": userRole.CanBeDeleted,
"description": userRole.Description,
"granted_space_permissions": userRole.GrantedSpacePermissions,
"granted_system_permissions": userRole.GrantedSystemPermissions,
"id": userRole.GetID(),
"name": userRole.Name,
"space_permission_descriptions": userRole.SpacePermissionDescriptions,
"supported_restrictions": userRole.SupportedRestrictions,
"system_permission_descriptions": userRole.SystemPermissionDescriptions,
}
}
func getUserRoleDataSchema() map[string]*schema.Schema {
dataSchema := getUserRoleSchema()
setDataSchema(&dataSchema)
return map[string]*schema.Schema{
"id": getDataSchemaID(),
"ids": getQueryIDs(),
"partial_name": getQueryPartialName(),
"skip": getQuerySkip(),
"take": getQueryTake(),
"user_roles": {
Computed: true,
Description: "A list of user roles that match the filter(s).",
Elem: &schema.Resource{Schema: dataSchema},
Optional: true,
Type: schema.TypeList,
},
}
}
func getUserRoleSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"can_be_deleted": {
Computed: true,
Optional: true,
Type: schema.TypeBool,
},
"description": getDescriptionSchema(),
"granted_space_permissions": {
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeList,
},
"granted_system_permissions": {
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeList,
},
"id": getIDSchema(),
"name": getNameSchema(true),
"space_permission_descriptions": {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeList,
},
"supported_restrictions": {
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeList,
},
"system_permission_descriptions": {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeList,
},
}
}
func setUserRole(ctx context.Context, d *schema.ResourceData, userRole *octopusdeploy.UserRole) error {
d.Set("can_be_deleted", userRole.CanBeDeleted)
d.Set("description", userRole.Description)
d.Set("id", userRole.GetID())
if err := d.Set("granted_space_permissions", userRole.GrantedSpacePermissions); err != nil {
return fmt.Errorf("error setting granted_space_permissions: %s", err)
}
if err := d.Set("granted_system_permissions", userRole.GrantedSystemPermissions); err != nil {
return fmt.Errorf("error setting granted_system_permissions: %s", err)
}
d.Set("name", userRole.Name)
if err := d.Set("space_permission_descriptions", userRole.SpacePermissionDescriptions); err != nil {
return fmt.Errorf("error setting space_permission_descriptions: %s", err)
}
if err := d.Set("supported_restrictions", userRole.SupportedRestrictions); err != nil {
return fmt.Errorf("error setting supported_restrictions: %s", err)
}
if err := d.Set("system_permission_descriptions", userRole.SystemPermissionDescriptions); err != nil {
return fmt.Errorf("error setting system_permission_descriptions: %s", err)
}
d.SetId(userRole.GetID())
return nil
}