Skip to content

Commit

Permalink
Add google_composer_environment data source (#4238) (#2745)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Nov 30, 2020
1 parent 90096cd commit bb9884c
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/4238.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
composer: added datasource for `google_composer_environment`
```
31 changes: 31 additions & 0 deletions google-beta/data_source_google_composer_environment.go
Original file line number Diff line number Diff line change
@@ -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)
}
112 changes: 112 additions & 0 deletions google-beta/data_source_google_composer_environment_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions google-beta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,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(),
Expand Down
60 changes: 60 additions & 0 deletions website/docs/d/composer_environment.html.markdown
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,10 @@
<a href="#">Data Sources</a>
<ul class="nav nav-auto-expand">

<li>
<a href="/docs/providers/google/d/composer_environment.html">google_composer_environment</a>
</li>

<li>
<a href="/docs/providers/google/d/composer_image_versions.html">google_composer_image_versions</a>
</li>
Expand Down

0 comments on commit bb9884c

Please sign in to comment.