forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add data source for composer image versions (hashicorp#752)
Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
56ea59c
commit 50d3d28
Showing
5 changed files
with
212 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,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
72
google-beta/data_source_google_composer_image_versions_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,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" { | ||
} | ||
` |
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
46 changes: 46 additions & 0 deletions
46
website/docs/d/datasource_google_composer_image_versions.html.markdown
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,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 |
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