Skip to content

Commit

Permalink
Support common_repository for google_artifact_registry_repository (
Browse files Browse the repository at this point in the history
…#12290) (#20305)

[upstream:817990220aced261b121b0e32dca4ab160e72c55]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Nov 12, 2024
1 parent b1b55b0 commit 3d8f4a2
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .changelog/12290.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
artifactregistry: added `common_repository` field to `google_artifact_registry_repository` resource
```
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,25 @@ snapshot versions.`,
},
},
},
ExactlyOneOf: []string{"remote_repository_config.0.apt_repository", "remote_repository_config.0.docker_repository", "remote_repository_config.0.maven_repository", "remote_repository_config.0.npm_repository", "remote_repository_config.0.python_repository", "remote_repository_config.0.yum_repository"},
ExactlyOneOf: []string{"remote_repository_config.0.apt_repository", "remote_repository_config.0.docker_repository", "remote_repository_config.0.maven_repository", "remote_repository_config.0.npm_repository", "remote_repository_config.0.python_repository", "remote_repository_config.0.yum_repository", "remote_repository_config.0.common_repository"},
},
"common_repository": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Description: `Specific settings for an Artifact Registory remote repository.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uri": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `Specific uri to the Artifact Registory repository, e.g. 'projects/UPSTREAM_PROJECT_ID/locations/REGION/repositories/UPSTREAM_REPOSITORY'`,
},
},
},
ExactlyOneOf: []string{"remote_repository_config.0.apt_repository", "remote_repository_config.0.docker_repository", "remote_repository_config.0.maven_repository", "remote_repository_config.0.npm_repository", "remote_repository_config.0.python_repository", "remote_repository_config.0.yum_repository", "remote_repository_config.0.common_repository"},
},
"description": {
Type: schema.TypeString,
Expand Down Expand Up @@ -390,7 +408,7 @@ not be validated.`,
},
},
},
ExactlyOneOf: []string{"remote_repository_config.0.apt_repository", "remote_repository_config.0.docker_repository", "remote_repository_config.0.maven_repository", "remote_repository_config.0.npm_repository", "remote_repository_config.0.python_repository", "remote_repository_config.0.yum_repository"},
ExactlyOneOf: []string{"remote_repository_config.0.apt_repository", "remote_repository_config.0.docker_repository", "remote_repository_config.0.maven_repository", "remote_repository_config.0.npm_repository", "remote_repository_config.0.python_repository", "remote_repository_config.0.yum_repository", "remote_repository_config.0.common_repository"},
},
"maven_repository": {
Type: schema.TypeList,
Expand Down Expand Up @@ -429,7 +447,7 @@ not be validated.`,
},
},
},
ExactlyOneOf: []string{"remote_repository_config.0.apt_repository", "remote_repository_config.0.docker_repository", "remote_repository_config.0.maven_repository", "remote_repository_config.0.npm_repository", "remote_repository_config.0.python_repository", "remote_repository_config.0.yum_repository"},
ExactlyOneOf: []string{"remote_repository_config.0.apt_repository", "remote_repository_config.0.docker_repository", "remote_repository_config.0.maven_repository", "remote_repository_config.0.npm_repository", "remote_repository_config.0.python_repository", "remote_repository_config.0.yum_repository", "remote_repository_config.0.common_repository"},
},
"npm_repository": {
Type: schema.TypeList,
Expand Down Expand Up @@ -468,7 +486,7 @@ not be validated.`,
},
},
},
ExactlyOneOf: []string{"remote_repository_config.0.apt_repository", "remote_repository_config.0.docker_repository", "remote_repository_config.0.maven_repository", "remote_repository_config.0.npm_repository", "remote_repository_config.0.python_repository", "remote_repository_config.0.yum_repository"},
ExactlyOneOf: []string{"remote_repository_config.0.apt_repository", "remote_repository_config.0.docker_repository", "remote_repository_config.0.maven_repository", "remote_repository_config.0.npm_repository", "remote_repository_config.0.python_repository", "remote_repository_config.0.yum_repository", "remote_repository_config.0.common_repository"},
},
"python_repository": {
Type: schema.TypeList,
Expand Down Expand Up @@ -507,7 +525,7 @@ not be validated.`,
},
},
},
ExactlyOneOf: []string{"remote_repository_config.0.apt_repository", "remote_repository_config.0.docker_repository", "remote_repository_config.0.maven_repository", "remote_repository_config.0.npm_repository", "remote_repository_config.0.python_repository", "remote_repository_config.0.yum_repository"},
ExactlyOneOf: []string{"remote_repository_config.0.apt_repository", "remote_repository_config.0.docker_repository", "remote_repository_config.0.maven_repository", "remote_repository_config.0.npm_repository", "remote_repository_config.0.python_repository", "remote_repository_config.0.yum_repository", "remote_repository_config.0.common_repository"},
},
"upstream_credentials": {
Type: schema.TypeList,
Expand Down Expand Up @@ -579,7 +597,7 @@ remote repository. Must be in the format of
},
},
},
ExactlyOneOf: []string{"remote_repository_config.0.apt_repository", "remote_repository_config.0.docker_repository", "remote_repository_config.0.maven_repository", "remote_repository_config.0.npm_repository", "remote_repository_config.0.python_repository", "remote_repository_config.0.yum_repository"},
ExactlyOneOf: []string{"remote_repository_config.0.apt_repository", "remote_repository_config.0.docker_repository", "remote_repository_config.0.maven_repository", "remote_repository_config.0.npm_repository", "remote_repository_config.0.python_repository", "remote_repository_config.0.yum_repository", "remote_repository_config.0.common_repository"},
},
},
},
Expand Down Expand Up @@ -1407,6 +1425,8 @@ func flattenArtifactRegistryRepositoryRemoteRepositoryConfig(v interface{}, d *s
flattenArtifactRegistryRepositoryRemoteRepositoryConfigPythonRepository(original["pythonRepository"], d, config)
transformed["yum_repository"] =
flattenArtifactRegistryRepositoryRemoteRepositoryConfigYumRepository(original["yumRepository"], d, config)
transformed["common_repository"] =
flattenArtifactRegistryRepositoryRemoteRepositoryConfigCommonRepository(original["commonRepository"], d, config)
transformed["upstream_credentials"] =
flattenArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentials(original["upstreamCredentials"], d, config)
transformed["disable_upstream_validation"] =
Expand Down Expand Up @@ -1649,6 +1669,23 @@ func flattenArtifactRegistryRepositoryRemoteRepositoryConfigYumRepositoryPublicR
return v
}

func flattenArtifactRegistryRepositoryRemoteRepositoryConfigCommonRepository(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["uri"] =
flattenArtifactRegistryRepositoryRemoteRepositoryConfigCommonRepositoryUri(original["uri"], d, config)
return []interface{}{transformed}
}
func flattenArtifactRegistryRepositoryRemoteRepositoryConfigCommonRepositoryUri(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentials(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
Expand Down Expand Up @@ -2075,6 +2112,13 @@ func expandArtifactRegistryRepositoryRemoteRepositoryConfig(v interface{}, d tpg
transformed["yumRepository"] = transformedYumRepository
}

transformedCommonRepository, err := expandArtifactRegistryRepositoryRemoteRepositoryConfigCommonRepository(original["common_repository"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedCommonRepository); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["commonRepository"] = transformedCommonRepository
}

transformedUpstreamCredentials, err := expandArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentials(original["upstream_credentials"], d, config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -2414,6 +2458,29 @@ func expandArtifactRegistryRepositoryRemoteRepositoryConfigYumRepositoryPublicRe
return v, nil
}

func expandArtifactRegistryRepositoryRemoteRepositoryConfigCommonRepository(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedUri, err := expandArtifactRegistryRepositoryRemoteRepositoryConfigCommonRepositoryUri(original["uri"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedUri); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["uri"] = transformedUri
}

return transformed, nil
}

func expandArtifactRegistryRepositoryRemoteRepositoryConfigCommonRepositoryUri(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandArtifactRegistryRepositoryRemoteRepositoryConfigUpstreamCredentials(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,56 @@ resource "google_artifact_registry_repository" "my-repo" {
`, context)
}

func TestAccArtifactRegistryRepository_artifactRegistryRepositoryRemoteCommonRepositoryWithDockerExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckArtifactRegistryRepositoryDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccArtifactRegistryRepository_artifactRegistryRepositoryRemoteCommonRepositoryWithDockerExample(context),
},
{
ResourceName: "google_artifact_registry_repository.my-repo",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels", "location", "remote_repository_config.0.disable_upstream_validation", "repository_id", "terraform_labels"},
},
},
})
}

func testAccArtifactRegistryRepository_artifactRegistryRepositoryRemoteCommonRepositoryWithDockerExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_artifact_registry_repository" "upstream_repo" {
location = "us-central1"
repository_id = "tf-test-example-upstream-repo%{random_suffix}"
description = "example upstream repository%{random_suffix}"
format = "DOCKER"
}
resource "google_artifact_registry_repository" "my-repo" {
location = "us-central1"
repository_id = "tf-test-example-common-remote%{random_suffix}"
description = "example remote common repository with docker upstream%{random_suffix}"
format = "DOCKER"
mode = "REMOTE_REPOSITORY"
remote_repository_config {
description = "pull-through cache of another Artifact Registry repository"
common_repository {
uri = google_artifact_registry_repository.upstream_repo.id
}
}
}
`, context)
}

func testAccCheckArtifactRegistryRepositoryDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
41 changes: 41 additions & 0 deletions website/docs/r/artifact_registry_repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,36 @@ resource "google_artifact_registry_repository" "my-repo" {
}
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_image=gcr.io%2Fcloudshell-images%2Fcloudshell%3Alatest&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md&cloudshell_working_dir=artifact_registry_repository_remote_common_repository_with_docker&open_in_editor=main.tf" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Artifact Registry Repository Remote Common Repository With Docker


```hcl
resource "google_artifact_registry_repository" "upstream_repo" {
location = "us-central1"
repository_id = "example-upstream-repo"
description = "example upstream repository"
format = "DOCKER"
}
resource "google_artifact_registry_repository" "my-repo" {
location = "us-central1"
repository_id = "example-common-remote"
description = "example remote common repository with docker upstream"
format = "DOCKER"
mode = "REMOTE_REPOSITORY"
remote_repository_config {
description = "pull-through cache of another Artifact Registry repository"
common_repository {
uri = google_artifact_registry_repository.upstream_repo.id
}
}
}
```

## Argument Reference

Expand Down Expand Up @@ -743,6 +773,11 @@ The following arguments are supported:
Specific settings for an Yum remote repository.
Structure is [documented below](#nested_yum_repository).

* `common_repository` -
(Optional)
Specific settings for an Artifact Registory remote repository.
Structure is [documented below](#nested_common_repository).

* `upstream_credentials` -
(Optional)
The credentials used to access the remote repository.
Expand Down Expand Up @@ -872,6 +907,12 @@ The following arguments are supported:
(Required)
Specific repository from the base, e.g. `"pub/rocky/9/BaseOS/x86_64/os"`

<a name="nested_common_repository"></a>The `common_repository` block supports:

* `uri` -
(Required)
Specific uri to the Artifact Registory repository, e.g. `projects/UPSTREAM_PROJECT_ID/locations/REGION/repositories/UPSTREAM_REPOSITORY`

<a name="nested_upstream_credentials"></a>The `upstream_credentials` block supports:

* `username_password_credentials` -
Expand Down

0 comments on commit 3d8f4a2

Please sign in to comment.