@@ -22,6 +22,7 @@ package cloudstack
2222import (
2323 "fmt"
2424 "log"
25+ "strconv"
2526
2627 "github.com/apache/cloudstack-go/v2/cloudstack"
2728 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -97,6 +98,49 @@ func resourceCloudStackServiceOffering() *schema.Resource {
9798 return
9899 },
99100 },
101+ "customized" : {
102+ Description : "Whether service offering allows custom CPU/memory or not" ,
103+ Type : schema .TypeBool ,
104+ Optional : true ,
105+ ForceNew : true ,
106+ Default : false ,
107+ },
108+ "min_cpu_number" : {
109+ Description : "Minimum number of CPU cores allowed" ,
110+ Type : schema .TypeInt ,
111+ Optional : true ,
112+ ForceNew : true ,
113+ },
114+ "max_cpu_number" : {
115+ Description : "Maximum number of CPU cores allowed" ,
116+ Type : schema .TypeInt ,
117+ Optional : true ,
118+ ForceNew : true ,
119+ },
120+ "min_memory" : {
121+ Description : "Minimum memory allowed (MB)" ,
122+ Type : schema .TypeInt ,
123+ Optional : true ,
124+ ForceNew : true ,
125+ },
126+ "max_memory" : {
127+ Description : "Maximum memory allowed (MB)" ,
128+ Type : schema .TypeInt ,
129+ Optional : true ,
130+ ForceNew : true ,
131+ },
132+ "encrypt_root" : {
133+ Description : "Encrypt the root disk for VMs using this service offering" ,
134+ Type : schema .TypeBool ,
135+ Optional : true ,
136+ ForceNew : true ,
137+ },
138+ "storage_tags" : {
139+ Description : "Storage tags to associate with the service offering" ,
140+ Type : schema .TypeString ,
141+ Optional : true ,
142+ ForceNew : true ,
143+ },
100144 },
101145 }
102146}
@@ -136,6 +180,34 @@ func resourceCloudStackServiceOfferingCreate(d *schema.ResourceData, meta interf
136180 p .SetStoragetype (v .(string ))
137181 }
138182
183+ if v , ok := d .GetOk ("customized" ); ok {
184+ p .SetCustomized (v .(bool ))
185+ }
186+
187+ if v , ok := d .GetOk ("min_cpu_number" ); ok {
188+ p .SetMincpunumber (v .(int ))
189+ }
190+
191+ if v , ok := d .GetOk ("max_cpu_number" ); ok {
192+ p .SetMaxcpunumber (v .(int ))
193+ }
194+
195+ if v , ok := d .GetOk ("min_memory" ); ok {
196+ p .SetMinmemory (v .(int ))
197+ }
198+
199+ if v , ok := d .GetOk ("max_memory" ); ok {
200+ p .SetMaxmemory (v .(int ))
201+ }
202+
203+ if v , ok := d .GetOk ("encrypt_root" ); ok {
204+ p .SetEncryptroot (v .(bool ))
205+ }
206+
207+ if v , ok := d .GetOk ("storage_tags" ); ok {
208+ p .SetTags (v .(string ))
209+ }
210+
139211 log .Printf ("[DEBUG] Creating Service Offering %s" , name )
140212 s , err := cs .ServiceOffering .CreateServiceOffering (p )
141213
@@ -177,6 +249,53 @@ func resourceCloudStackServiceOfferingRead(d *schema.ResourceData, meta interfac
177249 "memory" : s .Memory ,
178250 "offer_ha" : s .Offerha ,
179251 "storage_type" : s .Storagetype ,
252+ "customized" : s .Iscustomized ,
253+ "min_cpu_number" : func () interface {} {
254+ if s .Serviceofferingdetails == nil {
255+ return nil
256+ }
257+ if v , ok := s .Serviceofferingdetails ["mincpunumber" ]; ok {
258+ if i , err := strconv .Atoi (v ); err == nil {
259+ return i
260+ }
261+ }
262+ return nil
263+ }(),
264+ "max_cpu_number" : func () interface {} {
265+ if s .Serviceofferingdetails == nil {
266+ return nil
267+ }
268+ if v , ok := s .Serviceofferingdetails ["maxcpunumber" ]; ok {
269+ if i , err := strconv .Atoi (v ); err == nil {
270+ return i
271+ }
272+ }
273+ return nil
274+ }(),
275+ "min_memory" : func () interface {} {
276+ if s .Serviceofferingdetails == nil {
277+ return nil
278+ }
279+ if v , ok := s .Serviceofferingdetails ["minmemory" ]; ok {
280+ if i , err := strconv .Atoi (v ); err == nil {
281+ return i
282+ }
283+ }
284+ return nil
285+ }(),
286+ "max_memory" : func () interface {} {
287+ if s .Serviceofferingdetails == nil {
288+ return nil
289+ }
290+ if v , ok := s .Serviceofferingdetails ["maxmemory" ]; ok {
291+ if i , err := strconv .Atoi (v ); err == nil {
292+ return i
293+ }
294+ }
295+ return nil
296+ }(),
297+ "encrypt_root" : s .Encryptroot ,
298+ "storage_tags" : s .Storagetags ,
180299 }
181300
182301 for k , v := range fields {
@@ -247,6 +366,24 @@ func resourceCloudStackServiceOfferingUpdate(d *schema.ResourceData, meta interf
247366
248367 }
249368
369+ if d .HasChange ("tags" ) {
370+ log .Printf ("[DEBUG] Tags changed for %s, starting update" , name )
371+
372+ // Create a new parameter struct
373+ p := cs .ServiceOffering .NewUpdateServiceOfferingParams (d .Id ())
374+
375+ // Set the new tags
376+ p .SetStoragetags (d .Get ("tags" ).(string ))
377+
378+ // Update the host tags
379+ _ , err := cs .ServiceOffering .UpdateServiceOffering (p )
380+ if err != nil {
381+ return fmt .Errorf (
382+ "Error updating the storage tags for service offering %s: %s" , name , err )
383+ }
384+
385+ }
386+
250387 return resourceCloudStackServiceOfferingRead (d , meta )
251388}
252389
0 commit comments