Skip to content

Commit f3b0f31

Browse files
oboukiliarmsnyder
andauthored
Handle manually removed resources (#318)
* enable handling of manually removed resources, fixed some IDE warnings * remove absent resources from state for new resources as well * empty ci trigger * reverted IDE warnings fixes * review comments #1 * added importers for gitlab_tag_protection, gitlab_pipeline_schedule, gitlab_pipeline_schedule_variable * fixed importers tests, fixed gitlab_tag_protection access_level attribute read persistence * Apply suggestions from code review * Update gitlab/resource_gitlab_tag_protection.go * Apply suggestions from code review * Update docs/resources/tag_protection.md * Update gitlab/resource_gitlab_tag_protection.go * Update gitlab/resource_gitlab_tag_protection.go * Update gitlab/resource_gitlab_tag_protection.go * Fix unused variable scopes * Fix lint issues Co-authored-by: Adam Snyder <[email protected]>
1 parent e04bd95 commit f3b0f31

File tree

50 files changed

+315
-85
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+315
-85
lines changed

docs/resources/pipeline_schedule_variable.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ The following arguments are supported:
3131
* `key` - (Required, string) Name of the variable.
3232

3333
* `value` - (Required, string) Value of the variable.
34+
35+
## Import
36+
37+
Pipeline schedule variables can be imported using an id made up of `project_id:pipeline_schedule_id:key`, e.g.
38+
39+
```
40+
$ terraform import gitlab_pipeline_schedule_variable.example 123456789:13:mykey
41+
```

docs/resources/tag_protection.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,11 @@ The following arguments are supported:
2121
* `tag` - (Required) Name of the tag or wildcard.
2222

2323
* `create_access_level` - (Required) One of five levels of access to the project.
24+
25+
## Import
26+
27+
Tag protections can be imported using an id made up of `project_id:tag_name`, e.g.
28+
29+
```
30+
$ terraform import gitlab_tag_protection.example 123456789:v1.0.0
31+
```

gitlab/resource_gitlab_branch_protection_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,13 @@ func testAccCheckGitlabBranchProtectionDestroy(s *terraform.State) error {
401401
}
402402
}
403403

404-
pb, response, err := conn.ProtectedBranches.GetProtectedBranch(project, branch)
404+
pb, _, err := conn.ProtectedBranches.GetProtectedBranch(project, branch)
405405
if err == nil {
406406
if pb != nil {
407407
return fmt.Errorf("project branch protection %s still exists", branch)
408408
}
409409
}
410-
if response.StatusCode != 404 {
410+
if !is404(err) {
411411
return err
412412
}
413413
return nil

gitlab/resource_gitlab_deploy_key.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ func resourceGitlabDeployKeyRead(d *schema.ResourceData, meta interface{}) error
8080

8181
deployKey, _, err := client.DeployKeys.GetDeployKey(project, deployKeyID)
8282
if err != nil {
83+
if is404(err) {
84+
log.Printf("[DEBUG] gitlab deploy key not found %s/%d", project, deployKeyID)
85+
d.SetId("")
86+
return nil
87+
}
8388
return err
8489
}
8590

@@ -99,7 +104,6 @@ func resourceGitlabDeployKeyDelete(d *schema.ResourceData, meta interface{}) err
99104
log.Printf("[DEBUG] Delete gitlab deploy key %s", d.Id())
100105

101106
_, err = client.DeployKeys.DeleteDeployKey(project, deployKeyID)
102-
103107
return err
104108
}
105109

gitlab/resource_gitlab_deploy_key_enable.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ func resourceGitlabDeployKeyEnableRead(d *schema.ResourceData, meta interface{})
7777

7878
deployKey, _, err := client.DeployKeys.GetDeployKey(project, deployKeyID)
7979
if err != nil {
80+
if is404(err) {
81+
log.Printf("[DEBUG] gitlab deploy key not found %s/%d", project, deployKeyID)
82+
d.SetId("")
83+
return nil
84+
}
8085
return err
8186
}
8287

@@ -100,7 +105,7 @@ func resourceGitlabDeployKeyEnableDelete(d *schema.ResourceData, meta interface{
100105
response, err := client.DeployKeys.DeleteDeployKey(project, deployKeyID)
101106

102107
// HTTP 2XX is success including 204 with no body
103-
if response.StatusCode/100 == 2 {
108+
if response != nil && response.StatusCode/100 == 2 {
104109
return nil
105110
}
106111
return err

gitlab/resource_gitlab_deploy_key_enable_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ func testAccCheckGitlabDeployKeyEnableDestroy(s *terraform.State) error {
103103
}
104104
}
105105

106-
gotDeployKey, resp, err := conn.DeployKeys.GetDeployKey(project, deployKeyID)
106+
gotDeployKey, _, err := conn.DeployKeys.GetDeployKey(project, deployKeyID)
107107
if err == nil {
108108
if gotDeployKey != nil {
109109
return fmt.Errorf("Deploy key still exists: %d", deployKeyID)
110110
}
111111
}
112-
if resp.StatusCode != 404 {
112+
if !is404(err) {
113113
return err
114114
}
115115
return nil

gitlab/resource_gitlab_deploy_key_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,13 @@ func testAccCheckGitlabDeployKeyDestroy(s *terraform.State) error {
156156
deployKeyID, err := strconv.Atoi(rs.Primary.ID) // nolint // TODO: Resolve this golangci-lint issue: ineffectual assignment to err (ineffassign)
157157
project := rs.Primary.Attributes["project"]
158158

159-
gotDeployKey, resp, err := conn.DeployKeys.GetDeployKey(project, deployKeyID)
159+
gotDeployKey, _, err := conn.DeployKeys.GetDeployKey(project, deployKeyID)
160160
if err == nil {
161161
if gotDeployKey != nil && fmt.Sprintf("%d", gotDeployKey.ID) == rs.Primary.ID {
162162
return fmt.Errorf("Deploy key still exists")
163163
}
164164
}
165-
if resp.StatusCode != 404 {
165+
if !is404(err) {
166166
return err
167167
}
168168
return nil

gitlab/resource_gitlab_deploy_token.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ func resourceGitlabDeployTokenRead(d *schema.ResourceData, meta interface{}) err
166166
return err
167167
}
168168

169-
var scopes []string
170-
171169
for _, token := range deployTokens {
172170
if token.ID == deployTokenID {
173171
d.Set("name", token.Name)
@@ -177,13 +175,17 @@ func resourceGitlabDeployTokenRead(d *schema.ResourceData, meta interface{}) err
177175
d.Set("expires_at", token.ExpiresAt.Format(time.RFC3339))
178176
}
179177

180-
scopes = append(scopes, token.Scopes...)
178+
if err := d.Set("scopes", token.Scopes); err != nil {
179+
return err
180+
}
181+
182+
return nil
181183
}
182184
}
183185

184-
if err := d.Set("scopes", scopes); err != nil {
185-
return err
186-
}
186+
log.Printf("[DEBUG] GitLab deploy token %d in group %s was not found", deployTokenID, group.(string))
187+
188+
d.SetId("")
187189

188190
return nil
189191
}

gitlab/resource_gitlab_deploy_token_test.go

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

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

@@ -114,12 +113,11 @@ func testAccCheckGitlabDeployTokenDestroy(s *terraform.State) error {
114113
group := rs.Primary.Attributes["group"]
115114

116115
var gotDeployTokens []*gitlab.DeployToken
117-
var resp *gitlab.Response
118116

119117
if project != "" {
120-
gotDeployTokens, resp, err = conn.DeployTokens.ListProjectDeployTokens(project, nil)
118+
gotDeployTokens, _, err = conn.DeployTokens.ListProjectDeployTokens(project, nil)
121119
} else if group != "" {
122-
gotDeployTokens, resp, err = conn.DeployTokens.ListGroupDeployTokens(group, nil)
120+
gotDeployTokens, _, err = conn.DeployTokens.ListGroupDeployTokens(group, nil)
123121
} else {
124122
return fmt.Errorf("somehow neither project nor group were set")
125123
}
@@ -132,7 +130,7 @@ func testAccCheckGitlabDeployTokenDestroy(s *terraform.State) error {
132130
}
133131
}
134132

135-
if resp.StatusCode != http.StatusNotFound {
133+
if !is404(err) {
136134
return err
137135
}
138136
}

gitlab/resource_gitlab_group_cluster.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ func resourceGitlabGroupClusterRead(d *schema.ResourceData, meta interface{}) er
161161

162162
cluster, _, err := client.GroupCluster.GetCluster(group, clusterId)
163163
if err != nil {
164+
if is404(err) {
165+
log.Printf("[DEBUG] gitlab group cluster not found %s/%d", group, clusterId)
166+
d.SetId("")
167+
return nil
168+
}
164169
return err
165170
}
166171

0 commit comments

Comments
 (0)