Skip to content

Commit 05698a8

Browse files
committed
get actions from plan / state and set them on rules after read
1 parent e4b38f6 commit 05698a8

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

internal/provider/allocation_group.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ import (
1616
"github.com/hashicorp/terraform-plugin-framework/types"
1717
)
1818

19+
func (plan *allocationGroupResourceModel) getActions(ctx context.Context) (actions map[string]string, d diag.Diagnostics) {
20+
actions = make(map[string]string)
21+
var diags diag.Diagnostics
22+
if !plan.Rules.IsNull() {
23+
planRules := []resource_allocation_group.RulesValue{}
24+
diags = plan.Rules.ElementsAs(ctx, &planRules, false)
25+
d.Append(diags...)
26+
if d.HasError() {
27+
return
28+
}
29+
for i := range planRules {
30+
if !planRules[i].Id.IsNull() {
31+
actions[planRules[i].Id.ValueString()] = planRules[i].Action.ValueString()
32+
}
33+
}
34+
}
35+
return
36+
}
37+
1938
func (plan *allocationGroupResourceModel) toRequest(ctx context.Context) (groupAllocationRequest models.GroupAllocationRequest, d diag.Diagnostics) {
2039
var diags diag.Diagnostics
2140
groupAllocationRequest.UnallocatedCosts = plan.UnallocatedCosts.ValueStringPointer()
@@ -67,7 +86,7 @@ func (plan *allocationGroupResourceModel) toRequest(ctx context.Context) (groupA
6786
return groupAllocationRequest, d
6887
}
6988

70-
func (state *allocationGroupResourceModel) populate(groupAllocation *models.GroupAllocation, client *ClientTest, ctx context.Context) (d diag.Diagnostics) {
89+
func (state *allocationGroupResourceModel) populate(groupAllocation *models.GroupAllocation, client *ClientTest, actions map[string]string, ctx context.Context) (d diag.Diagnostics) {
7190
var diags diag.Diagnostics
7291
state.Description = types.StringPointerValue(groupAllocation.Description)
7392
state.Id = types.StringPointerValue(groupAllocation.Id)
@@ -89,7 +108,17 @@ func (state *allocationGroupResourceModel) populate(groupAllocation *models.Grou
89108
Owner: types.StringPointerValue(rule.Owner),
90109
RulesType: types.StringPointerValue(rule.Type),
91110
UpdateTime: types.Int64PointerValue(rule.UpdateTime),
111+
// Doesn't exist on the AllocationListItem data model
112+
// AllocationType: types.StringPointerValue(string(rule.AllocationType)),
113+
// Description: types.StringPointerValue(rule.Description),
114+
// UrlUi: types.StringPointerValue(rule.UrlUi),
92115
}
116+
if rule.Id != nil {
117+
stateRule.Action = types.StringValue(actions[*rule.Id])
118+
} else {
119+
stateRule.Action = types.StringValue("create")
120+
}
121+
93122
var singleAllocation *models.SingleAllocation
94123
singleAllocation, err := client.GetAllocation(stateRule.Id.ValueString())
95124
if err != nil {
@@ -99,6 +128,9 @@ func (state *allocationGroupResourceModel) populate(groupAllocation *models.Grou
99128
)
100129
return
101130
}
131+
// This is an ugly hack because we have two different version of the ComponentsValue / ComponentsType and we need to convert between them
132+
// This would not be necessary if we could unify resource_allocation and resource_allocation_group but the generator cannot be used with the
133+
// "full" OpenAPI spec, so we have to split it
102134
singleAllocationState := allocationResourceModel{}
103135
diags = singleAllocationState.populate(singleAllocation, ctx)
104136
d.Append(diags...)
@@ -138,7 +170,7 @@ func (state *allocationGroupResourceModel) populate(groupAllocation *models.Grou
138170
}
139171

140172
stateRules[i], diags = resource_allocation_group.NewRulesValue(stateRule.AttributeTypes(ctx), map[string]attr.Value{
141-
"action": types.StringValue("select"),
173+
"action": stateRule.Action,
142174
"allocation_type": stateRule.AllocationType,
143175
"components": stateRule.Components,
144176
"create_time": stateRule.CreateTime,

internal/provider/allocation_group_resource.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ func (r *allocationGroupResource) Create(ctx context.Context, req resource.Creat
6464
return
6565
}
6666

67+
// get actions from plan
68+
actions, diags := plan.getActions(ctx)
69+
resp.Diagnostics.Append(diags...)
70+
if resp.Diagnostics.HasError() {
71+
return
72+
}
73+
6774
// Generate API request body from plan
6875
allocationGroup, diags := plan.toRequest(ctx)
6976
resp.Diagnostics.Append(diags...)
@@ -95,7 +102,7 @@ func (r *allocationGroupResource) Create(ctx context.Context, req resource.Creat
95102
return
96103
}
97104
plan.Id = types.StringPointerValue(allocationgroupResponse.Id)
98-
plan.populate(allocationCreated, r.client, ctx)
105+
plan.populate(allocationCreated, r.client, actions, ctx)
99106

100107
// Set state to fully populated data
101108
diags = resp.State.Set(ctx, plan)
@@ -118,6 +125,13 @@ func (r *allocationGroupResource) Read(ctx context.Context, req resource.ReadReq
118125
log.Print("state id:::::::::::::::::::::::::")
119126
log.Print(state.Id.ValueString())
120127

128+
// get actions from state
129+
actions, diags := state.getActions(ctx)
130+
resp.Diagnostics.Append(diags...)
131+
if resp.Diagnostics.HasError() {
132+
return
133+
}
134+
121135
// Get refreshed allocation value from DoiT
122136
allocation, err := r.client.GetAllocationGroup(state.Id.ValueString())
123137
if err != nil {
@@ -131,7 +145,7 @@ func (r *allocationGroupResource) Read(ctx context.Context, req resource.ReadReq
131145
)
132146
return
133147
}
134-
diags = state.populate(allocation, r.client, ctx)
148+
diags = state.populate(allocation, r.client, actions, ctx)
135149
resp.Diagnostics.Append(diags...)
136150
if resp.Diagnostics.HasError() {
137151
return
@@ -155,6 +169,13 @@ func (r *allocationGroupResource) Update(ctx context.Context, req resource.Updat
155169
return
156170
}
157171

172+
// get actions from plan
173+
actions, diags := plan.getActions(ctx)
174+
resp.Diagnostics.Append(diags...)
175+
if resp.Diagnostics.HasError() {
176+
return
177+
}
178+
158179
var state allocationGroupResourceModel
159180
diags = req.State.Get(ctx, &state)
160181
resp.Diagnostics.Append(diags...)
@@ -190,7 +211,7 @@ func (r *allocationGroupResource) Update(ctx context.Context, req resource.Updat
190211
return
191212
}
192213
fmt.Println("Rules length", len(*allocationResponse.Rules))
193-
diags = state.populate(allocationResponse, r.client, ctx)
214+
diags = state.populate(allocationResponse, r.client, actions, ctx)
194215
resp.Diagnostics.Append(diags...)
195216
if resp.Diagnostics.HasError() {
196217
return

0 commit comments

Comments
 (0)