From d8aea17b19bbdaa2bb149457326006b059f97968 Mon Sep 17 00:00:00 2001 From: Nathan McKinley Date: Wed, 17 Jan 2018 18:47:25 -0800 Subject: [PATCH] New simple data sources for GCR Repo and Image. (#954) New, very simple data sources for GCR Repo and Image, plus docs. --- .../data_source_container_registry_image.go | 67 ++++++++++++++++ ...ta_source_container_registry_repository.go | 45 +++++++++++ google/data_source_container_registry_test.go | 78 +++++++++++++++++++ google/provider.go | 4 +- ...gle_container_registry_image.html.markdown | 36 +++++++++ ...ontainer_registry_repository.html.markdown | 31 ++++++++ website/google.erb | 6 ++ 7 files changed, 266 insertions(+), 1 deletion(-) create mode 100644 google/data_source_container_registry_image.go create mode 100644 google/data_source_container_registry_repository.go create mode 100644 google/data_source_container_registry_test.go create mode 100644 website/docs/d/google_container_registry_image.html.markdown create mode 100644 website/docs/d/google_container_registry_repository.html.markdown diff --git a/google/data_source_container_registry_image.go b/google/data_source_container_registry_image.go new file mode 100644 index 00000000..093784b5 --- /dev/null +++ b/google/data_source_container_registry_image.go @@ -0,0 +1,67 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceGoogleContainerImage() *schema.Resource { + return &schema.Resource{ + Read: containerRegistryImageRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "tag": { + Type: schema.TypeString, + Optional: true, + }, + "digest": { + Type: schema.TypeString, + Optional: true, + }, + "region": { + Type: schema.TypeString, + Optional: true, + }, + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "image_url": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func containerRegistryImageRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + d.Set("project", project) + region, ok := d.GetOk("region") + var url_base string + if ok && region != nil && region != "" { + url_base = fmt.Sprintf("%s.gcr.io/%s", region, project) + } else { + url_base = fmt.Sprintf("gcr.io/%s", project) + } + tag, t_ok := d.GetOk("tag") + digest, d_ok := d.GetOk("digest") + if t_ok && tag != nil && tag != "" { + d.Set("image_url", fmt.Sprintf("%s/%s:%s", url_base, d.Get("name").(string), tag)) + } else if d_ok && digest != nil && digest != "" { + d.Set("image_url", fmt.Sprintf("%s/%s@%s", url_base, d.Get("name").(string), digest)) + } else { + d.Set("image_url", fmt.Sprintf("%s/%s", url_base, d.Get("name").(string))) + } + d.SetId(d.Get("image_url").(string)) + return nil +} diff --git a/google/data_source_container_registry_repository.go b/google/data_source_container_registry_repository.go new file mode 100644 index 00000000..1ddcae3b --- /dev/null +++ b/google/data_source_container_registry_repository.go @@ -0,0 +1,45 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceGoogleContainerRepo() *schema.Resource { + return &schema.Resource{ + Read: containerRegistryRepoRead, + Schema: map[string]*schema.Schema{ + "region": { + Type: schema.TypeString, + Optional: true, + }, + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "repository_url": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func containerRegistryRepoRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + d.Set("project", project) + region, ok := d.GetOk("region") + if ok && region != nil && region != "" { + d.Set("repository_url", fmt.Sprintf("%s.gcr.io/%s", region, project)) + } else { + d.Set("repository_url", fmt.Sprintf("gcr.io/%s", project)) + } + d.SetId(d.Get("repository_url").(string)) + return nil +} diff --git a/google/data_source_container_registry_test.go b/google/data_source_container_registry_test.go new file mode 100644 index 00000000..0829a5d8 --- /dev/null +++ b/google/data_source_container_registry_test.go @@ -0,0 +1,78 @@ +package google + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestDataSourceGoogleContainerRegistryRepository(t *testing.T) { + t.Parallel() + + resourceName := "data.google_container_registry_repository.default" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckGoogleContainerRegistryRepo_basic, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceName, "project"), + resource.TestCheckResourceAttrSet(resourceName, "region"), + resource.TestCheckResourceAttr(resourceName, "repository_url", "bar.gcr.io/foo"), + ), + }, + }, + }) +} + +const testAccCheckGoogleContainerRegistryRepo_basic = ` +data "google_container_registry_repository" "default" { + project = "foo" + region = "bar" +} +` + +func TestDataSourceGoogleContainerRegistryImage(t *testing.T) { + t.Parallel() + + resourceName := "data.google_container_registry_image.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckGoogleContainerRegistryImage_basic, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceName, "project"), + resource.TestCheckResourceAttrSet(resourceName, "region"), + 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"), + ), + }, + }, + }) +} + +const testAccCheckGoogleContainerRegistryImage_basic = ` +data "google_container_registry_image" "test" { + project = "foo" + region = "bar" + name = "baz" +} +data "google_container_registry_image" "test2" { + project = "foo" + region = "bar" + name = "baz" + tag = "qux" +} +data "google_container_registry_image" "test3" { + project = "foo" + region = "bar" + name = "baz" + digest = "1234" +} +` diff --git a/google/provider.go b/google/provider.go index 4b4f925b..1d3cca5e 100644 --- a/google/provider.go +++ b/google/provider.go @@ -60,6 +60,7 @@ func Provider() terraform.ResourceProvider { }, DataSourcesMap: map[string]*schema.Resource{ + "google_active_folder": dataSourceGoogleActiveFolder(), "google_billing_account": dataSourceGoogleBillingAccount(), "google_dns_managed_zone": dataSourceDnsManagedZone(), "google_client_config": dataSourceGoogleClientConfig(), @@ -75,7 +76,8 @@ func Provider() terraform.ResourceProvider { "google_compute_region_instance_group": dataSourceGoogleComputeRegionInstanceGroup(), "google_container_cluster": dataSourceGoogleContainerCluster(), "google_container_engine_versions": dataSourceGoogleContainerEngineVersions(), - "google_active_folder": dataSourceGoogleActiveFolder(), + "google_container_registry_repository": dataSourceGoogleContainerRepo(), + "google_container_registry_image": dataSourceGoogleContainerImage(), "google_iam_policy": dataSourceGoogleIamPolicy(), "google_kms_secret": dataSourceGoogleKmsSecret(), "google_organization": dataSourceGoogleOrganization(), diff --git a/website/docs/d/google_container_registry_image.html.markdown b/website/docs/d/google_container_registry_image.html.markdown new file mode 100644 index 00000000..9296d839 --- /dev/null +++ b/website/docs/d/google_container_registry_image.html.markdown @@ -0,0 +1,36 @@ +--- +layout: "google" +page_title: "Google: google_container_registry_image" +sidebar_current: "docs-google-datasource-container-image" +description: |- + Get URLs for a given project's container registry image. +--- + +# google\_container\_registry\_image + +This data source fetches the project name, and provides the appropriate URLs to use for container registry for this project. + +The URLs are computed entirely offline - as long as the project exists, they will be valid, but this data source does not contact Google Container Registry (GCR) at any point. + +## Example Usage + +```hcl +data "google_container_registry_image" { + name = "debian" +} + +output "gcr_location" { + value = "${data.google_container_registry_image.image_url}" +} +``` + +## Argument Reference +* `name`: (Required) The image name. +* `project`: (Optional) The project ID that this image is attached to. If not provider, provider project will be used instead. +* `region`: (Optional) The GCR region to use. As of this writing, one of `asia`, `eu`, and `us`. See [the documentation](https://cloud.google.com/container-registry/docs/pushing-and-pulling) for additional information. +* `tag`: (Optional) The tag to fetch, if any. +* `digest`: (Optional) The image digest to fetch, if any. + +## Attributes Reference +In addition to the arguments listed above, this data source exports: +* `image_url`: The URL at which the image can be accessed. diff --git a/website/docs/d/google_container_registry_repository.html.markdown b/website/docs/d/google_container_registry_repository.html.markdown new file mode 100644 index 00000000..99448644 --- /dev/null +++ b/website/docs/d/google_container_registry_repository.html.markdown @@ -0,0 +1,31 @@ +--- +layout: "google" +page_title: "Google: google_container_registry_repository" +sidebar_current: "docs-google-datasource-container-repo" +description: |- + Get URLs for a given project's container registry repository. +--- + +# google\_container\_registry\_repository + +This data source fetches the project name, and provides the appropriate URLs to use for container registry for this project. + +The URLs are computed entirely offline - as long as the project exists, they will be valid, but this data source does not contact Google Container Registry (GCR) at any point. + +## Example Usage + +```hcl +data "google_container_registry_repository" {} + +output "gcr_location" { + value = "${data.google_container_registry_repository.repository_url}" +} +``` + +## Argument Reference +* `project`: (Optional) The project ID that this repository is attached to. If not provider, provider project will be used instead. +* `region`: (Optional) The GCR region to use. As of this writing, one of `asia`, `eu`, and `us`. See [the documentation](https://cloud.google.com/container-registry/docs/pushing-and-pulling) for additional information. + +## Attributes Reference +In addition to the arguments listed above, this data source exports: +* `repository_url`: The URL at which the repository can be accessed. diff --git a/website/google.erb b/website/google.erb index ec10e037..b76d4e32 100644 --- a/website/google.erb +++ b/website/google.erb @@ -52,6 +52,12 @@ > google_container_engine_versions + > + google_container_registry_repository + + > + google_container_registry_image + > dns_managed_zone