Skip to content

Commit 9408600

Browse files
committed
serviceoffering: add params for custom offering, storage tags,
encryptroot Fixes #182 Adds params for Custom offerings. Constrained custom offerings allow min/max values for CPU and memory. Also adds params for encrypt root and storage tags. New params added, - customized - min_cpu_number - max_cpu_number - min_memory - max_memory - encrypt_root - storage_tags Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 4eb6d4b commit 9408600

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed

cloudstack/resource_cloudstack_service_offering.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package cloudstack
2222
import (
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

cloudstack/resource_cloudstack_service_offering_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,42 @@ func testAccCheckCloudStackServiceOfferingExists(n string, so *cloudstack.Servic
8383
return nil
8484
}
8585
}
86+
87+
func TestAccCloudStackServiceOffering_customized(t *testing.T) {
88+
var so cloudstack.ServiceOffering
89+
resource.Test(t, resource.TestCase{
90+
PreCheck: func() { testAccPreCheck(t) },
91+
Providers: testAccProviders,
92+
Steps: []resource.TestStep{
93+
{
94+
Config: testAccCloudStackServiceOffering_customized,
95+
Check: resource.ComposeTestCheckFunc(
96+
testAccCheckCloudStackServiceOfferingExists("cloudstack_service_offering.custom", &so),
97+
resource.TestCheckResourceAttr("cloudstack_service_offering.custom", "customized", "true"),
98+
resource.TestCheckResourceAttr("cloudstack_service_offering.custom", "min_cpu_number", "1"),
99+
resource.TestCheckResourceAttr("cloudstack_service_offering.custom", "max_cpu_number", "8"),
100+
resource.TestCheckResourceAttr("cloudstack_service_offering.custom", "min_memory", "1024"),
101+
resource.TestCheckResourceAttr("cloudstack_service_offering.custom", "max_memory", "16384"),
102+
resource.TestCheckResourceAttr("cloudstack_service_offering.custom", "cpu_speed", "1000"),
103+
resource.TestCheckResourceAttr("cloudstack_service_offering.custom", "encrypt_root", "true"),
104+
resource.TestCheckResourceAttr("cloudstack_service_offering.custom", "storage_tags", "production,ssd"),
105+
),
106+
},
107+
},
108+
})
109+
}
110+
111+
const testAccCloudStackServiceOffering_customized = `
112+
resource "cloudstack_service_offering" "custom" {
113+
name = "custom_service_offering"
114+
display_text = "Custom Test"
115+
customized = true
116+
min_cpu_number = 1
117+
max_cpu_number = 8
118+
min_memory = 1024
119+
max_memory = 16384
120+
cpu_speed = 1000
121+
encrypt_root = true
122+
storage_tags = "production,ssd"
123+
}
124+
`

website/docs/r/service_offering.html.markdown

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ The following arguments are supported:
4949
* `storage_type` - (Optional) The storage type of the service offering. Values are `local` and `shared`.
5050
Changing this forces a new resource to be created.
5151

52+
* `customized` - (Optional) Whether the service offering allows custom CPU and memory values. Set to `true` to enable users to specify CPU/memory within the min/max constraints for constrained offerings and any value for unconstrained offerings.
53+
Changing this forces a new resource to be created.
54+
55+
* `min_cpu_number` - (Optional) Minimum number of CPU cores allowed for customized offerings.
56+
Changing this forces a new resource to be created.
57+
58+
* `max_cpu_number` - (Optional) Maximum number of CPU cores allowed for customized offerings.
59+
Changing this forces a new resource to be created.
60+
61+
* `min_memory` - (Optional) Minimum memory (in MB) allowed for customized offerings.
62+
Changing this forces a new resource to be created.
63+
64+
* `max_memory` - (Optional) Maximum memory (in MB) allowed for customized offerings.
65+
Changing this forces a new resource to be created.
66+
67+
* `encrypt_root` - (Optional) Whether to encrypt the root disk for VMs using this service offering.
68+
Changing this forces a new resource to be created.
69+
70+
* `storage_tags` - (Optional) Storage tags to associate with the service offering.
71+
5272
## Attributes Reference
5373

5474
The following attributes are exported:

0 commit comments

Comments
 (0)