@@ -44,10 +44,9 @@ func resourceCloudProjectUser() *schema.Resource {
44
44
ForceNew : true ,
45
45
},
46
46
"role_name" : {
47
- Type : schema .TypeString ,
48
- Optional : true ,
49
- ForceNew : false ,
50
- ValidateFunc : validateCloudProjectUserRoleFunc ,
47
+ Type : schema .TypeString ,
48
+ Optional : true ,
49
+ ForceNew : false ,
51
50
},
52
51
"role_names" : {
53
52
Type : schema .TypeList ,
@@ -108,6 +107,30 @@ func resourceCloudProjectUser() *schema.Resource {
108
107
}
109
108
}
110
109
110
+ func validateCloudProjectUserRoleFunc (config * Config , serviceName string , roles []string , role string ) (* CloudProjectrolesResponse , error ) {
111
+
112
+ endpoint := fmt .Sprintf ("/cloud/project/%s/role" ,
113
+ url .PathEscape (serviceName ),
114
+ )
115
+ res := & CloudProjectrolesResponse {}
116
+ if err := config .OVHClient .Get (endpoint , res ); err != nil {
117
+ return nil , fmt .Errorf ("calling Get %s" , endpoint )
118
+ }
119
+
120
+ ovhRole := make ([]string , 0 , len (res .Roles ))
121
+ for _ , val := range res .Roles {
122
+ ovhRole = append (ovhRole , val .Name )
123
+ }
124
+
125
+ for _ , role := range append (roles , role ) {
126
+ if ! slices .Contains (ovhRole , role ) {
127
+ return nil , fmt .Errorf ("Role %q does not exist" , role )
128
+ }
129
+ }
130
+
131
+ return res , nil
132
+ }
133
+
111
134
func resourceCloudProjectUserImportState (ctx context.Context , d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
112
135
userId := d .Id ()
113
136
// Fallback to the environment variable if service_name not given
@@ -129,47 +152,18 @@ func resourceCloudProjectUserImportState(ctx context.Context, d *schema.Resource
129
152
return []* schema.ResourceData {d }, nil
130
153
}
131
154
132
- func validateCloudProjectUserRoleFunc (v interface {}, k string ) (ws []string , errors []error ) {
133
- err := helpers .ValidateStringEnum (v .(string ), []string {
134
- "administrator" ,
135
- "ai_training_operator" ,
136
- "ai_training_read" ,
137
- "authentication" ,
138
- "backup_operator" ,
139
- "compute_operator" ,
140
- "image_operator" ,
141
- "infrastructure_supervisor" ,
142
- "network_operator" ,
143
- "network_security_operator" ,
144
- "objectstore_operator" ,
145
- "volume_operator" ,
146
- })
147
-
148
- if err != nil {
149
- errors = append (errors , err )
150
- }
151
- return
152
- }
153
-
154
155
func resourceCloudProjectUserUpdate (d * schema.ResourceData , meta interface {}) error {
155
156
config := meta .(* Config )
156
157
serviceName := d .Get ("service_name" ).(string )
157
158
userId := d .Id ()
158
159
role := d .Get ("role_name" )
159
160
roles , err := helpers .StringsFromSchema (d , "role_names" )
161
+ res := & CloudProjectrolesResponse {}
160
162
163
+ res , err = validateCloudProjectUserRoleFunc (config , serviceName , roles , role .(string ))
161
164
if err != nil {
162
- log . Fatal ( err )
165
+ return err
163
166
}
164
-
165
- endpoint := fmt .Sprintf ("/cloud/project/%s/role" ,
166
- url .PathEscape (serviceName ),
167
- )
168
- res := & CloudProjectrolesResponse {}
169
- if err := config .OVHClient .Get (endpoint , res ); err != nil {
170
- return fmt .Errorf ("calling Get %s" , endpoint )
171
- }
172
-
173
167
update := []string {}
174
168
for _ , i := range res .Roles {
175
169
if slices .Contains (roles , i .Name ) {
@@ -182,7 +176,7 @@ func resourceCloudProjectUserUpdate(d *schema.ResourceData, meta interface{}) er
182
176
183
177
log .Printf ("[DEBUG] roles IDs %s" , update )
184
178
log .Printf ("[DEBUG] user %s" , userId )
185
- endpoint = fmt .Sprintf ("/cloud/project/%s/user/%s/role" ,
179
+ endpoint : = fmt .Sprintf ("/cloud/project/%s/user/%s/role" ,
186
180
url .PathEscape (serviceName ),
187
181
url .PathEscape (userId ),
188
182
)
@@ -200,16 +194,14 @@ func resourceCloudProjectUserUpdate(d *schema.ResourceData, meta interface{}) er
200
194
func resourceCloudProjectUserCreate (d * schema.ResourceData , meta interface {}) error {
201
195
config := meta .(* Config )
202
196
serviceName := d .Get ("service_name" ).(string )
203
-
197
+ role := d . Get ( "role_name" )
204
198
params := (& CloudProjectUserCreateOpts {}).FromResource (d )
205
199
206
- for _ , role := range params .Roles {
207
- if _ , errs := validateCloudProjectUserRoleFunc (role , "" ); errs != nil {
208
- return fmt .Errorf ("roles contains unsupported value: %s." , role )
209
- }
210
- }
211
-
212
200
r := & CloudProjectUser {}
201
+ _ , err := validateCloudProjectUserRoleFunc (config , serviceName , params .Roles , role .(string ))
202
+ if err != nil {
203
+ return err
204
+ }
213
205
214
206
log .Printf ("[DEBUG] Will create public cloud user: %s" , params )
215
207
endpoint := fmt .Sprintf (
0 commit comments