-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_datacenter_connectivity_template_assignment.go
233 lines (201 loc) · 8.33 KB
/
resource_datacenter_connectivity_template_assignment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package tfapstra
import (
"context"
"fmt"
"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/Juniper/terraform-provider-apstra/apstra/blueprint"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var _ resource.ResourceWithConfigure = &resourceDatacenterConnectivityTemplateAssignment{}
var _ resourceWithSetBpClientFunc = &resourceDatacenterConnectivityTemplateAssignment{}
var _ resourceWithSetBpLockFunc = &resourceDatacenterConnectivityTemplateAssignment{}
type resourceDatacenterConnectivityTemplateAssignment struct {
getBpClientFunc func(context.Context, string) (*apstra.TwoStageL3ClosClient, error)
lockFunc func(context.Context, string) error
}
func (o *resourceDatacenterConnectivityTemplateAssignment) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_datacenter_connectivity_template_assignment"
}
func (o *resourceDatacenterConnectivityTemplateAssignment) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
configureResource(ctx, o, req, resp)
}
func (o *resourceDatacenterConnectivityTemplateAssignment) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
deprecationMessage := "This resource has been deprecated and will be removed in a future release. Please migrate " +
"your configuration to use the `apstra_datacenter_connectivity_templates_assignment` resource."
resp.Schema = schema.Schema{
DeprecationMessage: deprecationMessage,
MarkdownDescription: docCategoryDatacenter + "**Deprecation Warning**\n\n" + deprecationMessage +
"\n\nThis resource assigns one or more Connectivity Templates to an " +
"Application Point. Application Points are graph nodes including interfaces at the " +
"fabric edge, and switches within the fabric.",
Attributes: blueprint.ConnectivityTemplatesAssignment{}.ResourceAttributes(),
}
}
func (o *resourceDatacenterConnectivityTemplateAssignment) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan.
var plan blueprint.ConnectivityTemplatesAssignment
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
// get a client for the datacenter reference design
bp, err := o.getBpClientFunc(ctx, plan.BlueprintId.ValueString())
if err != nil {
if utils.IsApstra404(err) {
resp.Diagnostics.AddError(fmt.Sprintf("blueprint %s not found", plan.BlueprintId), err.Error())
return
}
resp.Diagnostics.AddError("failed to create blueprint client", err.Error())
return
}
// Lock the blueprint mutex.
err = o.lockFunc(ctx, plan.BlueprintId.ValueString())
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("failed locking blueprint %q mutex", plan.BlueprintId.ValueString()),
err.Error())
return
}
addIds, _ := plan.AddDelRequest(ctx, nil, &resp.Diagnostics)
err = bp.SetApplicationPointConnectivityTemplates(ctx, apstra.ObjectId(plan.ApplicationPointId.ValueString()), addIds)
if err != nil {
resp.Diagnostics.AddError("failed applying Connectivity Template", err.Error())
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (o *resourceDatacenterConnectivityTemplateAssignment) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Retrieve values from state
var state blueprint.ConnectivityTemplatesAssignment
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
// get a client for the datacenter reference design
bp, err := o.getBpClientFunc(ctx, state.BlueprintId.ValueString())
if err != nil {
if utils.IsApstra404(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("failed to create blueprint client", err.Error())
return
}
// currentCtIds come from the API, may include CTs unrelated to this resource
currentCtIds, err := bp.GetInterfaceConnectivityTemplates(ctx, apstra.ObjectId(state.ApplicationPointId.ValueString()))
if err != nil {
if utils.IsApstra404(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("failed reading Connectivity Template assignments", err.Error())
return
}
// stateCtIds come from the history of this resource. What CTs have been previously assigned?
var stateCtIds []apstra.ObjectId
resp.Diagnostics.Append(state.ConnectivityTemplateIds.ElementsAs(ctx, &stateCtIds, false)...)
if resp.Diagnostics.HasError() {
return
}
// remainingCtIds are the previously assigned IDs (state) which are still assigned (current)
remainingCtIds := utils.SliceIntersectionOfAB(currentCtIds, stateCtIds)
state.ConnectivityTemplateIds = utils.SetValueOrNull(ctx, types.StringType, remainingCtIds, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
// Set state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
func (o *resourceDatacenterConnectivityTemplateAssignment) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Retrieve values from plan
var plan blueprint.ConnectivityTemplatesAssignment
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
// Retrieve values from state
var state blueprint.ConnectivityTemplatesAssignment
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
// get a client for the datacenter reference design
bp, err := o.getBpClientFunc(ctx, plan.BlueprintId.ValueString())
if err != nil {
resp.Diagnostics.AddError("failed to create blueprint client", err.Error())
return
}
// Lock the blueprint mutex.
err = o.lockFunc(ctx, plan.BlueprintId.ValueString())
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("failed locking blueprint %q mutex", plan.BlueprintId.ValueString()),
err.Error())
return
}
// calculate the add/del sets
addIds, delIds := plan.AddDelRequest(ctx, &state, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
// add any required CTs
err = bp.SetApplicationPointConnectivityTemplates(ctx, apstra.ObjectId(plan.ApplicationPointId.ValueString()), addIds)
if err != nil {
resp.Diagnostics.AddError("failed assigning connectivity templates", err.Error())
return
}
// clear any undesired CTs
err = bp.DelApplicationPointConnectivityTemplates(ctx, apstra.ObjectId(plan.ApplicationPointId.ValueString()), delIds)
if err != nil {
resp.Diagnostics.AddError("failed clearing connectivity template assignments", err.Error())
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (o *resourceDatacenterConnectivityTemplateAssignment) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Retrieve values from state
var state blueprint.ConnectivityTemplatesAssignment
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
// get a client for the datacenter reference design
bp, err := o.getBpClientFunc(ctx, state.BlueprintId.ValueString())
if err != nil {
if utils.IsApstra404(err) {
return // 404 is okay
}
resp.Diagnostics.AddError("failed to create blueprint client", err.Error())
return
}
// Lock the blueprint mutex.
err = o.lockFunc(ctx, state.BlueprintId.ValueString())
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("failed locking blueprint %q mutex", state.BlueprintId.ValueString()),
err.Error())
return
}
var delIds []apstra.ObjectId
resp.Diagnostics.Append(state.ConnectivityTemplateIds.ElementsAs(ctx, &delIds, false)...)
if resp.Diagnostics.HasError() {
return
}
err = bp.DelApplicationPointConnectivityTemplates(ctx, apstra.ObjectId(state.ApplicationPointId.ValueString()), delIds)
if err != nil {
if utils.IsApstra404(err) {
return // 404 is okay
}
resp.Diagnostics.AddError("failed clearing connectivity template assignments", err.Error())
return
}
}
func (o *resourceDatacenterConnectivityTemplateAssignment) setBpClientFunc(f func(context.Context, string) (*apstra.TwoStageL3ClosClient, error)) {
o.getBpClientFunc = f
}
func (o *resourceDatacenterConnectivityTemplateAssignment) setBpLockFunc(f func(context.Context, string) error) {
o.lockFunc = f
}