Skip to content

Commit

Permalink
Do not recreate project mirror on every run.
Browse files Browse the repository at this point in the history
GitLab does not return username and password from the mirrors.
Therefore, let's ignore it in the provider.

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
  • Loading branch information
roidelapluie committed Jun 8, 2021
1 parent df460ed commit 8108f05
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 15 deletions.
4 changes: 4 additions & 0 deletions docs/resources/project_mirror.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ The following arguments are supported:

* `url` - (Required) The URL of the remote repository to be mirrored.

**Note**: Due to limitations of the GitLab API, the provider cannot update the
username and passwords that are present in the URL. To change the username or
the password, we recommend you to taint the resources.

* `enabled` - Determines if the mirror is enabled.

* `only_protected_branches` - Determines if only protected branches are mirrored.
Expand Down
19 changes: 19 additions & 0 deletions gitlab/resource_gitlab_project_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gitlab

import (
"log"
"net/url"
"strconv"
"strings"

Expand Down Expand Up @@ -34,6 +35,23 @@ func resourceGitlabProjectMirror() *schema.Resource {
ForceNew: true,
Required: true,
Sensitive: true, // Username and password must be provided in the URL for https.
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
oldURL, err := url.Parse(old)
if err != nil {
return old == new
}
newURL, err := url.Parse(new)
if err != nil {
return old == new
}
if oldURL.User != nil {
oldURL.User = url.UserPassword("redacted", "redacted")
}
if newURL.User != nil {
newURL.User = url.UserPassword("redacted", "redacted")
}
return oldURL.String() == newURL.String()
},
},
"enabled": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -155,6 +173,7 @@ func resourceGitlabProjectMirrorRead(d *schema.ResourceData, meta interface{}) e
if m.ID == integerMirrorID {
mirror = m
found = true
break
}
}

Expand Down
65 changes: 50 additions & 15 deletions gitlab/resource_gitlab_project_mirror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,47 @@ import (
)

func TestAccGitlabProjectMirror_basic(t *testing.T) {
var hook gitlab.ProjectMirror
var mirror gitlab.ProjectMirror
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGitlabProjectMirrorDestroy,
Steps: []resource.TestStep{
// Create a project and hook with default options
// Create a project and mirror with default options
{
Config: testAccGitlabProjectMirrorConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectMirrorExists("gitlab_project_mirror.foo", &hook),
testAccCheckGitlabProjectMirrorAttributes(&hook, &testAccGitlabProjectMirrorExpectedAttributes{
URL: fmt.Sprintf("https://example.com/hook-%d", rInt),
testAccCheckGitlabProjectMirrorExists("gitlab_project_mirror.foo", &mirror),
testAccCheckGitlabProjectMirrorAttributes(&mirror, &testAccGitlabProjectMirrorExpectedAttributes{
URL: fmt.Sprintf("https://example.com/mirror-%d", rInt),
Enabled: true,
OnlyProtectedBranches: true,
KeepDivergentRefs: true,
}),
),
},
// Update the project hook to toggle all the values to their inverse
// Update the project mirror to toggle all the values to their inverse
{
Config: testAccGitlabProjectMirrorUpdateConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectMirrorExists("gitlab_project_mirror.foo", &hook),
testAccCheckGitlabProjectMirrorAttributes(&hook, &testAccGitlabProjectMirrorExpectedAttributes{
URL: fmt.Sprintf("https://example.com/hook-%d", rInt),
testAccCheckGitlabProjectMirrorExists("gitlab_project_mirror.foo", &mirror),
testAccCheckGitlabProjectMirrorAttributes(&mirror, &testAccGitlabProjectMirrorExpectedAttributes{
URL: fmt.Sprintf("https://example.com/mirror-%d", rInt),
Enabled: false,
OnlyProtectedBranches: false,
KeepDivergentRefs: false,
}),
),
},
// Update the project hook to toggle the options back
// Update the project mirror to toggle the options back
{
Config: testAccGitlabProjectMirrorConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectMirrorExists("gitlab_project_mirror.foo", &hook),
testAccCheckGitlabProjectMirrorAttributes(&hook, &testAccGitlabProjectMirrorExpectedAttributes{
URL: fmt.Sprintf("https://example.com/hook-%d", rInt),
testAccCheckGitlabProjectMirrorExists("gitlab_project_mirror.foo", &mirror),
testAccCheckGitlabProjectMirrorAttributes(&mirror, &testAccGitlabProjectMirrorExpectedAttributes{
URL: fmt.Sprintf("https://example.com/mirror-%d", rInt),
Enabled: true,
OnlyProtectedBranches: true,
KeepDivergentRefs: true,
Expand All @@ -65,6 +65,23 @@ func TestAccGitlabProjectMirror_basic(t *testing.T) {
})
}

func TestAccGitlabProjectMirror_withPassword(t *testing.T) {
//var mirror gitlab.ProjectMirror
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGitlabProjectMirrorDestroy,
Steps: []resource.TestStep{
// Create a project and mirror with a username / password.
{
Config: testAccGitlabProjectMirrorConfigWithPassword(rInt),
},
},
})
}

func TestAccGitlabProjectMirror_import(t *testing.T) {
rInt := acctest.RandInt()

Expand Down Expand Up @@ -182,11 +199,29 @@ resource "gitlab_project" "foo" {
resource "gitlab_project_mirror" "foo" {
project = "${gitlab_project.foo.id}"
url = "https://example.com/hook-%d"
url = "https://example.com/mirror-%d"
}
`, rInt, rInt)
}

func testAccGitlabProjectMirrorConfigWithPassword(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_project" "foo" {
name = "foo-%d"
description = "Terraform acceptance tests"
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
}
resource "gitlab_project_mirror" "foo" {
project = "${gitlab_project.foo.id}"
url = "https://foo:%d@example.com/mirror-%d"
}
`, rInt, rInt, rInt)
}

func testAccGitlabProjectMirrorUpdateConfig(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_project" "foo" {
Expand All @@ -200,7 +235,7 @@ resource "gitlab_project" "foo" {
resource "gitlab_project_mirror" "foo" {
project = "${gitlab_project.foo.id}"
url = "https://example.com/hook-%d"
url = "https://example.com/mirror-%d"
enabled = false
only_protected_branches = false
keep_divergent_refs = false
Expand Down

0 comments on commit 8108f05

Please sign in to comment.