20
20
package cloudstack
21
21
22
22
import (
23
+ "errors"
23
24
"fmt"
24
- "log"
25
25
26
26
"github.com/apache/cloudstack-go/v2/cloudstack"
27
27
"github.com/hashicorp/terraform/helper/schema"
@@ -34,86 +34,217 @@ func resourceCloudStackZone() *schema.Resource {
34
34
Update : resourceCloudStackZoneUpdate ,
35
35
Delete : resourceCloudStackZoneDelete ,
36
36
Schema : map [string ]* schema.Schema {
37
- "name " : {
37
+ "allocationstate " : {
38
38
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 ,
40
51
},
41
52
"dns1" : {
42
53
Type : schema .TypeString ,
43
54
Required : true ,
44
55
},
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
+ },
45
72
"internal_dns1" : {
46
73
Type : schema .TypeString ,
47
74
Required : true ,
48
75
},
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
+ },
49
97
"network_type" : {
50
98
Type : schema .TypeString ,
51
99
Required : true ,
52
100
},
101
+ "securitygroupenabled" : {
102
+ Type : schema .TypeBool ,
103
+ Optional : true ,
104
+ },
53
105
},
54
106
}
55
107
}
56
108
57
109
func resourceCloudStackZoneCreate (d * schema.ResourceData , meta interface {}) error {
58
110
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 )
66
111
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
+ }
69
144
145
+ // Create zone
146
+ r , err := cs .Zone .CreateZone (p )
70
147
if err != nil {
71
148
return err
72
149
}
73
150
74
- log .Printf ("[DEBUG] Zone %s successfully created" , name )
75
- d .SetId (n .Id )
151
+ d .SetId (r .Id )
76
152
77
153
return resourceCloudStackZoneRead (d , meta )
78
154
}
79
155
80
156
func resourceCloudStackZoneRead (d * schema.ResourceData , meta interface {}) error {
81
157
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 ))
86
158
159
+ z , count , err := cs .Zone .GetZoneByID (d .Id ())
87
160
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
- }
93
161
return err
94
162
}
163
+ if count != 1 {
164
+ return errors .New (fmt .Sprintf ("Multiple zones. Invalid zone id: %s" , d .Id ()))
165
+ }
95
166
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 )
98
170
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 )
99
175
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 )
100
181
d .Set ("network_type" , z .Networktype )
182
+ d .Set ("securitygroupenabled" , z .Securitygroupsenabled )
101
183
102
184
return nil
103
185
}
104
186
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
+ }
106
237
107
238
func resourceCloudStackZoneDelete (d * schema.ResourceData , meta interface {}) error {
108
239
cs := meta .(* cloudstack.CloudStackClient )
109
240
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 ()))
114
243
if err != nil {
115
244
return fmt .Errorf ("Error deleting Zone: %s" , err )
116
245
}
117
246
247
+ d .SetId ("" )
248
+
118
249
return nil
119
250
}
0 commit comments