Skip to content

Commit

Permalink
feat: TestingOnlyStringSliceToTypeList -> StringSliceToTypeList
Browse files Browse the repository at this point in the history
Avoid error checking all over the place when it can be done safely.
  • Loading branch information
ligfx committed Oct 8, 2024
1 parent 5461210 commit 4bdb8db
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 147 deletions.
88 changes: 25 additions & 63 deletions redpanda/resources/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
package cluster

import (
"context"
"fmt"
"reflect"

controlplanev1beta2 "buf.build/gen/go/redpandadata/cloud/protocolbuffers/go/redpanda/api/controlplane/v1beta2"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/redpanda-data/terraform-provider-redpanda/redpanda/models"
Expand All @@ -48,24 +46,15 @@ func gcpConnectConsumerStructToModel(accept []*controlplanev1beta2.GCPPrivateSer
return output
}

func toMtlsModel(ctx context.Context, mtls *controlplanev1beta2.MTLSSpec) (*models.Mtls, diag.Diagnostics) {
func toMtlsModel(mtls *controlplanev1beta2.MTLSSpec) *models.Mtls {
if isMtlsSpecNil(mtls) {
return nil, nil
}

capem, err := types.ListValueFrom(ctx, types.StringType, mtls.GetCaCertificatesPem())
if err != nil {
return nil, err
}
maprules, err := types.ListValueFrom(ctx, types.StringType, mtls.GetPrincipalMappingRules())
if err != nil {
return nil, err
return nil
}
return &models.Mtls{
Enabled: types.BoolValue(mtls.GetEnabled()),
CaCertificatesPem: capem,
PrincipalMappingRules: maprules,
}, nil
CaCertificatesPem: utils.StringSliceToTypeList(mtls.GetCaCertificatesPem()),
PrincipalMappingRules: utils.StringSliceToTypeList(mtls.GetPrincipalMappingRules()),
}
}

func toMtlsSpec(mtls *models.Mtls) *controlplanev1beta2.MTLSSpec {
Expand Down Expand Up @@ -215,27 +204,23 @@ func generateClusterRequest(model models.Cluster) (*controlplanev1beta2.ClusterC
}

// generateModel populates the Cluster model to be persisted to state for Create, Read and Update operations. It is also indirectly used by Import
func generateModel(ctx context.Context, cfg models.Cluster, cluster *controlplanev1beta2.Cluster) (*models.Cluster, error) {
func generateModel(cfg models.Cluster, cluster *controlplanev1beta2.Cluster) (*models.Cluster, error) {
output := &models.Cluster{
Name: types.StringValue(cluster.Name),
ConnectionType: types.StringValue(utils.ConnectionTypeToString(cluster.ConnectionType)),
CloudProvider: types.StringValue(utils.CloudProviderToString(cluster.CloudProvider)),
ClusterType: types.StringValue(utils.ClusterTypeToString(cluster.Type)),
RedpandaVersion: cfg.RedpandaVersion,
ThroughputTier: types.StringValue(cluster.ThroughputTier),
Region: types.StringValue(cluster.Region),
AllowDeletion: cfg.AllowDeletion,
Tags: cfg.Tags,
ResourceGroupID: types.StringValue(cluster.ResourceGroupId),
NetworkID: types.StringValue(cluster.NetworkId),
ID: types.StringValue(cluster.Id),
}

clusterZones, d := types.ListValueFrom(ctx, types.StringType, cluster.Zones)
if d.HasError() {
return nil, fmt.Errorf("failed to parse cluster zones: %v", d)
Name: types.StringValue(cluster.Name),
ConnectionType: types.StringValue(utils.ConnectionTypeToString(cluster.ConnectionType)),
CloudProvider: types.StringValue(utils.CloudProviderToString(cluster.CloudProvider)),
ClusterType: types.StringValue(utils.ClusterTypeToString(cluster.Type)),
RedpandaVersion: cfg.RedpandaVersion,
ThroughputTier: types.StringValue(cluster.ThroughputTier),
Region: types.StringValue(cluster.Region),
AllowDeletion: cfg.AllowDeletion,
Tags: cfg.Tags,
ResourceGroupID: types.StringValue(cluster.ResourceGroupId),
NetworkID: types.StringValue(cluster.NetworkId),
ID: types.StringValue(cluster.Id),
ReadReplicaClusterIDs: utils.StringSliceToTypeList(cluster.ReadReplicaClusterIds),
Zones: utils.StringSliceToTypeList(cluster.Zones),
}
output.Zones = clusterZones

if cluster.GetDataplaneApi() != nil {
clusterURL, err := utils.SplitSchemeDefPort(cluster.DataplaneApi.Url, "443")
Expand All @@ -245,21 +230,11 @@ func generateModel(ctx context.Context, cfg models.Cluster, cluster *controlplan
output.ClusterAPIURL = basetypes.NewStringValue(clusterURL)
}

rr, d := types.ListValueFrom(ctx, types.StringType, cluster.ReadReplicaClusterIds)
if d.HasError() {
return nil, fmt.Errorf("failed to parse read replica cluster IDs: %v", d)
}
output.ReadReplicaClusterIDs = rr

if !isAwsPrivateLinkSpecNil(cluster.AwsPrivateLink) {
ap, dg := types.ListValueFrom(ctx, types.StringType, cluster.AwsPrivateLink.AllowedPrincipals)
if dg.HasError() {
return nil, fmt.Errorf("failed to parse AWS Private Link: %v", dg)
}
output.AwsPrivateLink = &models.AwsPrivateLink{
Enabled: types.BoolValue(cluster.AwsPrivateLink.Enabled),
ConnectConsole: types.BoolValue(cluster.AwsPrivateLink.ConnectConsole),
AllowedPrincipals: ap,
AllowedPrincipals: utils.StringSliceToTypeList(cluster.AwsPrivateLink.AllowedPrincipals),
}
}
if !isGcpPrivateServiceConnectSpecNil(cluster.GcpPrivateServiceConnect) {
Expand All @@ -271,38 +246,25 @@ func generateModel(ctx context.Context, cfg models.Cluster, cluster *controlplan
}

if !isAzurePrivateLinkSpecNil(cluster.AzurePrivateLink) {
as, dg := types.ListValueFrom(ctx, types.StringType, cluster.AzurePrivateLink.AllowedSubscriptions)
if dg.HasError() {
return nil, fmt.Errorf("failed to parse Azure Private Link: %v", dg)
}
output.AzurePrivateLink = &models.AzurePrivateLink{
Enabled: types.BoolValue(cluster.AzurePrivateLink.Enabled),
ConnectConsole: types.BoolValue(cluster.AzurePrivateLink.ConnectConsole),
AllowedSubscriptions: as,
AllowedSubscriptions: utils.StringSliceToTypeList(cluster.AzurePrivateLink.AllowedSubscriptions),
}
}
kAPI, err := toMtlsModel(ctx, cluster.GetKafkaApi().GetMtls())
if err != nil {
return nil, fmt.Errorf("failed to parse Kafka API MTLS: %v", err)
}
kAPI := toMtlsModel(cluster.GetKafkaApi().GetMtls())
if kAPI != nil {
output.KafkaAPI = &models.KafkaAPI{
Mtls: kAPI,
}
}
ht, err := toMtlsModel(ctx, cluster.GetHttpProxy().GetMtls())
if err != nil {
return nil, fmt.Errorf("failed to parse HTTP Proxy MTLS: %v", err)
}
ht := toMtlsModel(cluster.GetHttpProxy().GetMtls())
if ht != nil {
output.HTTPProxy = &models.HTTPProxy{
Mtls: ht,
}
}
sr, err := toMtlsModel(ctx, cluster.GetSchemaRegistry().GetMtls())
if err != nil {
return nil, fmt.Errorf("failed to parse Schema Registry MTLS: %v", err)
}
sr := toMtlsModel(cluster.GetSchemaRegistry().GetMtls())
if sr != nil {
output.SchemaRegistry = &models.SchemaRegistry{
Mtls: sr,
Expand Down
61 changes: 13 additions & 48 deletions redpanda/resources/cluster/data_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ func (d *DataSourceCluster) Read(ctx context.Context, req datasource.ReadRequest
resp.Diagnostics.AddError(fmt.Sprintf("failed to read cluster %s", model.ID), err.Error())
return
}
clusterZones, dg := types.ListValueFrom(ctx, types.StringType, cluster.Zones)
if dg.HasError() {
resp.Diagnostics.Append(dg...)
return
}
clusterURL, err := utils.SplitSchemeDefPort(cluster.DataplaneApi.Url, "443")
if err != nil {
resp.Diagnostics.AddError("unable to parse Cluster API URL", err.Error())
Expand All @@ -96,11 +91,6 @@ func (d *DataSourceCluster) Read(ctx context.Context, req datasource.ReadRequest
resp.Diagnostics.AddError("unable to parse Cloud tags", err.Error())
return
}
rr, derr := types.ListValueFrom(ctx, types.StringType, cluster.ReadReplicaClusterIds)
if derr.HasError() {
resp.Diagnostics.Append(derr...)
return
}

// Mapping the fields from the cluster to the Terraform state
persist := &models.Cluster{
Expand All @@ -111,25 +101,29 @@ func (d *DataSourceCluster) Read(ctx context.Context, req datasource.ReadRequest
RedpandaVersion: types.StringValue(cluster.RedpandaVersion),
ThroughputTier: types.StringValue(cluster.ThroughputTier),
Region: types.StringValue(cluster.Region),
Zones: clusterZones,
Zones: utils.StringSliceToTypeList(cluster.Zones),
Tags: tagsValue,
ResourceGroupID: types.StringValue(cluster.ResourceGroupId),
NetworkID: types.StringValue(cluster.NetworkId),
ID: types.StringValue(cluster.Id),
ClusterAPIURL: types.StringValue(clusterURL),
ReadReplicaClusterIDs: rr,
ReadReplicaClusterIDs: utils.StringSliceToTypeList(cluster.ReadReplicaClusterIds),
KafkaAPI: &models.KafkaAPI{
Mtls: toMtlsModel(cluster.GetKafkaApi().GetMtls()),
},
HTTPProxy: &models.HTTPProxy{
Mtls: toMtlsModel(cluster.GetHttpProxy().GetMtls()),
},
SchemaRegistry: &models.SchemaRegistry{
Mtls: toMtlsModel(cluster.GetSchemaRegistry().GetMtls()),
},
}

if !isAwsPrivateLinkSpecNil(cluster.AwsPrivateLink) {
ap, dig := types.ListValueFrom(ctx, types.StringType, cluster.AwsPrivateLink.AllowedPrincipals)
if dig.HasError() {
resp.Diagnostics.Append(dig...)
return
}
persist.AwsPrivateLink = &models.AwsPrivateLink{
Enabled: types.BoolValue(cluster.AwsPrivateLink.Enabled),
ConnectConsole: types.BoolValue(cluster.AwsPrivateLink.ConnectConsole),
AllowedPrincipals: ap,
AllowedPrincipals: utils.StringSliceToTypeList(cluster.AwsPrivateLink.AllowedPrincipals),
}
}
if !isGcpPrivateServiceConnectSpecNil(cluster.GcpPrivateServiceConnect) {
Expand All @@ -143,42 +137,13 @@ func (d *DataSourceCluster) Read(ctx context.Context, req datasource.ReadRequest
}

if !isAzurePrivateLinkSpecNil(cluster.AzurePrivateLink) {
as, dig := types.ListValueFrom(ctx, types.StringType, cluster.AzurePrivateLink.AllowedSubscriptions)
if dig.HasError() {
resp.Diagnostics.Append(dig...)
return
}
persist.AzurePrivateLink = &models.AzurePrivateLink{
Enabled: types.BoolValue(cluster.AzurePrivateLink.Enabled),
ConnectConsole: types.BoolValue(cluster.AzurePrivateLink.ConnectConsole),
AllowedSubscriptions: as,
AllowedSubscriptions: utils.StringSliceToTypeList(cluster.AzurePrivateLink.AllowedSubscriptions),
}
}

kAPI, dg := toMtlsModel(ctx, cluster.GetKafkaApi().GetMtls())
if dg != nil {
resp.Diagnostics.Append(dg...)
return
}
persist.KafkaAPI = &models.KafkaAPI{
Mtls: kAPI,
}
hp, dg := toMtlsModel(ctx, cluster.GetHttpProxy().GetMtls())
if dg != nil {
resp.Diagnostics.Append(dg...)
return
}
persist.HTTPProxy = &models.HTTPProxy{
Mtls: hp,
}
sr, dg := toMtlsModel(ctx, cluster.GetSchemaRegistry().GetMtls())
if dg != nil {
resp.Diagnostics.Append(dg...)
return
}
persist.SchemaRegistry = &models.SchemaRegistry{
Mtls: sr,
}
resp.Diagnostics.Append(resp.State.Set(ctx, persist)...)
}

Expand Down
6 changes: 3 additions & 3 deletions redpanda/resources/cluster/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (c *Cluster) Create(ctx context.Context, req resource.CreateRequest, resp *
resp.Diagnostics.AddError(fmt.Sprintf("successfully created the cluster with ID %q, but failed to read the cluster configuration: %v", clusterID, err), err.Error())
return
}
persist, err := generateModel(ctx, model, cluster)
persist, err := generateModel(model, cluster)
if err != nil {
resp.Diagnostics.AddError("failed to generate model for state during cluster.Create", err.Error())
return
Expand Down Expand Up @@ -391,7 +391,7 @@ func (c *Cluster) Read(ctx context.Context, req resource.ReadRequest, resp *reso
return
}

persist, err := generateModel(ctx, model, cluster)
persist, err := generateModel(model, cluster)
if err != nil {
resp.Diagnostics.AddError("failed to generate model for state during cluster.Read", err.Error())
return
Expand Down Expand Up @@ -486,7 +486,7 @@ func (c *Cluster) Update(ctx context.Context, req resource.UpdateRequest, resp *
var cfg models.Cluster
resp.Diagnostics.Append(req.Config.Get(ctx, &cfg)...)

persist, err := generateModel(ctx, cfg, cluster)
persist, err := generateModel(cfg, cluster)
if err != nil {
resp.Diagnostics.AddError("failed to generate model for state during cluster.Update", err.Error())
return
Expand Down
Loading

0 comments on commit 4bdb8db

Please sign in to comment.