diff --git a/gitlab/resource_gitlab_project.go b/gitlab/resource_gitlab_project.go index 001c2966e..d75c54754 100644 --- a/gitlab/resource_gitlab_project.go +++ b/gitlab/resource_gitlab_project.go @@ -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{ @@ -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) @@ -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)] @@ -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 diff --git a/gitlab/resource_gitlab_project_test.go b/gitlab/resource_gitlab_project_test.go index fc476d340..b37f2116c 100644 --- a/gitlab/resource_gitlab_project_test.go +++ b/gitlab/resource_gitlab_project_test.go @@ -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}}, }), ), }, @@ -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}}, }), ), }, @@ -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 }