Skip to content

Commit 3fde542

Browse files
committed
cluster data source
1 parent aa9c798 commit 3fde542

File tree

6 files changed

+606
-0
lines changed

6 files changed

+606
-0
lines changed

docs/data-sources/cluster.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "astronomer_cluster Data Source - astronomer"
4+
subcategory: ""
5+
description: |-
6+
Cluster data source
7+
---
8+
9+
# astronomer_cluster (Data Source)
10+
11+
Cluster data source
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `id` (String) Cluster identifier
21+
22+
### Read-Only
23+
24+
- `cloud_provider` (String) Cluster cloud provider
25+
- `created_at` (String) Cluster creation timestamp
26+
- `db_instance_type` (String) Cluster database instance type
27+
- `is_limited` (Boolean) Whether the cluster is limited
28+
- `metadata` (Attributes) Cluster metadata (see [below for nested schema](#nestedatt--metadata))
29+
- `name` (String) Cluster name
30+
- `node_pools` (Attributes List) Cluster node pools (see [below for nested schema](#nestedatt--node_pools))
31+
- `pod_subnet_range` (String) Cluster pod subnet range
32+
- `provider_account` (String) Cluster provider account
33+
- `region` (String) Cluster region
34+
- `service_peering_range` (String) Cluster service peering range
35+
- `service_subnet_range` (String) Cluster service subnet range
36+
- `status` (String) Cluster status
37+
- `tags` (Attributes List) Cluster tags (see [below for nested schema](#nestedatt--tags))
38+
- `tenant_id` (String) Cluster tenant ID
39+
- `type` (String) Cluster type
40+
- `updated_at` (String) Cluster last updated timestamp
41+
- `vpc_subnet_range` (String) Cluster VPC subnet range
42+
- `workspace_ids` (List of String) Cluster workspace IDs
43+
44+
<a id="nestedatt--metadata"></a>
45+
### Nested Schema for `metadata`
46+
47+
Read-Only:
48+
49+
- `external_ips` (List of String) Cluster external IPs
50+
- `oidc_issuer_url` (String) Cluster OIDC issuer URL
51+
52+
53+
<a id="nestedatt--node_pools"></a>
54+
### Nested Schema for `node_pools`
55+
56+
Read-Only:
57+
58+
- `cloud_provider` (String) Node pool cloud provider
59+
- `cluster_id` (String) Node pool cluster identifier
60+
- `created_at` (String) Node pool creation timestamp
61+
- `id` (String) Node pool identifier
62+
- `is_default` (Boolean) Whether the node pool is the default node pool of the cluster
63+
- `max_node_count` (Number) Node pool maximum node count
64+
- `name` (String) Node pool name
65+
- `node_instance_type` (String) Node pool node instance type
66+
- `supported_astro_machines` (List of String) Node pool supported Astro machines
67+
- `updated_at` (String) Node pool last updated timestamp
68+
69+
70+
<a id="nestedatt--tags"></a>
71+
### Nested Schema for `tags`
72+
73+
Read-Only:
74+
75+
- `key` (String) Cluster tag key
76+
- `value` (String) Cluster tag value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package datasources
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/astronomer/astronomer-terraform-provider/internal/clients"
8+
"github.com/astronomer/astronomer-terraform-provider/internal/clients/platform"
9+
"github.com/astronomer/astronomer-terraform-provider/internal/provider/models"
10+
"github.com/astronomer/astronomer-terraform-provider/internal/provider/schemas"
11+
"github.com/astronomer/astronomer-terraform-provider/internal/utils"
12+
"github.com/hashicorp/terraform-plugin-framework/datasource"
13+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
14+
"github.com/hashicorp/terraform-plugin-log/tflog"
15+
)
16+
17+
// Ensure provider defined types fully satisfy framework interfaces.
18+
var _ datasource.DataSource = &clusterDataSource{}
19+
var _ datasource.DataSourceWithConfigure = &clusterDataSource{}
20+
21+
func NewClusterDataSource() datasource.DataSource {
22+
return &clusterDataSource{}
23+
}
24+
25+
// clusterDataSource defines the data source implementation.
26+
type clusterDataSource struct {
27+
PlatformClient platform.ClientWithResponsesInterface
28+
OrganizationId string
29+
}
30+
31+
func (d *clusterDataSource) Metadata(
32+
ctx context.Context,
33+
req datasource.MetadataRequest,
34+
resp *datasource.MetadataResponse,
35+
) {
36+
resp.TypeName = req.ProviderTypeName + "_cluster"
37+
}
38+
39+
func (d *clusterDataSource) Schema(
40+
ctx context.Context,
41+
req datasource.SchemaRequest,
42+
resp *datasource.SchemaResponse,
43+
) {
44+
resp.Schema = schema.Schema{
45+
// This description is used by the documentation generator and the language server.
46+
MarkdownDescription: "Cluster data source",
47+
Attributes: schemas.ClusterDataSourceSchemaAttributes(),
48+
}
49+
}
50+
51+
func (d *clusterDataSource) Configure(
52+
ctx context.Context,
53+
req datasource.ConfigureRequest,
54+
resp *datasource.ConfigureResponse,
55+
) {
56+
// Prevent panic if the provider has not been configured.
57+
if req.ProviderData == nil {
58+
return
59+
}
60+
61+
apiClients, ok := req.ProviderData.(models.ApiClientsModel)
62+
if !ok {
63+
utils.DataSourceApiClientConfigureError(ctx, req, resp)
64+
return
65+
}
66+
67+
d.PlatformClient = apiClients.PlatformClient
68+
d.OrganizationId = apiClients.OrganizationId
69+
}
70+
71+
func (d *clusterDataSource) Read(
72+
ctx context.Context,
73+
req datasource.ReadRequest,
74+
resp *datasource.ReadResponse,
75+
) {
76+
var data models.ClusterDataSource
77+
78+
// Read Terraform configuration data into the model
79+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
80+
if resp.Diagnostics.HasError() {
81+
return
82+
}
83+
84+
cluster, err := d.PlatformClient.GetClusterWithResponse(
85+
ctx,
86+
d.OrganizationId,
87+
data.Id.ValueString(),
88+
)
89+
if err != nil {
90+
tflog.Error(ctx, "failed to get cluster", map[string]interface{}{"error": err})
91+
resp.Diagnostics.AddError(
92+
"Client Error",
93+
fmt.Sprintf("Unable to read cluster, got error: %s", err),
94+
)
95+
return
96+
}
97+
_, diagnostic := clients.NormalizeAPIError(ctx, cluster.HTTPResponse, cluster.Body)
98+
if diagnostic != nil {
99+
resp.Diagnostics.Append(diagnostic)
100+
return
101+
}
102+
if cluster.JSON200 == nil {
103+
tflog.Error(ctx, "failed to get cluster", map[string]interface{}{"error": "nil response"})
104+
resp.Diagnostics.AddError("Client Error", "Unable to read cluster, got nil response")
105+
return
106+
}
107+
108+
// Populate the model with the response data
109+
diags := data.ReadFromResponse(ctx, cluster.JSON200)
110+
if diags.HasError() {
111+
resp.Diagnostics.Append(diags...)
112+
return
113+
}
114+
115+
// Save data into Terraform state
116+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package datasources_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
astronomerprovider "github.com/astronomer/astronomer-terraform-provider/internal/provider"
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
)
11+
12+
func TestAcc_DataSourceCluster(t *testing.T) {
13+
hybridClusterId := os.Getenv("HYBRID_CLUSTER_ID")
14+
resourceName := "test_data_cluster_hybrid"
15+
resourceVar := fmt.Sprintf("data.astronomer_cluster.%v", resourceName)
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() {
18+
astronomerprovider.TestAccPreCheck(t)
19+
},
20+
ProtoV6ProviderFactories: astronomerprovider.TestAccProtoV6ProviderFactories,
21+
Steps: []resource.TestStep{
22+
//Check the data source for deployments for a hosted organization
23+
{
24+
Config: astronomerprovider.ProviderConfig(t, false) + cluster(resourceName, hybridClusterId),
25+
Check: resource.ComposeTestCheckFunc(
26+
// These checks are for the deployment data source (singular)
27+
resource.TestCheckResourceAttrSet(resourceVar, "id"),
28+
resource.TestCheckResourceAttrSet(resourceVar, "name"),
29+
resource.TestCheckResourceAttrSet(resourceVar, "cloud_provider"),
30+
resource.TestCheckResourceAttrSet(resourceVar, "db_instance_type"),
31+
resource.TestCheckResourceAttrSet(resourceVar, "region"),
32+
resource.TestCheckResourceAttrSet(resourceVar, "pod_subnet_range"),
33+
resource.TestCheckResourceAttrSet(resourceVar, "created_at"),
34+
resource.TestCheckResourceAttrSet(resourceVar, "updated_at"),
35+
resource.TestCheckResourceAttr(resourceVar, "type", "HYBRID"),
36+
resource.TestCheckResourceAttrSet(resourceVar, "provider_account"),
37+
resource.TestCheckResourceAttrSet(resourceVar, "node_pools.0.id"),
38+
resource.TestCheckResourceAttrSet(resourceVar, "node_pools.0.name"),
39+
resource.TestCheckResourceAttrSet(resourceVar, "metadata.external_ips.0"),
40+
),
41+
},
42+
},
43+
})
44+
}
45+
46+
func cluster(resourceName, clusterId string) string {
47+
return fmt.Sprintf(`
48+
data astronomer_cluster "%v" {
49+
id = "%v"
50+
}`, resourceName, clusterId)
51+
}

0 commit comments

Comments
 (0)