Skip to content

Commit 89f0fe4

Browse files
committed
fix(resource): vip, instance_offering, image produced inconsisternt result after apply
1 parent 0418809 commit 89f0fe4

3 files changed

Lines changed: 87 additions & 48 deletions

File tree

zstack/provider/resource_zstack_image.go

Lines changed: 77 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ type imageResourceModel struct {
4040
Architecture types.String `tfsdk:"architecture"`
4141
Virtio types.Bool `tfsdk:"virtio"`
4242
Type types.String `tfsdk:"type"`
43-
Marketplace types.Bool `tfsdk:"marketplace"`
44-
BootMode types.String `tfsdk:"boot_mode"`
43+
//Marketplace types.Bool `tfsdk:"marketplace"`
44+
BootMode types.String `tfsdk:"boot_mode"`
4545
}
4646

4747
// Configure implements resource.ResourceWithConfigure.
@@ -115,53 +115,64 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
115115
return
116116
}
117117
}
118-
119-
if imagePlan.Marketplace.ValueBool() {
120-
121-
// for marketplace image, if the image with same name status is Ready and state is Enabled, then skip add it.
122-
qparam := param.NewQueryParam()
123-
qparam.AddQ("name=" + imagePlan.Name.ValueString())
124-
//qparam.AddQ("url=" + imagePlan.Url.ValueString())
125-
qparam.AddQ("architecture=" + imagePlan.Architecture.ValueString())
126-
qparam.AddQ("status=Ready")
127-
qparam.AddQ("state=Enabled")
128-
129-
images, err := r.client.QueryImage(qparam)
130-
if err != nil {
131-
resp.Diagnostics.AddError(
132-
"fail to get image",
133-
fmt.Sprintf("fail to get image: %v", err),
134-
)
135-
return
136-
}
137-
138-
tflog.Info(ctx, fmt.Sprintf("find %d images", len(images)))
139-
for _, image := range images {
140-
for _, backupStorageRef := range image.BackupStorageRefs {
141-
backupStorageUuids = removeStringFromSlice(backupStorageUuids, backupStorageRef.BackupStorageUuid)
118+
/*
119+
if imagePlan.Marketplace.ValueBool() {
120+
121+
// for marketplace image, if the image with same name status is Ready and state is Enabled, then skip add it.
122+
qparam := param.NewQueryParam()
123+
qparam.AddQ("name=" + imagePlan.Name.ValueString())
124+
//qparam.AddQ("url=" + imagePlan.Url.ValueString())
125+
qparam.AddQ("architecture=" + imagePlan.Architecture.ValueString())
126+
qparam.AddQ("status=Ready")
127+
qparam.AddQ("state=Enabled")
128+
129+
images, err := r.client.QueryImage(qparam)
130+
if err != nil {
131+
resp.Diagnostics.AddError(
132+
"fail to get image",
133+
fmt.Sprintf("fail to get image: %v", err),
134+
)
135+
return
142136
}
143137
144-
if len(backupStorageUuids) == 0 {
145-
tflog.Info(ctx, "image has been imported to all backup storage")
146-
147-
imagePlan.Uuid = types.StringValue(image.UUID)
148-
imagePlan.Name = types.StringValue(image.Name)
149-
imagePlan.Description = types.StringValue(image.Description)
150-
imagePlan.Url = types.StringValue(image.Url)
151-
imagePlan.GuestOsType = types.StringValue(image.GuestOsType)
152-
imagePlan.System = types.StringValue(image.System)
153-
imagePlan.Platform = types.StringValue(image.Platform)
154-
imagePlan.Type = types.StringValue(image.Type)
155-
imagePlan.LastUpdated = types.StringValue(image.LastOpDate.String())
156-
ctx = tflog.SetField(ctx, "url", image.Url)
157-
diags = resp.State.Set(ctx, imagePlan)
158-
resp.Diagnostics.Append(diags...)
159-
return
138+
tflog.Info(ctx, fmt.Sprintf("find %d images", len(images)))
139+
for _, image := range images {
140+
for _, backupStorageRef := range image.BackupStorageRefs {
141+
backupStorageUuids = removeStringFromSlice(backupStorageUuids, backupStorageRef.BackupStorageUuid)
142+
}
143+
144+
if len(backupStorageUuids) == 0 {
145+
tflog.Info(ctx, "image has been imported to all backup storage")
146+
147+
imagePlan.Uuid = types.StringValue(image.UUID)
148+
imagePlan.Name = types.StringValue(image.Name)
149+
imagePlan.Description = types.StringValue(image.Description)
150+
imagePlan.Url = types.StringValue(image.Url)
151+
imagePlan.GuestOsType = types.StringValue(image.GuestOsType)
152+
imagePlan.System = types.StringValue(image.System)
153+
imagePlan.Platform = types.StringValue(image.Platform)
154+
imagePlan.Type = types.StringValue(image.Type)
155+
imagePlan.LastUpdated = types.StringValue(image.LastOpDate.String())
156+
ctx = tflog.SetField(ctx, "url", image.Url)
157+
diags = resp.State.Set(ctx, imagePlan)
158+
resp.Diagnostics.Append(diags...)
159+
return
160+
}
160161
}
162+
163+
tflog.Info(ctx, fmt.Sprintf("unimported backupStorageUuids: %v", backupStorageUuids))
164+
systemTags = append(systemTags, "marketplace::true")
161165
}
166+
*/
162167

163-
tflog.Info(ctx, fmt.Sprintf("unimported backupStorageUuids: %v", backupStorageUuids))
164-
systemTags = append(systemTags, "marketplace::true")
168+
if imagePlan.Description.IsNull() {
169+
imagePlan.Description = types.StringValue("")
170+
}
171+
if imagePlan.GuestOsType.IsNull() {
172+
imagePlan.GuestOsType = types.StringValue("Linux")
173+
}
174+
if imagePlan.Platform.IsNull() {
175+
imagePlan.Platform = types.StringValue("Linux")
165176
}
166177

167178
tflog.Info(ctx, "Configuring ZStack client")
@@ -261,6 +272,16 @@ func (r *imageResource) Read(ctx context.Context, req resource.ReadRequest, resp
261272
state.LastUpdated = types.StringValue(image.LastOpDate.GoString())
262273
//state.Description = types.StringValue(image.Description)
263274

275+
if !state.Description.IsNull() {
276+
state.Description = types.StringValue(image.Description)
277+
}
278+
if !state.GuestOsType.IsNull() {
279+
state.GuestOsType = types.StringValue(image.GuestOsType)
280+
}
281+
if !state.Platform.IsNull() {
282+
state.Platform = types.StringValue(image.Platform)
283+
}
284+
264285
diags = resp.State.Set(ctx, &state)
265286
resp.Diagnostics.Append(diags...)
266287
if resp.Diagnostics.HasError() {
@@ -289,6 +310,7 @@ func (r *imageResource) Schema(_ context.Context, req resource.SchemaRequest, re
289310
},
290311
"description": schema.StringAttribute{
291312
Optional: true,
313+
Computed: true,
292314
Description: "A description of the image, providing additional context or details.",
293315
},
294316
"url": schema.StringAttribute{
@@ -301,6 +323,7 @@ func (r *imageResource) Schema(_ context.Context, req resource.SchemaRequest, re
301323
},
302324
"guest_os_type": schema.StringAttribute{
303325
Optional: true,
326+
Computed: true,
304327
Description: "The guest operating system type that the image is optimized for.",
305328
},
306329
"system": schema.StringAttribute{
@@ -309,6 +332,7 @@ func (r *imageResource) Schema(_ context.Context, req resource.SchemaRequest, re
309332
},
310333
"platform": schema.StringAttribute{
311334
Optional: true,
335+
Computed: true,
312336
Description: "The platform that the image is intended for, such as 'Linux', 'Windows', or others.",
313337
},
314338
"format": schema.StringAttribute{
@@ -332,10 +356,12 @@ func (r *imageResource) Schema(_ context.Context, req resource.SchemaRequest, re
332356
Optional: true,
333357
Description: "Indicates if the VirtIO drivers are required for the image.",
334358
},
335-
"marketplace": schema.BoolAttribute{
336-
Optional: true,
337-
Description: "Specifies whether the image is from a marketplace.",
338-
},
359+
/*
360+
"marketplace": schema.BoolAttribute{
361+
Optional: true,
362+
Description: "Specifies whether the image is from a marketplace.",
363+
},
364+
*/
339365
"boot_mode": schema.StringAttribute{
340366
Optional: true,
341367
Description: "The boot mode supported by the image, such as 'Legacy' or 'UEFI'.",
@@ -348,6 +374,8 @@ func (r *imageResource) Update(ctx context.Context, req resource.UpdateRequest,
348374

349375
}
350376

377+
/*
378+
351379
func removeStringFromSlice(slice []string, s string) []string {
352380
for i, v := range slice {
353381
if v == s {
@@ -357,3 +385,4 @@ func removeStringFromSlice(slice []string, s string) []string {
357385
}
358386
return slice
359387
}
388+
*/

zstack/provider/resource_zstack_instance_offering.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ func (r *instanceOfferingResource) Create(ctx context.Context, req resource.Crea
6666

6767
offerType := "UserVm"
6868
tflog.Info(ctx, "Configuring ZStack client")
69+
if plan.Description.IsNull() {
70+
plan.Description = types.StringValue("")
71+
}
72+
6973
offerParam := param.CreateInstanceOfferingParam{
7074
BaseParam: param.BaseParam{},
7175
Params: param.CreateInstanceOfferingDetailParam{
@@ -181,6 +185,7 @@ func (r *instanceOfferingResource) Schema(_ context.Context, req resource.Schema
181185
},
182186
"description": schema.StringAttribute{
183187
Optional: true,
188+
Computed: true,
184189
Description: "A description of the instance offering, providing additional context or details about the configuration.",
185190
},
186191
"cpu_num": schema.Int64Attribute{

zstack/provider/resource_zstack_vip.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func (r *vipResource) Schema(_ context.Context, request resource.SchemaRequest,
7373
},
7474
"description": schema.StringAttribute{
7575
Optional: true,
76+
Computed: true,
7677
Description: "A description for the VIP network service.",
7778
},
7879
"l3_network_uuid": schema.StringAttribute{
@@ -100,6 +101,10 @@ func (r *vipResource) Create(ctx context.Context, request resource.CreateRequest
100101
return
101102
}
102103

104+
if plan.Description.IsNull() {
105+
plan.Description = types.StringValue("")
106+
}
107+
103108
if r.client == nil {
104109
response.Diagnostics.AddWarning("Client Not Configured", "The client was not properly configured.")
105110
return

0 commit comments

Comments
 (0)