-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathresource_cloudstack_port_forward_test.go
More file actions
393 lines (346 loc) · 12 KB
/
Copy pathresource_cloudstack_port_forward_test.go
File metadata and controls
393 lines (346 loc) · 12 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//
// 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"
"strings"
"testing"
"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)
func TestAccCloudStackPortForward_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudStackPortForwardDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudStackPortForward_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackPortForwardsExist("cloudstack_port_forward.foo"),
resource.TestCheckResourceAttr(
"cloudstack_port_forward.foo", "forward.#", "1"),
resource.TestCheckResourceAttrSet(
"cloudstack_port_forward.foo", "forward.0.uuid"),
resource.TestCheckResourceAttr(
"cloudstack_port_forward.foo", "forward.0.protocol", "tcp"),
resource.TestCheckResourceAttr(
"cloudstack_port_forward.foo", "forward.0.private_port", "443"),
resource.TestCheckResourceAttr(
"cloudstack_port_forward.foo", "forward.0.public_port", "8443"),
resource.TestCheckResourceAttrSet(
"cloudstack_port_forward.foo", "forward.0.virtual_machine_id"),
),
},
},
})
}
func TestAccCloudStackPortForward_import(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudStackPortForwardDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudStackPortForward_basic,
},
{
ResourceName: "cloudstack_port_forward.foo",
ImportState: true,
ImportStateVerify: true,
// The importer sets managed=true so unrelated forwards on the same IP
// aren't silently dropped; the applied config leaves it at its default.
ImportStateVerifyIgnore: []string{"managed"},
},
},
})
}
func TestAccCloudStackPortForward_update(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudStackPortForwardDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudStackPortForward_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackPortForwardsExist("cloudstack_port_forward.foo"),
resource.TestCheckResourceAttr(
"cloudstack_port_forward.foo", "forward.#", "1"),
),
},
{
Config: testAccCloudStackPortForward_update,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackPortForwardsExist("cloudstack_port_forward.foo"),
resource.TestCheckResourceAttr(
"cloudstack_port_forward.foo", "forward.#", "2"),
// Validate first forward rule
resource.TestCheckResourceAttrSet(
"cloudstack_port_forward.foo", "forward.0.uuid"),
resource.TestCheckResourceAttr(
"cloudstack_port_forward.foo", "forward.0.protocol", "tcp"),
resource.TestCheckResourceAttrSet(
"cloudstack_port_forward.foo", "forward.0.virtual_machine_id"),
// Validate second forward rule
resource.TestCheckResourceAttrSet(
"cloudstack_port_forward.foo", "forward.1.uuid"),
resource.TestCheckResourceAttr(
"cloudstack_port_forward.foo", "forward.1.protocol", "tcp"),
resource.TestCheckResourceAttrSet(
"cloudstack_port_forward.foo", "forward.1.virtual_machine_id"),
),
},
},
})
}
func TestAccCloudStackPortForward_portRange(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudStackPortForwardDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudStackPortForward_portRange,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudStackPortForwardsExist("cloudstack_port_forward.foo"),
resource.TestCheckResourceAttr(
"cloudstack_port_forward.foo", "forward.#", "2"),
testAccCheckCloudStackPortForwardAttributes("cloudstack_port_forward.foo"),
// Note: We don't check specific indices since sets are unordered
// The testAccCheckCloudStackPortForwardAttributes function handles validation
),
},
},
})
}
func testAccCheckCloudStackPortForwardsExist(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No port forward ID is set")
}
for k, id := range rs.Primary.Attributes {
if !strings.Contains(k, "uuid") {
continue
}
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
_, count, err := cs.Firewall.GetPortForwardingRuleByID(id)
if err != nil {
return err
}
if count == 0 {
return fmt.Errorf("Port forward for %s not found", k)
}
}
return nil
}
}
func testAccCheckCloudStackPortForwardAttributes(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No port forward ID is set")
}
// Verify we have 2 forward rules
if rs.Primary.Attributes["forward.#"] != "2" {
return fmt.Errorf("Expected 2 forward rules, got %s", rs.Primary.Attributes["forward.#"])
}
var foundTCPRange, foundUDPSingle bool
// Check both forward rules to find the expected configurations
for i := 0; i < 2; i++ {
protocolKey := fmt.Sprintf("forward.%d.protocol", i)
privatePortKey := fmt.Sprintf("forward.%d.private_port", i)
privateEndPortKey := fmt.Sprintf("forward.%d.private_end_port", i)
publicPortKey := fmt.Sprintf("forward.%d.public_port", i)
publicEndPortKey := fmt.Sprintf("forward.%d.public_end_port", i)
uuidKey := fmt.Sprintf("forward.%d.uuid", i)
protocol := rs.Primary.Attributes[protocolKey]
privatePort := rs.Primary.Attributes[privatePortKey]
privateEndPort := rs.Primary.Attributes[privateEndPortKey]
publicPort := rs.Primary.Attributes[publicPortKey]
publicEndPort := rs.Primary.Attributes[publicEndPortKey]
uuid := rs.Primary.Attributes[uuidKey]
// Verify basic required fields exist
if protocol == "" {
return fmt.Errorf("Missing protocol for forward rule %d", i)
}
if privatePort == "" {
return fmt.Errorf("Missing private_port for forward rule %d", i)
}
if publicPort == "" {
return fmt.Errorf("Missing public_port for forward rule %d", i)
}
if uuid == "" {
return fmt.Errorf("Missing uuid for forward rule %d", i)
}
// Check for TCP rule with port range (8080-8090)
if protocol == "tcp" && privatePort == "8080" && publicPort == "8080" {
if privateEndPort != "8090" {
return fmt.Errorf("Expected TCP rule to have private_end_port=8090, got %s", privateEndPort)
}
if publicEndPort != "8090" {
return fmt.Errorf("Expected TCP rule to have public_end_port=8090, got %s", publicEndPort)
}
foundTCPRange = true
}
// Check for UDP rule with single port (9000)
if protocol == "udp" && privatePort == "9000" && publicPort == "9000" {
// For single port rules, end ports should be empty, "0", or equal to start ports
if privateEndPort != "" && privateEndPort != "0" && privateEndPort != "9000" {
return fmt.Errorf("Expected UDP rule to have empty, '0', or matching private_end_port, got %s", privateEndPort)
}
if publicEndPort != "" && publicEndPort != "0" && publicEndPort != "9000" {
return fmt.Errorf("Expected UDP rule to have empty, '0', or matching public_end_port, got %s", publicEndPort)
}
foundUDPSingle = true
}
}
if !foundTCPRange {
return fmt.Errorf("Expected to find TCP rule with port range 8080-8090")
}
if !foundUDPSingle {
return fmt.Errorf("Expected to find UDP rule with single port 9000")
}
return nil
}
}
func testAccCheckCloudStackPortForwardDestroy(s *terraform.State) error {
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
for _, rs := range s.RootModule().Resources {
if rs.Type != "cloudstack_port_forward" {
continue
}
if rs.Primary.ID == "" {
return fmt.Errorf("No port forward ID is set")
}
for k, id := range rs.Primary.Attributes {
if !strings.Contains(k, "uuid") {
continue
}
_, _, err := cs.Firewall.GetPortForwardingRuleByID(id)
if err == nil {
return fmt.Errorf("Port forward %s still exists", rs.Primary.ID)
}
}
}
return nil
}
const testAccCloudStackPortForward_basic = `
resource "cloudstack_network" "foo" {
name = "terraform-network"
display_text = "terraform-network"
cidr = "10.1.1.0/24"
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
source_nat_ip = true
zone = "Sandbox-simulator"
}
resource "cloudstack_instance" "foobar" {
name = "terraform-test"
display_name = "terraform-updated"
service_offering= "Medium Instance"
network_id = cloudstack_network.foo.id
template = "CentOS 5.6 (64-bit) no GUI (Simulator)"
zone = "Sandbox-simulator"
expunge = true
}
resource "cloudstack_port_forward" "foo" {
ip_address_id = cloudstack_network.foo.source_nat_ip_id
forward {
protocol = "tcp"
private_port = 443
public_port = 8443
virtual_machine_id = cloudstack_instance.foobar.id
}
}`
const testAccCloudStackPortForward_update = `
resource "cloudstack_network" "foo" {
name = "terraform-network"
display_text = "terraform-network"
cidr = "10.1.1.0/24"
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
source_nat_ip = true
zone = "Sandbox-simulator"
}
resource "cloudstack_instance" "foobar" {
name = "terraform-test"
display_name = "terraform-updated"
service_offering= "Medium Instance"
network_id = cloudstack_network.foo.id
template = "CentOS 5.6 (64-bit) no GUI (Simulator)"
zone = "Sandbox-simulator"
expunge = true
}
resource "cloudstack_port_forward" "foo" {
ip_address_id = cloudstack_network.foo.source_nat_ip_id
forward {
protocol = "tcp"
private_port = 443
public_port = 8443
virtual_machine_id = cloudstack_instance.foobar.id
}
forward {
protocol = "tcp"
private_port = 80
public_port = 8080
virtual_machine_id = cloudstack_instance.foobar.id
}
}`
const testAccCloudStackPortForward_portRange = `
resource "cloudstack_network" "foo" {
name = "terraform-network"
display_text = "terraform-network"
cidr = "10.1.1.0/24"
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
source_nat_ip = true
zone = "Sandbox-simulator"
}
resource "cloudstack_instance" "foobar" {
name = "terraform-test"
display_name = "terraform-updated"
service_offering= "Medium Instance"
network_id = cloudstack_network.foo.id
template = "CentOS 5.6 (64-bit) no GUI (Simulator)"
zone = "Sandbox-simulator"
expunge = true
}
resource "cloudstack_port_forward" "foo" {
ip_address_id = cloudstack_network.foo.source_nat_ip_id
forward {
protocol = "tcp"
private_port = 8080
private_end_port = 8090
public_port = 8080
public_end_port = 8090
virtual_machine_id = cloudstack_instance.foobar.id
}
forward {
protocol = "udp"
private_port = 9000
public_port = 9000
virtual_machine_id = cloudstack_instance.foobar.id
}
}`