Merge pull request #14280 from hashicorp/paddy_container_versions

providers/google: add google_container_versions data source.
This commit is contained in:
Paddy 2017-05-10 15:50:16 -07:00 committed by GitHub
commit 25ed7cc6c0
6 changed files with 223 additions and 5 deletions

View File

@ -0,0 +1,67 @@
package google
import (
"fmt"
"time"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceGoogleContainerEngineVersions() *schema.Resource {
return &schema.Resource{
Read: dataSourceGoogleContainerEngineVersionsRead,
Schema: map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Optional: true,
},
"zone": {
Type: schema.TypeString,
Required: true,
},
"latest_master_version": {
Type: schema.TypeString,
Computed: true,
},
"latest_node_version": {
Type: schema.TypeString,
Computed: true,
},
"valid_master_versions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"valid_node_versions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceGoogleContainerEngineVersionsRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
zone := d.Get("zone").(string)
resp, err := config.clientContainer.Projects.Zones.GetServerconfig(project, zone).Do()
if err != nil {
return fmt.Errorf("Error retrieving available container cluster versions: %s", err.Error())
}
d.Set("valid_master_versions", resp.ValidMasterVersions)
d.Set("valid_node_versions", resp.ValidNodeVersions)
d.Set("latest_master_version", resp.ValidMasterVersions[0])
d.Set("latest_node_version", resp.ValidNodeVersions[0])
d.SetId(time.Now().UTC().String())
return nil
}

View File

@ -0,0 +1,97 @@
package google
import (
"errors"
"fmt"
"strconv"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccGoogleContainerEngineVersions_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGoogleContainerEngineVersionsConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleContainerEngineVersionsMeta("data.google_container_engine_versions.versions"),
),
},
},
})
}
func testAccCheckGoogleContainerEngineVersionsMeta(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.")
}
nodeCount, ok := rs.Primary.Attributes["valid_node_versions.#"]
if !ok {
return errors.New("can't find 'valid_node_versions' attribute")
}
noOfNodes, err := strconv.Atoi(nodeCount)
if err != nil {
return errors.New("failed to read number of valid node versions")
}
if noOfNodes < 2 {
return fmt.Errorf("expected at least 2 valid node versions, received %d, this is most likely a bug",
noOfNodes)
}
for i := 0; i < noOfNodes; i++ {
idx := "valid_node_versions." + strconv.Itoa(i)
v, ok := rs.Primary.Attributes[idx]
if !ok {
return fmt.Errorf("valid node versions list is corrupt (%q not found), this is definitely a bug", idx)
}
if len(v) < 1 {
return fmt.Errorf("Empty node version (%q), this is definitely a bug", idx)
}
}
masterCount, ok := rs.Primary.Attributes["valid_master_versions.#"]
if !ok {
return errors.New("can't find 'valid_master_versions' attribute")
}
noOfMasters, err := strconv.Atoi(masterCount)
if err != nil {
return errors.New("failed to read number of valid master versions")
}
if noOfMasters < 2 {
return fmt.Errorf("expected at least 2 valid master versions, received %d, this is most likely a bug",
noOfMasters)
}
for i := 0; i < noOfMasters; i++ {
idx := "valid_master_versions." + strconv.Itoa(i)
v, ok := rs.Primary.Attributes[idx]
if !ok {
return fmt.Errorf("valid master versions list is corrupt (%q not found), this is definitely a bug", idx)
}
if len(v) < 1 {
return fmt.Errorf("Empty master version (%q), this is definitely a bug", idx)
}
}
return nil
}
}
var testAccCheckGoogleContainerEngineVersionsConfig = `
data "google_container_engine_versions" "versions" {
zone = "us-central1-b"
}
`

View File

@ -52,6 +52,7 @@ func Provider() terraform.ResourceProvider {
"google_compute_network": dataSourceGoogleComputeNetwork(),
"google_compute_subnetwork": dataSourceGoogleComputeSubnetwork(),
"google_compute_zones": dataSourceGoogleComputeZones(),
"google_container_engine_versions": dataSourceGoogleContainerEngineVersions(),
"google_iam_policy": dataSourceGoogleIamPolicy(),
},

View File

@ -400,10 +400,14 @@ resource "google_container_cluster" "with_additional_zones" {
}`, acctest.RandString(10))
var testAccContainerCluster_withVersion = fmt.Sprintf(`
data "google_container_engine_versions" "central1a" {
zone = "us-central1-a"
}
resource "google_container_cluster" "with_version" {
name = "cluster-test-%s"
zone = "us-central1-a"
node_version = "1.6.1"
node_version = "${data.google_container_engine_versions.central1a.latest_node_version}"
initial_node_count = 1
master_auth {

View File

@ -0,0 +1,46 @@
---
layout: "google"
page_title: "Google: google_container_engine_versions"
sidebar_current: "docs-google-datasource-container-versions"
description: |-
Provides lists of available Google Container Engine versions for masters and nodes.
---
# google\_container\_engine\_versions
Provides access to available Google Container Engine versions in a zone for a given project.
```hcl
data "google_container_engine_versions" "central1b" {
zone = "us-central1-b"
}
resource "google_container_cluster" "foo" {
name = "terraform-test-cluster"
zone = "us-central1-b"
node_version = "${data.google_container_engine_versions.central1b.latest_node_version}"
initial_node_count = 1
master_auth {
username = "mr.yoda"
password = "adoy.rm"
}
}
```
## Argument Reference
The following arguments are supported:
* `zone` (required) - Zone to list available cluster versions for. Should match the zone the cluster will be deployed in.
* `project` (optional) - ID of the project to list available cluster versions for. Should match the project the cluster will be deployed to.
Defaults to the project that the provider is authenticated with.
## Attributes Reference
The following attributes are exported:
* `valid_master_versions` - A list of versions available in the given zone for use with master instances.
* `valid_node_versions` - A list of versions available in the given zone for use with node instances.
* `latest_master_version` - The latest version available in the given zone for use with master instances.
* `latest_node_version` - The latest version available in the given zone for use with node instances.

View File

@ -33,6 +33,9 @@
<li<%= sidebar_current("docs-google-datasource-compute-zones") %>>
<a href="/docs/providers/google/d/google_compute_zones.html">google_compute_zones</a>
</li>
<li<%= sidebar_current("docs-google-datasource-container-versions") %>>
<a href="/docs/providers/google/d/google_container_engine_versions.html">google_container_engine_versions</a>
</li>
<li<%= sidebar_current("docs-google-datasource-iam-policy") %>>
<a href="/docs/providers/google/d/google_iam_policy.html">google_iam_policy</a>
</li>