Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle domain-scoped projects with GCR #2169

Merged
merged 2 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"strings"

"github.com/hashicorp/terraform/helper/schema"
)
Expand Down Expand Up @@ -48,10 +49,11 @@ func containerRegistryImageRead(d *schema.ResourceData, meta interface{}) error
d.Set("project", project)
region, ok := d.GetOk("region")
var url_base string
escapedProject := strings.Replace(project, ":", "/", -1)
if ok && region != nil && region != "" {
url_base = fmt.Sprintf("%s.gcr.io/%s", region, project)
url_base = fmt.Sprintf("%s.gcr.io/%s", region, escapedProject)
} else {
url_base = fmt.Sprintf("gcr.io/%s", project)
url_base = fmt.Sprintf("gcr.io/%s", escapedProject)
}
tag, t_ok := d.GetOk("tag")
digest, d_ok := d.GetOk("digest")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"strings"

"github.com/hashicorp/terraform/helper/schema"
)
Expand Down Expand Up @@ -35,10 +36,11 @@ func containerRegistryRepoRead(d *schema.ResourceData, meta interface{}) error {
}
d.Set("project", project)
region, ok := d.GetOk("region")
escapedProject := strings.Replace(project, ":", "/", -1)
if ok && region != nil && region != "" {
d.Set("repository_url", fmt.Sprintf("%s.gcr.io/%s", region, project))
d.Set("repository_url", fmt.Sprintf("%s.gcr.io/%s", region, escapedProject))
} else {
d.Set("repository_url", fmt.Sprintf("gcr.io/%s", project))
d.Set("repository_url", fmt.Sprintf("gcr.io/%s", escapedProject))
}
d.SetId(d.Get("repository_url").(string))
return nil
Expand Down
18 changes: 16 additions & 2 deletions third_party/terraform/tests/data_source_container_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestDataSourceGoogleContainerRegistryRepository(t *testing.T) {
t.Parallel()

resourceName := "data.google_container_registry_repository.default"
resourceName := "data.google_container_registry_repository.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -21,17 +21,23 @@ func TestDataSourceGoogleContainerRegistryRepository(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "project"),
resource.TestCheckResourceAttrSet(resourceName, "region"),
resource.TestCheckResourceAttr(resourceName, "repository_url", "bar.gcr.io/foo"),
resource.TestCheckResourceAttrSet(resourceName+"Scoped", "project"),
resource.TestCheckResourceAttr(resourceName+"Scoped", "repository_url", "bar.gcr.io/example.com/foo"),
),
},
},
})
}

const testAccCheckGoogleContainerRegistryRepo_basic = `
data "google_container_registry_repository" "default" {
data "google_container_registry_repository" "test" {
project = "foo"
region = "bar"
}
data "google_container_registry_repository" "testScoped" {
project = "example.com:foo"
region = "bar"
}
`

func TestDataSourceGoogleContainerRegistryImage(t *testing.T) {
Expand All @@ -51,6 +57,8 @@ func TestDataSourceGoogleContainerRegistryImage(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "image_url", "bar.gcr.io/foo/baz"),
resource.TestCheckResourceAttr(resourceName+"2", "image_url", "bar.gcr.io/foo/baz:qux"),
resource.TestCheckResourceAttr(resourceName+"3", "image_url", "bar.gcr.io/foo/baz@1234"),
resource.TestCheckResourceAttrSet(resourceName+"Scoped", "project"),
resource.TestCheckResourceAttr(resourceName+"Scoped", "image_url", "bar.gcr.io/example.com/foo/baz:qux"),
),
},
},
Expand All @@ -75,4 +83,10 @@ data "google_container_registry_image" "test3" {
name = "baz"
digest = "1234"
}
data "google_container_registry_image" "testScoped" {
project = "example.com:foo"
region = "bar"
name = "baz"
tag = "qux"
}
`