Skip to content

Commit e97ade9

Browse files
Merge pull request #38 from pluralsh/marcin/eng-2117-annoying-terraform-provider-bug-when-creating-cluster-wo
fix: Fix inconsistent result after apply error for cluster tags
2 parents e897b93 + b946547 commit e97ade9

File tree

8 files changed

+43
-29
lines changed

8 files changed

+43
-29
lines changed

internal/common/cluster_tags.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

internal/common/map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func MapFrom(values map[string]any, ctx context.Context, d diag.Diagnostics) typ
2020
func MapFromWithConfig(values map[string]any, config types.Map, ctx context.Context, d diag.Diagnostics) types.Map {
2121
if len(values) == 0 {
2222
// Rewriting config to state to avoid inconsistent result errors.
23-
// This could happen, for example, when sending "nil" to API and "[]" is returned as a result.
23+
// This could happen, for example, when sending "nil" to API and "{}" is returned as a result.
2424
return config
2525
}
2626

internal/common/tags.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package common
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/attr"
5+
"github.com/hashicorp/terraform-plugin-framework/diag"
6+
"github.com/hashicorp/terraform-plugin-framework/types"
7+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
8+
console "github.com/pluralsh/console-client-go"
9+
)
10+
11+
func TagsFrom(tags []*console.ClusterTags, config types.Map, d diag.Diagnostics) basetypes.MapValue {
12+
if len(tags) == 0 {
13+
// Rewriting config to state to avoid inconsistent result errors.
14+
// This could happen, for example, when sending "nil" to API and "{}" is returned as a result.
15+
return config
16+
}
17+
18+
resultMap := make(map[string]attr.Value, len(tags))
19+
for _, tag := range tags {
20+
resultMap[tag.Name] = types.StringValue(tag.Value)
21+
}
22+
23+
result, tagsDiagnostics := types.MapValue(types.StringType, resultMap)
24+
d.Append(tagsDiagnostics...)
25+
26+
return result
27+
}

internal/datasource/cluster_model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (c *cluster) From(cl *console.ClusterFragment, ctx context.Context, d diag.
3939
c.Handle = types.StringPointerValue(cl.Handle)
4040
c.DesiredVersion = types.StringPointerValue(cl.Version)
4141
c.Protect = types.BoolPointerValue(cl.Protect)
42-
c.Tags = common.ClusterTagsFrom(cl.Tags, d)
42+
c.Tags = common.TagsFrom(cl.Tags, c.Tags, d)
4343
c.Metadata = types.StringValue(string(metadata))
4444
c.ProviderId = common.ClusterProviderIdFrom(cl.Provider)
4545
c.NodePools = common.ClusterNodePoolsFrom(cl.NodePools, c.NodePools, ctx, d)

internal/resource/cluster_model.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ type cluster struct {
5656
// }
5757

5858
func (c *cluster) TagsAttribute(ctx context.Context, d diag.Diagnostics) []*console.TagAttributes {
59+
if c.Tags.IsNull() {
60+
return nil
61+
}
62+
5963
result := make([]*console.TagAttributes, 0)
6064
elements := make(map[string]types.String, len(c.Tags.Elements()))
6165
d.Append(c.Tags.ElementsAs(ctx, &elements, false)...)
@@ -107,7 +111,7 @@ func (c *cluster) From(cl *console.ClusterFragment, ctx context.Context, d diag.
107111
c.Handle = types.StringPointerValue(cl.Handle)
108112
// c.DesiredVersion = c.ClusterVersionFrom(cl.Provider, cl.Version, cl.CurrentVersion)
109113
c.Protect = types.BoolPointerValue(cl.Protect)
110-
c.Tags = common.ClusterTagsFrom(cl.Tags, d)
114+
c.Tags = common.TagsFrom(cl.Tags, c.Tags, d)
111115
// c.ProviderId = common.ClusterProviderIdFrom(cl.Provider)
112116
// c.NodePools = common.ClusterNodePoolsFrom(cl.NodePools, c.NodePools, ctx, d)
113117
c.Metadata = types.StringValue(string(metadata))
@@ -120,7 +124,7 @@ func (c *cluster) FromCreate(cc *console.CreateCluster, ctx context.Context, d d
120124
c.Handle = types.StringPointerValue(cc.CreateCluster.Handle)
121125
// c.DesiredVersion = c.ClusterVersionFrom(cc.CreateCluster.Provider, cc.CreateCluster.Version, cc.CreateCluster.CurrentVersion)
122126
c.Protect = types.BoolPointerValue(cc.CreateCluster.Protect)
123-
c.Tags = common.ClusterTagsFrom(cc.CreateCluster.Tags, d)
127+
c.Tags = common.TagsFrom(cc.CreateCluster.Tags, c.Tags, d)
124128
// c.ProviderId = common.ClusterProviderIdFrom(cc.CreateCluster.Provider)
125129
// c.NodePools = common.ClusterNodePoolsFrom(cc.CreateCluster.NodePools, c.NodePools, ctx, d)
126130
}

internal/resource/cluster_schema.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
1010
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1111
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
12-
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier"
1312
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
1413
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1514
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
@@ -217,7 +216,6 @@ func (r *clusterResource) schema() schema.Schema {
217216
MarkdownDescription: "Key-value tags used to filter clusters.",
218217
Optional: true,
219218
ElementType: types.StringType,
220-
PlanModifiers: []planmodifier.Map{mapplanmodifier.UseStateForUnknown()},
221219
},
222220
"bindings": schema.SingleNestedAttribute{
223221
Description: "Read and write policies of this cluster.",

internal/resource/global_service_model.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package resource
33
import (
44
"context"
55
"strings"
6+
67
"terraform-provider-plural/internal/common"
78

89
"github.com/hashicorp/terraform-plugin-framework/diag"
@@ -30,7 +31,8 @@ func (g *globalService) From(response *gqlclient.GlobalServiceFragment, d diag.D
3031
if response.Provider != nil {
3132
g.ProviderId = types.StringValue(response.Provider.ID)
3233
}
33-
g.Tags = common.ClusterTagsFrom(response.Tags, d)
34+
g.Tags = common.TagsFrom(response.Tags, g.Tags, d)
35+
3436
}
3537

3638
func (g *globalService) Attributes(ctx context.Context, d diag.Diagnostics) gqlclient.GlobalServiceAttributes {
@@ -47,6 +49,10 @@ func (g *globalService) Attributes(ctx context.Context, d diag.Diagnostics) gqlc
4749
}
4850

4951
func (g *globalService) TagsAttribute(ctx context.Context, d diag.Diagnostics) []*gqlclient.TagAttributes {
52+
if g.Tags.IsNull() {
53+
return nil
54+
}
55+
5056
result := make([]*gqlclient.TagAttributes, 0)
5157
elements := make(map[string]types.String, len(g.Tags.Elements()))
5258
d.Append(g.Tags.ElementsAs(ctx, &elements, false)...)

internal/resource/infrastructure_stack_model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (is *infrastructureStack) From(stack *gqlclient.InfrastructureStackFragment
109109
func infrastructureStackFilesFrom(files []*gqlclient.StackFileFragment, config types.Map, d diag.Diagnostics) types.Map {
110110
if len(files) == 0 {
111111
// Rewriting config to state to avoid inconsistent result errors.
112-
// This could happen, for example, when sending "nil" to API and "[]" is returned as a result.
112+
// This could happen, for example, when sending "nil" to API and "{}" is returned as a result.
113113
return config
114114
}
115115

0 commit comments

Comments
 (0)