Skip to content

Commit 4a18d91

Browse files
committed
sdk framework rewrite
1 parent a653531 commit 4a18d91

12 files changed

+2053
-47
lines changed

cloudstack/provider_v6.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ func (p *CloudstackProvider) ConfigValidators(ctx context.Context) []provider.Co
147147
}
148148

149149
func (p *CloudstackProvider) Resources(ctx context.Context) []func() resource.Resource {
150-
return []func() resource.Resource{}
150+
return []func() resource.Resource{
151+
NewserviceOfferingUnconstrainedResource,
152+
NewserviceOfferingConstrainedResource,
153+
NewserviceOfferingFixedResource,
154+
}
151155
}
152156

153157
func (p *CloudstackProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
package cloudstack
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/apache/cloudstack-go/v2/cloudstack"
8+
"github.com/hashicorp/terraform-plugin-framework/resource"
9+
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
10+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int32planmodifier"
11+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
12+
"github.com/hashicorp/terraform-plugin-framework/types"
13+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
14+
)
15+
16+
var (
17+
_ resource.Resource = &serviceOfferingConstrainedResource{}
18+
_ resource.ResourceWithConfigure = &serviceOfferingConstrainedResource{}
19+
)
20+
21+
func NewserviceOfferingConstrainedResource() resource.Resource {
22+
return &serviceOfferingConstrainedResource{}
23+
}
24+
25+
type serviceOfferingConstrainedResource struct {
26+
client *cloudstack.CloudStackClient
27+
}
28+
29+
func (r *serviceOfferingConstrainedResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
30+
resp.Schema = schema.Schema{
31+
Attributes: serviceOfferingMergeCommonSchema(map[string]schema.Attribute{
32+
"cpu_speed": schema.Int32Attribute{
33+
Required: true,
34+
PlanModifiers: []planmodifier.Int32{
35+
int32planmodifier.RequiresReplace(),
36+
},
37+
},
38+
"max_cpu_number": schema.Int32Attribute{
39+
Required: true,
40+
PlanModifiers: []planmodifier.Int32{
41+
int32planmodifier.RequiresReplace(),
42+
},
43+
},
44+
"max_memory": schema.Int32Attribute{
45+
Required: true,
46+
PlanModifiers: []planmodifier.Int32{
47+
int32planmodifier.RequiresReplace(),
48+
},
49+
},
50+
"min_cpu_number": schema.Int32Attribute{
51+
Required: true,
52+
PlanModifiers: []planmodifier.Int32{
53+
int32planmodifier.RequiresReplace(),
54+
},
55+
},
56+
"min_memory": schema.Int32Attribute{
57+
Required: true,
58+
PlanModifiers: []planmodifier.Int32{
59+
int32planmodifier.RequiresReplace(),
60+
},
61+
},
62+
}),
63+
}
64+
}
65+
66+
func (r *serviceOfferingConstrainedResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
67+
//
68+
var plan serviceOfferingConstrainedResourceModel
69+
var planDiskQosHypervisor ServiceOfferingDiskQosHypervisor
70+
var planDiskOffering ServiceOfferingDiskOffering
71+
var planDiskQosStorage ServiceOfferingDiskQosStorage
72+
73+
//
74+
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
75+
if !plan.ServiceOfferingDiskQosHypervisor.IsNull() {
76+
resp.Diagnostics.Append(plan.ServiceOfferingDiskQosHypervisor.As(ctx, &planDiskQosHypervisor, basetypes.ObjectAsOptions{})...)
77+
}
78+
if !plan.ServiceOfferingDiskOffering.IsNull() {
79+
resp.Diagnostics.Append(plan.ServiceOfferingDiskOffering.As(ctx, &planDiskOffering, basetypes.ObjectAsOptions{})...)
80+
}
81+
if !plan.ServiceOfferingDiskQosStorage.IsNull() {
82+
resp.Diagnostics.Append(plan.ServiceOfferingDiskQosStorage.As(ctx, &planDiskQosStorage, basetypes.ObjectAsOptions{})...)
83+
}
84+
if resp.Diagnostics.HasError() {
85+
return
86+
}
87+
88+
// common params
89+
params := r.client.ServiceOffering.NewCreateServiceOfferingParams(plan.DisplayText.ValueString(), plan.Name.ValueString())
90+
plan.commonCreateParams(params)
91+
planDiskQosHypervisor.commonCreateParams(params)
92+
planDiskOffering.commonCreateParams(params)
93+
planDiskQosStorage.commonCreateParams(params)
94+
95+
// resource specific params
96+
if !plan.CpuSpeed.IsNull() {
97+
params.SetCpuspeed(int(plan.CpuSpeed.ValueInt32()))
98+
}
99+
if !plan.MaxCpuNumber.IsNull() {
100+
params.SetMaxcpunumber(int(plan.MaxCpuNumber.ValueInt32()))
101+
}
102+
if !plan.MaxMemory.IsNull() {
103+
params.SetMaxmemory(int(plan.MaxMemory.ValueInt32()))
104+
}
105+
if !plan.MinCpuNumber.IsNull() {
106+
params.SetMincpunumber(int(plan.MinCpuNumber.ValueInt32()))
107+
}
108+
if !plan.MinMemory.IsNull() {
109+
params.SetMinmemory(int(plan.MinMemory.ValueInt32()))
110+
}
111+
112+
// create offering
113+
cs, err := r.client.ServiceOffering.CreateServiceOffering(params)
114+
if err != nil {
115+
resp.Diagnostics.AddError(
116+
"Error creating service offering",
117+
"Could not create constrained offering, unexpected error: "+err.Error(),
118+
)
119+
return
120+
}
121+
122+
//
123+
plan.Id = types.StringValue(cs.Id)
124+
125+
//
126+
resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
127+
}
128+
129+
func (r *serviceOfferingConstrainedResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
130+
//
131+
var state serviceOfferingConstrainedResourceModel
132+
var stateDiskQosHypervisor ServiceOfferingDiskQosHypervisor
133+
var stateDiskOffering ServiceOfferingDiskOffering
134+
var stateDiskQosStorage ServiceOfferingDiskQosStorage
135+
136+
//
137+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
138+
if !state.ServiceOfferingDiskQosHypervisor.IsNull() {
139+
resp.Diagnostics.Append(state.ServiceOfferingDiskQosHypervisor.As(ctx, &stateDiskQosHypervisor, basetypes.ObjectAsOptions{})...)
140+
}
141+
if !state.ServiceOfferingDiskOffering.IsNull() {
142+
resp.Diagnostics.Append(state.ServiceOfferingDiskOffering.As(ctx, &stateDiskOffering, basetypes.ObjectAsOptions{})...)
143+
}
144+
if !state.ServiceOfferingDiskQosStorage.IsNull() {
145+
resp.Diagnostics.Append(state.ServiceOfferingDiskQosStorage.As(ctx, &stateDiskQosStorage, basetypes.ObjectAsOptions{})...)
146+
}
147+
if resp.Diagnostics.HasError() {
148+
return
149+
}
150+
151+
//
152+
cs, _, err := r.client.ServiceOffering.GetServiceOfferingByID(state.Id.ValueString())
153+
if err != nil {
154+
resp.Diagnostics.AddError(
155+
"Error reading service offering",
156+
"Could not read constrained service offering, unexpected error: "+err.Error(),
157+
)
158+
return
159+
}
160+
161+
// resource specific
162+
if cs.Cpuspeed > 0 {
163+
state.CpuSpeed = types.Int32Value(int32(cs.Cpuspeed))
164+
}
165+
// These fields arent returned from list
166+
// max_cpu_number
167+
// max_memory
168+
// min_cpu_number
169+
// min_memory
170+
171+
//
172+
state.commonRead(ctx, cs)
173+
stateDiskQosHypervisor.commonRead(ctx, cs)
174+
stateDiskOffering.commonRead(ctx, cs)
175+
stateDiskQosStorage.commonRead(ctx, cs)
176+
if resp.Diagnostics.HasError() {
177+
return
178+
}
179+
180+
//
181+
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
182+
183+
}
184+
185+
// Update updates the resource and sets the updated Terraform state on success.
186+
func (r *serviceOfferingConstrainedResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
187+
//
188+
var state serviceOfferingConstrainedResourceModel
189+
190+
//
191+
resp.Diagnostics.Append(req.Plan.Get(ctx, &state)...)
192+
if resp.Diagnostics.HasError() {
193+
return
194+
}
195+
196+
//
197+
params := r.client.ServiceOffering.NewUpdateServiceOfferingParams(state.Id.ValueString())
198+
state.commonUpdateParams(ctx, params)
199+
200+
//
201+
cs, err := r.client.ServiceOffering.UpdateServiceOffering(params)
202+
if err != nil {
203+
resp.Diagnostics.AddError(
204+
"Error updating service offering",
205+
"Could not update constrained service offering, unexpected error: "+err.Error(),
206+
)
207+
return
208+
}
209+
210+
//
211+
state.commonUpdate(ctx, cs)
212+
if resp.Diagnostics.HasError() {
213+
return
214+
}
215+
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
216+
}
217+
218+
// Delete deletes the resource and removes the Terraform state on success.
219+
func (r *serviceOfferingConstrainedResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
220+
//
221+
var state serviceOfferingConstrainedResourceModel
222+
223+
//
224+
diags := req.State.Get(ctx, &state)
225+
resp.Diagnostics.Append(diags...)
226+
if resp.Diagnostics.HasError() {
227+
return
228+
}
229+
230+
// Delete the service offering
231+
_, err := r.client.ServiceOffering.DeleteServiceOffering(r.client.ServiceOffering.NewDeleteServiceOfferingParams(state.Id.ValueString()))
232+
if err != nil {
233+
resp.Diagnostics.AddError(
234+
"Error deleting service offering",
235+
"Could not delete constrained offering, unexpected error: "+err.Error(),
236+
)
237+
return
238+
}
239+
}
240+
241+
// Configure adds the provider configured client to the resource.
242+
func (r *serviceOfferingConstrainedResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
243+
// Add a nil check when handling ProviderData because Terraform
244+
// sets that data after it calls the ConfigureProvider RPC.
245+
if req.ProviderData == nil {
246+
return
247+
}
248+
249+
client, ok := req.ProviderData.(*cloudstack.CloudStackClient)
250+
251+
if !ok {
252+
resp.Diagnostics.AddError(
253+
"Unexpected Data Source Configure Type",
254+
fmt.Sprintf("Expected *cloudstack.CloudStackClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
255+
)
256+
return
257+
}
258+
259+
r.client = client
260+
}
261+
262+
// Metadata returns the resource type name.
263+
func (r *serviceOfferingConstrainedResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
264+
resp.TypeName = req.ProviderTypeName + "_service_offering_constrained"
265+
}

0 commit comments

Comments
 (0)