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 Cluster Options Datasource #32

Merged
merged 8 commits into from
Apr 22, 2024
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
118 changes: 118 additions & 0 deletions docs/data-sources/cluster_options.md
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)
Copy link
Collaborator

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 examples

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


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
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 internal/provider/datasources/data_source_cluster_options.go
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 internal/provider/datasources/data_source_cluster_options_test.go
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
}
}
Loading