Skip to content

Commit db3961c

Browse files
committed
fix
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent a29245e commit db3961c

File tree

1 file changed

+46
-64
lines changed

1 file changed

+46
-64
lines changed

cloudstack/resource_cloudstack_service_offering.go

Lines changed: 46 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func resourceCloudStackServiceOffering() *schema.Resource {
103103
Type: schema.TypeBool,
104104
Optional: true,
105105
ForceNew: true,
106-
Default: false,
106+
Computed: true,
107107
},
108108
"min_cpu_number": {
109109
Description: "Minimum number of CPU cores allowed",
@@ -151,12 +151,15 @@ func resourceCloudStackServiceOfferingCreate(d *schema.ResourceData, meta interf
151151

152152
// Create a new parameter struct
153153
p := cs.ServiceOffering.NewCreateServiceOfferingParams(display_text, name)
154-
if v, ok := d.GetOk("cpu_number"); ok {
155-
p.SetCpunumber(v.(int))
154+
155+
cpuNumber, cpuNumberOk := d.GetOk("cpu_number")
156+
if cpuNumberOk {
157+
p.SetCpunumber(cpuNumber.(int))
156158
}
157159

158-
if v, ok := d.GetOk("cpu_speed"); ok {
159-
p.SetCpuspeed(v.(int))
160+
cpuSpeed, cpuSpeedOk := d.GetOk("cpu_speed")
161+
if cpuSpeedOk {
162+
p.SetCpuspeed(cpuSpeed.(int))
160163
}
161164

162165
if v, ok := d.GetOk("host_tags"); ok {
@@ -167,8 +170,9 @@ func resourceCloudStackServiceOfferingCreate(d *schema.ResourceData, meta interf
167170
p.SetLimitcpuuse(v.(bool))
168171
}
169172

170-
if v, ok := d.GetOk("memory"); ok {
171-
p.SetMemory(v.(int))
173+
memory, memoryOk := d.GetOk("memory")
174+
if memoryOk {
175+
p.SetMemory(memory.(int))
172176
}
173177

174178
if v, ok := d.GetOk("offer_ha"); ok {
@@ -179,9 +183,14 @@ func resourceCloudStackServiceOfferingCreate(d *schema.ResourceData, meta interf
179183
p.SetStoragetype(v.(string))
180184
}
181185

186+
customized := false
182187
if v, ok := d.GetOk("customized"); ok {
183-
p.SetCustomized(v.(bool))
188+
customized = v.(bool)
184189
}
190+
if !cpuNumberOk && !cpuSpeedOk && !memoryOk {
191+
customized = true
192+
}
193+
p.SetCustomized(customized)
185194

186195
if v, ok := d.GetOk("min_cpu_number"); ok {
187196
p.SetMincpunumber(v.(int))
@@ -239,62 +248,22 @@ func resourceCloudStackServiceOfferingRead(d *schema.ResourceData, meta interfac
239248
d.SetId(s.Id)
240249

241250
fields := map[string]interface{}{
242-
"name": s.Name,
243-
"display_text": s.Displaytext,
244-
"cpu_number": s.Cpunumber,
245-
"cpu_speed": s.Cpuspeed,
246-
"host_tags": s.Hosttags,
247-
"limit_cpu_use": s.Limitcpuuse,
248-
"memory": s.Memory,
249-
"offer_ha": s.Offerha,
250-
"storage_type": s.Storagetype,
251-
"customized": s.Iscustomized,
252-
"min_cpu_number": func() interface{} {
253-
if s.Serviceofferingdetails == nil {
254-
return nil
255-
}
256-
if v, ok := s.Serviceofferingdetails["mincpunumber"]; ok {
257-
if i, err := strconv.Atoi(v); err == nil {
258-
return i
259-
}
260-
}
261-
return nil
262-
}(),
263-
"max_cpu_number": func() interface{} {
264-
if s.Serviceofferingdetails == nil {
265-
return nil
266-
}
267-
if v, ok := s.Serviceofferingdetails["maxcpunumber"]; ok {
268-
if i, err := strconv.Atoi(v); err == nil {
269-
return i
270-
}
271-
}
272-
return nil
273-
}(),
274-
"min_memory": func() interface{} {
275-
if s.Serviceofferingdetails == nil {
276-
return nil
277-
}
278-
if v, ok := s.Serviceofferingdetails["minmemory"]; ok {
279-
if i, err := strconv.Atoi(v); err == nil {
280-
return i
281-
}
282-
}
283-
return nil
284-
}(),
285-
"max_memory": func() interface{} {
286-
if s.Serviceofferingdetails == nil {
287-
return nil
288-
}
289-
if v, ok := s.Serviceofferingdetails["maxmemory"]; ok {
290-
if i, err := strconv.Atoi(v); err == nil {
291-
return i
292-
}
293-
}
294-
return nil
295-
}(),
296-
"encrypt_root": s.Encryptroot,
297-
"storage_tags": s.Storagetags,
251+
"name": s.Name,
252+
"display_text": s.Displaytext,
253+
"cpu_number": s.Cpunumber,
254+
"cpu_speed": s.Cpuspeed,
255+
"host_tags": s.Hosttags,
256+
"limit_cpu_use": s.Limitcpuuse,
257+
"memory": s.Memory,
258+
"offer_ha": s.Offerha,
259+
"storage_type": s.Storagetype,
260+
"customized": s.Iscustomized,
261+
"min_cpu_number": getIntFromDetails(s.Serviceofferingdetails, "mincpunumber"),
262+
"max_cpu_number": getIntFromDetails(s.Serviceofferingdetails, "maxcpunumber"),
263+
"min_memory": getIntFromDetails(s.Serviceofferingdetails, "minmemory"),
264+
"max_memory": getIntFromDetails(s.Serviceofferingdetails, "maxmemory"),
265+
"encrypt_root": s.Encryptroot,
266+
"storage_tags": s.Storagetags,
298267
}
299268

300269
for k, v := range fields {
@@ -399,3 +368,16 @@ func resourceCloudStackServiceOfferingDelete(d *schema.ResourceData, meta interf
399368

400369
return nil
401370
}
371+
372+
// getIntFromDetails extracts an integer value from the service offering details map.
373+
func getIntFromDetails(details map[string]string, key string) interface{} {
374+
if details == nil {
375+
return nil
376+
}
377+
if val, ok := details[key]; ok {
378+
if i, err := strconv.Atoi(val); err == nil {
379+
return i
380+
}
381+
}
382+
return nil
383+
}

0 commit comments

Comments
 (0)