From 9624a1189aee046993a919cb10841f2d1a5c0f43 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Mon, 30 Nov 2020 19:14:54 +0000 Subject: [PATCH] Add google_composer_environment data source (#4238) Signed-off-by: Modular Magician --- .changelog/4238.txt | 3 + ...data_source_google_composer_environment.go | 31 +++++ ...source_google_composer_environment_test.go | 112 ++++++++++++++++++ google/provider.go | 1 + .../docs/d/composer_environment.html.markdown | 60 ++++++++++ website/docs/r/bigquery_table.html.markdown | 2 +- website/google.erb | 4 + 7 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 .changelog/4238.txt create mode 100644 google/data_source_google_composer_environment.go create mode 100644 google/data_source_google_composer_environment_test.go create mode 100644 website/docs/d/composer_environment.html.markdown diff --git a/.changelog/4238.txt b/.changelog/4238.txt new file mode 100644 index 00000000000..5f6c62cd679 --- /dev/null +++ b/.changelog/4238.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +composer: added datasource for `google_composer_environment` +``` diff --git a/google/data_source_google_composer_environment.go b/google/data_source_google_composer_environment.go new file mode 100644 index 00000000000..6713545b03b --- /dev/null +++ b/google/data_source_google_composer_environment.go @@ -0,0 +1,31 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGoogleComposerEnvironment() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGoogleComposerEnvironmentRead, + Schema: resourceComposerEnvironment().Schema, + } +} + +func dataSourceGoogleComposerEnvironmentRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + region, err := getRegion(d, config) + if err != nil { + return err + } + envName := d.Get("name").(string) + + d.SetId(fmt.Sprintf("projects/%s/locations/%s/environments/%s", project, region, envName)) + + return resourceComposerEnvironmentRead(d, meta) +} diff --git a/google/data_source_google_composer_environment_test.go b/google/data_source_google_composer_environment_test.go new file mode 100644 index 00000000000..9fe045bd6cc --- /dev/null +++ b/google/data_source_google_composer_environment_test.go @@ -0,0 +1,112 @@ +package google + +import ( + "errors" + "fmt" + "strconv" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func TestAccDataSourceComposerEnvironment_basic(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceComposerEnvironment_basic(context), + Check: resource.ComposeTestCheckFunc( + testAccCheckGoogleComposerEnvironmentMeta("data.google_composer_environment.test"), + ), + }, + }, + }) +} + +func testAccCheckGoogleComposerEnvironmentMeta(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("can't find environment data source: %s", n) + } + + if rs.Primary.ID == "" { + return errors.New("environment data source ID not set.") + } + + configCountStr, ok := rs.Primary.Attributes["config.#"] + if !ok { + return errors.New("can't find 'config' attribute") + } + + configCount, err := strconv.Atoi(configCountStr) + if err != nil { + return errors.New("failed to read number of valid config entries") + } + if configCount < 1 { + return fmt.Errorf("expected at least 1 valid config entry, received %d, this is most likely a bug", + configCount) + } + + for i := 0; i < configCount; i++ { + idx := "config." + strconv.Itoa(i) + + if v, ok := rs.Primary.Attributes[idx+".airflow_uri"]; !ok || v == "" { + return fmt.Errorf("config %v is missing airflow_uri", i) + } + if v, ok := rs.Primary.Attributes[idx+".dag_gcs_prefix"]; !ok || v == "" { + return fmt.Errorf("config %v is missing dag_gcs_prefix", i) + } + if v, ok := rs.Primary.Attributes[idx+".gke_cluster"]; !ok || v == "" { + return fmt.Errorf("config %v is missing gke_cluster", i) + } + } + + return nil + } +} + +func testAccDataSourceComposerEnvironment_basic(context map[string]interface{}) string { + return Nprintf(` +resource "google_composer_environment" "test" { + name = "composer-env-%{random_suffix}" + region = "us-central1" + + config { + node_config { + network = google_compute_network.test.self_link + subnetwork = google_compute_subnetwork.test.self_link + zone = "us-central1-a" + } + } +} + +// use a separate network to avoid conflicts with other tests running in parallel +// that use the default network/subnet +resource "google_compute_network" "test" { + name = "composer-net-%{random_suffix}" + auto_create_subnetworks = false +} + +resource "google_compute_subnetwork" "test" { + name = "composer-subnet-%{random_suffix}" + ip_cidr_range = "10.2.0.0/16" + region = "us-central1" + network = google_compute_network.test.self_link +} + +data "google_composer_environment" "test" { + name = google_composer_environment.test.name + + depends_on = [google_composer_environment.test] +} +`, context) +} diff --git a/google/provider.go b/google/provider.go index 1d93ebe45a9..221d26d2c14 100644 --- a/google/provider.go +++ b/google/provider.go @@ -589,6 +589,7 @@ func Provider() *schema.Provider { "google_cloud_identity_groups": dataSourceGoogleCloudIdentityGroups(), "google_cloud_identity_group_memberships": dataSourceGoogleCloudIdentityGroupMemberships(), "google_cloud_run_service": dataSourceGoogleCloudRunService(), + "google_composer_environment": dataSourceGoogleComposerEnvironment(), "google_composer_image_versions": dataSourceGoogleComposerImageVersions(), "google_compute_address": dataSourceGoogleComputeAddress(), "google_compute_backend_service": dataSourceGoogleComputeBackendService(), diff --git a/website/docs/d/composer_environment.html.markdown b/website/docs/d/composer_environment.html.markdown new file mode 100644 index 00000000000..b96d60a9528 --- /dev/null +++ b/website/docs/d/composer_environment.html.markdown @@ -0,0 +1,60 @@ +--- +subcategory: "Cloud Composer" +layout: "google" +page_title: "Google: google_composer_environment" +sidebar_current: "docs-google-datasource-composer-environment" +description: |- + Provides Cloud Composer environment configuration data. +--- + +# google\_composer\_environment + +Provides access to Cloud Composer environment configuration in a region for a given project. + +## Example Usage + +```hcl +resource "google_composer_environment" "composer_env" { + name = "composer-environment" +} + +data "google_composer_environment" "composer_env" { + name = google_composer_environment.test.name + + depends_on = [google_composer_environment.composer_env] +} + +output "debug" { + value = data.google_composer_environment.composer_env.config +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) Name of the environment. + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. + +* `region` - (Optional) The location or Compute Engine region of the environment. + +## Attributes Reference + +The following attributes are exported: + +* `id` - An identifier for the resource in format `projects/{{project}}/locations/{{region}}/environments/{{name}}` + +* `config` - Configuration parameters for the environment. + Full structure is provided by [composer environment resource documentation](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/composer_environment#config). + + * `config.0.gke_cluster` - + The Kubernetes Engine cluster used to run the environment. + + * `config.0.dag_gcs_prefix` - + The Cloud Storage prefix of the DAGs for the environment. + + * `config.0.airflow_uri` - + The URI of the Apache Airflow Web UI hosted within the + environment. diff --git a/website/docs/r/bigquery_table.html.markdown b/website/docs/r/bigquery_table.html.markdown index 33baffe4018..8ea824628d7 100644 --- a/website/docs/r/bigquery_table.html.markdown +++ b/website/docs/r/bigquery_table.html.markdown @@ -330,5 +330,5 @@ exported: BigQuery tables can be imported using the `project`, `dataset_id`, and `table_id`, e.g. ``` -$ terraform import google_bigquery_table.default projects/{{project}}/datasets/{{dataset}}/tables/{{name}} +$ terraform import google_bigquery_table.default gcp-project/foo/bar ``` diff --git a/website/google.erb b/website/google.erb index d2eac89e268..2ab7e7b181e 100644 --- a/website/google.erb +++ b/website/google.erb @@ -610,6 +610,10 @@ Data Sources