-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
90096cd
commit bb9884c
Showing
6 changed files
with
211 additions
and
0 deletions.
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,3 @@ | ||
```release-note:new-datasource | ||
composer: added datasource for `google_composer_environment` | ||
``` |
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 @@ | ||
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
112
google-beta/data_source_google_composer_environment_test.go
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,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) | ||
} |
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
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,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. |
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