Skip to content

Commit b3ca9a7

Browse files
committed
Enhancing zone resource
1 parent ea87939 commit b3ca9a7

File tree

2 files changed

+233
-30
lines changed

2 files changed

+233
-30
lines changed

cloudstack/resource_cloudstack_zone.go

Lines changed: 161 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
package cloudstack
2121

2222
import (
23+
"errors"
2324
"fmt"
24-
"log"
2525

2626
"github.com/apache/cloudstack-go/v2/cloudstack"
2727
"github.com/hashicorp/terraform/helper/schema"
@@ -34,86 +34,217 @@ func resourceCloudStackZone() *schema.Resource {
3434
Update: resourceCloudStackZoneUpdate,
3535
Delete: resourceCloudStackZoneDelete,
3636
Schema: map[string]*schema.Schema{
37-
"name": {
37+
"allocationstate": {
3838
Type: schema.TypeString,
39-
Required: true,
39+
Optional: true,
40+
Computed: true,
41+
},
42+
"description": {
43+
Type: schema.TypeString,
44+
Optional: true,
45+
Computed: true,
46+
},
47+
"dhcp_provider": {
48+
Type: schema.TypeString,
49+
Optional: true,
50+
Computed: true,
4051
},
4152
"dns1": {
4253
Type: schema.TypeString,
4354
Required: true,
4455
},
56+
"dns2": {
57+
Type: schema.TypeString,
58+
Optional: true,
59+
},
60+
"domain": {
61+
Type: schema.TypeString,
62+
Optional: true,
63+
},
64+
"domainid": {
65+
Type: schema.TypeString,
66+
Optional: true,
67+
},
68+
"guestcidraddress": {
69+
Type: schema.TypeString,
70+
Optional: true,
71+
},
4572
"internal_dns1": {
4673
Type: schema.TypeString,
4774
Required: true,
4875
},
76+
"internal_dns2": {
77+
Type: schema.TypeString,
78+
Optional: true,
79+
},
80+
"ip6dns1": {
81+
Type: schema.TypeString,
82+
Optional: true,
83+
},
84+
"ip6dns2": {
85+
Type: schema.TypeString,
86+
Optional: true,
87+
},
88+
"localstorageenabled": {
89+
Type: schema.TypeBool,
90+
Optional: true,
91+
Computed: true,
92+
},
93+
"name": {
94+
Type: schema.TypeString,
95+
Required: true,
96+
},
4997
"network_type": {
5098
Type: schema.TypeString,
5199
Required: true,
52100
},
101+
"securitygroupenabled": {
102+
Type: schema.TypeBool,
103+
Optional: true,
104+
},
53105
},
54106
}
55107
}
56108

57109
func resourceCloudStackZoneCreate(d *schema.ResourceData, meta interface{}) error {
58110
cs := meta.(*cloudstack.CloudStackClient)
59-
name := d.Get("name").(string)
60-
dns1 := d.Get("dns1").(string)
61-
internal_dns1 := d.Get("internal_dns1").(string)
62-
network_type := d.Get("network_type").(string)
63-
64-
// Create a new parameter struct
65-
p := cs.Zone.NewCreateZoneParams(dns1, internal_dns1, name, network_type)
66111

67-
log.Printf("[DEBUG] Creating Zone %s", name)
68-
n, err := cs.Zone.CreateZone(p)
112+
// Create a new parameters
113+
p := cs.Zone.NewCreateZoneParams(d.Get("dns1").(string), d.Get("internal_dns1").(string), d.Get("name").(string), d.Get("network_type").(string))
114+
if v, ok := d.GetOk("allocationstate"); ok {
115+
p.SetAllocationstate(v.(string))
116+
}
117+
if v, ok := d.GetOk("dns2"); ok {
118+
p.SetDns2(v.(string))
119+
}
120+
if v, ok := d.GetOk("domain"); ok {
121+
p.SetDomain(v.(string))
122+
}
123+
if v, ok := d.GetOk("domainid"); ok {
124+
p.SetDomainid(v.(string))
125+
}
126+
if v, ok := d.GetOk("guestcidraddress"); ok {
127+
p.SetGuestcidraddress(v.(string))
128+
}
129+
if v, ok := d.GetOk("internal_dns2"); ok {
130+
p.SetInternaldns2(v.(string))
131+
}
132+
if v, ok := d.GetOk("ip6dns1"); ok {
133+
p.SetIp6dns1(v.(string))
134+
}
135+
if v, ok := d.GetOk("ip6dns2"); ok {
136+
p.SetIp6dns2(v.(string))
137+
}
138+
if v, ok := d.GetOk("localstorageenabled"); ok {
139+
p.SetLocalstorageenabled(v.(bool))
140+
}
141+
if v, ok := d.GetOk("securitygroupenabled"); ok {
142+
p.SetSecuritygroupenabled(v.(bool))
143+
}
69144

145+
// Create zone
146+
r, err := cs.Zone.CreateZone(p)
70147
if err != nil {
71148
return err
72149
}
73150

74-
log.Printf("[DEBUG] Zone %s successfully created", name)
75-
d.SetId(n.Id)
151+
d.SetId(r.Id)
76152

77153
return resourceCloudStackZoneRead(d, meta)
78154
}
79155

80156
func resourceCloudStackZoneRead(d *schema.ResourceData, meta interface{}) error {
81157
cs := meta.(*cloudstack.CloudStackClient)
82-
log.Printf("[DEBUG] Retrieving Zone %s", d.Get("name").(string))
83-
84-
// Get the Zone details
85-
z, count, err := cs.Zone.GetZoneByName(d.Get("name").(string))
86158

159+
z, count, err := cs.Zone.GetZoneByID(d.Id())
87160
if err != nil {
88-
if count == 0 {
89-
log.Printf("[DEBUG] Zone %s does no longer exist", d.Get("name").(string))
90-
d.SetId("")
91-
return nil
92-
}
93161
return err
94162
}
163+
if count != 1 {
164+
return errors.New(fmt.Sprintf("Multiple zones. Invalid zone id: %s", d.Id()))
165+
}
95166

96-
d.SetId(z.Id)
97-
d.Set("name", z.Name)
167+
d.Set("allocationstate", z.Allocationstate)
168+
d.Set("description", z.Description)
169+
d.Set("dhcp_provider", z.Dhcpprovider)
98170
d.Set("dns1", z.Dns1)
171+
d.Set("dns2", z.Dns2)
172+
d.Set("domain", z.Domain)
173+
d.Set("domainid", z.Domainid)
174+
d.Set("guestcidraddress", z.Guestcidraddress)
99175
d.Set("internal_dns1", z.Internaldns1)
176+
d.Set("internal_dns2", z.Internaldns2)
177+
d.Set("ip6dns1", z.Ip6dns1)
178+
d.Set("ip6dns2", z.Ip6dns2)
179+
d.Set("localstorageenabled", z.Localstorageenabled)
180+
d.Set("name", z.Name)
100181
d.Set("network_type", z.Networktype)
182+
d.Set("securitygroupenabled", z.Securitygroupsenabled)
101183

102184
return nil
103185
}
104186

105-
func resourceCloudStackZoneUpdate(d *schema.ResourceData, meta interface{}) error { return nil }
187+
func resourceCloudStackZoneUpdate(d *schema.ResourceData, meta interface{}) error {
188+
cs := meta.(*cloudstack.CloudStackClient)
189+
190+
p := cs.Zone.NewUpdateZoneParams(d.Id())
191+
192+
if v, ok := d.GetOk("allocationstate"); ok {
193+
p.SetAllocationstate(v.(string))
194+
}
195+
if v, ok := d.GetOk("dhcp_provider"); ok {
196+
p.SetDhcpprovider(v.(string))
197+
}
198+
if v, ok := d.GetOk("dns1"); ok {
199+
p.SetDns1(v.(string))
200+
}
201+
if v, ok := d.GetOk("dns2"); ok {
202+
p.SetDns2(v.(string))
203+
}
204+
if v, ok := d.GetOk("domain"); ok {
205+
p.SetDomain(v.(string))
206+
}
207+
if v, ok := d.GetOk("guestcidraddress"); ok {
208+
p.SetGuestcidraddress(v.(string))
209+
}
210+
211+
if v, ok := d.GetOk("internal_dns1"); ok {
212+
p.SetInternaldns1(v.(string))
213+
}
214+
if v, ok := d.GetOk("internal_dns2"); ok {
215+
p.SetInternaldns2(v.(string))
216+
}
217+
if v, ok := d.GetOk("ip6dns1"); ok {
218+
p.SetIp6dns1(v.(string))
219+
}
220+
if v, ok := d.GetOk("ip6dns2"); ok {
221+
p.SetIp6dns2(v.(string))
222+
}
223+
if v, ok := d.GetOk("localstorageenabled"); ok {
224+
p.SetLocalstorageenabled(v.(bool))
225+
}
226+
if v, ok := d.GetOk("name"); ok {
227+
p.SetName(v.(string))
228+
}
229+
230+
_, err := cs.Zone.UpdateZone(p)
231+
if err != nil {
232+
return err
233+
}
234+
235+
return resourceCloudStackZoneRead(d, meta)
236+
}
106237

107238
func resourceCloudStackZoneDelete(d *schema.ResourceData, meta interface{}) error {
108239
cs := meta.(*cloudstack.CloudStackClient)
109240

110-
// Create a new parameter struct
111-
p := cs.Zone.NewDeleteZoneParams(d.Id())
112-
_, err := cs.Zone.DeleteZone(p)
113-
241+
// Delete zone
242+
_, err := cs.Zone.DeleteZone(cs.Zone.NewDeleteZoneParams(d.Id()))
114243
if err != nil {
115244
return fmt.Errorf("Error deleting Zone: %s", err)
116245
}
117246

247+
d.SetId("")
248+
118249
return nil
119250
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 TestAccCloudStackZone_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: testAccCloudStackZone_basic,
35+
},
36+
{
37+
Config: testAccCloudStackZone_update,
38+
Check: resource.ComposeTestCheckFunc(
39+
resource.TestCheckResourceAttr("cloudstack_zone.test", "name", "acctestupdated"),
40+
),
41+
},
42+
},
43+
})
44+
}
45+
46+
const testAccCloudStackZone_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 = "foo.cloudstack.com"
55+
56+
}
57+
`
58+
59+
// guestcidraddress = "172.29.1.0/20"
60+
61+
const testAccCloudStackZone_update = `
62+
resource "cloudstack_zone" "test" {
63+
name = "acctestupdated"
64+
dns1 = "8.8.4.4"
65+
dns2 = "8.8.4.4"
66+
internal_dns1 = "8.8.8.8"
67+
internal_dns2 = "8.8.8.8"
68+
network_type = "Advanced"
69+
domain = "foo.cloudstack.com"
70+
guestcidraddress = "172.29.2.0/20"
71+
}
72+
`

0 commit comments

Comments
 (0)