Skip to content

Commit

Permalink
Replace the ':' in project IDs with '/' when constructing gcr.io URLs…
Browse files Browse the repository at this point in the history
…. This is necessary to work correctly with domain-scoped projects in GCR: https://cloud.google.com/container-registry/docs/overview#domain-scoped_projects
  • Loading branch information
kevinmdavis authored and danawillow committed Aug 15, 2019
1 parent 215362c commit 09f492b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
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"
}
`

0 comments on commit 09f492b

Please sign in to comment.