@@ -172,28 +172,6 @@ var resourceGitLabProjectSchema = map[string]*schema.Schema{
172
172
Elem : & schema.Schema {Type : schema .TypeString },
173
173
Set : schema .HashString ,
174
174
},
175
- "shared_with_groups" : {
176
- Type : schema .TypeSet ,
177
- Optional : true ,
178
- Elem : & schema.Resource {
179
- Schema : map [string ]* schema.Schema {
180
- "group_id" : {
181
- Type : schema .TypeInt ,
182
- Required : true ,
183
- },
184
- "group_access_level" : {
185
- Type : schema .TypeString ,
186
- Required : true ,
187
- ValidateFunc : validation .StringInSlice ([]string {
188
- "no one" , "guest" , "reporter" , "developer" , "maintainer" }, false ),
189
- },
190
- "group_name" : {
191
- Type : schema .TypeString ,
192
- Computed : true ,
193
- },
194
- },
195
- },
196
- },
197
175
"archived" : {
198
176
Type : schema .TypeBool ,
199
177
Description : "Whether the project is archived." ,
@@ -249,7 +227,6 @@ func resourceGitlabProjectSetToState(d *schema.ResourceData, project *gitlab.Pro
249
227
d .Set ("web_url" , project .WebURL )
250
228
d .Set ("runners_token" , project .RunnersToken )
251
229
d .Set ("shared_runners_enabled" , project .SharedRunnersEnabled )
252
- d .Set ("shared_with_groups" , flattenSharedWithGroupsOptions (project ))
253
230
d .Set ("tags" , project .TagList )
254
231
d .Set ("archived" , project .Archived )
255
232
d .Set ("remove_source_branch_after_merge" , project .RemoveSourceBranchAfterMerge )
@@ -334,14 +311,6 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
334
311
}
335
312
}
336
313
337
- if v , ok := d .GetOk ("shared_with_groups" ); ok {
338
- for _ , option := range expandSharedWithGroupsOptions (v ) {
339
- if _ , err := client .Projects .ShareProjectWithGroup (project .ID , option ); err != nil {
340
- return err
341
- }
342
- }
343
- }
344
-
345
314
if d .Get ("archived" ).(bool ) {
346
315
// strange as it may seem, this project is created in archived state...
347
316
if _ , _ , err := client .Projects .ArchiveProject (d .Id ()); err != nil {
@@ -476,12 +445,6 @@ func resourceGitlabProjectUpdate(d *schema.ResourceData, meta interface{}) error
476
445
}
477
446
}
478
447
479
- if d .HasChange ("shared_with_groups" ) {
480
- if err := updateSharedWithGroups (d , meta ); err != nil {
481
- return err
482
- }
483
- }
484
-
485
448
if d .HasChange ("archived" ) {
486
449
if d .Get ("archived" ).(bool ) {
487
450
if _ , _ , err := client .Projects .ArchiveProject (d .Id ()); err != nil {
@@ -538,121 +501,3 @@ func resourceGitlabProjectDelete(d *schema.ResourceData, meta interface{}) error
538
501
}
539
502
return nil
540
503
}
541
-
542
- func expandSharedWithGroupsOptions (v interface {}) []* gitlab.ShareWithGroupOptions {
543
- shareWithGroupOptionsList := []* gitlab.ShareWithGroupOptions {}
544
-
545
- for _ , config := range v .(* schema.Set ).List () {
546
- data := config .(map [string ]interface {})
547
-
548
- groupAccess := accessLevelNameToValue [data ["group_access_level" ].(string )]
549
-
550
- shareWithGroupOptions := & gitlab.ShareWithGroupOptions {
551
- GroupID : gitlab .Int (data ["group_id" ].(int )),
552
- GroupAccess : & groupAccess ,
553
- }
554
-
555
- shareWithGroupOptionsList = append (shareWithGroupOptionsList ,
556
- shareWithGroupOptions )
557
- }
558
-
559
- return shareWithGroupOptionsList
560
- }
561
-
562
- func flattenSharedWithGroupsOptions (project * gitlab.Project ) []interface {} {
563
- sharedWithGroups := project .SharedWithGroups
564
- sharedWithGroupsList := []interface {}{}
565
-
566
- for _ , option := range sharedWithGroups {
567
- values := map [string ]interface {}{
568
- "group_id" : option .GroupID ,
569
- "group_access_level" : accessLevelValueToName [gitlab .AccessLevelValue (
570
- option .GroupAccessLevel )],
571
- "group_name" : option .GroupName ,
572
- }
573
-
574
- sharedWithGroupsList = append (sharedWithGroupsList , values )
575
- }
576
-
577
- return sharedWithGroupsList
578
- }
579
-
580
- func findGroupProjectSharedWith (target * gitlab.ShareWithGroupOptions ,
581
- groups []* gitlab.ShareWithGroupOptions ) (* gitlab.ShareWithGroupOptions , int , error ) {
582
- for i , group := range groups {
583
- if * group .GroupID == * target .GroupID {
584
- return group , i , nil
585
- }
586
- }
587
-
588
- return nil , 0 , fmt .Errorf ("group not found" )
589
- }
590
-
591
- func getGroupsProjectSharedWith (project * gitlab.Project ) []* gitlab.ShareWithGroupOptions {
592
- sharedGroups := []* gitlab.ShareWithGroupOptions {}
593
-
594
- for _ , group := range project .SharedWithGroups {
595
- sharedGroups = append (sharedGroups , & gitlab.ShareWithGroupOptions {
596
- GroupID : gitlab .Int (group .GroupID ),
597
- GroupAccess : gitlab .AccessLevel (gitlab .AccessLevelValue (
598
- group .GroupAccessLevel )),
599
- })
600
- }
601
-
602
- return sharedGroups
603
- }
604
-
605
- func updateSharedWithGroups (d * schema.ResourceData , meta interface {}) error {
606
- client := meta .(* gitlab.Client )
607
-
608
- var groupsToUnshare []* gitlab.ShareWithGroupOptions
609
- var groupsToShare []* gitlab.ShareWithGroupOptions
610
-
611
- // Get target groups from the TF config and current groups from Gitlab server
612
- targetGroups := expandSharedWithGroupsOptions (d .Get ("shared_with_groups" ))
613
- project , _ , err := client .Projects .GetProject (d .Id (), nil )
614
- if err != nil {
615
- return err
616
- }
617
- currentGroups := getGroupsProjectSharedWith (project )
618
-
619
- for _ , targetGroup := range targetGroups {
620
- currentGroup , index , err := findGroupProjectSharedWith (targetGroup , currentGroups )
621
-
622
- // If no corresponding group is found, it must be added
623
- if err != nil {
624
- groupsToShare = append (groupsToShare , targetGroup )
625
- continue
626
- }
627
-
628
- // If group is different it must be deleted and added again
629
- if * targetGroup .GroupAccess != * currentGroup .GroupAccess {
630
- groupsToShare = append (groupsToShare , targetGroup )
631
- groupsToUnshare = append (groupsToUnshare , targetGroup )
632
- }
633
-
634
- // Remove currentGroup from from list
635
- currentGroups = append (currentGroups [:index ], currentGroups [index + 1 :]... )
636
- }
637
-
638
- // All groups still present in currentGroup must be deleted
639
- groupsToUnshare = append (groupsToUnshare , currentGroups ... )
640
-
641
- // Unshare groups to delete and update
642
- for _ , group := range groupsToUnshare {
643
- _ , err := client .Projects .DeleteSharedProjectFromGroup (d .Id (), * group .GroupID )
644
- if err != nil {
645
- return err
646
- }
647
- }
648
-
649
- // Share groups to add and update
650
- for _ , group := range groupsToShare {
651
- _ , err := client .Projects .ShareProjectWithGroup (d .Id (), group )
652
- if err != nil {
653
- return err
654
- }
655
- }
656
-
657
- return nil
658
- }
0 commit comments