Skip to content

Commit 11d3079

Browse files
committed
fix mapping issues
1 parent 6014ae2 commit 11d3079

File tree

4 files changed

+57
-47
lines changed

4 files changed

+57
-47
lines changed

internal/common/cluster_bindings.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ func (cb *ClusterBindings) From(readBindings []*console.PolicyBindingFragment, w
5656
return
5757
}
5858

59-
cb.Read = clusterBindingsFrom(readBindings, ctx, d)
60-
cb.Write = clusterBindingsFrom(writeBindings, ctx, d)
59+
cb.Read = clusterBindingsFrom(readBindings, cb.Read, ctx, d)
60+
cb.Write = clusterBindingsFrom(writeBindings, cb.Write, ctx, d)
6161
}
6262

63-
func clusterBindingsFrom(bindings []*console.PolicyBindingFragment, ctx context.Context, d diag.Diagnostics) types.Set {
64-
if bindings == nil {
65-
return types.SetNull(basetypes.ObjectType{AttrTypes: ClusterPolicyBindingAttrTypes})
63+
func clusterBindingsFrom(bindings []*console.PolicyBindingFragment, config types.Set, ctx context.Context, d diag.Diagnostics) types.Set {
64+
if len(bindings) == 0 {
65+
// Rewriting config to state to avoid inconsistent result errors.
66+
// This could happen, for example, when sending "nil" to API and "[]" is returned as a result.
67+
return config
6668
}
6769

6870
values := make([]attr.Value, len(bindings))

internal/common/map.go

+12
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@ func MapFrom(values map[string]any, ctx context.Context, d diag.Diagnostics) typ
1616
d.Append(diags...)
1717
return mapValue
1818
}
19+
20+
func MapFromWithConfig(values map[string]any, config types.Map, ctx context.Context, d diag.Diagnostics) types.Map {
21+
if len(values) == 0 {
22+
// 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.
24+
return config
25+
}
26+
27+
mapValue, diags := types.MapValueFrom(ctx, types.StringType, values)
28+
d.Append(diags...)
29+
return mapValue
30+
}

internal/resource/infrastructure_stack_model.go

+38-32
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,17 @@ func (is *infrastructureStack) From(stack *gqlclient.InfrastructureStackFragment
8989
is.ClusterId = types.StringValue(stack.Cluster.ID)
9090
is.Repository.From(stack.Repository, stack.Git)
9191
is.Configuration.From(stack.Configuration)
92-
is.Files = infrastructureStackFilesFrom(stack.Files, d)
93-
is.Environment = infrastructureStackEnvironmentsFrom(stack.Environment, ctx, d)
92+
is.Files = infrastructureStackFilesFrom(stack.Files, is.Files, d)
93+
is.Environment = infrastructureStackEnvironmentsFrom(stack.Environment, is.Environment, ctx, d)
9494
is.Bindings.From(stack.ReadBindings, stack.WriteBindings, ctx, d)
9595
is.JobSpec.From(stack.JobSpec, ctx, d)
9696
}
9797

98-
func infrastructureStackFilesFrom(files []*gqlclient.StackFileFragment, d diag.Diagnostics) basetypes.MapValue {
99-
if files == nil {
100-
return types.MapNull(types.StringType)
98+
func infrastructureStackFilesFrom(files []*gqlclient.StackFileFragment, config types.Map, d diag.Diagnostics) types.Map {
99+
if len(files) == 0 {
100+
// Rewriting config to state to avoid inconsistent result errors.
101+
// This could happen, for example, when sending "nil" to API and "[]" is returned as a result.
102+
return config
101103
}
102104

103105
resultMap := make(map[string]attr.Value, len(files))
@@ -111,6 +113,29 @@ func infrastructureStackFilesFrom(files []*gqlclient.StackFileFragment, d diag.D
111113
return result
112114
}
113115

116+
func infrastructureStackEnvironmentsFrom(envs []*gqlclient.StackEnvironmentFragment, config types.Set, ctx context.Context, d diag.Diagnostics) types.Set {
117+
if len(envs) == 0 {
118+
// Rewriting config to state to avoid inconsistent result errors.
119+
// This could happen, for example, when sending "nil" to API and "[]" is returned as a result.
120+
return config
121+
}
122+
123+
values := make([]attr.Value, len(envs))
124+
for i, file := range envs {
125+
objValue, diags := types.ObjectValueFrom(ctx, InfrastructureStackEnvironmentAttrTypes, InfrastructureStackEnvironment{
126+
Name: types.StringValue(file.Name),
127+
Value: types.StringValue(file.Value),
128+
Secret: types.BoolPointerValue(file.Secret),
129+
})
130+
values[i] = objValue
131+
d.Append(diags...)
132+
}
133+
134+
setValue, diags := types.SetValue(basetypes.ObjectType{AttrTypes: InfrastructureStackEnvironmentAttrTypes}, values)
135+
d.Append(diags...)
136+
return setValue
137+
}
138+
114139
type InfrastructureStackRepository struct {
115140
Id types.String `tfsdk:"id"`
116141
Ref types.String `tfsdk:"ref"`
@@ -180,27 +205,6 @@ var InfrastructureStackEnvironmentAttrTypes = map[string]attr.Type{
180205
"secret": types.BoolType,
181206
}
182207

183-
func infrastructureStackEnvironmentsFrom(envs []*gqlclient.StackEnvironmentFragment, ctx context.Context, d diag.Diagnostics) types.Set {
184-
if envs == nil {
185-
return types.SetNull(basetypes.ObjectType{AttrTypes: InfrastructureStackEnvironmentAttrTypes})
186-
}
187-
188-
values := make([]attr.Value, len(envs))
189-
for i, file := range envs {
190-
objValue, diags := types.ObjectValueFrom(ctx, InfrastructureStackEnvironmentAttrTypes, InfrastructureStackEnvironment{
191-
Name: types.StringValue(file.Name),
192-
Value: types.StringValue(file.Value),
193-
Secret: types.BoolPointerValue(file.Secret),
194-
})
195-
values[i] = objValue
196-
d.Append(diags...)
197-
}
198-
199-
setValue, diags := types.SetValue(basetypes.ObjectType{AttrTypes: InfrastructureStackEnvironmentAttrTypes}, values)
200-
d.Append(diags...)
201-
return setValue
202-
}
203-
204208
type InfrastructureStackBindings struct {
205209
Read []*InfrastructureStackPolicyBinding `tfsdk:"read"`
206210
Write []*InfrastructureStackPolicyBinding `tfsdk:"write"`
@@ -279,15 +283,17 @@ func (isjs *InfrastructureStackJobSpec) From(spec *gqlclient.JobGateSpecFragment
279283

280284
isjs.Namespace = types.StringValue(spec.Namespace)
281285
isjs.Raw = types.StringPointerValue(spec.Raw)
282-
isjs.Containers = infrastructureStackJobSpecContainersFrom(spec.Containers, ctx, d)
283-
isjs.Labels = common.MapFrom(spec.Labels, ctx, d)
284-
isjs.Annotations = common.MapFrom(spec.Annotations, ctx, d)
286+
isjs.Containers = infrastructureStackJobSpecContainersFrom(spec.Containers, isjs.Containers, ctx, d)
287+
isjs.Labels = common.MapFromWithConfig(spec.Labels, isjs.Labels, ctx, d)
288+
isjs.Annotations = common.MapFromWithConfig(spec.Annotations, isjs.Annotations, ctx, d)
285289
isjs.ServiceAccount = types.StringPointerValue(spec.ServiceAccount)
286290
}
287291

288-
func infrastructureStackJobSpecContainersFrom(containers []*gqlclient.ContainerSpecFragment, ctx context.Context, d diag.Diagnostics) types.Set {
289-
if containers == nil {
290-
return types.SetNull(basetypes.ObjectType{AttrTypes: InfrastructureStackContainerSpecAttrTypes})
292+
func infrastructureStackJobSpecContainersFrom(containers []*gqlclient.ContainerSpecFragment, config types.Set, ctx context.Context, d diag.Diagnostics) types.Set {
293+
if len(containers) == 0 {
294+
// Rewriting config to state to avoid inconsistent result errors.
295+
// This could happen, for example, when sending "nil" to API and "[]" is returned as a result.
296+
return config
291297
}
292298

293299
values := make([]attr.Value, len(containers))

internal/resource/infrastructure_stack_schema.go

-10
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ import (
66
"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
77
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
88
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
9-
"github.com/hashicorp/terraform-plugin-framework/attr"
109
"github.com/hashicorp/terraform-plugin-framework/path"
1110
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1211
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
13-
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapdefault"
1412
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
1513
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1614
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
@@ -108,7 +106,6 @@ func (r *InfrastructureStackResource) schema() schema.Schema {
108106
Description: "Defines environment variables for the stack.",
109107
MarkdownDescription: "Defines environment variables for the stack.",
110108
Optional: true,
111-
Computed: true,
112109
NestedObject: schema.NestedAttributeObject{
113110
Attributes: map[string]schema.Attribute{
114111
"name": schema.StringAttribute{
@@ -155,17 +152,13 @@ func (r *InfrastructureStackResource) schema() schema.Schema {
155152
MarkdownDescription: "Kubernetes labels applied to the job.",
156153
ElementType: types.StringType,
157154
Optional: true,
158-
Computed: true,
159-
Default: mapdefault.StaticValue(types.MapValueMust(types.StringType, map[string]attr.Value{})),
160155
Validators: []validator.Map{mapvalidator.ConflictsWith(path.MatchRelative().AtParent().AtName("raw"))},
161156
},
162157
"annotations": schema.MapAttribute{
163158
Description: "Kubernetes annotations applied to the job.",
164159
MarkdownDescription: "Kubernetes annotations applied to the job.",
165160
ElementType: types.StringType,
166161
Optional: true,
167-
Computed: true,
168-
Default: mapdefault.StaticValue(types.MapValueMust(types.StringType, map[string]attr.Value{})),
169162
Validators: []validator.Map{mapvalidator.ConflictsWith(path.MatchRelative().AtParent().AtName("raw"))},
170163
},
171164
"service_account": schema.StringAttribute{
@@ -176,7 +169,6 @@ func (r *InfrastructureStackResource) schema() schema.Schema {
176169
},
177170
"containers": schema.SetNestedAttribute{
178171
Optional: true,
179-
Computed: true,
180172
NestedObject: schema.NestedAttributeObject{
181173
Attributes: map[string]schema.Attribute{
182174
"image": schema.StringAttribute{
@@ -226,7 +218,6 @@ func (r *InfrastructureStackResource) schema() schema.Schema {
226218
Description: "Read policies of this stack.",
227219
MarkdownDescription: "Read policies of this stack.",
228220
Optional: true,
229-
Computed: true,
230221
NestedObject: schema.NestedAttributeObject{
231222
Attributes: map[string]schema.Attribute{
232223
"group_id": schema.StringAttribute{
@@ -245,7 +236,6 @@ func (r *InfrastructureStackResource) schema() schema.Schema {
245236
Description: "Write policies of this stack.",
246237
MarkdownDescription: "Write policies of this stack.",
247238
Optional: true,
248-
Computed: true,
249239
NestedObject: schema.NestedAttributeObject{
250240
Attributes: map[string]schema.Attribute{
251241
"group_id": schema.StringAttribute{

0 commit comments

Comments
 (0)