From 81ee60712b182ab381cdf468ec9b116ec1896498 Mon Sep 17 00:00:00 2001 From: Toby Brain Date: Sat, 17 Jun 2023 21:42:41 +1000 Subject: [PATCH] Handle unknown values in deployment snapshot repository references (#666) * Handle unknown values in deployment snapshot repository references * Changelog --- .changelog/666.txt | 3 ++ .../v2/elasticsearch_snapshot.go | 44 +++++++++++++------ 2 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 .changelog/666.txt diff --git a/.changelog/666.txt b/.changelog/666.txt new file mode 100644 index 000000000..b1edba50a --- /dev/null +++ b/.changelog/666.txt @@ -0,0 +1,3 @@ +```release-note:bug-fix +resource/deployment: Fix a value conversion error encountered when attempting to parse deployments without a snapshot repository +``` diff --git a/ec/ecresource/deploymentresource/elasticsearch/v2/elasticsearch_snapshot.go b/ec/ecresource/deploymentresource/elasticsearch/v2/elasticsearch_snapshot.go index 9cc506f3a..0a49aa447 100644 --- a/ec/ecresource/deploymentresource/elasticsearch/v2/elasticsearch_snapshot.go +++ b/ec/ecresource/deploymentresource/elasticsearch/v2/elasticsearch_snapshot.go @@ -23,6 +23,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/tfsdk" + "github.com/hashicorp/terraform-plugin-framework/types" "github.com/elastic/cloud-sdk-go/pkg/models" ) @@ -40,6 +41,11 @@ type ElasticsearchSnapshotRepositoryReference struct { RepositoryName string `tfsdk:"repository_name"` } +type ElasticsearchSnapshotTF struct { + Enabled bool `tfsdk:"enabled"` + Repository types.Object `tfsdk:"repository"` //< ElasticsearchSnapshotRepositoryInfo +} + func readElasticsearchSnapshot(in *models.ElasticsearchClusterSettings) (*ElasticsearchSnapshot, error) { if in == nil || in.Snapshot == nil { return nil, nil @@ -50,20 +56,22 @@ func readElasticsearchSnapshot(in *models.ElasticsearchClusterSettings) (*Elasti if in.Snapshot.Enabled != nil { snapshot.Enabled = *in.Snapshot.Enabled } - if in.Snapshot.Repository != nil { - snapshot.Repository = &ElasticsearchSnapshotRepositoryInfo{} - if in.Snapshot.Repository.Reference != nil { - snapshot.Repository.Reference = &ElasticsearchSnapshotRepositoryReference{ - RepositoryName: in.Snapshot.Repository.Reference.RepositoryName, - } - } + + if in.Snapshot.Repository == nil || in.Snapshot.Repository.Reference == nil { + return &snapshot, nil + } + + snapshot.Repository = &ElasticsearchSnapshotRepositoryInfo{ + Reference: &ElasticsearchSnapshotRepositoryReference{ + RepositoryName: in.Snapshot.Repository.Reference.RepositoryName, + }, } return &snapshot, nil } func elasticsearchSnapshotPayload(ctx context.Context, srcObj attr.Value, model *models.ElasticsearchClusterSettings) (*models.ElasticsearchClusterSettings, diag.Diagnostics) { - var snapshot *ElasticsearchSnapshot + var snapshot *ElasticsearchSnapshotTF if srcObj.IsNull() || srcObj.IsUnknown() { return model, nil } @@ -76,14 +84,22 @@ func elasticsearchSnapshotPayload(ctx context.Context, srcObj attr.Value, model model = &models.ElasticsearchClusterSettings{} } model.Snapshot = &models.ClusterSnapshotSettings{ - Enabled: &snapshot.Enabled, - Repository: &models.ClusterSnapshotRepositoryInfo{}, + Enabled: &snapshot.Enabled, + } + + if snapshot.Repository.IsNull() || snapshot.Repository.IsUnknown() { + return model, nil + } + + var repo *ElasticsearchSnapshotRepositoryInfo + if diags := tfsdk.ValueAs(ctx, snapshot.Repository, &repo); diags.HasError() { + return model, diags } - if snapshot.Repository != nil && snapshot.Repository.Reference != nil { - model.Snapshot.Repository.Reference = &models.ClusterSnapshotRepositoryReference{ - RepositoryName: snapshot.Repository.Reference.RepositoryName, - } + model.Snapshot.Repository = &models.ClusterSnapshotRepositoryInfo{ + Reference: &models.ClusterSnapshotRepositoryReference{ + RepositoryName: repo.Reference.RepositoryName, + }, } return model, nil