@@ -6,20 +6,17 @@ package nsxt
6
6
import (
7
7
"fmt"
8
8
"log"
9
- "regexp"
10
9
11
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12
11
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13
12
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/aaa"
14
13
nsxModel "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
15
14
)
16
15
17
- // Only support local user at the moment
18
16
var roleBindingUserTypes = [](string ){
19
17
nsxModel .RoleBinding_TYPE_LOCAL_USER ,
20
18
nsxModel .RoleBinding_TYPE_REMOTE_USER ,
21
19
nsxModel .RoleBinding_TYPE_REMOTE_GROUP ,
22
- nsxModel .RoleBinding_TYPE_PRINCIPAL_IDENTITY ,
23
20
}
24
21
25
22
var roleBindingIdentitySourceTypes = [](string ){
@@ -71,40 +68,31 @@ func resourceNsxtPolicyUserManagementRoleBinding() *schema.Resource {
71
68
Optional : true ,
72
69
ValidateFunc : validation .StringInSlice (roleBindingIdentitySourceTypes , false ),
73
70
},
74
- "roles_for_path" : {
75
- Type : schema .TypeList ,
76
- Description : "List of roles that are associated with the user, limiting them to a path" ,
77
- Required : true ,
78
- Elem : & schema.Resource {
79
- Schema : map [string ]* schema.Schema {
80
- "path" : {
81
- Type : schema .TypeString ,
82
- Description : "Path of the entity in parent hierarchy." ,
83
- Required : true ,
84
- },
85
- "role" : {
86
- Type : schema .TypeList ,
87
- Description : "Applicable roles" ,
88
- Required : true ,
89
- Elem : & schema.Resource {
90
- Schema : map [string ]* schema.Schema {
91
- "role" : {
92
- Type : schema .TypeString ,
93
- Description : "Short identifier for the role" ,
94
- Required : true ,
95
- ValidateFunc : validation .StringMatch (
96
- regexp .MustCompile (
97
- `^[_a-z0-9-]+$` ),
98
- "Must be a valid role identifier matching: ^[_a-z0-9-]+$" ),
99
- },
100
- "role_display_name" : {
101
- Type : schema .TypeString ,
102
- Description : "Display name for role" ,
103
- Computed : true ,
104
- },
105
- },
106
- },
107
- },
71
+ "roles_for_path" : getRolesForPathSchema (false ),
72
+ },
73
+ }
74
+ }
75
+
76
+ // getRolesForPathSchema return schema for RolesForPath, which is shared between role bindings and PI
77
+ func getRolesForPathSchema (forceNew bool ) * schema.Schema {
78
+ return & schema.Schema {
79
+ Type : schema .TypeList ,
80
+ Description : "List of roles that are associated with the user, limiting them to a path" ,
81
+ Required : true ,
82
+ ForceNew : forceNew ,
83
+ Elem : & schema.Resource {
84
+ Schema : map [string ]* schema.Schema {
85
+ "path" : {
86
+ Type : schema .TypeString ,
87
+ Description : "Path of the entity in parent hierarchy." ,
88
+ Required : true ,
89
+ },
90
+ "roles" : {
91
+ Type : schema .TypeSet ,
92
+ Description : "Applicable roles" ,
93
+ Required : true ,
94
+ Elem : & schema.Schema {
95
+ Type : schema .TypeString ,
108
96
},
109
97
},
110
98
},
@@ -121,12 +109,10 @@ func getRolesForPathFromSchema(d *schema.ResourceData) rolesForPath {
121
109
for _ , rolesPerPathInput := range rolesForPathInput {
122
110
data := rolesPerPathInput .(map [string ]interface {})
123
111
path := data ["path" ].(string )
124
- roles := data ["role " ].([] interface {} )
112
+ roles := interface2StringList ( data ["roles " ].(* schema. Set ). List () )
125
113
rolesPerPathMap := make (rolesPerPath )
126
114
for _ , role := range roles {
127
- roleData := role .(map [string ]interface {})
128
- roleInput := roleData ["role" ].(string )
129
- rolesPerPathMap [roleInput ] = true
115
+ rolesPerPathMap [role ] = true
130
116
}
131
117
rolesForPathMap [path ] = rolesPerPathMap
132
118
}
@@ -139,14 +125,11 @@ func setRolesForPathInSchema(d *schema.ResourceData, nsxRolesForPathList []nsxMo
139
125
for _ , nsxRolesForPath := range nsxRolesForPathList {
140
126
elem := make (map [string ]interface {})
141
127
elem ["path" ] = nsxRolesForPath .Path
142
- var roles [] map [ string ] interface {}
128
+ roles := make ([] string , 0 , len ( nsxRolesForPath . Roles ))
143
129
for _ , nsxRole := range nsxRolesForPath .Roles {
144
- rElem := make (map [string ]interface {})
145
- rElem ["role" ] = nsxRole .Role
146
- rElem ["role_display_name" ] = nsxRole .RoleDisplayName
147
- roles = append (roles , rElem )
130
+ roles = append (roles , * nsxRole .Role )
148
131
}
149
- elem ["role " ] = roles
132
+ elem ["roles " ] = roles
150
133
rolesForPathList = append (rolesForPathList , elem )
151
134
}
152
135
err := d .Set ("roles_for_path" , rolesForPathList )
@@ -155,15 +138,8 @@ func setRolesForPathInSchema(d *schema.ResourceData, nsxRolesForPathList []nsxMo
155
138
}
156
139
}
157
140
158
- func getRoleBindingObject (d * schema.ResourceData ) * nsxModel.RoleBinding {
141
+ func getRolesForPathList (d * schema.ResourceData ) [] nsxModel.RolesForPath {
159
142
boolTrue := true
160
- displayName := d .Get ("display_name" ).(string )
161
- description := d .Get ("description" ).(string )
162
- tags := getPolicyTagsFromSchema (d )
163
- name := d .Get ("name" ).(string )
164
- identitySrcID := d .Get ("identity_source_id" ).(string )
165
- identitySrcType := d .Get ("identity_source_type" ).(string )
166
- roleBindingType := d .Get ("type" ).(string )
167
143
rolesPerPathMap := getRolesForPathFromSchema (d )
168
144
nsxRolesForPaths := make ([]nsxModel.RolesForPath , 0 )
169
145
@@ -193,19 +169,30 @@ func getRoleBindingObject(d *schema.ResourceData) *nsxModel.RoleBinding {
193
169
continue
194
170
}
195
171
// Add one role in the list to make NSX happy
196
- roles := data ["role " ].([] interface {} )
172
+ roles := interface2StringList ( data ["roles " ].(* schema. Set ). List () )
197
173
if len (roles ) == 0 {
198
174
continue
199
175
}
200
- roleData := roles [0 ].(map [string ]interface {})
201
- roleID := roleData ["role" ].(string )
202
176
nsxRolesForPaths = append (nsxRolesForPaths , nsxModel.RolesForPath {
203
177
Path : & path ,
204
178
DeletePath : & boolTrue ,
205
- Roles : []nsxModel.Role {{Role : & roleID }},
179
+ Roles : []nsxModel.Role {{Role : & roles [ 0 ] }},
206
180
})
207
181
}
208
182
}
183
+ return nsxRolesForPaths
184
+ }
185
+
186
+ func getRoleBindingObject (d * schema.ResourceData ) * nsxModel.RoleBinding {
187
+ boolTrue := true
188
+ displayName := d .Get ("display_name" ).(string )
189
+ description := d .Get ("description" ).(string )
190
+ tags := getPolicyTagsFromSchema (d )
191
+ name := d .Get ("name" ).(string )
192
+ identitySrcID := d .Get ("identity_source_id" ).(string )
193
+ identitySrcType := d .Get ("identity_source_type" ).(string )
194
+ roleBindingType := d .Get ("type" ).(string )
195
+ nsxRolesForPaths := getRolesForPathList (d )
209
196
210
197
obj := nsxModel.RoleBinding {
211
198
DisplayName : & displayName ,
0 commit comments