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.

This commit also adds pagination support.

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
  • Loading branch information
roidelapluie committed Jul 23, 2021
1 parent 446a7d1 commit 94e87ea
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 13 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.

~> 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 [replace](https://www.terraform.io/docs/cli/commands/plan.html#replace-address) the resources.

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

* `only_protected_branches` - Determines if only protected branches are mirrored.
Expand Down
49 changes: 39 additions & 10 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 @@ -141,21 +159,32 @@ func resourceGitlabProjectMirrorRead(d *schema.ResourceData, meta interface{}) e
}
log.Printf("[DEBUG] read gitlab project mirror %s id %v", projectID, mirrorID)

mirrors, _, err := client.ProjectMirrors.ListProjectMirror(projectID)
var mirror *gitlab.ProjectMirror
found := false

if err != nil {
return err
opts := &gitlab.ListProjectMirrorOptions{
Page: 1,
PerPage: 20,
}

var mirror *gitlab.ProjectMirror
found := false
for {
mirrors, response, err := client.ProjectMirrors.ListProjectMirror(projectID, opts)
if err != nil {
return err
}

for _, m := range mirrors {
log.Printf("[DEBUG] project mirror found %v", m.ID)
if m.ID == integerMirrorID {
mirror = m
found = true
for _, m := range mirrors {
log.Printf("[DEBUG] project mirror found %v", m.ID)
if m.ID == integerMirrorID {
mirror = m
found = true
break
}
}
if response.CurrentPage >= response.TotalPages {
break
}
opts.Page++
}

if !found {
Expand Down
70 changes: 70 additions & 0 deletions gitlab/resource_gitlab_project_mirror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,39 @@ 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_withCount(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{
{
Config: testAccGitlabProjectMirrorConfigWithCount(rInt),
},
},
})
}

// lintignore: AT002 // TODO: Resolve this tfproviderlint issue
func TestAccGitlabProjectMirror_import(t *testing.T) {
rInt := acctest.RandInt()
Expand Down Expand Up @@ -188,6 +221,43 @@ resource "gitlab_project_mirror" "foo" {
`, rInt, rInt)
}

func testAccGitlabProjectMirrorConfigWithCount(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"
count = 40
}
`, rInt, 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 Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ require (
github.com/hashicorp/terraform-plugin-sdk v1.16.1
github.com/mitchellh/hashstructure v1.0.0
github.com/onsi/gomega v1.14.0
github.com/xanzy/go-gitlab v0.50.0
github.com/xanzy/go-gitlab v0.50.1
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvC
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
github.com/vmihailenco/tagparser v0.1.1 h1:quXMXlA39OCbd2wAdTsGDlK9RkOk6Wuw+x37wVyIuWY=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/xanzy/go-gitlab v0.50.0 h1:t7IoYTrnLSbdEZN7d8X/5zcr+ZM4TZQ2mXa8MqWlAZQ=
github.com/xanzy/go-gitlab v0.50.0/go.mod h1:Q+hQhV508bDPoBijv7YjK/Lvlb4PhVhJdKqXVQrUoAE=
github.com/xanzy/go-gitlab v0.50.1 h1:eH1G0/ZV1j81rhGrtbcePjbM5Ern7mPA4Xjt+yE+2PQ=
github.com/xanzy/go-gitlab v0.50.1/go.mod h1:Q+hQhV508bDPoBijv7YjK/Lvlb4PhVhJdKqXVQrUoAE=
github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down

0 comments on commit 94e87ea

Please sign in to comment.