diff --git a/mmv1/templates/terraform/examples/container_attached_cluster_basic.tf.erb b/mmv1/templates/terraform/examples/container_attached_cluster_basic.tf.erb index 5a7f3776f6b4..271242951679 100644 --- a/mmv1/templates/terraform/examples/container_attached_cluster_basic.tf.erb +++ b/mmv1/templates/terraform/examples/container_attached_cluster_basic.tf.erb @@ -1,6 +1,11 @@ data "google_project" "project" { } +data "google_container_attached_versions" "versions" { + location = "us-west1" + project = data.google_project.project.project_id +} + resource "google_container_attached_cluster" "primary" { name = "<%= ctx[:vars]['name'] %>" location = "us-west1" @@ -10,7 +15,7 @@ resource "google_container_attached_cluster" "primary" { oidc_config { issuer_url = "https://oidc.issuer.url" } - platform_version = "1.24.0-gke.1" + platform_version = data.google_container_attached_versions.versions.valid_versions[0] fleet { project = "projects/${data.google_project.project.number}" } diff --git a/mmv1/templates/terraform/examples/container_attached_cluster_full.tf.erb b/mmv1/templates/terraform/examples/container_attached_cluster_full.tf.erb index 2004d7768ace..676937a176d4 100644 --- a/mmv1/templates/terraform/examples/container_attached_cluster_full.tf.erb +++ b/mmv1/templates/terraform/examples/container_attached_cluster_full.tf.erb @@ -1,6 +1,11 @@ data "google_project" "project" { } +data "google_container_attached_versions" "versions" { + location = "us-west1" + project = data.google_project.project.project_id +} + resource "google_container_attached_cluster" "primary" { name = "<%= ctx[:vars]['name'] %>" project = data.google_project.project.project_id @@ -17,7 +22,7 @@ resource "google_container_attached_cluster" "primary" { issuer_url = "https://oidc.issuer.url" jwks = base64encode("{\"keys\":[{\"use\":\"sig\",\"kty\":\"RSA\",\"kid\":\"testid\",\"alg\":\"RS256\",\"n\":\"somedata\",\"e\":\"AQAB\"}]}") } - platform_version = "1.24.0-gke.1" + platform_version = data.google_container_attached_versions.versions.valid_versions[0] fleet { project = "projects/${data.google_project.project.number}" } diff --git a/mmv1/third_party/terraform/data_sources/data_source_google_container_attached_versions.go b/mmv1/third_party/terraform/data_sources/data_source_google_container_attached_versions.go new file mode 100644 index 000000000000..a8812248ff54 --- /dev/null +++ b/mmv1/third_party/terraform/data_sources/data_source_google_container_attached_versions.go @@ -0,0 +1,70 @@ +package google + +import ( + "fmt" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGoogleContainerAttachedVersions() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGoogleContainerAttachedVersionsRead, + Schema: map[string]*schema.Schema{ + "project": { + Type: schema.TypeString, + Required: true, + }, + "location": { + Type: schema.TypeString, + Required: true, + }, + "valid_versions": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + } +} + +func dataSourceGoogleContainerAttachedVersionsRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + + location, err := getLocation(d, config) + if err != nil { + return err + } + if len(location) == 0 { + return fmt.Errorf("Cannot determine location: set location in this data source or at provider-level") + } + + url, err := replaceVars(d, config, "{{ContainerAttachedBasePath}}projects/{{project}}/locations/{{location}}/attachedServerConfig") + if err != nil { + return err + } + res, err := sendRequest(config, "GET", project, url, userAgent, nil) + if err != nil { + return err + } + var validVersions []string + for _, v := range res["validVersions"].([]interface{}) { + vm := v.(map[string]interface{}) + validVersions = append(validVersions, vm["version"].(string)) + } + if err := d.Set("valid_versions", validVersions); err != nil { + return err + } + + d.SetId(time.Now().UTC().String()) + return nil +} diff --git a/mmv1/third_party/terraform/tests/data_source_google_container_attached_versions_test.go b/mmv1/third_party/terraform/tests/data_source_google_container_attached_versions_test.go new file mode 100644 index 000000000000..f11c53fb99e4 --- /dev/null +++ b/mmv1/third_party/terraform/tests/data_source_google_container_attached_versions_test.go @@ -0,0 +1,73 @@ +package google + +import ( + "errors" + "fmt" + "strconv" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func TestAccDataSourceGoogleContainerAttachedVersions(t *testing.T) { + t.Parallel() + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleContainerAttachedVersionsConfig(), + Check: resource.ComposeTestCheckFunc( + testAccDataSourceGoogleContainerAttachedVersionsCheck("data.google_container_attached_versions.versions"), + ), + }, + }, + }) +} + +func testAccDataSourceGoogleContainerAttachedVersionsCheck(data_source_name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + ds, ok := s.RootModule().Resources[data_source_name] + if !ok { + return fmt.Errorf("root module has no resource called %s", data_source_name) + } + + count, ok := ds.Primary.Attributes["valid_versions.#"] + if !ok { + return fmt.Errorf("cannot find 'valid_versions' attribute") + } + noOfVersions, err := strconv.Atoi(count) + if err != nil { + return errors.New("failed to read number of versions") + } + if noOfVersions < 1 { + return fmt.Errorf("expected at least 1 version, received %d", noOfVersions) + } + + for i := 0; i < noOfVersions; i++ { + idx := "valid_versions." + strconv.Itoa(i) + v, ok := ds.Primary.Attributes[idx] + if !ok { + return fmt.Errorf("versions list is corrupt (%q not found)", idx) + } + if v == "" { + return fmt.Errorf("empty version returned for %q", idx) + } + } + return nil + } +} + +func testAccDataSourceGoogleContainerAttachedVersionsConfig() string { + return ` +data "google_project" "project" { +} + +data "google_container_attached_versions" "versions" { + location = "us-west1" + project = data.google_project.project.project_id +} +` +} diff --git a/mmv1/third_party/terraform/tests/resource_container_attached_cluster_update_test.go b/mmv1/third_party/terraform/tests/resource_container_attached_cluster_update_test.go index 5c50c016beda..baa65a8b21f5 100644 --- a/mmv1/third_party/terraform/tests/resource_container_attached_cluster_update_test.go +++ b/mmv1/third_party/terraform/tests/resource_container_attached_cluster_update_test.go @@ -54,6 +54,11 @@ func testAccContainerAttachedCluster_containerAttachedCluster_full(context map[s data "google_project" "project" { } +data "google_container_attached_versions" "versions" { + location = "us-west1" + project = data.google_project.project.project_id +} + resource "google_container_attached_cluster" "primary" { name = "update%{random_suffix}" project = data.google_project.project.project_id @@ -70,7 +75,7 @@ resource "google_container_attached_cluster" "primary" { issuer_url = "https://oidc.issuer.url" jwks = base64encode("{\"keys\":[{\"use\":\"sig\",\"kty\":\"RSA\",\"kid\":\"testid\",\"alg\":\"RS256\",\"n\":\"somedata\",\"e\":\"AQAB\"}]}") } - platform_version = "1.24.0-gke.1" + platform_version = data.google_container_attached_versions.versions.valid_versions[0] fleet { project = "projects/${data.google_project.project.number}" } @@ -93,6 +98,11 @@ func testAccContainerAttachedCluster_containerAttachedCluster_update(context map data "google_project" "project" { } +data "google_container_attached_versions" "versions" { + location = "us-west1" + project = data.google_project.project.project_id +} + resource "google_container_attached_cluster" "primary" { name = "update%{random_suffix}" project = data.google_project.project.project_id @@ -110,7 +120,7 @@ resource "google_container_attached_cluster" "primary" { issuer_url = "https://oidc.issuer.url" jwks = base64encode("{\"keys\":[{\"use\":\"sig\",\"kty\":\"RSA\",\"kid\":\"testid\",\"alg\":\"RS256\",\"n\":\"somedata\",\"e\":\"AQAB\"}]}") } - platform_version = "1.24.0-gke.1" + platform_version = data.google_container_attached_versions.versions.valid_versions[0] fleet { project = "projects/${data.google_project.project.number}" } @@ -131,6 +141,11 @@ func testAccContainerAttachedCluster_containerAttachedCluster_destroy(context ma data "google_project" "project" { } +data "google_container_attached_versions" "versions" { + location = "us-west1" + project = data.google_project.project.project_id +} + resource "google_container_attached_cluster" "primary" { name = "update%{random_suffix}" project = data.google_project.project.project_id @@ -148,7 +163,7 @@ resource "google_container_attached_cluster" "primary" { issuer_url = "https://oidc.issuer.url" jwks = base64encode("{\"keys\":[{\"use\":\"sig\",\"kty\":\"RSA\",\"kid\":\"testid\",\"alg\":\"RS256\",\"n\":\"somedata\",\"e\":\"AQAB\"}]}") } - platform_version = "1.24.0-gke.1" + platform_version = data.google_container_attached_versions.versions.valid_versions[0] fleet { project = "projects/${data.google_project.project.number}" } diff --git a/mmv1/third_party/terraform/utils/provider.go.erb b/mmv1/third_party/terraform/utils/provider.go.erb index c281096a1323..798854f3cdcc 100644 --- a/mmv1/third_party/terraform/utils/provider.go.erb +++ b/mmv1/third_party/terraform/utils/provider.go.erb @@ -251,6 +251,7 @@ func Provider() *schema.Provider { "google_compute_zones": dataSourceGoogleComputeZones(), "google_container_azure_versions": dataSourceGoogleContainerAzureVersions(), "google_container_aws_versions": dataSourceGoogleContainerAwsVersions(), + "google_container_attached_versions": dataSourceGoogleContainerAttachedVersions(), "google_container_cluster": dataSourceGoogleContainerCluster(), "google_container_engine_versions": dataSourceGoogleContainerEngineVersions(), "google_container_registry_image": dataSourceGoogleContainerImage(), diff --git a/mmv1/third_party/terraform/website/docs/d/container_attached_versions.html.markdown b/mmv1/third_party/terraform/website/docs/d/container_attached_versions.html.markdown new file mode 100644 index 000000000000..3d460986a758 --- /dev/null +++ b/mmv1/third_party/terraform/website/docs/d/container_attached_versions.html.markdown @@ -0,0 +1,39 @@ +--- +subcategory: "ContainerAttached" +page_title: "Google: google_container_attached_versions" +description: |- + Provides lists of available platform versions for the Container Attached resources. +--- + +# google\_container\_attached\_versions + +Provides access to available platform versions in a location for a given project. + +## Example Usage + +```hcl +data "google_container_attached_versions" "uswest" { + location = "us-west1" + project = "my-project" +} + + +output "first_available_version" { + value = data.google_container_attached_versions.versions.valid_versions[0] +} +``` + +## Argument Reference + +The following arguments are supported: + +* `location` (Optional) - The location to list versions for. + +* `project` (Optional) - ID of the project to list available platform 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_versions` - A list of versions available for use with this project and location.