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

Add container_attached_install_manifest data source. #13455

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
3 changes: 3 additions & 0 deletions .changelog/7108.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
google_container_attached_install_manifest
```
84 changes: 84 additions & 0 deletions google/data_source_google_container_attached_install_manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package google

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGoogleContainerAttachedInstallManifest() *schema.Resource {
return &schema.Resource{
Read: dataSourceGoogleContainerAttachedInstallManifestRead,
Schema: map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Required: true,
},
"location": {
Type: schema.TypeString,
Required: true,
},
"cluster_id": {
Type: schema.TypeString,
Required: true,
},
"platform_version": {
Type: schema.TypeString,
Required: true,
},
"manifest": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceGoogleContainerAttachedInstallManifestRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}

clusterId := d.Get("cluster_id").(string)
platformVersion := d.Get("platform_version").(string)

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}}:generateAttachedClusterInstallManifest")
if err != nil {
return err
}
params := map[string]string{
"attached_cluster_id": clusterId,
"platform_version": platformVersion,
}
url, err = addQueryParams(url, params)
if err != nil {
return err
}
res, err := sendRequest(config, "GET", project, url, userAgent, nil)
if err != nil {
return err
}

if err := d.Set("manifest", res["manifest"]); err != nil {
return fmt.Errorf("Error setting manifest: %s", err)
}

d.SetId(time.Now().UTC().String())
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccDataSourceGoogleContainerAttachedInstallManifest(t *testing.T) {
t.Parallel()

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleContainerAttachedInstallManifestConfig(randString(t, 10)),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceGoogleContainerAttachedInstallManifestCheck("data.google_container_attached_install_manifest.manifest"),
),
},
},
})
}

func testAccDataSourceGoogleContainerAttachedInstallManifestCheck(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)
}

manifest, ok := ds.Primary.Attributes["manifest"]
if !ok {
return fmt.Errorf("cannot find 'manifest' attribute")
}
if manifest == "" {
return fmt.Errorf("install manifest data is empty")
}
return nil
}
}

func testAccDataSourceGoogleContainerAttachedInstallManifestConfig(suffix string) string {
return fmt.Sprintf(`
data "google_project" "project" {
}

data "google_container_attached_versions" "versions" {
location = "us-west1"
project = data.google_project.project.project_id
}

data "google_container_attached_install_manifest" "manifest" {
location = "us-west1"
project = data.google_project.project.project_id
cluster_id = "test-cluster-%s"
platform_version = data.google_container_attached_versions.versions.valid_versions[0]
}
`, suffix)
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ func Provider() *schema.Provider {
"google_container_azure_versions": dataSourceGoogleContainerAzureVersions(),
"google_container_aws_versions": dataSourceGoogleContainerAwsVersions(),
"google_container_attached_versions": dataSourceGoogleContainerAttachedVersions(),
"google_container_attached_install_manifest": dataSourceGoogleContainerAttachedInstallManifest(),
"google_container_cluster": dataSourceGoogleContainerCluster(),
"google_container_engine_versions": dataSourceGoogleContainerEngineVersions(),
"google_container_registry_image": dataSourceGoogleContainerImage(),
Expand Down
45 changes: 45 additions & 0 deletions website/docs/d/container_attached_install_manifest.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
subcategory: "ContainerAttached"
page_title: "Google: google_container_attached_install_manifest"
description: |-
Generates a YAML manifest for boot-strapping an Attached cluster registration.
---

# google\_container\_attached\_install_manifest

Provides access to available platform versions in a location for a given project.

## Example Usage

```hcl
data "google_container_attached_install_manifest" "manifest" {
location = "us-west1"
project = "my-project"
cluster_id = "test-cluster-1"
platform_version = "1.25.0-gke.1"
}


output "install_manifest" {
value = data.google_container_attached_install_manifest.manifest
}
```

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

* `cluster_id` (Required) - The name that will be used when creating the attached cluster resource.

* `platform_version` (Required) - The platform version for the cluster. A list of valid values can be retrieved using the `google_container_attached_versions` data source.

## Attributes Reference

The following attributes are exported:

* `manifest` - A string with the YAML manifest that needs to be applied to the cluster.