From 8b78d5b2332f9e2e290fe7c61e36a0d0fd9bc0b5 Mon Sep 17 00:00:00 2001 From: Paddy Date: Mon, 8 May 2017 00:38:11 -0700 Subject: [PATCH 1/4] providers/google: add google_container_versions data source. Add a data source for listing available versions for Container Engine clusters or retrieving the latest available version. This is mostly to support our tests for specifying a version for cluster creation; the withVersion test has been updated to use the data source, meaning it will stop failing on us as new versions get released. --- .../data_source_google_container_versions.go | 67 +++++++++++++ ...a_source_google_container_versions_test.go | 97 +++++++++++++++++++ builtin/providers/google/provider.go | 1 + .../google/resource_container_cluster_test.go | 6 +- .../d/google_container_versions.html.markdown | 46 +++++++++ website/source/layouts/google.erb | 3 + 6 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 builtin/providers/google/data_source_google_container_versions.go create mode 100644 builtin/providers/google/data_source_google_container_versions_test.go create mode 100644 website/source/docs/providers/google/d/google_container_versions.html.markdown diff --git a/builtin/providers/google/data_source_google_container_versions.go b/builtin/providers/google/data_source_google_container_versions.go new file mode 100644 index 000000000..9ae0f2a7b --- /dev/null +++ b/builtin/providers/google/data_source_google_container_versions.go @@ -0,0 +1,67 @@ +package google + +import ( + "fmt" + "time" + + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceGoogleContainerVersions() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGoogleContainerVersionsRead, + 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 dataSourceGoogleContainerVersionsRead(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 +} diff --git a/builtin/providers/google/data_source_google_container_versions_test.go b/builtin/providers/google/data_source_google_container_versions_test.go new file mode 100644 index 000000000..d4399a6b6 --- /dev/null +++ b/builtin/providers/google/data_source_google_container_versions_test.go @@ -0,0 +1,97 @@ +package google + +import ( + "errors" + "fmt" + "strconv" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccGoogleContainerVersions_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckGoogleContainerVersionsConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckGoogleContainerVersionsMeta("data.google_container_versions.versions"), + ), + }, + }, + }) +} + +func testAccCheckGoogleContainerVersionsMeta(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 testAccCheckGoogleContainerVersionsConfig = ` +data "google_container_versions" "versions" { + zone = "us-central1-b" +} +` diff --git a/builtin/providers/google/provider.go b/builtin/providers/google/provider.go index 431a29f1d..bb1bbedc7 100644 --- a/builtin/providers/google/provider.go +++ b/builtin/providers/google/provider.go @@ -52,6 +52,7 @@ func Provider() terraform.ResourceProvider { "google_compute_network": dataSourceGoogleComputeNetwork(), "google_compute_subnetwork": dataSourceGoogleComputeSubnetwork(), "google_compute_zones": dataSourceGoogleComputeZones(), + "google_container_versions": dataSourceGoogleContainerVersions(), "google_iam_policy": dataSourceGoogleIamPolicy(), }, diff --git a/builtin/providers/google/resource_container_cluster_test.go b/builtin/providers/google/resource_container_cluster_test.go index 236785e43..e7f2a3968 100644 --- a/builtin/providers/google/resource_container_cluster_test.go +++ b/builtin/providers/google/resource_container_cluster_test.go @@ -400,10 +400,14 @@ resource "google_container_cluster" "with_additional_zones" { }`, acctest.RandString(10)) var testAccContainerCluster_withVersion = fmt.Sprintf(` +data "google_container_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_versions.central1a.latest_node_version}" initial_node_count = 1 master_auth { diff --git a/website/source/docs/providers/google/d/google_container_versions.html.markdown b/website/source/docs/providers/google/d/google_container_versions.html.markdown new file mode 100644 index 000000000..b7b8afd30 --- /dev/null +++ b/website/source/docs/providers/google/d/google_container_versions.html.markdown @@ -0,0 +1,46 @@ +--- +layout: "google" +page_title: "Google: google_container_versions" +sidebar_current: "docs-google-datasource-container-versions" +description: |- + Provides lists of available Google Container versions for masters and nodes. +--- + +# google\_container\_versions + +Provides access to available Google Container versions in a zone for a given project. + +```hcl +data "google_container_versions" "central1b" { + zone = "us-central1-b" +} + +resource "google_container_cluster" "foo" { + name = "terraform-test-cluster" + zone = "us-central1-b" + node_version = "${data.google_container_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. diff --git a/website/source/layouts/google.erb b/website/source/layouts/google.erb index 77971bb16..368829edf 100644 --- a/website/source/layouts/google.erb +++ b/website/source/layouts/google.erb @@ -31,6 +31,9 @@ > google_compute_zones + > + google_container_versions + > google_iam_policy From 1709ea441416540530dbbd512b496a968def2d7f Mon Sep 17 00:00:00 2001 From: Paddy Date: Wed, 10 May 2017 15:01:15 -0700 Subject: [PATCH 2/4] google_container_versions => google_container_engine_versions As per feedback from @danawillow and @radeksimko. --- ...=> data_source_google_container_engine_versions.go} | 6 +++--- ...ta_source_google_container_engine_versions_test.go} | 4 ++-- builtin/providers/google/provider.go | 10 +++++----- .../google/resource_container_cluster_test.go | 4 ++-- ... => google_container_engine_versions.html.markdown} | 6 +++--- website/source/layouts/google.erb | 2 +- 6 files changed, 16 insertions(+), 16 deletions(-) rename builtin/providers/google/{data_source_google_container_versions.go => data_source_google_container_engine_versions.go} (86%) rename builtin/providers/google/{data_source_google_container_versions_test.go => data_source_google_container_engine_versions_test.go} (96%) rename website/source/docs/providers/google/d/{google_container_versions.html.markdown => google_container_engine_versions.html.markdown} (87%) diff --git a/builtin/providers/google/data_source_google_container_versions.go b/builtin/providers/google/data_source_google_container_engine_versions.go similarity index 86% rename from builtin/providers/google/data_source_google_container_versions.go rename to builtin/providers/google/data_source_google_container_engine_versions.go index 9ae0f2a7b..3eaf8043a 100644 --- a/builtin/providers/google/data_source_google_container_versions.go +++ b/builtin/providers/google/data_source_google_container_engine_versions.go @@ -7,9 +7,9 @@ import ( "github.com/hashicorp/terraform/helper/schema" ) -func dataSourceGoogleContainerVersions() *schema.Resource { +func dataSourceGoogleContainerEngineVersions() *schema.Resource { return &schema.Resource{ - Read: dataSourceGoogleContainerVersionsRead, + Read: dataSourceGoogleContainerEngineVersionsRead, Schema: map[string]*schema.Schema{ "project": { Type: schema.TypeString, @@ -41,7 +41,7 @@ func dataSourceGoogleContainerVersions() *schema.Resource { } } -func dataSourceGoogleContainerVersionsRead(d *schema.ResourceData, meta interface{}) error { +func dataSourceGoogleContainerEngineVersionsRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) project, err := getProject(d, config) diff --git a/builtin/providers/google/data_source_google_container_versions_test.go b/builtin/providers/google/data_source_google_container_engine_versions_test.go similarity index 96% rename from builtin/providers/google/data_source_google_container_versions_test.go rename to builtin/providers/google/data_source_google_container_engine_versions_test.go index d4399a6b6..208dff034 100644 --- a/builtin/providers/google/data_source_google_container_versions_test.go +++ b/builtin/providers/google/data_source_google_container_engine_versions_test.go @@ -18,7 +18,7 @@ func TestAccGoogleContainerVersions_basic(t *testing.T) { { Config: testAccCheckGoogleContainerVersionsConfig, Check: resource.ComposeTestCheckFunc( - testAccCheckGoogleContainerVersionsMeta("data.google_container_versions.versions"), + testAccCheckGoogleContainerVersionsMeta("data.google_container_engine_versions.versions"), ), }, }, @@ -91,7 +91,7 @@ func testAccCheckGoogleContainerVersionsMeta(n string) resource.TestCheckFunc { } var testAccCheckGoogleContainerVersionsConfig = ` -data "google_container_versions" "versions" { +data "google_container_engine_versions" "versions" { zone = "us-central1-b" } ` diff --git a/builtin/providers/google/provider.go b/builtin/providers/google/provider.go index bb1bbedc7..8f61f8bc2 100644 --- a/builtin/providers/google/provider.go +++ b/builtin/providers/google/provider.go @@ -49,11 +49,11 @@ func Provider() terraform.ResourceProvider { }, DataSourcesMap: map[string]*schema.Resource{ - "google_compute_network": dataSourceGoogleComputeNetwork(), - "google_compute_subnetwork": dataSourceGoogleComputeSubnetwork(), - "google_compute_zones": dataSourceGoogleComputeZones(), - "google_container_versions": dataSourceGoogleContainerVersions(), - "google_iam_policy": dataSourceGoogleIamPolicy(), + "google_compute_network": dataSourceGoogleComputeNetwork(), + "google_compute_subnetwork": dataSourceGoogleComputeSubnetwork(), + "google_compute_zones": dataSourceGoogleComputeZones(), + "google_container_engine_versions": dataSourceGoogleContainerEngineVersions(), + "google_iam_policy": dataSourceGoogleIamPolicy(), }, ResourcesMap: map[string]*schema.Resource{ diff --git a/builtin/providers/google/resource_container_cluster_test.go b/builtin/providers/google/resource_container_cluster_test.go index e7f2a3968..549803f55 100644 --- a/builtin/providers/google/resource_container_cluster_test.go +++ b/builtin/providers/google/resource_container_cluster_test.go @@ -400,14 +400,14 @@ resource "google_container_cluster" "with_additional_zones" { }`, acctest.RandString(10)) var testAccContainerCluster_withVersion = fmt.Sprintf(` -data "google_container_versions" "central1a" { +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 = "${data.google_container_versions.central1a.latest_node_version}" + node_version = "${data.google_container_engine_versions.central1a.latest_node_version}" initial_node_count = 1 master_auth { diff --git a/website/source/docs/providers/google/d/google_container_versions.html.markdown b/website/source/docs/providers/google/d/google_container_engine_versions.html.markdown similarity index 87% rename from website/source/docs/providers/google/d/google_container_versions.html.markdown rename to website/source/docs/providers/google/d/google_container_engine_versions.html.markdown index b7b8afd30..ad48595f7 100644 --- a/website/source/docs/providers/google/d/google_container_versions.html.markdown +++ b/website/source/docs/providers/google/d/google_container_engine_versions.html.markdown @@ -1,6 +1,6 @@ --- layout: "google" -page_title: "Google: google_container_versions" +page_title: "Google: google_container_engine_versions" sidebar_current: "docs-google-datasource-container-versions" description: |- Provides lists of available Google Container versions for masters and nodes. @@ -11,14 +11,14 @@ description: |- Provides access to available Google Container versions in a zone for a given project. ```hcl -data "google_container_versions" "central1b" { +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_versions.central1b.latest_node_version}" + node_version = "${data.google_container_engine_versions.central1b.latest_node_version}" initial_node_count = 1 master_auth { diff --git a/website/source/layouts/google.erb b/website/source/layouts/google.erb index 368829edf..00b971dc9 100644 --- a/website/source/layouts/google.erb +++ b/website/source/layouts/google.erb @@ -32,7 +32,7 @@ google_compute_zones > - google_container_versions + google_container_engine_versions > google_iam_policy From 9078efc4787a597826cddaaab0e9eadb1b392647 Mon Sep 17 00:00:00 2001 From: Paddy Date: Wed, 10 May 2017 15:12:14 -0700 Subject: [PATCH 3/4] Update test names. --- ...ata_source_google_container_engine_versions_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/builtin/providers/google/data_source_google_container_engine_versions_test.go b/builtin/providers/google/data_source_google_container_engine_versions_test.go index 208dff034..baf880943 100644 --- a/builtin/providers/google/data_source_google_container_engine_versions_test.go +++ b/builtin/providers/google/data_source_google_container_engine_versions_test.go @@ -10,22 +10,22 @@ import ( "github.com/hashicorp/terraform/terraform" ) -func TestAccGoogleContainerVersions_basic(t *testing.T) { +func TestAccGoogleContainerEngineVersions_basic(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccCheckGoogleContainerVersionsConfig, + Config: testAccCheckGoogleContainerEngineVersionsConfig, Check: resource.ComposeTestCheckFunc( - testAccCheckGoogleContainerVersionsMeta("data.google_container_engine_versions.versions"), + testAccCheckGoogleContainerEngineVersionsMeta("data.google_container_engine_versions.versions"), ), }, }, }) } -func testAccCheckGoogleContainerVersionsMeta(n string) resource.TestCheckFunc { +func testAccCheckGoogleContainerEngineVersionsMeta(n string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -90,7 +90,7 @@ func testAccCheckGoogleContainerVersionsMeta(n string) resource.TestCheckFunc { } } -var testAccCheckGoogleContainerVersionsConfig = ` +var testAccCheckGoogleContainerEngineVersionsConfig = ` data "google_container_engine_versions" "versions" { zone = "us-central1-b" } From de930823759876fc6edc86d49d1b0e36927bbc7a Mon Sep 17 00:00:00 2001 From: Paddy Date: Wed, 10 May 2017 15:41:02 -0700 Subject: [PATCH 4/4] More doc updates container => container engine. --- .../google/d/google_container_engine_versions.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/website/source/docs/providers/google/d/google_container_engine_versions.html.markdown b/website/source/docs/providers/google/d/google_container_engine_versions.html.markdown index ad48595f7..855e9bc9e 100644 --- a/website/source/docs/providers/google/d/google_container_engine_versions.html.markdown +++ b/website/source/docs/providers/google/d/google_container_engine_versions.html.markdown @@ -3,12 +3,12 @@ layout: "google" page_title: "Google: google_container_engine_versions" sidebar_current: "docs-google-datasource-container-versions" description: |- - Provides lists of available Google Container versions for masters and nodes. + Provides lists of available Google Container Engine versions for masters and nodes. --- -# google\_container\_versions +# google\_container\_engine\_versions -Provides access to available Google Container versions in a zone for a given project. +Provides access to available Google Container Engine versions in a zone for a given project. ```hcl data "google_container_engine_versions" "central1b" {