@@ -3,9 +3,11 @@ package provider
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "regexp"
6
7
7
8
"github.com/coder/coder/v2/codersdk"
8
9
"github.com/coder/terraform-provider-coderd/internal/codersdkvalidator"
10
+ "github.com/google/uuid"
9
11
"github.com/hashicorp/terraform-plugin-framework/path"
10
12
"github.com/hashicorp/terraform-plugin-framework/resource"
11
13
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -33,6 +35,9 @@ type OrganizationResourceModel struct {
33
35
DisplayName types.String `tfsdk:"display_name"`
34
36
Description types.String `tfsdk:"description"`
35
37
Icon types.String `tfsdk:"icon"`
38
+
39
+ GroupSync types.Object `tfsdk:"group_sync"`
40
+ RoleSync types.Object `tfsdk:"role_sync"`
36
41
}
37
42
38
43
func NewOrganizationResource () resource.Resource {
@@ -82,6 +87,13 @@ func (r *OrganizationResource) Schema(ctx context.Context, req resource.SchemaRe
82
87
Computed : true ,
83
88
Default : stringdefault .StaticString ("" ),
84
89
},
90
+
91
+ "group_sync" : schema.ObjectAttribute {
92
+ Optional : true ,
93
+ },
94
+ "role_sync" : schema.ObjectAttribute {
95
+ Optional : true ,
96
+ },
85
97
},
86
98
}
87
99
}
@@ -207,6 +219,14 @@ func (r *OrganizationResource) Update(ctx context.Context, req resource.UpdateRe
207
219
}
208
220
tflog .Trace (ctx , "successfully updated organization" )
209
221
222
+ if data .GroupSync .IsNull () {
223
+ err = r .patchGroupSync (ctx , orgID , data .GroupSync )
224
+ if err != nil {
225
+ resp .Diagnostics .AddError ("Group Sync Update error" , "uh oh john" )
226
+ return
227
+ }
228
+ }
229
+
210
230
// Save updated data into Terraform state
211
231
resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
212
232
}
@@ -240,3 +260,52 @@ func (r *OrganizationResource) ImportState(ctx context.Context, req resource.Imp
240
260
// set the `name` attribute.
241
261
resource .ImportStatePassthroughID (ctx , path .Root ("name" ), req , resp )
242
262
}
263
+
264
+ func (r * OrganizationResource ) patchGroupSync (
265
+ ctx context.Context ,
266
+ orgID uuid.UUID ,
267
+ groupSyncAttr types.Object ,
268
+ ) error {
269
+ var settings codersdk.GroupSyncSettings
270
+
271
+ field , ok := groupSyncAttr .Attributes ()["field" ].(types.String )
272
+ if ! ok {
273
+ return fmt .Errorf ("oh jeez" )
274
+ }
275
+ settings .Field = field .ValueString ()
276
+
277
+ mappingMap , ok := groupSyncAttr .Attributes ()["mapping" ].(types.Map )
278
+ if ! ok {
279
+ return fmt .Errorf ("oh jeez" )
280
+ }
281
+ var mapping map [string ][]uuid.UUID
282
+ diags := mappingMap .ElementsAs (ctx , mapping , false )
283
+ if diags .HasError () {
284
+ return fmt .Errorf ("oh jeez" )
285
+ }
286
+ settings .Mapping = mapping
287
+
288
+ regexFilterStr , ok := groupSyncAttr .Attributes ()["regex_filter" ].(types.String )
289
+ if ! ok {
290
+ return fmt .Errorf ("oh jeez" )
291
+ }
292
+ regexFilter , err := regexp .Compile (regexFilterStr .ValueString ())
293
+ if err != nil {
294
+ return err
295
+ }
296
+ settings .RegexFilter = regexFilter
297
+
298
+ legacyMappingMap , ok := groupSyncAttr .Attributes ()["legacy_group_name_mapping" ].(types.Map )
299
+ if ! ok {
300
+ return fmt .Errorf ("oh jeez" )
301
+ }
302
+ var legacyMapping map [string ]string
303
+ diags = legacyMappingMap .ElementsAs (ctx , legacyMapping , false )
304
+ if diags .HasError () {
305
+ return fmt .Errorf ("oh jeez" )
306
+ }
307
+ settings .LegacyNameMapping = legacyMapping
308
+
309
+ _ , err = r .Client .PatchGroupIDPSyncSettings (ctx , orgID .String (), settings )
310
+ return err
311
+ }
0 commit comments