Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attached clusters data sources #7086

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
data "google_project" "project" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer splitting this down to two PRs.. one for each data source. To keep the context more focused. It doesn't seem like the data sources are dependent on eachother.


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"
Expand All @@ -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}"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
`
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
}
Expand All @@ -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
Expand All @@ -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}"
}
Expand All @@ -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
Expand All @@ -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}"
}
Expand Down
1 change: 1 addition & 0 deletions mmv1/third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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.