-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_property_set.go
172 lines (153 loc) · 5.1 KB
/
resource_property_set.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
package tfapstra
import (
"context"
"fmt"
"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/Juniper/terraform-provider-apstra/apstra/design"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/terraform-plugin-framework/path"
"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 = &resourcePropertySet{}
var _ resourceWithSetClient = &resourcePropertySet{}
type resourcePropertySet struct {
client *apstra.Client
}
func (o *resourcePropertySet) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_property_set"
}
func (o *resourcePropertySet) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
configureResource(ctx, o, req, resp)
}
func (o *resourcePropertySet) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: docCategoryDesign + "This resource creates a Property Set in the Apstra Design tab.",
Attributes: design.PropertySet{}.ResourceAttributes(),
}
}
func (o *resourcePropertySet) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var plan design.PropertySet
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
// Convert the plan into an API Request
psRequest := plan.Request(ctx, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
psid, err := o.client.CreatePropertySet(ctx, psRequest)
if err != nil {
resp.Diagnostics.AddError("error creating new PropertySet", err.Error())
return
}
plan.Id = types.StringValue(psid.String())
plan.Blueprints = types.SetNull(types.StringType)
k, err := utils.GetKeysFromJSON(plan.Data)
if err != nil {
resp.Diagnostics.AddError("failed to load keys", err.Error())
return
}
plan.Keys = types.SetValueMust(types.StringType, k)
// set state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (o *resourcePropertySet) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state design.PropertySet
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
var err error
var api *apstra.PropertySet
api, err = o.client.GetPropertySet(ctx, apstra.ObjectId(state.Id.ValueString()))
if utils.IsApstra404(err) {
resp.State.RemoveResource(ctx)
return
}
if err != nil { // catch errors other than 404 from above
resp.Diagnostics.AddError("Error retrieving PropertySet", err.Error())
return
}
// create new state object
var newstate design.PropertySet
newstate.Id = types.StringValue(api.Id.String())
newstate.LoadApiData(ctx, api.Data, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
if utils.JSONEqual(newstate.Data, state.Data, &resp.Diagnostics) {
newstate.Data = state.Data
}
if resp.Diagnostics.HasError() {
return
}
// Set state
//
resp.Diagnostics.Append(resp.State.Set(ctx, &newstate)...)
}
// Update resource
func (o *resourcePropertySet) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Get plan values
var plan design.PropertySet
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
psReq := plan.Request(ctx, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
// Update Property Set
err := o.client.UpdatePropertySet(ctx, apstra.ObjectId(plan.Id.ValueString()), psReq)
if err != nil {
resp.Diagnostics.AddError("error updating Property Set", err.Error())
return
}
// read the state fromm the API
api, err := o.client.GetPropertySet(ctx, apstra.ObjectId(plan.Id.ValueString()))
if err != nil {
if utils.IsApstra404(err) {
resp.Diagnostics.AddAttributeError(
path.Root("id"),
"PropertySet not found",
fmt.Sprintf("just-updated PropertySet with ID %q not found.", plan.Id.ValueString()))
return
}
resp.Diagnostics.AddError("error updating property set", err.Error())
return
}
// save the old data
d := plan.Data
plan.LoadApiData(ctx, api.Data, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
if utils.JSONEqual(plan.Data, d, &resp.Diagnostics) {
plan.Data = d
}
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
// Delete resource
func (o *resourcePropertySet) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state design.PropertySet
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
// Delete Property Set by calling API
err := o.client.DeletePropertySet(ctx, apstra.ObjectId(state.Id.ValueString()))
if err != nil {
if utils.IsApstra404(err) {
return // 404 is okay
}
resp.Diagnostics.AddError("error deleting Property Set", err.Error())
return
}
}
func (o *resourcePropertySet) setClient(client *apstra.Client) {
o.client = client
}