-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathresource_cloudstack_zone.go
More file actions
262 lines (242 loc) · 7.09 KB
/
resource_cloudstack_zone.go
File metadata and controls
262 lines (242 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package cloudstack
import (
"fmt"
"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceCloudStackZone() *schema.Resource {
return &schema.Resource{
Create: resourceCloudStackZoneCreate,
Read: resourceCloudStackZoneRead,
Update: resourceCloudStackZoneUpdate,
Delete: resourceCloudStackZoneDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"allocation_state": {
Description: "Allocation state of this Zone for allocation of new resources",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"dhcp_provider": {
Description: "the dhcp Provider for the Zone",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"dns1": {
Description: "the first DNS for the Zone",
Type: schema.TypeString,
Required: true,
},
"dns2": {
Description: "the second DNS for the Zone",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"domain": {
Description: "Network domain name for the networks in the zone",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"domain_id": {
Description: "the ID of the containing domain, null for public zones",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"guest_cidr_address": {
Description: "the guest CIDR address for the Zone",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"internal_dns1": {
Description: "the first internal DNS for the Zone",
Type: schema.TypeString,
Required: true,
},
"internal_dns2": {
Description: "the second internal DNS for the Zone",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"ip6_dns1": {
Description: "the first DNS for IPv6 network in the Zone",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"ip6_dns2": {
Description: "the second DNS for IPv6 network in the Zone",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"local_storage_enabled": {
Description: "true if local storage offering enabled, false otherwise",
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"name": {
Description: "the name of the Zone",
Type: schema.TypeString,
Required: true,
},
"network_type": {
Description: "network type of the zone, can be Basic or Advanced",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"security_group_enabled": {
Description: "true if network is security group enabled, false otherwise",
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceCloudStackZoneCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
p := cs.Zone.NewCreateZoneParams(d.Get("dns1").(string), d.Get("internal_dns1").(string), d.Get("name").(string), d.Get("network_type").(string))
if v, ok := d.GetOk("allocation_state"); ok {
p.SetAllocationstate(v.(string))
}
if v, ok := d.GetOk("dns2"); ok {
p.SetDns2(v.(string))
}
if v, ok := d.GetOk("domain"); ok {
p.SetDomain(v.(string))
}
if v, ok := d.GetOk("domain_id"); ok {
p.SetDomainid(v.(string))
}
if v, ok := d.GetOk("guest_cidr_address"); ok {
p.SetGuestcidraddress(v.(string))
}
if v, ok := d.GetOk("internal_dns2"); ok {
p.SetInternaldns2(v.(string))
}
if v, ok := d.GetOk("ip6_dns1"); ok {
p.SetIp6dns1(v.(string))
}
if v, ok := d.GetOk("ip6_dns2"); ok {
p.SetIp6dns2(v.(string))
}
if v, ok := d.GetOk("local_storage_enabled"); ok {
p.SetLocalstorageenabled(v.(bool))
}
if v, ok := d.GetOk("security_group_enabled"); ok {
p.SetSecuritygroupenabled(v.(bool))
}
// Create zone
r, err := cs.Zone.CreateZone(p)
if err != nil {
return err
}
d.SetId(r.Id)
return resourceCloudStackZoneRead(d, meta)
}
func resourceCloudStackZoneRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
z, _, err := cs.Zone.GetZoneByID(d.Id())
if err != nil {
return err
}
d.Set("allocation_state", z.Allocationstate)
d.Set("dhcp_provider", z.Dhcpprovider)
d.Set("dns1", z.Dns1)
d.Set("dns2", z.Dns2)
d.Set("domain", z.Domain)
d.Set("domain_id", z.Domainid)
d.Set("guest_cidr_address", z.Guestcidraddress)
d.Set("internal_dns1", z.Internaldns1)
d.Set("internal_dns2", z.Internaldns2)
d.Set("ip6_dns1", z.Ip6dns1)
d.Set("ip6_dns2", z.Ip6dns2)
d.Set("local_storage_enabled", z.Localstorageenabled)
d.Set("name", z.Name)
d.Set("network_type", z.Networktype)
d.Set("security_group_enabled", z.Securitygroupsenabled)
return nil
}
func resourceCloudStackZoneUpdate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
p := cs.Zone.NewUpdateZoneParams(d.Id())
if v, ok := d.GetOk("allocation_state"); ok {
p.SetAllocationstate(v.(string))
}
if v, ok := d.GetOk("dhcp_provider"); ok {
p.SetDhcpprovider(v.(string))
}
if v, ok := d.GetOk("dns1"); ok {
p.SetDns1(v.(string))
}
if v, ok := d.GetOk("dns2"); ok {
p.SetDns2(v.(string))
}
if v, ok := d.GetOk("domain"); ok {
p.SetDomain(v.(string))
}
if v, ok := d.GetOk("guest_cidr_address"); ok {
p.SetGuestcidraddress(v.(string))
}
if v, ok := d.GetOk("internal_dns1"); ok {
p.SetInternaldns1(v.(string))
}
if v, ok := d.GetOk("internal_dns2"); ok {
p.SetInternaldns2(v.(string))
}
if v, ok := d.GetOk("ip6_dns1"); ok {
p.SetIp6dns1(v.(string))
}
if v, ok := d.GetOk("ip6_dns2"); ok {
p.SetIp6dns2(v.(string))
}
if v, ok := d.GetOk("local_storage_enabled"); ok {
p.SetLocalstorageenabled(v.(bool))
}
if v, ok := d.GetOk("name"); ok {
p.SetName(v.(string))
}
_, err := cs.Zone.UpdateZone(p)
if err != nil {
return err
}
return resourceCloudStackZoneRead(d, meta)
}
func resourceCloudStackZoneDelete(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Delete zone
_, err := cs.Zone.DeleteZone(cs.Zone.NewDeleteZoneParams(d.Id()))
if err != nil {
return fmt.Errorf("Error deleting Zone: %s", err)
}
return nil
}