-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
ScottSuarez
merged 6 commits into
GoogleCloudPlatform:main
from
hankfreund:attached_data_sources
Jan 10, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
32d4fb3
Split attached cluster data sources into a separate PR
hankfreund 6907a88
Merge branch 'GoogleCloudPlatform:main' into attached_data_sources
hankfreund 6e9df90
Update tests to use attached clusters platform version datasource.
hankfreund ea0934f
Code formatting.
hankfreund 22370fc
Move container_attached_install_manifest to a separate PR.
hankfreund 7ae9974
Add markdown for container_attached_versions.
hankfreund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
data "google_project" "project" { | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
@@ -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}" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
mmv1/third_party/terraform/data_sources/data_source_google_container_attached_versions.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
73 changes: 73 additions & 0 deletions
73
mmv1/third_party/terraform/tests/data_source_google_container_attached_versions_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
.../third_party/terraform/website/docs/d/container_attached_versions.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both data sources could use a datasource markdown file.
Checkout https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/website/docs/d