Skip to content

Commit 86dc428

Browse files
Add support for autoscale VM groups (#213)
* Add support for Autoscale VM groups * register resources * Add support for datasources for all autoscale resources * add support to delete asg with cleanup. Support to enable and disable asg * fix conflict * add documentation * update * update go sdk version * wait for asg to move to disabled state before making modifications to lb rule associated to an asg or autoscale vm profile --------- Co-authored-by: dahn <[email protected]>
1 parent f79a66b commit 86dc428

23 files changed

+2846
-93
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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 dataSourceCloudstackAutoscalePolicy() *schema.Resource {
31+
return &schema.Resource{
32+
Read: dataSourceCloudstackAutoscalePolicyRead,
33+
34+
Schema: map[string]*schema.Schema{
35+
"id": {
36+
Type: schema.TypeString,
37+
Optional: true,
38+
Computed: true,
39+
},
40+
41+
"name": {
42+
Type: schema.TypeString,
43+
Optional: true,
44+
Computed: true,
45+
},
46+
47+
"action": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
},
51+
52+
"duration": {
53+
Type: schema.TypeInt,
54+
Computed: true,
55+
},
56+
57+
"quiet_time": {
58+
Type: schema.TypeInt,
59+
Computed: true,
60+
},
61+
62+
"condition_ids": {
63+
Type: schema.TypeSet,
64+
Computed: true,
65+
Elem: &schema.Schema{Type: schema.TypeString},
66+
},
67+
68+
"account_name": {
69+
Type: schema.TypeString,
70+
Computed: true,
71+
},
72+
73+
"domain_id": {
74+
Type: schema.TypeString,
75+
Computed: true,
76+
},
77+
78+
"project_id": {
79+
Type: schema.TypeString,
80+
Computed: true,
81+
},
82+
},
83+
}
84+
}
85+
86+
func dataSourceCloudstackAutoscalePolicyRead(d *schema.ResourceData, meta interface{}) error {
87+
cs := meta.(*cloudstack.CloudStackClient)
88+
89+
id, idOk := d.GetOk("id")
90+
name, nameOk := d.GetOk("name")
91+
92+
if !idOk && !nameOk {
93+
return fmt.Errorf("either 'id' or 'name' must be specified")
94+
}
95+
96+
var policy *cloudstack.AutoScalePolicy
97+
98+
if idOk {
99+
p := cs.AutoScale.NewListAutoScalePoliciesParams()
100+
p.SetId(id.(string))
101+
102+
resp, err := cs.AutoScale.ListAutoScalePolicies(p)
103+
if err != nil {
104+
return fmt.Errorf("failed to list autoscale policies: %s", err)
105+
}
106+
107+
if resp.Count == 0 {
108+
return fmt.Errorf("autoscale policy with ID %s not found", id.(string))
109+
}
110+
111+
policy = resp.AutoScalePolicies[0]
112+
} else {
113+
p := cs.AutoScale.NewListAutoScalePoliciesParams()
114+
115+
resp, err := cs.AutoScale.ListAutoScalePolicies(p)
116+
if err != nil {
117+
return fmt.Errorf("failed to list autoscale policies: %s", err)
118+
}
119+
120+
for _, pol := range resp.AutoScalePolicies {
121+
if pol.Name == name.(string) {
122+
policy = pol
123+
break
124+
}
125+
}
126+
127+
if policy == nil {
128+
return fmt.Errorf("autoscale policy with name %s not found", name.(string))
129+
}
130+
}
131+
132+
log.Printf("[DEBUG] Found autoscale policy: %s", policy.Name)
133+
134+
d.SetId(policy.Id)
135+
d.Set("name", policy.Name)
136+
d.Set("action", policy.Action)
137+
d.Set("duration", policy.Duration)
138+
d.Set("quiet_time", policy.Quiettime)
139+
d.Set("account_name", policy.Account)
140+
d.Set("domain_id", policy.Domainid)
141+
if policy.Projectid != "" {
142+
d.Set("project_id", policy.Projectid)
143+
}
144+
145+
conditionIds := make([]string, len(policy.Conditions))
146+
for i, condition := range policy.Conditions {
147+
conditionIds[i] = condition.Id
148+
}
149+
d.Set("condition_ids", conditionIds)
150+
151+
return nil
152+
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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 dataSourceCloudstackAutoscaleVMGroup() *schema.Resource {
31+
return &schema.Resource{
32+
Read: dataSourceCloudstackAutoscaleVMGroupRead,
33+
34+
Schema: map[string]*schema.Schema{
35+
"id": {
36+
Type: schema.TypeString,
37+
Optional: true,
38+
Computed: true,
39+
},
40+
41+
"name": {
42+
Type: schema.TypeString,
43+
Optional: true,
44+
Computed: true,
45+
},
46+
47+
"lbrule_id": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
},
51+
52+
"min_members": {
53+
Type: schema.TypeInt,
54+
Computed: true,
55+
},
56+
57+
"max_members": {
58+
Type: schema.TypeInt,
59+
Computed: true,
60+
},
61+
62+
"vm_profile_id": {
63+
Type: schema.TypeString,
64+
Computed: true,
65+
},
66+
67+
"scaleup_policy_ids": {
68+
Type: schema.TypeSet,
69+
Computed: true,
70+
Elem: &schema.Schema{Type: schema.TypeString},
71+
},
72+
73+
"scaledown_policy_ids": {
74+
Type: schema.TypeSet,
75+
Computed: true,
76+
Elem: &schema.Schema{Type: schema.TypeString},
77+
},
78+
79+
"state": {
80+
Type: schema.TypeString,
81+
Computed: true,
82+
},
83+
84+
"interval": {
85+
Type: schema.TypeInt,
86+
Computed: true,
87+
},
88+
89+
"available_virtual_machine_count": {
90+
Type: schema.TypeInt,
91+
Computed: true,
92+
},
93+
94+
"account_name": {
95+
Type: schema.TypeString,
96+
Computed: true,
97+
},
98+
99+
"domain_id": {
100+
Type: schema.TypeString,
101+
Computed: true,
102+
},
103+
104+
"project_id": {
105+
Type: schema.TypeString,
106+
Computed: true,
107+
},
108+
},
109+
}
110+
}
111+
112+
func dataSourceCloudstackAutoscaleVMGroupRead(d *schema.ResourceData, meta interface{}) error {
113+
cs := meta.(*cloudstack.CloudStackClient)
114+
115+
id, idOk := d.GetOk("id")
116+
name, nameOk := d.GetOk("name")
117+
118+
if !idOk && !nameOk {
119+
return fmt.Errorf("either 'id' or 'name' must be specified")
120+
}
121+
122+
var group *cloudstack.AutoScaleVmGroup
123+
124+
if idOk {
125+
p := cs.AutoScale.NewListAutoScaleVmGroupsParams()
126+
p.SetId(id.(string))
127+
128+
resp, err := cs.AutoScale.ListAutoScaleVmGroups(p)
129+
if err != nil {
130+
return fmt.Errorf("failed to list autoscale VM groups: %s", err)
131+
}
132+
133+
if resp.Count == 0 {
134+
return fmt.Errorf("autoscale VM group with ID %s not found", id.(string))
135+
}
136+
137+
group = resp.AutoScaleVmGroups[0]
138+
} else {
139+
p := cs.AutoScale.NewListAutoScaleVmGroupsParams()
140+
141+
resp, err := cs.AutoScale.ListAutoScaleVmGroups(p)
142+
if err != nil {
143+
return fmt.Errorf("failed to list autoscale VM groups: %s", err)
144+
}
145+
146+
for _, grp := range resp.AutoScaleVmGroups {
147+
if grp.Name == name.(string) {
148+
group = grp
149+
break
150+
}
151+
}
152+
153+
if group == nil {
154+
return fmt.Errorf("autoscale VM group with name %s not found", name.(string))
155+
}
156+
}
157+
158+
log.Printf("[DEBUG] Found autoscale VM group: %s", group.Name)
159+
160+
d.SetId(group.Id)
161+
d.Set("name", group.Name)
162+
d.Set("lbrule_id", group.Lbruleid)
163+
d.Set("min_members", group.Minmembers)
164+
d.Set("max_members", group.Maxmembers)
165+
d.Set("vm_profile_id", group.Vmprofileid)
166+
d.Set("state", group.State)
167+
d.Set("interval", group.Interval)
168+
d.Set("available_virtual_machine_count", group.Availablevirtualmachinecount)
169+
d.Set("account_name", group.Account)
170+
d.Set("domain_id", group.Domainid)
171+
if group.Projectid != "" {
172+
d.Set("project_id", group.Projectid)
173+
}
174+
175+
scaleupPolicyIds := make([]string, len(group.Scaleuppolicies))
176+
for i, policy := range group.Scaleuppolicies {
177+
scaleupPolicyIds[i] = policy.Id
178+
}
179+
d.Set("scaleup_policy_ids", scaleupPolicyIds)
180+
181+
scaledownPolicyIds := make([]string, len(group.Scaledownpolicies))
182+
for i, policy := range group.Scaledownpolicies {
183+
scaledownPolicyIds[i] = policy.Id
184+
}
185+
d.Set("scaledown_policy_ids", scaledownPolicyIds)
186+
187+
return nil
188+
}

0 commit comments

Comments
 (0)