diff --git a/.changelog/7108.txt b/.changelog/7108.txt new file mode 100644 index 00000000000..1de1d57fa7a --- /dev/null +++ b/.changelog/7108.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +google_container_attached_install_manifest +``` diff --git a/google/data_source_google_container_attached_install_manifest.go b/google/data_source_google_container_attached_install_manifest.go new file mode 100644 index 00000000000..1047e954e68 --- /dev/null +++ b/google/data_source_google_container_attached_install_manifest.go @@ -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 +} diff --git a/google/data_source_google_container_attached_install_manifest_test.go b/google/data_source_google_container_attached_install_manifest_test.go new file mode 100644 index 00000000000..6aff47e654f --- /dev/null +++ b/google/data_source_google_container_attached_install_manifest_test.go @@ -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) +} diff --git a/google/provider.go b/google/provider.go index 00de6b3e348..1eb0d43ffb9 100644 --- a/google/provider.go +++ b/google/provider.go @@ -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(), diff --git a/website/docs/d/container_attached_install_manifest.html.markdown b/website/docs/d/container_attached_install_manifest.html.markdown new file mode 100644 index 00000000000..85da92cdcd1 --- /dev/null +++ b/website/docs/d/container_attached_install_manifest.html.markdown @@ -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.