Skip to content

Commit

Permalink
Merge pull request #291 from terraform-providers/soft-delete
Browse files Browse the repository at this point in the history
Support for Gitlab EE soft-delete of groups and projects
  • Loading branch information
ringods authored Apr 4, 2020
2 parents d0cbbce + 49b79d4 commit da9cca3
Show file tree
Hide file tree
Showing 36 changed files with 603 additions and 153 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/acceptance-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.12
go-version: 1.14
id: go

- name: Check out code repository source code
Expand All @@ -36,7 +36,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.12
go-version: 1.14
id: go

- name: Check out code repository source code
Expand All @@ -59,6 +59,7 @@ jobs:
echo "::set-env name=GO111MODULE::on"
echo "::set-env name=GITLAB_TOKEN::20char-testing-token"
echo "::set-env name=GITLAB_BASE_URL::http://127.0.0.1:8080/api/v4"
echo "::set-env name=TF_LOG::DEBUG"
- name: Start Gitlab and run acceptance tests
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
fail-fast: false
matrix:
go: [1.12, 1.13, 1.14]
go: [1.13, 1.14]
os: [ubuntu-latest, macos-latest, windows-latest]
make_target: [test, vet]

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.12
go-version: 1.14
id: go

# https://help.github.com/en/actions/reference/workflow-commands-for-github-actions
Expand Down
30 changes: 0 additions & 30 deletions .travis.yml

This file was deleted.

7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
## 2.5.1 (Unreleased)

BUGFIXES:
* Support for soft-delete of groups and projects in Gitlab Enterprise Edition ([GH-282][GH-283][GH-285][GH-291])

ENHANCEMENTS:
* Switched from Travis CI to Github Actions ([GH-216])

## 2.5.0 (December 05, 2019)

ENHANCEMENTS:
Expand Down
4 changes: 2 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
TEST?=$$(go list ./... |grep -v 'vendor')
TEST?=./gitlab
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
WEBSITE_REPO=github.com/hashicorp/terraform-website
PKG_NAME=gitlab
Expand All @@ -14,7 +14,7 @@ test: fmtcheck
xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4

testacc: fmtcheck
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
TF_ACC=1 go test -v $(TEST) $(TESTARGS) -timeout 40m

vet:
@echo "go vet ."
Expand Down
12 changes: 11 additions & 1 deletion gitlab/resource_gitlab_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net/http"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -127,6 +128,11 @@ func resourceGitlabGroupRead(d *schema.ResourceData, meta interface{}) error {
}
return err
}
if group.MarkedForDeletionOn != nil {
log.Printf("[DEBUG] gitlab group %s is marked for deletion", d.Id())
d.SetId("")
return nil
}

d.SetId(fmt.Sprintf("%d", group.ID))
d.Set("name", group.Name)
Expand Down Expand Up @@ -190,7 +196,7 @@ func resourceGitlabGroupDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Delete gitlab group %s", d.Id())

_, err := client.Groups.DeleteGroup(d.Id())
if err != nil {
if err != nil && !strings.Contains(err.Error(), "Group has been already marked for deletion") {
return fmt.Errorf("error deleting group %s: %s", d.Id(), err)
}

Expand All @@ -208,6 +214,10 @@ func resourceGitlabGroupDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[ERROR] Received error: %#v", err)
return out, "Error", err
}
if out.MarkedForDeletionOn != nil {
// Represents a Gitlab EE soft-delete
return out, "Deleted", nil
}
return out, "Deleting", nil
},

Expand Down
4 changes: 3 additions & 1 deletion gitlab/resource_gitlab_group_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ func testAccCheckGitlabGroupLabelDestroy(s *terraform.State) error {
group, resp, err := conn.Groups.GetGroup(rs.Primary.ID)
if err == nil {
if group != nil && fmt.Sprintf("%d", group.ID) == rs.Primary.ID {
return fmt.Errorf("Group still exists")
if group.MarkedForDeletionOn == nil {
return fmt.Errorf("Group still exists")
}
}
}
if resp.StatusCode != 404 {
Expand Down
35 changes: 19 additions & 16 deletions gitlab/resource_gitlab_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,22 @@ func TestAccGitlabGroup_nested(t *testing.T) {
}),
),
},
{
Config: testAccGitlabNestedGroupConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
Name: fmt.Sprintf("nfoo-name-%d", rInt),
Path: fmt.Sprintf("nfoo-path-%d", rInt),
Description: "Terraform acceptance tests",
LFSEnabled: true,
Parent: &group,
}),
),
},
// TODO In EE version, re-creating on the same path where a previous group was soft-deleted doesn't work.
// {
// Config: testAccGitlabNestedGroupConfig(rInt),
// Check: resource.ComposeTestCheckFunc(
// testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
// testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
// testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
// testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
// Name: fmt.Sprintf("nfoo-name-%d", rInt),
// Path: fmt.Sprintf("nfoo-path-%d", rInt),
// Description: "Terraform acceptance tests",
// LFSEnabled: true,
// Parent: &group,
// }),
// ),
// },
},
})
}
Expand Down Expand Up @@ -264,7 +265,9 @@ func testAccCheckGitlabGroupDestroy(s *terraform.State) error {
group, resp, err := conn.Groups.GetGroup(rs.Primary.ID)
if err == nil {
if group != nil && fmt.Sprintf("%d", group.ID) == rs.Primary.ID {
return fmt.Errorf("Group still exists")
if group.MarkedForDeletionOn == nil {
return fmt.Errorf("Group still exists")
}
}
}
if resp.StatusCode != 404 {
Expand Down
4 changes: 3 additions & 1 deletion gitlab/resource_gitlab_group_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ func testAccCheckGitlabGroupVariableDestroy(s *terraform.State) error {
_, resp, err := conn.Groups.GetGroup(rs.Primary.ID)
if err == nil {
//if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
// return fmt.Errorf("Repository still exists")
// if gotRepo.MarkedForDeletionAt == nil {
// return fmt.Errorf("Repository still exists")
// }
//}
}
if resp.StatusCode != 404 {
Expand Down
4 changes: 3 additions & 1 deletion gitlab/resource_gitlab_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ func testAccCheckGitlabLabelDestroy(s *terraform.State) error {
gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil)
if err == nil {
if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
return fmt.Errorf("Repository still exists")
if gotRepo.MarkedForDeletionAt == nil {
return fmt.Errorf("Repository still exists")
}
}
}
if resp.StatusCode != 404 {
Expand Down
4 changes: 3 additions & 1 deletion gitlab/resource_gitlab_pipeline_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ func testAccCheckGitlabPipelineScheduleDestroy(s *terraform.State) error {
gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil)
if err == nil {
if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
return fmt.Errorf("Repository still exists")
if gotRepo.MarkedForDeletionAt == nil {
return fmt.Errorf("Repository still exists")
}
}
}
if resp.StatusCode != 404 {
Expand Down
4 changes: 3 additions & 1 deletion gitlab/resource_gitlab_pipeline_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ func testAccCheckGitlabPipelineTriggerDestroy(s *terraform.State) error {
gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil)
if err == nil {
if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
return fmt.Errorf("Repository still exists")
if gotRepo.MarkedForDeletionAt == nil {
return fmt.Errorf("Repository still exists")
}
}
}
if resp.StatusCode != 404 {
Expand Down
9 changes: 9 additions & 0 deletions gitlab/resource_gitlab_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ func resourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return err
}
if project.MarkedForDeletionAt != nil {
log.Printf("[DEBUG] gitlab project %s is marked for deletion", d.Id())
d.SetId("")
return nil
}

resourceGitlabProjectSetToState(d, project)
return nil
Expand Down Expand Up @@ -544,6 +549,10 @@ func resourceGitlabProjectDelete(d *schema.ResourceData, meta interface{}) error
log.Printf("[ERROR] Received error: %#v", err)
return out, "Error", err
}
if out.MarkedForDeletionAt != nil {
// Represents a Gitlab EE soft-delete
return out, "Deleted", nil
}
return out, "Deleting", nil
},

Expand Down
4 changes: 3 additions & 1 deletion gitlab/resource_gitlab_project_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ func testAccCheckGitlabProjectHookDestroy(s *terraform.State) error {
gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil)
if err == nil {
if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
return fmt.Errorf("Repository still exists")
if gotRepo.MarkedForDeletionAt == nil {
return fmt.Errorf("Repository still exists")
}
}
}
if resp.StatusCode != 404 {
Expand Down
4 changes: 3 additions & 1 deletion gitlab/resource_gitlab_project_push_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ func testAccCheckGitlabProjectPushRulesDestroy(s *terraform.State) error {
gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil)
if err == nil {
if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
return fmt.Errorf("Repository still exists")
if gotRepo.MarkedForDeletionAt == nil {
return fmt.Errorf("Repository still exists")
}
}
}
if resp.StatusCode != 404 {
Expand Down
4 changes: 3 additions & 1 deletion gitlab/resource_gitlab_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,9 @@ func testAccCheckGitlabProjectDestroy(s *terraform.State) error {
gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil)
if err == nil {
if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
return fmt.Errorf("Repository still exists")
if gotRepo.MarkedForDeletionAt == nil {
return fmt.Errorf("Repository still exists")
}
}
}
if resp.StatusCode != 404 {
Expand Down
4 changes: 3 additions & 1 deletion gitlab/resource_gitlab_project_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ func testAccCheckGitlabProjectVariableDestroy(s *terraform.State) error {
gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil)
if err == nil {
if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
return fmt.Errorf("Repository still exists")
if gotRepo.MarkedForDeletionAt == nil {
return fmt.Errorf("Repository still exists")
}
}
}
if resp.StatusCode != 404 {
Expand Down
4 changes: 3 additions & 1 deletion gitlab/resource_gitlab_service_jira_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ func testAccCheckGitlabServiceJiraDestroy(s *terraform.State) error {
gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil)
if err == nil {
if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
return fmt.Errorf("Repository still exists")
if gotRepo.MarkedForDeletionAt == nil {
return fmt.Errorf("Repository still exists")
}
}
}
if resp.StatusCode != 404 {
Expand Down
4 changes: 3 additions & 1 deletion gitlab/resource_gitlab_service_slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ func testAccCheckGitlabServiceSlackDestroy(s *terraform.State) error {
gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil)
if err == nil {
if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
return fmt.Errorf("Repository still exists")
if gotRepo.MarkedForDeletionAt == nil {
return fmt.Errorf("Repository still exists")
}
}
}
if resp.StatusCode != 404 {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ require (
github.com/posener/complete v1.2.3 // indirect
github.com/ulikunitz/xz v0.5.7 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/xanzy/go-gitlab v0.29.0
github.com/xanzy/go-gitlab v0.30.1
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 // indirect
)

go 1.13
go 1.14
Loading

0 comments on commit da9cca3

Please sign in to comment.