@@ -3,9 +3,11 @@ package provider
33import (
44 "context"
55 "fmt"
6+ "regexp"
67
78 "github.com/coder/coder/v2/codersdk"
89 "github.com/coder/terraform-provider-coderd/internal/codersdkvalidator"
10+ "github.com/google/uuid"
911 "github.com/hashicorp/terraform-plugin-framework/path"
1012 "github.com/hashicorp/terraform-plugin-framework/resource"
1113 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -33,6 +35,9 @@ type OrganizationResourceModel struct {
3335 DisplayName types.String `tfsdk:"display_name"`
3436 Description types.String `tfsdk:"description"`
3537 Icon types.String `tfsdk:"icon"`
38+
39+ GroupSync types.Object `tfsdk:"group_sync"`
40+ RoleSync types.Object `tfsdk:"role_sync"`
3641}
3742
3843func NewOrganizationResource () resource.Resource {
@@ -82,6 +87,13 @@ func (r *OrganizationResource) Schema(ctx context.Context, req resource.SchemaRe
8287 Computed : true ,
8388 Default : stringdefault .StaticString ("" ),
8489 },
90+
91+ "group_sync" : schema.ObjectAttribute {
92+ Optional : true ,
93+ },
94+ "role_sync" : schema.ObjectAttribute {
95+ Optional : true ,
96+ },
8597 },
8698 }
8799}
@@ -207,6 +219,14 @@ func (r *OrganizationResource) Update(ctx context.Context, req resource.UpdateRe
207219 }
208220 tflog .Trace (ctx , "successfully updated organization" )
209221
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+
210230 // Save updated data into Terraform state
211231 resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
212232}
@@ -240,3 +260,52 @@ func (r *OrganizationResource) ImportState(ctx context.Context, req resource.Imp
240260 // set the `name` attribute.
241261 resource .ImportStatePassthroughID (ctx , path .Root ("name" ), req , resp )
242262}
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