generated from hashicorp/terraform-provider-scaffolding-framework
/
camunda_cluster_resource.go
265 lines (213 loc) · 7.69 KB
/
camunda_cluster_resource.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package provider
import (
"context"
"fmt"
"net/http"
"time"
console "github.com/camunda-community-hub/console-customer-api-go"
"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/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
)
var _ resource.Resource = &CamundaClusterResource{}
var _ resource.ResourceWithImportState = &CamundaClusterResource{}
type camundaClusterData struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Channel types.String `tfsdk:"channel"`
Region types.String `tfsdk:"region"`
PlanType types.String `tfsdk:"plan_type"`
Generation types.String `tfsdk:"generation"`
}
type CamundaClusterResource struct {
provider *CamundaCloudProvider
}
func NewCamundaClusterResource() resource.Resource {
return &CamundaClusterResource{}
}
func (r *CamundaClusterResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_cluster"
}
func (r *CamundaClusterResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Manage a cluster on Camunda SaaS",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Cluster ID",
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the cluster",
Required: true,
},
"channel": schema.StringAttribute{
MarkdownDescription: "Channel",
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"region": schema.StringAttribute{
MarkdownDescription: "Region",
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"plan_type": schema.StringAttribute{
MarkdownDescription: "Plan type",
Required: true,
},
"generation": schema.StringAttribute{
MarkdownDescription: "Generation",
Required: true,
},
},
}
}
func (r *CamundaClusterResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Provider not yet configured
if req.ProviderData == nil {
return
}
provider, ok := req.ProviderData.(*CamundaCloudProvider)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *incidentio.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.provider = provider
}
func (r *CamundaClusterResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data camundaClusterData
diags := req.Plan.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
newClusterConfiguration := console.CreateClusterBody{
Name: data.Name.ValueString(),
PlanTypeId: data.PlanType.ValueString(),
ChannelId: data.Channel.ValueString(),
GenerationId: data.Generation.ValueString(),
RegionId: data.Region.ValueString(),
}
ctx = context.WithValue(ctx, console.ContextAccessToken, r.provider.accessToken)
inline, _, err := r.provider.client.DefaultAPI.CreateCluster(ctx).
CreateClusterBody(newClusterConfiguration).
Execute()
if err != nil {
resp.Diagnostics.AddError(
"Unable to create cluster",
fmt.Sprintf("Unable to create cluster, got error: %s", formatClientError(err)),
)
return
}
clusterId := inline.GetClusterId()
data.Id = types.StringValue(clusterId)
tflog.Info(ctx, "Camunda cluster created", map[string]interface{}{
"clusterID": data.Id,
})
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
// Creating a cluster takes some time, wait until it's marked healthy.
createState := &retry.StateChangeConf{
// The cluster states that we need to keep waiting on
Pending: []string{
string(console.CLUSTERCOMPONENTSTATUS_CREATING),
string(console.CLUSTERCOMPONENTSTATUS_UPDATING),
},
// The cluster states that we would like to reach
Target: []string{
string(console.CLUSTERCOMPONENTSTATUS_HEALTHY),
},
// How many times the target state has to be reached to continue.
ContinuousTargetOccurence: 2,
Refresh: func() (interface{}, string, error) {
cluster, _, err := r.provider.client.DefaultAPI.
GetCluster(ctx, clusterId).
Execute()
if err != nil {
return nil, "", err
}
tflog.Info(ctx, "Camunda cluster status", map[string]interface{}{
"clusterID": cluster.Uuid,
"clusterStatus": cluster.Status.Ready,
})
return cluster, string(cluster.Status.Ready), nil
},
Timeout: 30 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 5 * time.Second,
}
_, err = createState.WaitForStateContext(ctx)
if err != nil {
resp.Diagnostics.AddError(
"Unable to create cluster",
fmt.Sprintf("Cluster %s never got healthy; got error: %s", clusterId, err),
)
return
}
}
func (r *CamundaClusterResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data camundaClusterData
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx = context.WithValue(ctx, console.ContextAccessToken, r.provider.accessToken)
cluster, response, err := r.provider.client.DefaultAPI.GetCluster(ctx, data.Id.ValueString()).Execute()
if err != nil && response.StatusCode == http.StatusNotFound {
resp.State.RemoveResource(ctx)
return
}
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
fmt.Sprintf("Unable to read cluster ID=%s, got error: %s", data.Id.ValueString(), formatClientError(err)),
)
return
}
data.Name = types.StringValue(cluster.Name)
data.Channel = types.StringValue(cluster.Channel.Uuid)
data.Region = types.StringValue(cluster.Region.Uuid)
data.PlanType = types.StringValue(cluster.PlanType.Uuid)
data.Generation = types.StringValue(cluster.Generation.Uuid)
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r *CamundaClusterResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data camundaClusterData
diags := req.Plan.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r *CamundaClusterResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data camundaClusterData
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx = context.WithValue(ctx, console.ContextAccessToken, r.provider.accessToken)
_, err := r.provider.client.DefaultAPI.DeleteCluster(ctx, data.Id.ValueString()).Execute()
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
fmt.Sprintf("Unable to delete cluster ID=%s, got error: %s", data.Id.ValueString(), formatClientError(err)),
)
return
}
}
func (r *CamundaClusterResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}