Skip to content

Commit d2f7ebb

Browse files
authored
Merge pull request #86 from jorcau/fix_project_shared-with-groups
Update project `shared_with_groups` option: change type to TypeSet
2 parents 075f015 + 1a159c2 commit d2f7ebb

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

gitlab/resource_gitlab_project.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func resourceGitlabProject() *schema.Resource {
9494
Computed: true,
9595
},
9696
"shared_with_groups": {
97-
Type: schema.TypeList,
97+
Type: schema.TypeSet,
9898
Optional: true,
9999
Elem: &schema.Resource{
100100
Schema: map[string]*schema.Schema{
@@ -170,7 +170,7 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
170170
}
171171

172172
if v, ok := d.GetOk("shared_with_groups"); ok {
173-
options := expandSharedWithGroupsOptions(v.([]interface{}))
173+
options := expandSharedWithGroupsOptions(v)
174174

175175
for _, option := range options {
176176
_, err := client.Projects.ShareProjectWithGroup(project.ID, option)
@@ -298,10 +298,10 @@ func resourceGitlabProjectDelete(d *schema.ResourceData, meta interface{}) error
298298
return nil
299299
}
300300

301-
func expandSharedWithGroupsOptions(d []interface{}) []*gitlab.ShareWithGroupOptions {
301+
func expandSharedWithGroupsOptions(v interface{}) []*gitlab.ShareWithGroupOptions {
302302
shareWithGroupOptionsList := []*gitlab.ShareWithGroupOptions{}
303303

304-
for _, config := range d {
304+
for _, config := range v.(*schema.Set).List() {
305305
data := config.(map[string]interface{})
306306

307307
groupAccess := accessLevelNameToValue[data["group_access_level"].(string)]
@@ -368,8 +368,7 @@ func updateSharedWithGroups(d *schema.ResourceData, meta interface{}) error {
368368
var groupsToShare []*gitlab.ShareWithGroupOptions
369369

370370
// Get target groups from the TF config and current groups from Gitlab server
371-
targetGroups := expandSharedWithGroupsOptions(
372-
d.Get("shared_with_groups").([]interface{}))
371+
targetGroups := expandSharedWithGroupsOptions(d.Get("shared_with_groups"))
373372
project, _, err := client.Projects.GetProject(d.Id())
374373
if err != nil {
375374
return err

gitlab/resource_gitlab_project_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestAccGitlabProject_basic(t *testing.T) {
8484
GroupID int
8585
GroupName string
8686
GroupAccessLevel int
87-
}{{0, "", 30}},
87+
}{{0, fmt.Sprintf("foo-name-%d", rInt), 30}},
8888
}),
8989
),
9090
},
@@ -106,7 +106,7 @@ func TestAccGitlabProject_basic(t *testing.T) {
106106
GroupID int
107107
GroupName string
108108
GroupAccessLevel int
109-
}{{0, "", 10}, {0, "", 30}},
109+
}{{0, fmt.Sprintf("foo-name-%d", rInt), 10}, {0, fmt.Sprintf("foo2-name-%d", rInt), 30}},
110110
}),
111111
),
112112
},
@@ -251,11 +251,18 @@ func testAccCheckGitlabProjectAttributes(project *gitlab.Project, want *testAccG
251251
return fmt.Errorf("got visibility %q; want %q", project.Visibility, want.Visibility)
252252
}
253253

254-
for i, group := range project.SharedWithGroups {
255-
if group.GroupAccessLevel != want.SharedWithGroups[i].GroupAccessLevel {
256-
return fmt.Errorf("got shared with groups access: %d; want %d", group.GroupAccessLevel, want.SharedWithGroups[i].GroupAccessLevel)
254+
groupsToCheck := want.SharedWithGroups
255+
for _, group := range project.SharedWithGroups {
256+
for i, groupToCheck := range groupsToCheck {
257+
if group.GroupName == groupToCheck.GroupName && group.GroupAccessLevel == groupToCheck.GroupAccessLevel {
258+
groupsToCheck = append(groupsToCheck[:i], groupsToCheck[i+1:]...)
259+
break
260+
}
257261
}
258262
}
263+
if len(groupsToCheck) != 0 {
264+
return fmt.Errorf("got shared with groups: %v; want %v", project.SharedWithGroups, want.SharedWithGroups)
265+
}
259266

260267
return nil
261268
}

0 commit comments

Comments
 (0)