generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 4
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 Cluster Options Datasource #32
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bcaf30b
wip
aliotta 50f2079
fix limited type
aliotta 38e8db0
add remaining params
aliotta 003baa1
remove commented out check
aliotta 37894fd
add test for use of datasource w/o provider filter
aliotta cc5bbc0
Merge branch 'main' into clusterOptionsDatasource
aliotta 98cc021
some pr review comments addressed
aliotta 31211b0
add example usage
aliotta 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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "astronomer_cluster_options Data Source - astronomer" | ||
subcategory: "" | ||
description: |- | ||
ClusterOptions data source | ||
--- | ||
|
||
# astronomer_cluster_options (Data Source) | ||
|
||
ClusterOptions data source | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "astronomer_cluster_options" "example_cluster_options" { | ||
type = "HYBRID" | ||
} | ||
|
||
data "astronomer_cluster_options" "example_cluster_options_filter_by_provider" { | ||
type = "HYBRID" | ||
cloud_provider = "AWS" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `type` (String) | ||
|
||
### Optional | ||
|
||
- `cloud_provider` (String) | ||
|
||
### Read-Only | ||
|
||
- `cluster_options` (Attributes List) (see [below for nested schema](#nestedatt--cluster_options)) | ||
|
||
<a id="nestedatt--cluster_options"></a> | ||
### Nested Schema for `cluster_options` | ||
|
||
Read-Only: | ||
|
||
- `database_instances` (Attributes List) ClusterOption database instances (see [below for nested schema](#nestedatt--cluster_options--database_instances)) | ||
- `default_database_instance` (Attributes) ClusterOption default database instance (see [below for nested schema](#nestedatt--cluster_options--default_database_instance)) | ||
- `default_node_instance` (Attributes) ClusterOption default node instance (see [below for nested schema](#nestedatt--cluster_options--default_node_instance)) | ||
- `default_pod_subnet_range` (String) ClusterOption default pod subnet range | ||
- `default_region` (Attributes) ClusterOption default region (see [below for nested schema](#nestedatt--cluster_options--default_region)) | ||
- `default_service_peering_range` (String) ClusterOption default service peering range | ||
- `default_service_subnet_range` (String) ClusterOption default service subnet range | ||
- `default_vpc_subnet_range` (String) ClusterOption default vps subnet range | ||
- `node_count_default` (Number) ClusterOption node count default | ||
- `node_count_max` (Number) ClusterOption node count max | ||
- `node_count_min` (Number) ClusterOption node count min | ||
- `node_instances` (Attributes List) ClusterOption node instances (see [below for nested schema](#nestedatt--cluster_options--node_instances)) | ||
- `provider` (String) ClusterOption provider | ||
- `regions` (Attributes List) ClusterOption regions (see [below for nested schema](#nestedatt--cluster_options--regions)) | ||
|
||
<a id="nestedatt--cluster_options--database_instances"></a> | ||
### Nested Schema for `cluster_options.database_instances` | ||
|
||
Read-Only: | ||
|
||
- `cpu` (Number) Provider instance cpu | ||
- `memory` (String) Provider instance memory | ||
- `name` (String) Provider instance name | ||
|
||
|
||
<a id="nestedatt--cluster_options--default_database_instance"></a> | ||
### Nested Schema for `cluster_options.default_database_instance` | ||
|
||
Read-Only: | ||
|
||
- `cpu` (Number) Provider instance cpu | ||
- `memory` (String) Provider instance memory | ||
- `name` (String) Provider instance name | ||
|
||
|
||
<a id="nestedatt--cluster_options--default_node_instance"></a> | ||
### Nested Schema for `cluster_options.default_node_instance` | ||
|
||
Read-Only: | ||
|
||
- `cpu` (Number) Provider instance cpu | ||
- `memory` (String) Provider instance memory | ||
- `name` (String) Provider instance name | ||
|
||
|
||
<a id="nestedatt--cluster_options--default_region"></a> | ||
### Nested Schema for `cluster_options.default_region` | ||
|
||
Read-Only: | ||
|
||
- `banned_instances` (List of String) Region banned instances | ||
- `limited` (Boolean) Region is limited bool | ||
- `name` (String) Region is limited bool | ||
|
||
|
||
<a id="nestedatt--cluster_options--node_instances"></a> | ||
### Nested Schema for `cluster_options.node_instances` | ||
|
||
Read-Only: | ||
|
||
- `cpu` (Number) Provider instance cpu | ||
- `memory` (String) Provider instance memory | ||
- `name` (String) Provider instance name | ||
|
||
|
||
<a id="nestedatt--cluster_options--regions"></a> | ||
### Nested Schema for `cluster_options.regions` | ||
|
||
Read-Only: | ||
|
||
- `banned_instances` (List of String) Region banned instances | ||
- `limited` (Boolean) Region is limited bool | ||
- `name` (String) Region is limited bool |
8 changes: 8 additions & 0 deletions
8
examples/data-sources/astronomer_cluster_options/data-source.tf
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,8 @@ | ||
data "astronomer_cluster_options" "example_cluster_options" { | ||
type = "HYBRID" | ||
} | ||
|
||
data "astronomer_cluster_options" "example_cluster_options_filter_by_provider" { | ||
type = "HYBRID" | ||
cloud_provider = "AWS" | ||
} |
125 changes: 125 additions & 0 deletions
125
internal/provider/datasources/data_source_cluster_options.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,125 @@ | ||
package datasources | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/astronomer/astronomer-terraform-provider/internal/clients" | ||
"github.com/astronomer/astronomer-terraform-provider/internal/clients/platform" | ||
"github.com/astronomer/astronomer-terraform-provider/internal/provider/models" | ||
"github.com/astronomer/astronomer-terraform-provider/internal/provider/schemas" | ||
"github.com/astronomer/astronomer-terraform-provider/internal/utils" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
) | ||
|
||
// Ensure provider defined types fully satisfy framework interfaces. | ||
var _ datasource.DataSource = &clusterOptionsDataSource{} | ||
var _ datasource.DataSourceWithConfigure = &clusterOptionsDataSource{} | ||
|
||
func NewClusterOptionsDataSource() datasource.DataSource { | ||
return &clusterOptionsDataSource{} | ||
} | ||
|
||
// clusterOptionsDataSource defines the data source implementation. | ||
type clusterOptionsDataSource struct { | ||
PlatformClient platform.ClientWithResponsesInterface | ||
OrganizationId string | ||
} | ||
|
||
func (d *clusterOptionsDataSource) Metadata( | ||
ctx context.Context, | ||
req datasource.MetadataRequest, | ||
resp *datasource.MetadataResponse, | ||
) { | ||
resp.TypeName = req.ProviderTypeName + "_cluster_options" | ||
} | ||
|
||
func (d *clusterOptionsDataSource) Schema( | ||
ctx context.Context, | ||
req datasource.SchemaRequest, | ||
resp *datasource.SchemaResponse, | ||
) { | ||
resp.Schema = schema.Schema{ | ||
// This description is used by the documentation generator and the language server. | ||
MarkdownDescription: "ClusterOptions data source", | ||
Attributes: schemas.ClusterOptionsDataSourceSchemaAttributes(), | ||
} | ||
} | ||
|
||
func (d *clusterOptionsDataSource) Configure( | ||
ctx context.Context, | ||
req datasource.ConfigureRequest, | ||
resp *datasource.ConfigureResponse, | ||
) { | ||
// Prevent panic if the provider has not been configured. | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
apiClients, ok := req.ProviderData.(models.ApiClientsModel) | ||
if !ok { | ||
utils.DataSourceApiClientConfigureError(ctx, req, resp) | ||
return | ||
} | ||
|
||
d.PlatformClient = apiClients.PlatformClient | ||
d.OrganizationId = apiClients.OrganizationId | ||
} | ||
|
||
func (d *clusterOptionsDataSource) Read( | ||
ctx context.Context, | ||
req datasource.ReadRequest, | ||
resp *datasource.ReadResponse, | ||
) { | ||
var data models.ClusterOptionsDataSource | ||
|
||
// Read Terraform configuration data into the model | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
provider := platform.GetClusterOptionsParamsProvider(data.CloudProvider.ValueString()) | ||
params := &platform.GetClusterOptionsParams{ | ||
Type: platform.GetClusterOptionsParamsType(data.Type.ValueString()), | ||
Provider: &provider, | ||
} | ||
|
||
var clusterOptions []platform.ClusterOptions | ||
clusterOptionsResp, err := d.PlatformClient.GetClusterOptionsWithResponse( | ||
ctx, | ||
d.OrganizationId, | ||
params, | ||
) | ||
|
||
if err != nil { | ||
tflog.Error(ctx, "failed to list clusterOptions", map[string]interface{}{"error": err}) | ||
resp.Diagnostics.AddError( | ||
"Client Error", | ||
fmt.Sprintf("Unable to read clusterOptions, got error: %s", err), | ||
) | ||
return | ||
} | ||
_, diagnostic := clients.NormalizeAPIError(ctx, clusterOptionsResp.HTTPResponse, clusterOptionsResp.Body) | ||
|
||
if diagnostic != nil { | ||
resp.Diagnostics.Append(diagnostic) | ||
return | ||
} | ||
if clusterOptionsResp.JSON200 == nil { | ||
tflog.Error(ctx, "failed to list clusterOptions", map[string]interface{}{"error": "nil response"}) | ||
resp.Diagnostics.AddError("Client Error", "Unable to read clusterOptions, got nil response") | ||
return | ||
} | ||
clusterOptions = append(clusterOptions, *clusterOptionsResp.JSON200...) | ||
|
||
// Populate the model with the response data | ||
diags := data.ReadFromResponse(ctx, clusterOptions) | ||
if diags.HasError() { | ||
resp.Diagnostics.Append(diags...) | ||
return | ||
} | ||
// Save data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} |
136 changes: 136 additions & 0 deletions
136
internal/provider/datasources/data_source_cluster_options_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,136 @@ | ||
package datasources_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/astronomer/astronomer-terraform-provider/internal/utils" | ||
|
||
astronomerprovider "github.com/astronomer/astronomer-terraform-provider/internal/provider" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/terraform" | ||
) | ||
|
||
func TestAcc_DataSourceClusterOptions(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
astronomerprovider.TestAccPreCheck(t) | ||
}, | ||
ProtoV6ProviderFactories: astronomerprovider.TestAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: astronomerprovider.ProviderConfig(t, true) + clusterOptions("HYBRID", "AWS"), | ||
Check: resource.ComposeTestCheckFunc( | ||
checkClusterOptions("AWS"), | ||
), | ||
}, | ||
{ | ||
Config: astronomerprovider.ProviderConfig(t, true) + clusterOptionsWithoutProviderFilter("HYBRID"), | ||
Check: resource.ComposeTestCheckFunc( | ||
checkClusterOptionsWithoutProviderFilter(), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func clusterOptions(clusterType, provider string) string { | ||
return fmt.Sprintf(` | ||
data astronomer_cluster_options "test_data_cluster_options" { | ||
type = "%v" | ||
cloud_provider = "%v" | ||
}`, clusterType, provider) | ||
} | ||
|
||
func clusterOptionsWithoutProviderFilter(clusterType string) string { | ||
return fmt.Sprintf(` | ||
data astronomer_cluster_options "test_data_cluster_options" { | ||
type = "%v" | ||
}`, clusterType) | ||
} | ||
|
||
func checkClusterOptions(provider string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
instanceState, numClusterOptions, err := utils.GetDataSourcesLength(s, "test_data_cluster_options", "cluster_options") | ||
if err != nil { | ||
return err | ||
} | ||
if numClusterOptions == 0 { | ||
return fmt.Errorf("expected clusterOptions to be greater or equal to 1, got %s", instanceState.Attributes["cluster_options.#"]) | ||
} | ||
|
||
clusterOptionIdx := -1 | ||
for i := 0; i < numClusterOptions; i++ { | ||
idxProvider := fmt.Sprintf("cluster_options.%d.provider", i) | ||
if instanceState.Attributes[idxProvider] == provider { | ||
clusterOptionIdx = i | ||
break | ||
} | ||
} | ||
if clusterOptionIdx == -1 { | ||
return fmt.Errorf("cluster option with provider %s not found", provider) | ||
} | ||
databaseInstance1 := fmt.Sprintf("cluster_options.%d.database_instances.0", clusterOptionIdx) | ||
resource.TestCheckResourceAttrSet(databaseInstance1, "cpu") | ||
resource.TestCheckResourceAttrSet(databaseInstance1, "memory") | ||
resource.TestCheckResourceAttrSet(databaseInstance1, "name") | ||
|
||
defaultDatabaseInstance := fmt.Sprintf("cluster_options.%d.default_database_instance", clusterOptionIdx) | ||
resource.TestCheckResourceAttrSet(defaultDatabaseInstance, "cpu") | ||
resource.TestCheckResourceAttrSet(defaultDatabaseInstance, "memory") | ||
resource.TestCheckResourceAttrSet(defaultDatabaseInstance, "name") | ||
|
||
nodeInstance1 := fmt.Sprintf("cluster_options.%d.node_instances.0", clusterOptionIdx) | ||
resource.TestCheckResourceAttrSet(nodeInstance1, "cpu") | ||
resource.TestCheckResourceAttrSet(nodeInstance1, "memory") | ||
resource.TestCheckResourceAttrSet(nodeInstance1, "name") | ||
|
||
defaultNodeInstance := fmt.Sprintf("cluster_options.%d.default_node_instance", clusterOptionIdx) | ||
resource.TestCheckResourceAttrSet(defaultNodeInstance, "cpu") | ||
resource.TestCheckResourceAttrSet(defaultNodeInstance, "memory") | ||
resource.TestCheckResourceAttrSet(defaultNodeInstance, "name") | ||
|
||
region1 := fmt.Sprintf("cluster_options.%d.regions.0", clusterOptionIdx) | ||
resource.TestCheckResourceAttrSet(region1, "name") | ||
|
||
defaultRegion := fmt.Sprintf("cluster_options.%d.default_region", clusterOptionIdx) | ||
resource.TestCheckResourceAttrSet(defaultRegion, "name") | ||
|
||
clusterOption1 := fmt.Sprintf("cluster_options.%d", clusterOptionIdx) | ||
resource.TestCheckResourceAttrSet(clusterOption1, "node_count_min") | ||
resource.TestCheckResourceAttrSet(clusterOption1, "node_count_max") | ||
resource.TestCheckResourceAttrSet(clusterOption1, "node_count_default") | ||
resource.TestCheckResourceAttrSet(clusterOption1, "default_vpc_subnet_range") | ||
resource.TestCheckResourceAttrSet(clusterOption1, "default_pod_subnet_range") | ||
resource.TestCheckResourceAttrSet(clusterOption1, "default_service_subnet_range") | ||
resource.TestCheckResourceAttrSet(clusterOption1, "default_service_peering_range") | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func checkClusterOptionsWithoutProviderFilter() resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
instanceState, numClusterOptions, err := utils.GetDataSourcesLength(s, "test_data_cluster_options", "cluster_options") | ||
if err != nil { | ||
return err | ||
} | ||
if numClusterOptions <= 1 { | ||
return fmt.Errorf("expected clusterOptions to be greater or equal to 1, got %s", instanceState.Attributes["cluster_options.#"]) | ||
} | ||
var providers []string | ||
for i := 0; i < numClusterOptions; i++ { | ||
idxProvider := fmt.Sprintf("cluster_options.%d.provider", i) | ||
providers = append(providers, instanceState.Attributes[idxProvider]) | ||
} | ||
if len(providers) == 0 { | ||
return fmt.Errorf("expected providers to be greater than 0") | ||
} | ||
|
||
for _, provider := range providers { | ||
checkClusterOptions(provider) | ||
} | ||
|
||
return nil | ||
} | ||
} |
Oops, something went wrong.
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.
can we add to
examples/
and re-generate the docs which will auto add the examplesThere 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.
done