@@ -4,64 +4,58 @@ import (
4
4
"fmt"
5
5
"testing"
6
6
7
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8
7
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9
8
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10
9
"github.com/xanzy/go-gitlab"
11
10
)
12
11
13
12
func TestAccGitlabGroupShareGroup_basic (t * testing.T ) {
14
- randName := acctest .RandomWithPrefix ("acctest" )
13
+ testAccCheck (t )
14
+
15
+ mainGroup := testAccCreateGroups (t , 1 )[0 ]
16
+ sharedGroup := testAccCreateGroups (t , 1 )[0 ]
15
17
16
- // lintignore: AT001 // TODO: Resolve this tfproviderlint issue
17
18
resource .Test (t , resource.TestCase {
18
19
PreCheck : func () { testAccPreCheck (t ) },
19
20
ProviderFactories : providerFactories ,
21
+ CheckDestroy : testAccCheckGitlabShareGroupDestroy ,
20
22
Steps : []resource.TestStep {
21
23
// Share a new group with another group
22
24
{
23
- Config : testAccGitlabGroupShareGroupConfig (
24
- randName ,
25
+ Config : testAccGitlabGroupShareGroupConfig (mainGroup .ID , sharedGroup .ID ,
25
26
`
26
27
group_access = "guest"
27
28
expires_at = "2099-01-01"
28
29
` ,
29
30
),
30
- Check : testAccCheckGitlabGroupSharedWithGroup (randName , "2099-01-01" , gitlab .GuestPermissions ),
31
+ Check : testAccCheckGitlabGroupSharedWithGroup (mainGroup .Name , sharedGroup .Name , "2099-01-01" , gitlab .GuestPermissions ),
32
+ },
33
+ {
34
+ // Verify Import
35
+ ResourceName : "gitlab_group_share_group.test" ,
36
+ ImportState : true ,
37
+ ImportStateVerify : true ,
31
38
},
32
39
// Update the share group
33
40
{
34
- Config : testAccGitlabGroupShareGroupConfig (randName , `group_access = "reporter"` ),
35
- Check : testAccCheckGitlabGroupSharedWithGroup (randName , "" , gitlab .ReporterPermissions ),
41
+ Config : testAccGitlabGroupShareGroupConfig (mainGroup . ID , sharedGroup . ID , `group_access = "reporter"` ),
42
+ Check : testAccCheckGitlabGroupSharedWithGroup (mainGroup . Name , sharedGroup . Name , "" , gitlab .ReporterPermissions ),
36
43
},
37
- // Delete the gitlab_group_share_group resource
38
44
{
39
- Config : testAccGitlabGroupShareGroupConfigDelete (randName ),
40
- Check : testAccCheckGitlabGroupIsNotShared (randName ),
45
+ // Verify Import
46
+ ResourceName : "gitlab_group_share_group.test" ,
47
+ ImportState : true ,
48
+ ImportStateVerify : true ,
41
49
},
42
- },
43
- })
44
- }
45
-
46
- // lintignore: AT002 // TODO: Resolve this tfproviderlint issue
47
- func TestAccGitlabGroupShareGroup_import (t * testing.T ) {
48
- randName := acctest .RandomWithPrefix ("acctest" )
49
-
50
- resource .Test (t , resource.TestCase {
51
- ProviderFactories : providerFactories ,
52
- PreCheck : func () { testAccPreCheck (t ) },
53
- CheckDestroy : testAccCheckGitlabGroupDestroy ,
54
- Steps : []resource.TestStep {
50
+ // Update share group back to initial settings
55
51
{
56
- // create shared groups
57
- Config : testAccGitlabGroupShareGroupConfig (
58
- randName ,
52
+ Config : testAccGitlabGroupShareGroupConfig (mainGroup .ID , sharedGroup .ID ,
59
53
`
60
54
group_access = "guest"
61
- expires_at = "2099-03-03 "
55
+ expires_at = "2099-01-01 "
62
56
` ,
63
57
),
64
- Check : testAccCheckGitlabGroupSharedWithGroup (randName , "2099-03-03 " , gitlab .GuestPermissions ),
58
+ Check : testAccCheckGitlabGroupSharedWithGroup (mainGroup . Name , sharedGroup . Name , "2099-01-01 " , gitlab .GuestPermissions ),
65
59
},
66
60
{
67
61
// Verify Import
@@ -73,13 +67,9 @@ func TestAccGitlabGroupShareGroup_import(t *testing.T) {
73
67
})
74
68
}
75
69
76
- func testAccCheckGitlabGroupSharedWithGroup (
77
- groupName string ,
78
- expireTime string ,
79
- accessLevel gitlab.AccessLevelValue ,
80
- ) resource.TestCheckFunc {
70
+ func testAccCheckGitlabGroupSharedWithGroup (mainGroupName string , sharedGroupName string , expireTime string , accessLevel gitlab.AccessLevelValue ) resource.TestCheckFunc {
81
71
return func (_ * terraform.State ) error {
82
- mainGroup , _ , err := testGitlabClient .Groups .GetGroup (fmt . Sprintf ( "%s_main" , groupName ) , nil )
72
+ mainGroup , _ , err := testGitlabClient .Groups .GetGroup (mainGroupName , nil )
83
73
if err != nil {
84
74
return err
85
75
}
@@ -91,8 +81,8 @@ func testAccCheckGitlabGroupSharedWithGroup(
91
81
92
82
sharedGroup := mainGroup .SharedWithGroups [0 ]
93
83
94
- if sharedGroup .GroupName != fmt . Sprintf ( "%s_share" , groupName ) {
95
- return fmt .Errorf ("group name was %s (wanted %s)" , sharedGroup .GroupName , fmt . Sprintf ( "%s_share" , groupName ) )
84
+ if sharedGroup .GroupName != sharedGroupName {
85
+ return fmt .Errorf ("group name was %s (wanted %s)" , sharedGroup .GroupName , sharedGroupName )
96
86
}
97
87
98
88
if gitlab .AccessLevelValue (sharedGroup .GroupAccessLevel ) != accessLevel {
@@ -109,62 +99,47 @@ func testAccCheckGitlabGroupSharedWithGroup(
109
99
}
110
100
}
111
101
112
- func testAccCheckGitlabGroupIsNotShared (groupName string ) resource.TestCheckFunc {
113
- return func (_ * terraform.State ) error {
114
- mainGroup , _ , err := testGitlabClient .Groups .GetGroup (fmt .Sprintf ("%s_main" , groupName ), nil )
115
- if err != nil {
116
- return err
117
- }
118
-
119
- sharedGroupsCount := len (mainGroup .SharedWithGroups )
120
- if sharedGroupsCount != 0 {
121
- return fmt .Errorf ("Number of shared groups was %d (wanted %d)" , sharedGroupsCount , 0 )
102
+ func testAccCheckGitlabShareGroupDestroy (s * terraform.State ) error {
103
+ var groupId string
104
+ var sharedGroupId int
105
+ var err error
106
+
107
+ for _ , rs := range s .RootModule ().Resources {
108
+ if rs .Type == "gitlab_group_share_group" {
109
+ groupId , sharedGroupId , err = groupIdsFromId (rs .Primary .ID )
110
+ if err != nil {
111
+ return fmt .Errorf ("[ERROR] cannot get Group ID and ShareGroupId from input: %v" , rs .Primary .ID )
112
+ }
113
+
114
+ // Get Main Group
115
+ group , _ , err := testGitlabClient .Groups .GetGroup (groupId , nil )
116
+ if err != nil {
117
+ return err
118
+ }
119
+
120
+ // Make sure that SharedWithGroups attribute on the main group does not contain the shared group id at all
121
+ for _ , sharedGroup := range group .SharedWithGroups {
122
+ if sharedGroupId == sharedGroup .GroupID {
123
+ return fmt .Errorf ("GitLab Group Share %d still exists" , sharedGroupId )
124
+ }
125
+ }
122
126
}
123
-
124
- return nil
125
127
}
128
+
129
+ return nil
126
130
}
127
131
128
- func testAccGitlabGroupShareGroupConfig (
129
- randName string ,
130
- shareGroupSettings string ,
131
- ) string {
132
+ func testAccGitlabGroupShareGroupConfig (mainGroupId int , shareGroupId int , shareGroupSettings string ) string {
132
133
return fmt .Sprintf (
133
134
`
134
- resource "gitlab_group" "test_main" {
135
- name = "%[1]s_main"
136
- path = "%[1]s_main"
137
- }
138
-
139
- resource "gitlab_group" "test_share" {
140
- name = "%[1]s_share"
141
- path = "%[1]s_share"
142
- }
143
-
144
135
resource "gitlab_group_share_group" "test" {
145
- group_id = gitlab_group.test_main.id
146
- share_group_id = gitlab_group.test_share.id
147
- %[2 ]s
136
+ group_id = %[1]d
137
+ share_group_id = %[2]d
138
+ %[3 ]s
148
139
}
149
140
` ,
150
- randName ,
141
+ mainGroupId ,
142
+ shareGroupId ,
151
143
shareGroupSettings ,
152
144
)
153
145
}
154
-
155
- func testAccGitlabGroupShareGroupConfigDelete (randName string ) string {
156
- return fmt .Sprintf (
157
- `
158
- resource "gitlab_group" "test_main" {
159
- name = "%[1]s_main"
160
- path = "%[1]s_main"
161
- }
162
-
163
- resource "gitlab_group" "test_share" {
164
- name = "%[1]s_share"
165
- path = "%[1]s_share"
166
- }
167
- ` ,
168
- randName ,
169
- )
170
- }
0 commit comments