From 5e5d71d825bbb5d39363fe75e6c42e6cb56dc036 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Fri, 23 Sep 2022 18:11:20 +0000 Subject: [PATCH] Artifact Registry Repository Data Source Added (#6590) * Artifact Registry Data Source Added * Artifact Registry Resource Location Changes Updated Signed-off-by: Modular Magician --- .changelog/6590.txt | 3 ++ ...ata_source_artifact_registry_repository.go | 47 +++++++++++++++++++ ...ource_artifact_registry_repository_test.go | 47 +++++++++++++++++++ google/provider.go | 1 + ...artifact_registry_repository.html.markdown | 38 +++++++++++++++ 5 files changed, 136 insertions(+) create mode 100644 .changelog/6590.txt create mode 100644 google/data_source_artifact_registry_repository.go create mode 100644 google/data_source_artifact_registry_repository_test.go create mode 100644 website/docs/d/artifact_registry_repository.html.markdown diff --git a/.changelog/6590.txt b/.changelog/6590.txt new file mode 100644 index 00000000000..852a2a0e847 --- /dev/null +++ b/.changelog/6590.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +`google_artifact_registry_repository` +``` diff --git a/google/data_source_artifact_registry_repository.go b/google/data_source_artifact_registry_repository.go new file mode 100644 index 00000000000..9bf733bccbb --- /dev/null +++ b/google/data_source_artifact_registry_repository.go @@ -0,0 +1,47 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceArtifactRegistryRepository() *schema.Resource { + // Generate datasource schema from resource + dsSchema := datasourceSchemaFromResourceSchema(resourceArtifactRegistryRepository().Schema) + + // Set 'Required' schema elements + addRequiredFieldsToSchema(dsSchema, "repository_id", "location") + + // Set 'Optional' schema elements + addOptionalFieldsToSchema(dsSchema, "project") + + return &schema.Resource{ + Read: dataSourceArtifactRegistryRepositoryRead, + Schema: dsSchema, + } +} + +func dataSourceArtifactRegistryRepositoryRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + project, err := getProject(d, config) + if err != nil { + return err + } + + location, err := getLocation(d, config) + if err != nil { + return err + } + + repository_id := d.Get("repository_id").(string) + d.SetId(fmt.Sprintf("projects/%s/locations/%s/repositories/%s", project, location, repository_id)) + + err = resourceArtifactRegistryRepositoryRead(d, meta) + if err != nil { + return err + } + + return nil +} diff --git a/google/data_source_artifact_registry_repository_test.go b/google/data_source_artifact_registry_repository_test.go new file mode 100644 index 00000000000..b1e0cc414c0 --- /dev/null +++ b/google/data_source_artifact_registry_repository_test.go @@ -0,0 +1,47 @@ +package google + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccDataSourceGoogleArtifactRegistryRepositoryConfig(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + } + funcDataName := "data.google_artifact_registry_repository.my-repo" + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckArtifactRegistryRepositoryDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleArtifactRegistryRepositoryConfig(context), + Check: resource.ComposeTestCheckFunc( + checkDataSourceStateMatchesResourceState(funcDataName, + "google_artifact_registry_repository.my-repo"), + ), + }, + }, + }) +} + +func testAccDataSourceGoogleArtifactRegistryRepositoryConfig(context map[string]interface{}) string { + return Nprintf(` +resource "google_artifact_registry_repository" "my-repo" { + location = "us-central1" + repository_id = "tf-test-my-repository%{random_suffix}" + description = "example docker repository%{random_suffix}" + format = "DOCKER" +} + +data "google_artifact_registry_repository" "my-repo" { + location = "us-central1" + repository_id = google_artifact_registry_repository.my-repo.repository_id +} +`, context) +} diff --git a/google/provider.go b/google/provider.go index e33083f835d..9d83ccc9eaa 100644 --- a/google/provider.go +++ b/google/provider.go @@ -773,6 +773,7 @@ func Provider() *schema.Provider { "google_access_approval_organization_service_account": dataSourceAccessApprovalOrganizationServiceAccount(), "google_access_approval_project_service_account": dataSourceAccessApprovalProjectServiceAccount(), "google_active_folder": dataSourceGoogleActiveFolder(), + "google_artifact_registry_repository": dataSourceArtifactRegistryRepository(), "google_app_engine_default_service_account": dataSourceGoogleAppEngineDefaultServiceAccount(), "google_billing_account": dataSourceGoogleBillingAccount(), "google_bigquery_default_service_account": dataSourceGoogleBigqueryDefaultServiceAccount(), diff --git a/website/docs/d/artifact_registry_repository.html.markdown b/website/docs/d/artifact_registry_repository.html.markdown new file mode 100644 index 00000000000..1908b11797a --- /dev/null +++ b/website/docs/d/artifact_registry_repository.html.markdown @@ -0,0 +1,38 @@ +--- +subcategory: "Artifact Registry Repository" +page_title: "Google: google_artifact_registry_repository" +description: |- + Get information about a Google Artifact Registry Repository. +--- + +# google\_artifact\_registry\_repository + +Get information about a Google Artifact Registry Repository. For more information see +the [official documentation](https://cloud.google.com/artifact-registry/docs/) +and [API](https://cloud.google.com/artifact-registry/docs/apis). + +## Example Usage + +```hcl +data "google_artifact_registry_repository" "my-repo" { + location = "us-central1" + repository_id = "my-repository" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `repository_id` - (Required) The last part of the repository name. + +* `location` - (Required) The location of the artifact registry repository. eg us-central1 + +- - - + +* `project` - (Optional) The project in which the resource belongs. If it + is not provided, the provider project is used. + +## Attributes Reference + +See [google_artifact_registry_repository](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/artifact_registry_repository#argument-reference) resource for details of the available attributes.