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

New, very simple data sources for GCR Repo and Image. #954

Merged
merged 9 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
105 changes: 105 additions & 0 deletions google/data_source_google_gcr_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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 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 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
}

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
}
78 changes: 78 additions & 0 deletions google/data_source_google_gcr_repository_test.go
Original file line number Diff line number Diff line change
@@ -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"
}
`
4 changes: 3 additions & 1 deletion google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
36 changes: 36 additions & 0 deletions website/docs/d/google_container_registry_image.html.markdown
Original file line number Diff line number Diff line change
@@ -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 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`.
* `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.
31 changes: 31 additions & 0 deletions website/docs/d/google_container_registry_repository.html.markdown
Original file line number Diff line number Diff line change
@@ -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 GCR at any point.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to use the GCR acronym, you should spell it out fully the first time you use it and add the acronym between parentheses after it.


## 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`.

## Attributes Reference
In addition to the arguments listed above, this data source exports:
* `repository_url`: The URL at which the repository can be accessed.
6 changes: 6 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
<li<%= sidebar_current("docs-google-datasource-container-versions") %>>
<a href="/docs/providers/google/d/google_container_engine_versions.html">google_container_engine_versions</a>
</li>
<li<%= sidebar_current("docs-google-datasource-container-repo") %>>
<a href="/docs/providers/google/d/google_container_registry_repository.html">google_container_registry_repository</a>
</li>
<li<%= sidebar_current("docs-google-datasource-container-image") %>>
<a href="/docs/providers/google/d/google_container_registry_image.html">google_container_registry_image</a>
</li>
<li<%= sidebar_current("docs-google-datasource-dns-managed-zone") %>>
<a href="/docs/providers/google/d/dns_managed_zone.html">dns_managed_zone</a>
</li>
Expand Down