|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/terraform" |
| 10 | + "github.com/xanzy/go-gitlab" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccGitlabGroupShareGroup_basic(t *testing.T) { |
| 14 | + randName := acctest.RandomWithPrefix("acctest") |
| 15 | + |
| 16 | + resource.Test(t, resource.TestCase{ |
| 17 | + PreCheck: func() { testAccPreCheck(t) }, |
| 18 | + Providers: testAccProviders, |
| 19 | + Steps: []resource.TestStep{ |
| 20 | + // Share a new group with another group |
| 21 | + { |
| 22 | + Config: testAccGitlabGroupShareGroupConfig(randName, "guest", "2099-01-01"), |
| 23 | + Check: testAccCheckGitlabGroupSharedWithGroup(randName, "2099-01-01", gitlab.GuestPermissions), |
| 24 | + }, |
| 25 | + // Update the share group |
| 26 | + { |
| 27 | + Config: testAccGitlabGroupShareGroupConfig(randName, "reporter", "2099-02-02"), |
| 28 | + Check: testAccCheckGitlabGroupSharedWithGroup(randName, "2099-02-02", gitlab.ReporterPermissions), |
| 29 | + }, |
| 30 | + // Delete the gitlab_group_share_group resource |
| 31 | + { |
| 32 | + Config: testAccGitlabGroupShareGroupConfigDelete(randName), |
| 33 | + Check: testAccCheckGitlabGroupIsNotShared(randName), |
| 34 | + }, |
| 35 | + }, |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +func TestAccGitlabGroupShareGroup_import(t *testing.T) { |
| 40 | + randName := acctest.RandomWithPrefix("acctest") |
| 41 | + |
| 42 | + resource.Test(t, resource.TestCase{ |
| 43 | + Providers: testAccProviders, |
| 44 | + PreCheck: func() { testAccPreCheck(t) }, |
| 45 | + CheckDestroy: testAccCheckGitlabGroupDestroy, |
| 46 | + Steps: []resource.TestStep{ |
| 47 | + { |
| 48 | + // create shared groups |
| 49 | + Config: testAccGitlabGroupShareGroupConfig(randName, "guest", "2099-03-03"), |
| 50 | + Check: testAccCheckGitlabGroupSharedWithGroup(randName, "2099-03-03", gitlab.GuestPermissions), |
| 51 | + }, |
| 52 | + { |
| 53 | + // Verify Import |
| 54 | + ResourceName: "gitlab_group_share_group.test", |
| 55 | + ImportState: true, |
| 56 | + ImportStateVerify: true, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +func testAccCheckGitlabGroupSharedWithGroup( |
| 63 | + groupName string, |
| 64 | + expireTime string, |
| 65 | + accessLevel gitlab.AccessLevelValue, |
| 66 | +) resource.TestCheckFunc { |
| 67 | + return func(_ *terraform.State) error { |
| 68 | + client := testAccProvider.Meta().(*gitlab.Client) |
| 69 | + |
| 70 | + mainGroup, _, err := client.Groups.GetGroup(fmt.Sprintf("%s_main", groupName)) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + sharedGroupsCount := len(mainGroup.SharedWithGroups) |
| 76 | + if sharedGroupsCount != 1 { |
| 77 | + return fmt.Errorf("Number of shared groups was %d (wanted %d)", sharedGroupsCount, 1) |
| 78 | + } |
| 79 | + |
| 80 | + sharedGroup := mainGroup.SharedWithGroups[0] |
| 81 | + |
| 82 | + if sharedGroup.GroupName != fmt.Sprintf("%s_share", groupName) { |
| 83 | + return fmt.Errorf("group name was %s (wanted %s)", sharedGroup.GroupName, fmt.Sprintf("%s_share", groupName)) |
| 84 | + } |
| 85 | + |
| 86 | + if gitlab.AccessLevelValue(sharedGroup.GroupAccessLevel) != accessLevel { |
| 87 | + return fmt.Errorf("groupAccessLevel was %d (wanted %d)", sharedGroup.GroupAccessLevel, accessLevel) |
| 88 | + } |
| 89 | + |
| 90 | + if sharedGroup.ExpiresAt.String() != expireTime { |
| 91 | + return fmt.Errorf("expired time was %s (wanted %s)", sharedGroup.ExpiresAt.String(), expireTime) |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func testAccCheckGitlabGroupIsNotShared(groupName string) resource.TestCheckFunc { |
| 99 | + return func(_ *terraform.State) error { |
| 100 | + client := testAccProvider.Meta().(*gitlab.Client) |
| 101 | + |
| 102 | + mainGroup, _, err := client.Groups.GetGroup(fmt.Sprintf("%s_main", groupName)) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + sharedGroupsCount := len(mainGroup.SharedWithGroups) |
| 108 | + if sharedGroupsCount != 0 { |
| 109 | + return fmt.Errorf("Number of shared groups was %d (wanted %d)", sharedGroupsCount, 0) |
| 110 | + } |
| 111 | + |
| 112 | + return nil |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func testAccGitlabGroupShareGroupConfig( |
| 117 | + randName string, |
| 118 | + accessLevel string, |
| 119 | + expireTime string, |
| 120 | +) string { |
| 121 | + return fmt.Sprintf( |
| 122 | + ` |
| 123 | + resource "gitlab_group" "test_main" { |
| 124 | + name = "%[1]s_main" |
| 125 | + path = "%[1]s_main" |
| 126 | + } |
| 127 | +
|
| 128 | + resource "gitlab_group" "test_share" { |
| 129 | + name = "%[1]s_share" |
| 130 | + path = "%[1]s_share" |
| 131 | + } |
| 132 | +
|
| 133 | + resource "gitlab_group_share_group" "test" { |
| 134 | + group_id = gitlab_group.test_main.id |
| 135 | + share_group_id = gitlab_group.test_share.id |
| 136 | + group_access = "%[2]s" |
| 137 | + expires_at = "%[3]s" |
| 138 | + } |
| 139 | + `, |
| 140 | + randName, |
| 141 | + accessLevel, |
| 142 | + expireTime, |
| 143 | + ) |
| 144 | +} |
| 145 | + |
| 146 | +func testAccGitlabGroupShareGroupConfigDelete(randName string) string { |
| 147 | + return fmt.Sprintf( |
| 148 | + ` |
| 149 | + resource "gitlab_group" "test_main" { |
| 150 | + name = "%[1]s_main" |
| 151 | + path = "%[1]s_main" |
| 152 | + } |
| 153 | +
|
| 154 | + resource "gitlab_group" "test_share" { |
| 155 | + name = "%[1]s_share" |
| 156 | + path = "%[1]s_share" |
| 157 | + } |
| 158 | + `, |
| 159 | + randName, |
| 160 | + ) |
| 161 | +} |
0 commit comments