Skip to content

Commit 60d19c4

Browse files
committed
Linter fixes + small refactors
1 parent dfd36e5 commit 60d19c4

13 files changed

+139
-123
lines changed

GNUmakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ endif
2020
test: ## Run unit tests.
2121
go test $(TESTARGS) $(PROVIDER_SRC_DIR)
2222

23-
TFPROVIDERLINTX_CHECKS = -XAT001=false -XR003=false -XS002=false
23+
TFPROVIDERLINTX_CHECKS = -XR003=false -XS002=false
2424

2525
fmt: tool-golangci-lint tool-tfproviderlintx tool-terraform tool-shfmt ## Format files and fix issues.
2626
gofmt -w -s .

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: 62 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,82 +4,61 @@ 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),
3132
},
32-
// Update the share group
33-
{
34-
Config: testAccGitlabGroupShareGroupConfig(randName, `group_access = "reporter"`),
35-
Check: testAccCheckGitlabGroupSharedWithGroup(randName, "", gitlab.ReporterPermissions),
36-
},
37-
// Delete the gitlab_group_share_group resource
3833
{
39-
Config: testAccGitlabGroupShareGroupConfigDelete(randName),
40-
Check: testAccCheckGitlabGroupIsNotShared(randName),
34+
// Verify Import
35+
ResourceName: "gitlab_group_share_group.test",
36+
ImportState: true,
37+
ImportStateVerify: true,
4138
},
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{
39+
// Update the share group
5540
{
56-
// create shared groups
57-
Config: testAccGitlabGroupShareGroupConfig(
58-
randName,
59-
`
60-
group_access = "guest"
61-
expires_at = "2099-03-03"
62-
`,
63-
),
64-
Check: testAccCheckGitlabGroupSharedWithGroup(randName, "2099-03-03", gitlab.GuestPermissions),
41+
Config: testAccGitlabGroupShareGroupConfig(mainGroup.ID, sharedGroup.ID, `group_access = "reporter"`),
42+
Check: testAccCheckGitlabGroupSharedWithGroup(mainGroup.Name, sharedGroup.Name, "", gitlab.ReporterPermissions),
6543
},
6644
{
6745
// Verify Import
6846
ResourceName: "gitlab_group_share_group.test",
6947
ImportState: true,
7048
ImportStateVerify: true,
7149
},
50+
// Delete the gitlab_group_share_group resource
51+
{
52+
Config: testAccGitlabGroupShareGroupConfigDelete(),
53+
Check: testAccCheckGitlabGroupIsNotShared(mainGroup.Name),
54+
},
7255
},
7356
})
7457
}
7558

76-
func testAccCheckGitlabGroupSharedWithGroup(
77-
groupName string,
78-
expireTime string,
79-
accessLevel gitlab.AccessLevelValue,
80-
) resource.TestCheckFunc {
59+
func testAccCheckGitlabGroupSharedWithGroup(mainGroupName string, sharedGroupName string, expireTime string, accessLevel gitlab.AccessLevelValue) resource.TestCheckFunc {
8160
return func(_ *terraform.State) error {
82-
mainGroup, _, err := testGitlabClient.Groups.GetGroup(fmt.Sprintf("%s_main", groupName), nil)
61+
mainGroup, _, err := testGitlabClient.Groups.GetGroup(mainGroupName, nil)
8362
if err != nil {
8463
return err
8564
}
@@ -91,8 +70,8 @@ func testAccCheckGitlabGroupSharedWithGroup(
9170

9271
sharedGroup := mainGroup.SharedWithGroups[0]
9372

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))
73+
if sharedGroup.GroupName != sharedGroupName {
74+
return fmt.Errorf("group name was %s (wanted %s)", sharedGroup.GroupName, sharedGroupName)
9675
}
9776

9877
if gitlab.AccessLevelValue(sharedGroup.GroupAccessLevel) != accessLevel {
@@ -109,9 +88,9 @@ func testAccCheckGitlabGroupSharedWithGroup(
10988
}
11089
}
11190

112-
func testAccCheckGitlabGroupIsNotShared(groupName string) resource.TestCheckFunc {
91+
func testAccCheckGitlabGroupIsNotShared(mainGroupName string) resource.TestCheckFunc {
11392
return func(_ *terraform.State) error {
114-
mainGroup, _, err := testGitlabClient.Groups.GetGroup(fmt.Sprintf("%s_main", groupName), nil)
93+
mainGroup, _, err := testGitlabClient.Groups.GetGroup(mainGroupName, nil)
11594
if err != nil {
11695
return err
11796
}
@@ -125,46 +104,51 @@ func testAccCheckGitlabGroupIsNotShared(groupName string) resource.TestCheckFunc
125104
}
126105
}
127106

128-
func testAccGitlabGroupShareGroupConfig(
129-
randName string,
130-
shareGroupSettings string,
131-
) string {
132-
return fmt.Sprintf(
133-
`
134-
resource "gitlab_group" "test_main" {
135-
name = "%[1]s_main"
136-
path = "%[1]s_main"
107+
func testAccCheckGitlabShareGroupDestroy(s *terraform.State) error {
108+
var groupId string
109+
var sharedGroupId int
110+
var err error
111+
112+
for _, rs := range s.RootModule().Resources {
113+
if rs.Type == "gitlab_group_share_group" {
114+
groupId, sharedGroupId, err = groupIdsFromId(rs.Primary.ID)
115+
if err != nil {
116+
return fmt.Errorf("[ERROR] cannot get Group ID and ShareGroupId from input: %v", rs.Primary.ID)
117+
}
118+
119+
// Get Main Group
120+
group, _, err := testGitlabClient.Groups.GetGroup(groupId, nil)
121+
if err != nil {
122+
return err
123+
}
124+
125+
// Make sure that SharedWithGroups attribute on the main group does not contain the shared group id at all
126+
for _, sharedGroup := range group.SharedWithGroups {
127+
if sharedGroupId == sharedGroup.GroupID {
128+
return fmt.Errorf("GitLab Group Share %d still exists", sharedGroupId)
129+
}
130+
}
137131
}
132+
}
138133

139-
resource "gitlab_group" "test_share" {
140-
name = "%[1]s_share"
141-
path = "%[1]s_share"
142-
}
134+
return nil
135+
}
143136

137+
func testAccGitlabGroupShareGroupConfig(mainGroupId int, shareGroupId int, shareGroupSettings string) string {
138+
return fmt.Sprintf(
139+
`
144140
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
141+
group_id = %[1]d
142+
share_group_id = %[2]d
143+
%[3]s
148144
}
149145
`,
150-
randName,
146+
mainGroupId,
147+
shareGroupId,
151148
shareGroupSettings,
152149
)
153150
}
154151

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-
)
152+
func testAccGitlabGroupShareGroupConfigDelete() string {
153+
return ``
170154
}

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"
@@ -96,9 +95,9 @@ func resourceGitlabInstanceVariableRead(ctx context.Context, d *schema.ResourceD
9695

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

99-
v, resp, err := client.InstanceVariables.GetVariable(key, gitlab.WithContext(ctx))
98+
v, _, err := client.InstanceVariables.GetVariable(key, gitlab.WithContext(ctx))
10099
if err != nil {
101-
if resp.StatusCode == http.StatusNotFound {
100+
if is404(err) {
102101
log.Printf("[DEBUG] gitlab instance level CI variable for %s not found so removing from state", d.Id())
103102
d.SetId("")
104103
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)