Skip to content

Commit 6549554

Browse files
committed
Add support for Autoscale VM groups
1 parent 2d90b19 commit 6549554

File tree

5 files changed

+981
-28
lines changed

5 files changed

+981
-28
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package cloudstack
21+
22+
import (
23+
"fmt"
24+
"log"
25+
26+
"github.com/apache/cloudstack-go/v2/cloudstack"
27+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
28+
)
29+
30+
func resourceCloudStackAutoScalePolicy() *schema.Resource {
31+
return &schema.Resource{
32+
Create: resourceCloudStackAutoScalePolicyCreate,
33+
Read: resourceCloudStackAutoScalePolicyRead,
34+
Update: resourceCloudStackAutoScalePolicyUpdate,
35+
Delete: resourceCloudStackAutoScalePolicyDelete,
36+
37+
Schema: map[string]*schema.Schema{
38+
"name": {
39+
Type: schema.TypeString,
40+
Optional: true,
41+
Description: "the name of the autoscale policy",
42+
},
43+
"action": {
44+
Type: schema.TypeString,
45+
Required: true,
46+
Description: "the action to be executed if all the conditions evaluate to true for the specified duration",
47+
ForceNew: true,
48+
},
49+
"duration": {
50+
Type: schema.TypeInt,
51+
Required: true,
52+
Description: "the duration in which the conditions have to be true before action is taken",
53+
},
54+
"quiet_time": {
55+
Type: schema.TypeInt,
56+
Optional: true,
57+
Description: "the cool down period in which the policy should not be evaluated after the action has been taken",
58+
},
59+
"condition_ids": {
60+
Type: schema.TypeSet,
61+
Elem: &schema.Schema{Type: schema.TypeString},
62+
Required: true,
63+
Description: "the list of IDs of the conditions that are being evaluated on every interval",
64+
},
65+
},
66+
}
67+
}
68+
69+
func resourceCloudStackAutoScalePolicyCreate(d *schema.ResourceData, meta interface{}) error {
70+
cs := meta.(*cloudstack.CloudStackClient)
71+
72+
action := d.Get("action").(string)
73+
duration := d.Get("duration").(int)
74+
75+
conditionIds := []string{}
76+
if v, ok := d.GetOk("condition_ids"); ok {
77+
conditionSet := v.(*schema.Set)
78+
for _, id := range conditionSet.List() {
79+
conditionIds = append(conditionIds, id.(string))
80+
}
81+
}
82+
83+
p := cs.AutoScale.NewCreateAutoScalePolicyParams(action, conditionIds, duration)
84+
85+
if v, ok := d.GetOk("name"); ok {
86+
p.SetName(v.(string))
87+
}
88+
if v, ok := d.GetOk("quiet_time"); ok {
89+
p.SetQuiettime(v.(int))
90+
}
91+
92+
log.Printf("[DEBUG] Creating autoscale policy")
93+
resp, err := cs.AutoScale.CreateAutoScalePolicy(p)
94+
if err != nil {
95+
return fmt.Errorf("Error creating autoscale policy: %s", err)
96+
}
97+
98+
d.SetId(resp.Id)
99+
log.Printf("[DEBUG] Autoscale policy created with ID: %s", resp.Id)
100+
101+
return resourceCloudStackAutoScalePolicyRead(d, meta)
102+
}
103+
104+
func resourceCloudStackAutoScalePolicyRead(d *schema.ResourceData, meta interface{}) error {
105+
cs := meta.(*cloudstack.CloudStackClient)
106+
107+
p := cs.AutoScale.NewListAutoScalePoliciesParams()
108+
p.SetId(d.Id())
109+
110+
resp, err := cs.AutoScale.ListAutoScalePolicies(p)
111+
if err != nil {
112+
return fmt.Errorf("Error retrieving autoscale policy: %s", err)
113+
}
114+
115+
if resp.Count == 0 {
116+
log.Printf("[DEBUG] Autoscale policy %s no longer exists", d.Id())
117+
d.SetId("")
118+
return nil
119+
}
120+
121+
policy := resp.AutoScalePolicies[0]
122+
d.Set("name", policy.Name)
123+
d.Set("action", policy.Action)
124+
d.Set("duration", policy.Duration)
125+
d.Set("quiet_time", policy.Quiettime)
126+
127+
conditionIds := schema.NewSet(schema.HashString, []interface{}{})
128+
for _, conditionId := range policy.Conditions {
129+
conditionIds.Add(conditionId)
130+
}
131+
d.Set("condition_ids", conditionIds)
132+
133+
return nil
134+
}
135+
136+
func resourceCloudStackAutoScalePolicyUpdate(d *schema.ResourceData, meta interface{}) error {
137+
cs := meta.(*cloudstack.CloudStackClient)
138+
139+
if d.HasChange("name") || d.HasChange("condition_ids") || d.HasChange("duration") || d.HasChange("quiet_time") {
140+
log.Printf("[DEBUG] Updating autoscale policy: %s", d.Id())
141+
142+
p := cs.AutoScale.NewUpdateAutoScalePolicyParams(d.Id())
143+
144+
if d.HasChange("name") {
145+
if v, ok := d.GetOk("name"); ok {
146+
p.SetName(v.(string))
147+
}
148+
}
149+
150+
if d.HasChange("duration") {
151+
duration := d.Get("duration").(int)
152+
p.SetDuration(duration)
153+
}
154+
155+
if d.HasChange("quiet_time") {
156+
if v, ok := d.GetOk("quiet_time"); ok {
157+
p.SetQuiettime(v.(int))
158+
}
159+
}
160+
161+
if d.HasChange("condition_ids") {
162+
conditionIds := []string{}
163+
if v, ok := d.GetOk("condition_ids"); ok {
164+
conditionSet := v.(*schema.Set)
165+
for _, id := range conditionSet.List() {
166+
conditionIds = append(conditionIds, id.(string))
167+
}
168+
}
169+
p.SetConditionids(conditionIds)
170+
}
171+
172+
_, err := cs.AutoScale.UpdateAutoScalePolicy(p)
173+
if err != nil {
174+
return fmt.Errorf("Error updating autoscale policy: %s", err)
175+
}
176+
177+
log.Printf("[DEBUG] Autoscale policy updated successfully: %s", d.Id())
178+
}
179+
180+
return resourceCloudStackAutoScalePolicyRead(d, meta)
181+
}
182+
183+
func resourceCloudStackAutoScalePolicyDelete(d *schema.ResourceData, meta interface{}) error {
184+
cs := meta.(*cloudstack.CloudStackClient)
185+
186+
p := cs.AutoScale.NewDeleteAutoScalePolicyParams(d.Id())
187+
188+
log.Printf("[DEBUG] Deleting autoscale policy: %s", d.Id())
189+
_, err := cs.AutoScale.DeleteAutoScalePolicy(p)
190+
if err != nil {
191+
return fmt.Errorf("Error deleting autoscale policy: %s", err)
192+
}
193+
194+
return nil
195+
}

0 commit comments

Comments
 (0)