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 #5073

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
6 changes: 6 additions & 0 deletions .changelog/7086.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```release-note:new-datasource
google_container_attached_versions
```
```release-note:new-datasource
google_container_attached_install_manifest
```
70 changes: 70 additions & 0 deletions google-beta/data_source_google_container_attached_versions.go
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
}
`
}
1 change: 1 addition & 0 deletions google-beta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,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
Expand Up @@ -53,6 +53,11 @@ func testAccContainerAttachedCluster_containerAttachedClusterBasicExample(contex
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 = "basic%{random_suffix}"
location = "us-west1"
Expand All @@ -62,7 +67,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 Expand Up @@ -100,6 +105,11 @@ func testAccContainerAttachedCluster_containerAttachedClusterFullExample(context
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 = "basic%{random_suffix}"
project = data.google_project.project.project_id
Expand All @@ -116,7 +126,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
21 changes: 18 additions & 3 deletions google-beta/resource_container_attached_cluster_update_test.go
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
39 changes: 39 additions & 0 deletions website/docs/d/container_attached_versions.html.markdown
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.
14 changes: 12 additions & 2 deletions website/docs/r/container_attached_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ To get more information about Cluster, see:
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 = "basic"
location = "us-west1"
Expand All @@ -51,7 +56,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 All @@ -69,6 +74,11 @@ resource "google_container_attached_cluster" "primary" {
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 = "basic"
project = data.google_project.project.project_id
Expand All @@ -85,7 +95,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