-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
nat-henderson
merged 9 commits into
hashicorp:master
from
nat-henderson:container-registry
Jan 18, 2018
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0e32f75
New, very simple data sources for GCR Repo and Image.
nat-henderson bd05904
Changed `gcr` to `container_registry`.
nat-henderson e9dc081
First draft of docs.
nat-henderson 3dfcca9
Website fix
nat-henderson 762be5a
another website tweak
nat-henderson 650a2c7
renames gcr->container registry
nat-henderson 2513975
File renames.
nat-henderson ff2a89c
GCR acronym expanded, link to docs.
nat-henderson fb1b6bc
Moved data sources to one-per-source-file.
nat-henderson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
website/docs/d/google_container_registry_image.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
website/docs/d/google_container_registry_repository.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would split the 2 data sources in different files to follow our convention of one resource or one data source per source file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.