Skip to content

Commit b3732f9

Browse files
authored
Merge pull request #998 from Shocktrooper/lint-issue-AT001
Lint Issues- Testing out addition of Check Destroy in some Acceptance Tests + other refactoring
2 parents 116b5bc + f3e1194 commit b3732f9

12 files changed

+132
-117
lines changed

internal/provider/resource_gitlab_group.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"log"
7-
"net/http"
87
"strings"
98
"time"
109

@@ -251,9 +250,9 @@ func resourceGitlabGroupRead(ctx context.Context, d *schema.ResourceData, meta i
251250
client := meta.(*gitlab.Client)
252251
log.Printf("[DEBUG] read gitlab group %s", d.Id())
253252

254-
group, resp, err := client.Groups.GetGroup(d.Id(), nil, gitlab.WithContext(ctx))
253+
group, _, err := client.Groups.GetGroup(d.Id(), nil, gitlab.WithContext(ctx))
255254
if err != nil {
256-
if resp != nil && resp.StatusCode == http.StatusNotFound {
255+
if is404(err) {
257256
log.Printf("[DEBUG] gitlab group %s not found so removing from state", d.Id())
258257
d.SetId("")
259258
return nil

internal/provider/resource_gitlab_group_share_group.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"log"
7-
"net/http"
87
"strconv"
98

109
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -93,9 +92,9 @@ func resourceGitlabGroupShareGroupRead(ctx context.Context, d *schema.ResourceDa
9392
}
9493

9594
// Query main group
96-
group, resp, err := client.Groups.GetGroup(groupId, nil, gitlab.WithContext(ctx))
95+
group, _, err := client.Groups.GetGroup(groupId, nil, gitlab.WithContext(ctx))
9796
if err != nil {
98-
if resp != nil && resp.StatusCode == http.StatusNotFound {
97+
if is404(err) {
9998
log.Printf("[DEBUG] gitlab group %s not found so removing from state", groupId)
10099
d.SetId("")
101100
return nil

internal/provider/resource_gitlab_group_share_group_test.go

Lines changed: 59 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,58 @@ import (
44
"fmt"
55
"testing"
66

7-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
87
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
98
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
109
"github.com/xanzy/go-gitlab"
1110
)
1211

1312
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]
1517

16-
// lintignore: AT001 // TODO: Resolve this tfproviderlint issue
1718
resource.Test(t, resource.TestCase{
1819
PreCheck: func() { testAccPreCheck(t) },
1920
ProviderFactories: providerFactories,
21+
CheckDestroy: testAccCheckGitlabShareGroupDestroy,
2022
Steps: []resource.TestStep{
2123
// Share a new group with another group
2224
{
23-
Config: testAccGitlabGroupShareGroupConfig(
24-
randName,
25+
Config: testAccGitlabGroupShareGroupConfig(mainGroup.ID, sharedGroup.ID,
2526
`
2627
group_access = "guest"
2728
expires_at = "2099-01-01"
2829
`,
2930
),
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,
3138
},
3239
// Update the share group
3340
{
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),
3643
},
37-
// Delete the gitlab_group_share_group resource
3844
{
39-
Config: testAccGitlabGroupShareGroupConfigDelete(randName),
40-
Check: testAccCheckGitlabGroupIsNotShared(randName),
45+
// Verify Import
46+
ResourceName: "gitlab_group_share_group.test",
47+
ImportState: true,
48+
ImportStateVerify: true,
4149
},
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
5551
{
56-
// create shared groups
57-
Config: testAccGitlabGroupShareGroupConfig(
58-
randName,
52+
Config: testAccGitlabGroupShareGroupConfig(mainGroup.ID, sharedGroup.ID,
5953
`
6054
group_access = "guest"
61-
expires_at = "2099-03-03"
55+
expires_at = "2099-01-01"
6256
`,
6357
),
64-
Check: testAccCheckGitlabGroupSharedWithGroup(randName, "2099-03-03", gitlab.GuestPermissions),
58+
Check: testAccCheckGitlabGroupSharedWithGroup(mainGroup.Name, sharedGroup.Name, "2099-01-01", gitlab.GuestPermissions),
6559
},
6660
{
6761
// Verify Import
@@ -73,13 +67,9 @@ func TestAccGitlabGroupShareGroup_import(t *testing.T) {
7367
})
7468
}
7569

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 {
8171
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)
8373
if err != nil {
8474
return err
8575
}
@@ -91,8 +81,8 @@ func testAccCheckGitlabGroupSharedWithGroup(
9181

9282
sharedGroup := mainGroup.SharedWithGroups[0]
9383

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)
9686
}
9787

9888
if gitlab.AccessLevelValue(sharedGroup.GroupAccessLevel) != accessLevel {
@@ -109,62 +99,47 @@ func testAccCheckGitlabGroupSharedWithGroup(
10999
}
110100
}
111101

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+
}
122126
}
123-
124-
return nil
125127
}
128+
129+
return nil
126130
}
127131

128-
func testAccGitlabGroupShareGroupConfig(
129-
randName string,
130-
shareGroupSettings string,
131-
) string {
132+
func testAccGitlabGroupShareGroupConfig(mainGroupId int, shareGroupId int, shareGroupSettings string) string {
132133
return fmt.Sprintf(
133134
`
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-
144135
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
148139
}
149140
`,
150-
randName,
141+
mainGroupId,
142+
shareGroupId,
151143
shareGroupSettings,
152144
)
153145
}
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-
}

internal/provider/resource_gitlab_group_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package provider
22

33
import (
44
"fmt"
5-
"net/http"
65
"testing"
76
"time"
87

@@ -284,8 +283,8 @@ func testAccCheckGitlabGroupDisappears(group *gitlab.Group) resource.TestCheckFu
284283
// Fixes groups API async deletion issue
285284
// https://github.com/gitlabhq/terraform-provider-gitlab/issues/319
286285
for start := time.Now(); time.Since(start) < 15*time.Second; {
287-
g, resp, err := testGitlabClient.Groups.GetGroup(group.ID, nil)
288-
if resp != nil && resp.StatusCode == http.StatusNotFound {
286+
g, _, err := testGitlabClient.Groups.GetGroup(group.ID, nil)
287+
if is404(err) {
289288
return nil
290289
}
291290
if g != nil && g.MarkedForDeletionOn != nil {

internal/provider/resource_gitlab_instance_variable.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package provider
33
import (
44
"context"
55
"log"
6-
"net/http"
76

87
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
98
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -62,9 +61,9 @@ func resourceGitlabInstanceVariableRead(ctx context.Context, d *schema.ResourceD
6261

6362
log.Printf("[DEBUG] read gitlab instance level CI variable %s", key)
6463

65-
v, resp, err := client.InstanceVariables.GetVariable(key, gitlab.WithContext(ctx))
64+
v, _, err := client.InstanceVariables.GetVariable(key, gitlab.WithContext(ctx))
6665
if err != nil {
67-
if resp.StatusCode == http.StatusNotFound {
66+
if is404(err) {
6867
log.Printf("[DEBUG] gitlab instance level CI variable for %s not found so removing from state", d.Id())
6968
d.SetId("")
7069
return nil

internal/provider/resource_gitlab_instance_variable_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ func TestAccGitlabInstanceVariable_basic(t *testing.T) {
1515
var instanceVariable gitlab.InstanceVariable
1616
rString := acctest.RandString(5)
1717

18-
// lintignore: AT001 // TODO: Resolve this tfproviderlint issue
1918
resource.Test(t, resource.TestCase{
2019
PreCheck: func() { testAccPreCheck(t) },
2120
ProviderFactories: providerFactories,
21+
CheckDestroy: testAccCheckGitlabInstanceVariableDestroy,
2222
Steps: []resource.TestStep{
2323
// Create a variable with default options
2424
{
@@ -120,6 +120,29 @@ func testAccCheckGitlabInstanceVariableExists(n string, instanceVariable *gitlab
120120
}
121121
}
122122

123+
func testAccCheckGitlabInstanceVariableDestroy(s *terraform.State) error {
124+
var key string
125+
126+
for _, rs := range s.RootModule().Resources {
127+
if rs.Type == "gitlab_instance_variable" {
128+
key = rs.Primary.ID
129+
}
130+
}
131+
132+
iv, _, err := testGitlabClient.InstanceVariables.GetVariable(key)
133+
if err == nil {
134+
if iv != nil {
135+
return fmt.Errorf("Instance Variable %s still exists", key)
136+
}
137+
} else {
138+
if !is404(err) {
139+
return err
140+
}
141+
}
142+
143+
return nil
144+
}
145+
123146
type testAccGitlabInstanceVariableExpectedAttributes struct {
124147
Key string
125148
Value string

internal/provider/resource_gitlab_project_freeze_period.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"log"
7-
"net/http"
87
"strconv"
98

109
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -84,9 +83,9 @@ func resourceGitlabProjectFreezePeriodRead(ctx context.Context, d *schema.Resour
8483

8584
log.Printf("[DEBUG] read gitlab FreezePeriod %s/%d", projectID, freezePeriodID)
8685

87-
freezePeriod, resp, err := client.FreezePeriods.GetFreezePeriod(projectID, freezePeriodID, gitlab.WithContext(ctx))
86+
freezePeriod, _, err := client.FreezePeriods.GetFreezePeriod(projectID, freezePeriodID, gitlab.WithContext(ctx))
8887
if err != nil {
89-
if resp != nil && resp.StatusCode == http.StatusNotFound {
88+
if is404(err) {
9089
log.Printf("[DEBUG] project freeze period for %s not found so removing it from state", d.Id())
9190
d.SetId("")
9291
return nil

internal/provider/resource_gitlab_project_membership.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"log"
7-
"net/http"
87
"strconv"
98
"strings"
109

@@ -85,9 +84,9 @@ func resourceGitlabProjectMembershipRead(ctx context.Context, d *schema.Resource
8584
return diag.FromErr(err)
8685
}
8786

88-
projectMember, resp, err := client.ProjectMembers.GetProjectMember(projectId, userId, gitlab.WithContext(ctx))
87+
projectMember, _, err := client.ProjectMembers.GetProjectMember(projectId, userId, gitlab.WithContext(ctx))
8988
if err != nil {
90-
if resp != nil && resp.StatusCode == http.StatusNotFound {
89+
if is404(err) {
9190
log.Printf("[DEBUG] gitlab project membership for %s not found so removing from state", d.Id())
9291
d.SetId("")
9392
return nil

0 commit comments

Comments
 (0)