New simple data sources for GCR Repo and Image. (#954)

New, very simple data sources for GCR Repo and Image, plus docs.
This commit is contained in:
Nathan McKinley 2018-01-17 18:47:25 -08:00 committed by GitHub
parent a8aa85e921
commit d8aea17b19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 266 additions and 1 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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"
}
`

View File

@ -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(),

View File

@ -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.

View File

@ -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.

View File

@ -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>