Skip to content

Commit 11c33b2

Browse files
committed
Adding resource cluster
1 parent 9eca72d commit 11c33b2

File tree

4 files changed

+326
-1
lines changed

4 files changed

+326
-1
lines changed

cloudstack/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func Provider() terraform.ResourceProvider {
9494
ResourcesMap: map[string]*schema.Resource{
9595
"cloudstack_affinity_group": resourceCloudStackAffinityGroup(),
9696
"cloudstack_autoscale_vm_profile": resourceCloudStackAutoScaleVMProfile(),
97+
"cloudstack_cluster": resourceCloudStackCluster(),
9798
"cloudstack_disk": resourceCloudStackDisk(),
9899
"cloudstack_egress_firewall": resourceCloudStackEgressFirewall(),
99100
"cloudstack_firewall": resourceCloudStackFirewall(),
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
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+
"errors"
24+
"fmt"
25+
26+
"github.com/apache/cloudstack-go/v2/cloudstack"
27+
"github.com/hashicorp/terraform/helper/schema"
28+
)
29+
30+
func resourceCloudStackCluster() *schema.Resource {
31+
return &schema.Resource{
32+
Create: resourceCloudStackClusterCreate,
33+
Read: resourceCloudStackClusterRead,
34+
Update: resourceCloudStackClusterUpdate,
35+
Delete: resourceCloudStackClusterDelete,
36+
Importer: &schema.ResourceImporter{
37+
State: schema.ImportStatePassthrough,
38+
},
39+
Schema: map[string]*schema.Schema{
40+
"allocation_state": {
41+
Type: schema.TypeString,
42+
Optional: true,
43+
Computed: true,
44+
},
45+
"cluster_name": {
46+
Type: schema.TypeString,
47+
Required: true,
48+
},
49+
"cluster_type": {
50+
Type: schema.TypeString,
51+
Required: true,
52+
},
53+
"guest_vswitch_name": {
54+
Type: schema.TypeString,
55+
Optional: true,
56+
Computed: true,
57+
},
58+
"guest_vswitch_type": {
59+
Type: schema.TypeString,
60+
Optional: true,
61+
Computed: true,
62+
},
63+
"hypervisor": {
64+
Type: schema.TypeString,
65+
Required: true,
66+
},
67+
"ovm3_cluster": {
68+
Type: schema.TypeString,
69+
Optional: true,
70+
Computed: true,
71+
},
72+
"ovm3_pool": {
73+
Type: schema.TypeString,
74+
Optional: true,
75+
Computed: true,
76+
},
77+
"ovm3_vip": {
78+
Type: schema.TypeString,
79+
Optional: true,
80+
Computed: true,
81+
},
82+
"password": {
83+
Type: schema.TypeString,
84+
Optional: true,
85+
Computed: true,
86+
},
87+
"public_vswitch_name": {
88+
Type: schema.TypeString,
89+
Optional: true,
90+
Computed: true,
91+
},
92+
"public_vswitch_type": {
93+
Type: schema.TypeString,
94+
Optional: true,
95+
Computed: true,
96+
},
97+
"pod_id": {
98+
Type: schema.TypeString,
99+
Required: true,
100+
},
101+
"url": {
102+
Type: schema.TypeString,
103+
Optional: true,
104+
Computed: true,
105+
},
106+
"username": {
107+
Type: schema.TypeString,
108+
Optional: true,
109+
Computed: true,
110+
},
111+
"vsm_ip_address": {
112+
Type: schema.TypeString,
113+
Optional: true,
114+
Computed: true,
115+
},
116+
"vsm_password": {
117+
Type: schema.TypeString,
118+
Optional: true,
119+
Computed: true,
120+
},
121+
"vsm_username": {
122+
Type: schema.TypeString,
123+
Optional: true,
124+
Computed: true,
125+
},
126+
"zone_id": {
127+
Type: schema.TypeString,
128+
Required: true,
129+
},
130+
},
131+
}
132+
}
133+
134+
func resourceCloudStackClusterCreate(d *schema.ResourceData, meta interface{}) error {
135+
cs := meta.(*cloudstack.CloudStackClient)
136+
p := cs.Cluster.NewAddClusterParams(d.Get("cluster_name").(string), d.Get("cluster_type").(string), d.Get("hypervisor").(string), d.Get("pod_id").(string), d.Get("zone_id").(string))
137+
if v, ok := d.GetOk("allocation_state"); ok {
138+
p.SetAllocationstate(v.(string))
139+
}
140+
if v, ok := d.GetOk("guest_vswitch_name"); ok {
141+
p.SetGuestvswitchname(v.(string))
142+
}
143+
if v, ok := d.GetOk("guest_vswitch_type"); ok {
144+
p.SetGuestvswitchtype(v.(string))
145+
}
146+
if v, ok := d.GetOk("hypervisor"); ok {
147+
p.SetHypervisor(v.(string))
148+
}
149+
if v, ok := d.GetOk("ovm3_cluster"); ok {
150+
p.SetOvm3cluster(v.(string))
151+
}
152+
if v, ok := d.GetOk("ovm3_pool"); ok {
153+
p.SetOvm3pool(v.(string))
154+
}
155+
if v, ok := d.GetOk("ovm3_vip"); ok {
156+
p.SetOvm3vip(v.(string))
157+
}
158+
if v, ok := d.GetOk("password"); ok {
159+
p.SetPassword(v.(string))
160+
}
161+
if v, ok := d.GetOk("public_vswitch_name"); ok {
162+
p.SetPublicvswitchname(v.(string))
163+
}
164+
if v, ok := d.GetOk("public_vswitch_type"); ok {
165+
p.SetPublicvswitchtype(v.(string))
166+
}
167+
if v, ok := d.GetOk("url"); ok {
168+
p.SetUrl(v.(string))
169+
}
170+
if v, ok := d.GetOk("username"); ok {
171+
p.SetUsername(v.(string))
172+
}
173+
if v, ok := d.GetOk("vsm_ip_address"); ok {
174+
p.SetVsmipaddress(v.(string))
175+
}
176+
if v, ok := d.GetOk("vsm_password"); ok {
177+
p.SetVsmpassword(v.(string))
178+
}
179+
if v, ok := d.GetOk("vsm_username"); ok {
180+
p.SetVsmusername(v.(string))
181+
}
182+
183+
r, err := cs.Cluster.AddCluster(p)
184+
if err != nil {
185+
return err
186+
}
187+
d.SetId(r.Id)
188+
189+
return resourceCloudStackClusterRead(d, meta)
190+
}
191+
192+
func resourceCloudStackClusterRead(d *schema.ResourceData, meta interface{}) error {
193+
cs := meta.(*cloudstack.CloudStackClient)
194+
195+
r, count, err := cs.Cluster.GetClusterByID(d.Id())
196+
if err != nil {
197+
return err
198+
}
199+
if count != 1 {
200+
return errors.New(fmt.Sprintf("Multiple clusters. Invalid zone id: %s", d.Id()))
201+
}
202+
203+
d.Set("allocation_state", r.Allocationstate)
204+
d.Set("cluster_type", r.Clustertype)
205+
d.Set("hypervisor", r.Hypervisortype)
206+
d.Set("cluster_name", r.Name)
207+
d.Set("ovm3_vip", r.Ovm3vip)
208+
d.Set("pod_id", r.Podid)
209+
d.Set("zone_id", r.Zoneid)
210+
211+
return nil
212+
}
213+
214+
func resourceCloudStackClusterUpdate(d *schema.ResourceData, meta interface{}) error {
215+
cs := meta.(*cloudstack.CloudStackClient)
216+
217+
p := cs.Cluster.NewUpdateClusterParams(d.Id())
218+
if v, ok := d.GetOk("allocation_state"); ok {
219+
p.SetAllocationstate(v.(string))
220+
}
221+
if v, ok := d.GetOk("cluster_name"); ok {
222+
p.SetClustername(v.(string))
223+
}
224+
if v, ok := d.GetOk("cluster_type"); ok {
225+
p.SetClustertype(v.(string))
226+
}
227+
if v, ok := d.GetOk("hypervisor"); ok {
228+
p.SetHypervisor(v.(string))
229+
}
230+
231+
_, err := cs.Cluster.UpdateCluster(p)
232+
if err != nil {
233+
return err
234+
}
235+
236+
return resourceCloudStackClusterRead(d, meta)
237+
}
238+
239+
func resourceCloudStackClusterDelete(d *schema.ResourceData, meta interface{}) error {
240+
cs := meta.(*cloudstack.CloudStackClient)
241+
242+
_, err := cs.Cluster.DeleteCluster(cs.Cluster.NewDeleteClusterParams(d.Id()))
243+
if err != nil {
244+
return err
245+
}
246+
247+
return nil
248+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
"testing"
24+
25+
"github.com/hashicorp/terraform/helper/resource"
26+
)
27+
28+
func TestAccCloudStackCluster_basic(t *testing.T) {
29+
resource.Test(t, resource.TestCase{
30+
PreCheck: func() { testAccPreCheck(t) },
31+
Providers: testAccProviders,
32+
Steps: []resource.TestStep{
33+
{
34+
Config: testAccCloudStackCluster_basic,
35+
},
36+
// {
37+
// Config: testAccCloudStackCluster_update,
38+
// Check: resource.ComposeTestCheckFunc(
39+
// resource.TestCheckResourceAttr("cloudstack_cluster.test", "name", "acctestupdated"),
40+
// ),
41+
// },
42+
},
43+
})
44+
}
45+
46+
const testAccCloudStackCluster_basic = `
47+
resource "cloudstack_zone" "test" {
48+
name = "acctest"
49+
dns1 = "8.8.8.8"
50+
dns2 = "8.8.8.8"
51+
internal_dns1 = "8.8.4.4"
52+
internal_dns2 = "8.8.4.4"
53+
network_type = "Advanced"
54+
domain = "cloudstack.apache.org"
55+
}
56+
resource "cloudstack_pod" "test" {
57+
allocation_state = "Disabled"
58+
gateway = "172.29.0.1"
59+
name = "accpod"
60+
netmask = "255.255.240.0"
61+
start_ip = "172.29.0.2"
62+
zone_id = cloudstack_zone.test.id
63+
}
64+
resource "cloudstack_cluster" "test" {
65+
cluster_name = "acccluster"
66+
cluster_type = "CloudManaged"
67+
hypervisor = "KVM"
68+
pod_id = cloudstack_pod.test.id
69+
zone_id = cloudstack_zone.test.id
70+
}
71+
`
72+
73+
const testAccCloudStackCluster_update = `
74+
75+
`

cloudstack/resource_cloudstack_traffic_type.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ func resourceCloudStackTrafficTypeCreate(d *schema.ResourceData, meta interface{
129129
}
130130

131131
func resourceCloudStackTrafficTypeRead(d *schema.ResourceData, meta interface{}) error {
132-
// TODO: Create cloudstack issue. While these fields are returned by the API
132+
// While these fields are returned by the API
133133
// they are not documented in the API spec or ListApi response.
134+
// see https://github.com/apache/cloudstack/issues/7837
134135

135136
return nil
136137
}

0 commit comments

Comments
 (0)