From 97e37ad4ce99c50ff52af9d56177edbebe647b1f Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Wed, 22 May 2019 17:48:58 +0000 Subject: [PATCH] add data source for composer image versions Signed-off-by: Modular Magician --- ...a_source_google_composer_image_versions.go | 90 +++++++++++++++++++ ...rce_google_composer_image_versions_test.go | 72 +++++++++++++++ google-beta/provider.go | 1 + ...ogle_composer_image_versions.html.markdown | 46 ++++++++++ website/google.erb | 3 + 5 files changed, 212 insertions(+) create mode 100644 google-beta/data_source_google_composer_image_versions.go create mode 100644 google-beta/data_source_google_composer_image_versions_test.go create mode 100644 website/docs/d/datasource_google_composer_image_versions.html.markdown diff --git a/google-beta/data_source_google_composer_image_versions.go b/google-beta/data_source_google_composer_image_versions.go new file mode 100644 index 0000000000..5ced5c4d5c --- /dev/null +++ b/google-beta/data_source_google_composer_image_versions.go @@ -0,0 +1,90 @@ +package google + +import ( + "fmt" + "log" + "time" + + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceGoogleComposerImageVersions() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGoogleComposerImageVersionsRead, + Schema: map[string]*schema.Schema{ + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "region": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "image_versions": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "image_version_id": { + Type: schema.TypeString, + Computed: true, + }, + "supported_python_versions": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + }, + }, + }, + } +} + +func dataSourceGoogleComposerImageVersionsRead(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 + } + + url, err := replaceVars(d, config, "https://composer.googleapis.com/v1/projects/{{project}}/locations/{{region}}/imageVersions") + if err != nil { + return err + } + + versions, err := paginatedListRequest(url, config, flattenGoogleComposerImageVersions) + if err != nil { + return fmt.Errorf("Error listing Composer image versions: %s", err) + } + + log.Printf("[DEBUG] Received Composer Image Versions: %q", versions) + + d.Set("image_versions", versions) + d.Set("region", region) + d.Set("project", project) + d.SetId(time.Now().UTC().String()) + + return nil +} + +func flattenGoogleComposerImageVersions(resp map[string]interface{}) []interface{} { + verObjList := resp["imageVersions"].([]interface{}) + versions := make([]interface{}, len(verObjList)) + for i, v := range verObjList { + verObj := v.(map[string]interface{}) + versions[i] = map[string]interface{}{ + "image_version_id": verObj["imageVersionId"], + "supported_python_versions": verObj["supportedPythonVersions"], + } + } + return versions +} diff --git a/google-beta/data_source_google_composer_image_versions_test.go b/google-beta/data_source_google_composer_image_versions_test.go new file mode 100644 index 0000000000..51f07cbaa2 --- /dev/null +++ b/google-beta/data_source_google_composer_image_versions_test.go @@ -0,0 +1,72 @@ +package google + +import ( + "errors" + "fmt" + "strconv" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccDataSourceComposerImageVersions_basic(t *testing.T) { + t.Parallel() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckGoogleComposerImageVersionsConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckGoogleComposerImageVersionsMeta("data.google_composer_image_versions.versions"), + ), + }, + }, + }) +} + +func testAccCheckGoogleComposerImageVersionsMeta(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Can't find versions data source: %s", n) + } + + if rs.Primary.ID == "" { + return errors.New("versions data source ID not set.") + } + + versionCountStr, ok := rs.Primary.Attributes["image_versions.#"] + if !ok { + return errors.New("can't find 'image_versions' attribute") + } + + versionCount, err := strconv.Atoi(versionCountStr) + if err != nil { + return errors.New("failed to read number of valid image versions") + } + if versionCount < 1 { + return fmt.Errorf("expected at least 1 valid image versions, received %d, this is most likely a bug", + versionCount) + } + + for i := 0; i < versionCount; i++ { + idx := "image_versions." + strconv.Itoa(i) + if v, ok := rs.Primary.Attributes[idx+".image_version_id"]; !ok || v == "" { + return fmt.Errorf("image_version %v is missing image_version_id", i) + } + if v, ok := rs.Primary.Attributes[idx+".supported_python_versions.#"]; !ok || v == "" || v == "0" { + return fmt.Errorf("image_version %v is missing supported_python_versions", i) + } + } + + return nil + } +} + +var testAccCheckGoogleComposerImageVersionsConfig = ` +data "google_composer_image_versions" "versions" { +} +` diff --git a/google-beta/provider.go b/google-beta/provider.go index f844e493c0..15bb4a8505 100644 --- a/google-beta/provider.go +++ b/google-beta/provider.go @@ -83,6 +83,7 @@ func Provider() terraform.ResourceProvider { "google_client_config": dataSourceGoogleClientConfig(), "google_client_openid_userinfo": dataSourceGoogleClientOpenIDUserinfo(), "google_cloudfunctions_function": dataSourceGoogleCloudFunctionsFunction(), + "google_composer_image_versions": dataSourceGoogleComposerImageVersions(), "google_compute_address": dataSourceGoogleComputeAddress(), "google_compute_backend_service": dataSourceGoogleComputeBackendService(), "google_compute_default_service_account": dataSourceGoogleComputeDefaultServiceAccount(), diff --git a/website/docs/d/datasource_google_composer_image_versions.html.markdown b/website/docs/d/datasource_google_composer_image_versions.html.markdown new file mode 100644 index 0000000000..fc3efb17b4 --- /dev/null +++ b/website/docs/d/datasource_google_composer_image_versions.html.markdown @@ -0,0 +1,46 @@ +--- +layout: "google" +page_title: "Google: google_composer_image_versions" +sidebar_current: "docs-google-datasource-composer-image-versions" +description: |- + Provides available Cloud Composer versions. +--- + +# google\_composer\_image\_versions + +Provides access to available Cloud Composer versions in a region for a given project. + +## Example Usage + +```hcl +data "google_composer_image_versions" "all" { +} + +resource "google_composer_environment" "test" { + name = "test-env" + region = "us-central1" + config { + software_config { + image_version = "${data.google_composer_image_versions.all.image_versions.0.image_version_id}" + } + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `project` - (Optional) The ID of the project to list versions in. + If it is not provided, the provider project is used. + +* `region` - (Optional) The location to list versions in. + If it is not provider, the provider region is used. + +## Attributes Reference + +The following attributes are exported: + +* `image_versions` - A list of composer image versions available in the given project and location. Each `image_version` contains: + * `image_version_id` - The string identifier of the image version, in the form: "composer-x.y.z-airflow-a.b(.c)" + * `supported_python_versions` - Supported python versions for this image version \ No newline at end of file diff --git a/website/google.erb b/website/google.erb index 6d6944f3b4..40598f225f 100644 --- a/website/google.erb +++ b/website/google.erb @@ -45,6 +45,9 @@ > google_cloudfunctions_function + > + google_composer_image_versions + > google_compute_address