Skip to content

Commit dc4fdfa

Browse files
committed
feat: switch to generate model in cluster datasource
Realized I could make a small change to the generateModel function call that would allow me to use it in datasource, permitting the removal of a lot of code.
1 parent 0ca114b commit dc4fdfa

File tree

3 files changed

+34
-131
lines changed

3 files changed

+34
-131
lines changed

redpanda/resources/cluster/data_cluster.go

Lines changed: 8 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ package cluster
2020
import (
2121
"context"
2222
"fmt"
23-
"time"
2423

2524
controlplanev1beta2 "buf.build/gen/go/redpandadata/cloud/protocolbuffers/go/redpanda/api/controlplane/v1beta2"
26-
"github.com/hashicorp/terraform-plugin-framework/attr"
2725
"github.com/hashicorp/terraform-plugin-framework/datasource"
2826
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
2927
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -86,135 +84,21 @@ func (d *DataSourceCluster) Read(ctx context.Context, req datasource.ReadRequest
8684
}
8785

8886
// Convert cloud provider tags to Terraform map
89-
tags := make(map[string]attr.Value)
90-
for k, v := range cluster.CloudProviderTags {
91-
tags[k] = types.StringValue(v)
92-
}
93-
tagsValue, diags := types.MapValue(types.StringType, tags)
94-
if diags.HasError() {
95-
resp.Diagnostics.AddError("unable to parse Cloud tags", utils.DeserializeGrpcError(err))
96-
return
97-
}
98-
99-
// Create persistence model
100-
persist := &models.Cluster{
101-
Name: types.StringValue(cluster.Name),
102-
ConnectionType: types.StringValue(utils.ConnectionTypeToString(cluster.ConnectionType)),
103-
CloudProvider: types.StringValue(utils.CloudProviderToString(cluster.CloudProvider)),
104-
ClusterType: types.StringValue(utils.ClusterTypeToString(cluster.Type)),
105-
RedpandaVersion: types.StringValue(cluster.RedpandaVersion),
106-
ThroughputTier: types.StringValue(cluster.ThroughputTier),
107-
Region: types.StringValue(cluster.Region),
108-
ResourceGroupID: types.StringValue(cluster.ResourceGroupId),
109-
NetworkID: types.StringValue(cluster.NetworkId),
110-
ID: types.StringValue(cluster.Id),
111-
Tags: tagsValue,
112-
Zones: utils.StringSliceToTypeList(cluster.Zones),
113-
ReadReplicaClusterIDs: utils.StringSliceToTypeList(cluster.ReadReplicaClusterIds),
114-
AllowDeletion: types.BoolValue(true), // Default to true for data source
115-
State: types.StringValue(cluster.State.String()),
116-
}
117-
118-
if cluster.HasCreatedAt() {
119-
persist.CreatedAt = types.StringValue(cluster.CreatedAt.AsTime().Format(time.RFC3339))
120-
}
121-
122-
if cluster.HasStateDescription() {
123-
stateDescription, dg := generateModelStateDescription(cluster, diags)
124-
if dg.HasError() {
125-
resp.Diagnostics.Append(dg...)
126-
return
127-
}
128-
persist.StateDescription = stateDescription
129-
}
130-
131-
if cluster.HasDataplaneApi() {
132-
persist.ClusterAPIURL = types.StringValue(cluster.DataplaneApi.Url)
133-
}
134-
135-
kafkaAPI, dg := generateModelKafkaAPI(cluster, diags)
136-
if dg.HasError() {
137-
resp.Diagnostics.Append(dg...)
138-
return
139-
}
140-
persist.KafkaAPI = kafkaAPI
141-
142-
httpProxy, dg := generateModelHTTPProxy(cluster, diags)
143-
if dg.HasError() {
144-
resp.Diagnostics.Append(dg...)
145-
return
146-
}
147-
persist.HTTPProxy = httpProxy
148-
149-
schemaRegistry, dg := generateModelSchemaRegistry(cluster, diags)
150-
if dg.HasError() {
151-
resp.Diagnostics.Append(dg...)
152-
return
153-
}
154-
persist.SchemaRegistry = schemaRegistry
155-
156-
console, dg := generateModelRedpandaConsole(cluster, diags)
157-
if dg.HasError() {
158-
resp.Diagnostics.Append(dg...)
159-
return
160-
}
161-
persist.RedpandaConsole = console
162-
163-
prometheus, dg := generateModelPrometheus(cluster, diags)
164-
if dg.HasError() {
165-
resp.Diagnostics.Append(dg...)
166-
return
167-
}
168-
persist.Prometheus = prometheus
169-
170-
window, dg := generateModelMaintenanceWindow(cluster, diags)
171-
if dg.HasError() {
172-
resp.Diagnostics.Append(dg...)
173-
return
174-
}
175-
persist.MaintenanceWindowConfig = window
176-
177-
awsPrivateLink, dg := generateModelAWSPrivateLink(cluster, resp.Diagnostics)
178-
if dg.HasError() {
179-
resp.Diagnostics.Append(dg...)
180-
return
181-
}
182-
persist.AwsPrivateLink = awsPrivateLink
183-
184-
gcpPSC, dg := generateModelGCPPrivateServiceConnect(cluster, resp.Diagnostics)
185-
if dg.HasError() {
186-
resp.Diagnostics.Append(dg...)
187-
return
188-
}
189-
persist.GcpPrivateServiceConnect = gcpPSC
190-
191-
azurePrivateLink, dg := generateModelAzurePrivateLink(cluster, resp.Diagnostics)
192-
if dg.HasError() {
193-
resp.Diagnostics.Append(dg...)
194-
return
195-
}
196-
persist.AzurePrivateLink = azurePrivateLink
197-
198-
connectivity, dg := generateModelConnectivity(cluster, resp.Diagnostics)
199-
if dg.HasError() {
200-
resp.Diagnostics.Append(dg...)
201-
return
202-
}
203-
persist.Connectivity = connectivity
204-
205-
kafkaConnect, dg := generateModelKafkaConnect(cluster, resp.Diagnostics)
206-
if dg.HasError() {
207-
resp.Diagnostics.Append(dg...)
87+
tags, err := utils.StringMapToTypesMap(cluster.GetCloudProviderTags())
88+
if err != nil {
89+
resp.Diagnostics.AddError("error converting tags to MapType", err.Error())
20890
return
20991
}
210-
persist.KafkaConnect = kafkaConnect
21192

212-
cmr, dg := generateModelCMR(cluster, resp.Diagnostics)
93+
persist, dg := generateModel(cluster, modelOrAPI{
94+
RedpandaVersion: types.StringValue(cluster.RedpandaVersion),
95+
Tags: tags,
96+
}, resp.Diagnostics)
21397
if dg.HasError() {
98+
resp.Diagnostics.AddError("error generating model", "failed to generate model in cluster datasource read")
21499
resp.Diagnostics.Append(dg...)
215100
return
216101
}
217-
persist.CustomerManagedResources = cmr
218102

219103
resp.Diagnostics.Append(resp.State.Set(ctx, persist)...)
220104
}

redpanda/resources/cluster/proto_to_model.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,26 @@ import (
1212
"github.com/redpanda-data/terraform-provider-redpanda/redpanda/utils"
1313
)
1414

15+
// used as an input to generateModel to allow populating these fields with either the model or the API
16+
type modelOrAPI struct {
17+
RedpandaVersion types.String
18+
AllowDeletion types.Bool
19+
Tags types.Map
20+
}
21+
1522
// generateModel populates the Cluster model to be persisted to state for Create, Read and Update operations. It is also indirectly used by Import
16-
func generateModel(cfg models.Cluster, cluster *controlplanev1beta2.Cluster, diagnostics diag.Diagnostics) (*models.Cluster, diag.Diagnostics) {
23+
func generateModel(cluster *controlplanev1beta2.Cluster, contingent modelOrAPI, diagnostics diag.Diagnostics) (*models.Cluster, diag.Diagnostics) {
1724
output := &models.Cluster{
1825
Name: types.StringValue(cluster.GetName()),
1926
ID: types.StringValue(cluster.GetId()),
2027
ConnectionType: types.StringValue(utils.ConnectionTypeToString(cluster.GetConnectionType())),
2128
CloudProvider: types.StringValue(utils.CloudProviderToString(cluster.GetCloudProvider())),
2229
ClusterType: types.StringValue(utils.ClusterTypeToString(cluster.GetType())),
23-
RedpandaVersion: cfg.RedpandaVersion,
30+
RedpandaVersion: contingent.RedpandaVersion,
2431
ThroughputTier: types.StringValue(cluster.GetThroughputTier()),
2532
Region: types.StringValue(cluster.GetRegion()),
26-
AllowDeletion: cfg.AllowDeletion,
27-
Tags: cfg.Tags,
33+
AllowDeletion: contingent.AllowDeletion,
34+
Tags: contingent.Tags,
2835
ResourceGroupID: types.StringValue(cluster.GetResourceGroupId()),
2936
NetworkID: types.StringValue(cluster.GetNetworkId()),
3037
ReadReplicaClusterIDs: utils.StringSliceToTypeList(cluster.GetReadReplicaClusterIds()),

redpanda/resources/cluster/resource_cluster.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ func (c *Cluster) Create(ctx context.Context, req resource.CreateRequest, resp *
126126

127127
// there are various states where cluster can be nil in which case we should default to the minimal model already persisted
128128
if cluster != nil {
129-
p, dg := generateModel(model, cluster, resp.Diagnostics)
129+
p, dg := generateModel(cluster, modelOrAPI{
130+
RedpandaVersion: model.RedpandaVersion,
131+
AllowDeletion: model.AllowDeletion,
132+
Tags: model.Tags,
133+
}, resp.Diagnostics)
130134
if dg.HasError() {
131135
// append minimal state because we failed
132136
resp.Diagnostics.Append(resp.State.Set(ctx, generateMinimalModel(clusterID))...)
@@ -161,7 +165,11 @@ func (c *Cluster) Read(ctx context.Context, req resource.ReadRequest, resp *reso
161165
return
162166
}
163167

164-
persist, d := generateModel(model, cluster, resp.Diagnostics)
168+
persist, d := generateModel(cluster, modelOrAPI{
169+
RedpandaVersion: model.RedpandaVersion,
170+
AllowDeletion: model.AllowDeletion,
171+
Tags: model.Tags,
172+
}, resp.Diagnostics)
165173
if d.HasError() {
166174
resp.Diagnostics.AddError("failed to generate model for state during cluster.Read", "")
167175
resp.Diagnostics.Append(d...)
@@ -203,7 +211,11 @@ func (c *Cluster) Update(ctx context.Context, req resource.UpdateRequest, resp *
203211
return
204212
}
205213

206-
persist, d := generateModel(plan, cluster, resp.Diagnostics)
214+
persist, d := generateModel(cluster, modelOrAPI{
215+
RedpandaVersion: plan.RedpandaVersion,
216+
AllowDeletion: plan.AllowDeletion,
217+
Tags: plan.Tags,
218+
}, resp.Diagnostics)
207219
if d.HasError() {
208220
resp.Diagnostics.AddError("failed to generate model for state during cluster.Update", "")
209221
resp.Diagnostics.Append(d...)

0 commit comments

Comments
 (0)