Skip to content

Commit

Permalink
Merge pull request #86 from jorcau/fix_project_shared-with-groups
Browse files Browse the repository at this point in the history
Update project `shared_with_groups` option: change type to TypeSet
  • Loading branch information
roidelapluie committed Feb 8, 2019
2 parents 075f015 + 1a159c2 commit d2f7ebb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
11 changes: 5 additions & 6 deletions gitlab/resource_gitlab_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func resourceGitlabProject() *schema.Resource {
Computed: true,
},
"shared_with_groups": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -170,7 +170,7 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
}

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

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

func expandSharedWithGroupsOptions(d []interface{}) []*gitlab.ShareWithGroupOptions {
func expandSharedWithGroupsOptions(v interface{}) []*gitlab.ShareWithGroupOptions {
shareWithGroupOptionsList := []*gitlab.ShareWithGroupOptions{}

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

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

// Get target groups from the TF config and current groups from Gitlab server
targetGroups := expandSharedWithGroupsOptions(
d.Get("shared_with_groups").([]interface{}))
targetGroups := expandSharedWithGroupsOptions(d.Get("shared_with_groups"))
project, _, err := client.Projects.GetProject(d.Id())
if err != nil {
return err
Expand Down
17 changes: 12 additions & 5 deletions gitlab/resource_gitlab_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestAccGitlabProject_basic(t *testing.T) {
GroupID int
GroupName string
GroupAccessLevel int
}{{0, "", 30}},
}{{0, fmt.Sprintf("foo-name-%d", rInt), 30}},
}),
),
},
Expand All @@ -106,7 +106,7 @@ func TestAccGitlabProject_basic(t *testing.T) {
GroupID int
GroupName string
GroupAccessLevel int
}{{0, "", 10}, {0, "", 30}},
}{{0, fmt.Sprintf("foo-name-%d", rInt), 10}, {0, fmt.Sprintf("foo2-name-%d", rInt), 30}},
}),
),
},
Expand Down Expand Up @@ -251,11 +251,18 @@ func testAccCheckGitlabProjectAttributes(project *gitlab.Project, want *testAccG
return fmt.Errorf("got visibility %q; want %q", project.Visibility, want.Visibility)
}

for i, group := range project.SharedWithGroups {
if group.GroupAccessLevel != want.SharedWithGroups[i].GroupAccessLevel {
return fmt.Errorf("got shared with groups access: %d; want %d", group.GroupAccessLevel, want.SharedWithGroups[i].GroupAccessLevel)
groupsToCheck := want.SharedWithGroups
for _, group := range project.SharedWithGroups {
for i, groupToCheck := range groupsToCheck {
if group.GroupName == groupToCheck.GroupName && group.GroupAccessLevel == groupToCheck.GroupAccessLevel {
groupsToCheck = append(groupsToCheck[:i], groupsToCheck[i+1:]...)
break
}
}
}
if len(groupsToCheck) != 0 {
return fmt.Errorf("got shared with groups: %v; want %v", project.SharedWithGroups, want.SharedWithGroups)
}

return nil
}
Expand Down

0 comments on commit d2f7ebb

Please sign in to comment.