Skip to content

Commit

Permalink
add data source for composer image versions (hashicorp#752)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and danawillow committed May 31, 2019
1 parent 56ea59c commit 50d3d28
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 0 deletions.
90 changes: 90 additions & 0 deletions google-beta/data_source_google_composer_image_versions.go
Original file line number Diff line number Diff line change
@@ -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
}
72 changes: 72 additions & 0 deletions google-beta/data_source_google_composer_image_versions_test.go
Original file line number Diff line number Diff line change
@@ -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" {
}
`
1 change: 1 addition & 0 deletions google-beta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<li<%= sidebar_current("docs-google-datasource-cloudfunctions-function") %>>
<a href="/docs/providers/google/d/datasource_cloudfunctions_function.html">google_cloudfunctions_function</a>
</li>
<li<%= sidebar_current("docs-google-datasource-composer-image-versions") %>>
<a href="/docs/providers/google/d/datasource_google_composer_image_versions.html">google_composer_image_versions</a>
</li>
<li<%= sidebar_current("docs-google-datasource-compute-address") %>>
<a href="/docs/providers/google/d/datasource_compute_address.html">google_compute_address</a>
</li>
Expand Down

0 comments on commit 50d3d28

Please sign in to comment.