Skip to content

Commit e5fecca

Browse files
author
Sander van Harmelen
committed
Add cloudstack_static_nat resource
1 parent 3a44fc7 commit e5fecca

5 files changed

Lines changed: 336 additions & 1 deletion

File tree

‎builtin/providers/cloudstack/provider.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func Provider() terraform.ResourceProvider {
5454
"cloudstack_port_forward": resourceCloudStackPortForward(),
5555
"cloudstack_secondary_ipaddress": resourceCloudStackSecondaryIPAddress(),
5656
"cloudstack_ssh_keypair": resourceCloudStackSSHKeyPair(),
57+
"cloudstack_static_nat": resourceCloudStackStaticNAT(),
5758
"cloudstack_template": resourceCloudStackTemplate(),
5859
"cloudstack_vpc": resourceCloudStackVPC(),
5960
"cloudstack_vpn_connection": resourceCloudStackVPNConnection(),

‎builtin/providers/cloudstack/resource_cloudstack_ipaddress.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func resourceCloudStackIPAddressCreate(d *schema.ResourceData, meta interface{})
9999
func resourceCloudStackIPAddressRead(d *schema.ResourceData, meta interface{}) error {
100100
cs := meta.(*cloudstack.CloudStackClient)
101101

102-
// Get the network ACL list details
102+
// Get the IP address details
103103
f, count, err := cs.Address.GetPublicIpAddressByID(d.Id())
104104
if err != nil {
105105
if count == 0 {
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package cloudstack
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strings"
7+
8+
"github.com/hashicorp/terraform/helper/schema"
9+
"github.com/xanzy/go-cloudstack/cloudstack"
10+
)
11+
12+
func resourceCloudStackStaticNAT() *schema.Resource {
13+
return &schema.Resource{
14+
Create: resourceCloudStackStaticNATCreate,
15+
Exists: resourceCloudStackStaticNATExists,
16+
Read: resourceCloudStackStaticNATRead,
17+
Delete: resourceCloudStackStaticNATDelete,
18+
19+
Schema: map[string]*schema.Schema{
20+
"ipaddress": &schema.Schema{
21+
Type: schema.TypeString,
22+
Required: true,
23+
ForceNew: true,
24+
},
25+
26+
"network": &schema.Schema{
27+
Type: schema.TypeString,
28+
Optional: true,
29+
Computed: true,
30+
ForceNew: true,
31+
},
32+
33+
"virtual_machine": &schema.Schema{
34+
Type: schema.TypeString,
35+
Required: true,
36+
ForceNew: true,
37+
},
38+
39+
"vm_guest_ip": &schema.Schema{
40+
Type: schema.TypeString,
41+
Optional: true,
42+
Computed: true,
43+
ForceNew: true,
44+
},
45+
},
46+
}
47+
}
48+
49+
func resourceCloudStackStaticNATCreate(d *schema.ResourceData, meta interface{}) error {
50+
cs := meta.(*cloudstack.CloudStackClient)
51+
52+
// Retrieve the ipaddress ID
53+
ipaddressid, e := retrieveID(cs, "ipaddress", d.Get("ipaddress").(string))
54+
if e != nil {
55+
return e.Error()
56+
}
57+
58+
// Retrieve the virtual_machine ID
59+
virtualmachineid, e := retrieveID(cs, "virtual_machine", d.Get("virtual_machine").(string))
60+
if e != nil {
61+
return e.Error()
62+
}
63+
64+
// Create a new parameter struct
65+
p := cs.NAT.NewEnableStaticNatParams(ipaddressid, virtualmachineid)
66+
67+
if network, ok := d.GetOk("network"); ok {
68+
// Retrieve the network ID
69+
networkid, e := retrieveID(cs, "network", network.(string))
70+
if e != nil {
71+
return e.Error()
72+
}
73+
74+
p.SetNetworkid(networkid)
75+
}
76+
77+
if vmGuestIP, ok := d.GetOk("vm_guest_ip"); ok {
78+
p.SetVmguestip(vmGuestIP.(string))
79+
}
80+
81+
_, err := cs.NAT.EnableStaticNat(p)
82+
if err != nil {
83+
return fmt.Errorf("Error enabling static NAT: %s", err)
84+
}
85+
86+
d.SetId(ipaddressid)
87+
88+
return resourceCloudStackStaticNATRead(d, meta)
89+
}
90+
91+
func resourceCloudStackStaticNATExists(d *schema.ResourceData, meta interface{}) (bool, error) {
92+
cs := meta.(*cloudstack.CloudStackClient)
93+
94+
// Get the IP address details
95+
ip, count, err := cs.Address.GetPublicIpAddressByID(d.Id())
96+
if err != nil {
97+
if count == 0 {
98+
log.Printf("[DEBUG] IP address with ID %s no longer exists", d.Id())
99+
return false, nil
100+
}
101+
102+
return false, err
103+
}
104+
105+
return ip.Isstaticnat, nil
106+
}
107+
108+
func resourceCloudStackStaticNATRead(d *schema.ResourceData, meta interface{}) error {
109+
cs := meta.(*cloudstack.CloudStackClient)
110+
111+
// Get the IP address details
112+
ip, count, err := cs.Address.GetPublicIpAddressByID(d.Id())
113+
if err != nil {
114+
if count == 0 {
115+
log.Printf("[DEBUG] IP address with ID %s no longer exists", d.Id())
116+
d.SetId("")
117+
return nil
118+
}
119+
120+
return err
121+
}
122+
123+
if !ip.Isstaticnat {
124+
log.Printf("[DEBUG] Static NAT is no longer enabled for IP address with ID %s", d.Id())
125+
d.SetId("")
126+
return nil
127+
}
128+
129+
setValueOrID(d, "network", ip.Associatednetworkname, ip.Associatednetworkid)
130+
setValueOrID(d, "virtual_machine", ip.Virtualmachinename, ip.Virtualmachineid)
131+
d.Set("vm_guest_ip", ip.Vmipaddress)
132+
133+
return nil
134+
}
135+
136+
func resourceCloudStackStaticNATDelete(d *schema.ResourceData, meta interface{}) error {
137+
cs := meta.(*cloudstack.CloudStackClient)
138+
139+
// Create a new parameter struct
140+
p := cs.NAT.NewDisableStaticNatParams(d.Id())
141+
142+
// Disable static NAT
143+
_, err := cs.NAT.DisableStaticNat(p)
144+
if err != nil {
145+
// This is a very poor way to be told the ID does no longer exist :(
146+
if strings.Contains(err.Error(), fmt.Sprintf(
147+
"Invalid parameter id value=%s due to incorrect long value format, "+
148+
"or entity does not exist", d.Id())) {
149+
return nil
150+
}
151+
152+
return fmt.Errorf("Error disabling static NAT: %s", err)
153+
}
154+
155+
return nil
156+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package cloudstack
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/resource"
8+
"github.com/hashicorp/terraform/terraform"
9+
"github.com/xanzy/go-cloudstack/cloudstack"
10+
)
11+
12+
func TestAccCloudStackStaticNAT_basic(t *testing.T) {
13+
var ipaddr cloudstack.PublicIpAddress
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheck(t) },
17+
Providers: testAccProviders,
18+
CheckDestroy: testAccCheckCloudStackStaticNATDestroy,
19+
Steps: []resource.TestStep{
20+
resource.TestStep{
21+
Config: testAccCloudStackStaticNAT_basic,
22+
Check: resource.ComposeTestCheckFunc(
23+
testAccCheckCloudStackStaticNATExists(
24+
"cloudstack_static_nat.foo", &ipaddr),
25+
testAccCheckCloudStackStaticNATAttributes(&ipaddr),
26+
),
27+
},
28+
},
29+
})
30+
}
31+
32+
func testAccCheckCloudStackStaticNATExists(
33+
n string, ipaddr *cloudstack.PublicIpAddress) resource.TestCheckFunc {
34+
return func(s *terraform.State) error {
35+
rs, ok := s.RootModule().Resources[n]
36+
if !ok {
37+
return fmt.Errorf("Not found: %s", n)
38+
}
39+
40+
if rs.Primary.ID == "" {
41+
return fmt.Errorf("No static NAT ID is set")
42+
}
43+
44+
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
45+
ip, _, err := cs.Address.GetPublicIpAddressByID(rs.Primary.ID)
46+
47+
if err != nil {
48+
return err
49+
}
50+
51+
if ip.Id != rs.Primary.ID {
52+
return fmt.Errorf("Static NAT not found")
53+
}
54+
55+
if !ip.Isstaticnat {
56+
return fmt.Errorf("Static NAT not enabled")
57+
}
58+
59+
*ipaddr = *ip
60+
61+
return nil
62+
}
63+
}
64+
65+
func testAccCheckCloudStackStaticNATAttributes(
66+
ipaddr *cloudstack.PublicIpAddress) resource.TestCheckFunc {
67+
return func(s *terraform.State) error {
68+
69+
if ipaddr.Associatednetworkname != CLOUDSTACK_NETWORK_1 {
70+
return fmt.Errorf("Bad network: %s", ipaddr.Associatednetworkname)
71+
}
72+
73+
if ipaddr.Virtualmachinename != "terraform-test" {
74+
return fmt.Errorf("Bad virtual_machine: %s", ipaddr.Virtualmachinename)
75+
}
76+
77+
return nil
78+
}
79+
}
80+
81+
func testAccCheckCloudStackStaticNATDestroy(s *terraform.State) error {
82+
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
83+
84+
for _, rs := range s.RootModule().Resources {
85+
if rs.Type != "cloudstack_static_nat" {
86+
continue
87+
}
88+
89+
if rs.Primary.ID == "" {
90+
return fmt.Errorf("No static NAT ID is set")
91+
}
92+
93+
ip, _, err := cs.Address.GetPublicIpAddressByID(rs.Primary.ID)
94+
if err == nil && ip.Isstaticnat {
95+
return fmt.Errorf("Static NAT %s still enabled", rs.Primary.ID)
96+
}
97+
}
98+
99+
return nil
100+
}
101+
102+
var testAccCloudStackStaticNAT_basic = fmt.Sprintf(`
103+
resource "cloudstack_instance" "foobar" {
104+
name = "terraform-test"
105+
display_name = "terraform-test"
106+
service_offering= "%s"
107+
network = "%s"
108+
template = "%s"
109+
zone = "%s"
110+
user_data = "foobar\nfoo\nbar"
111+
expunge = true
112+
}
113+
114+
resource "cloudstack_ipaddress" "foo" {
115+
network = "%s"
116+
}
117+
118+
resource "cloudstack_static_nat" "foo" {
119+
ipaddress = "${cloudstack_ipaddress.foo.id}"
120+
network = "${cloudstack_ipaddress.foo.network}"
121+
virtual_machine = "${cloudstack_instance.foobar.id}"
122+
}`,
123+
CLOUDSTACK_SERVICE_OFFERING_1,
124+
CLOUDSTACK_NETWORK_1,
125+
CLOUDSTACK_TEMPLATE,
126+
CLOUDSTACK_ZONE,
127+
CLOUDSTACK_NETWORK_1,
128+
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
layout: "cloudstack"
3+
page_title: "CloudStack: cloudstack_static_nat"
4+
sidebar_current: "docs-cloudstack-resource-static-nat"
5+
description: |-
6+
Enables static NAT for a given IP address.
7+
---
8+
9+
# cloudstack\_static\_nat
10+
11+
Enables static NAT for a given IP address
12+
13+
## Example Usage
14+
15+
```
16+
resource "cloudstack_static_nat" "default" {
17+
ipaddress = "192.168.0.1"
18+
virtual_machine = "server-1"
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
The following arguments are supported:
25+
26+
* `ipaddress` - (Required) The name or ID of the public IP address for which
27+
static NAT will be enabled. Changing this forces a new resource to be
28+
created.
29+
30+
* `network` - (Optional) The name or ID of the network of the VM the static
31+
NAT will be enabled for. Required when public IP address is not
32+
associated with any guest network yet (VPC case). Changing this forces
33+
a new resource to be created.
34+
35+
* `virtual_machine` - (Required) The name or ID of the virtual machine to
36+
enable the static NAT feature for. Changing this forces a new resource
37+
to be created.
38+
39+
* `vm_guest_ip` - (Optional) The virtual machine IP address for the port
40+
forwarding rule (useful when the virtual machine has a secondairy NIC).
41+
Changing this forces a new resource to be created.
42+
43+
## Attributes Reference
44+
45+
The following attributes are exported:
46+
47+
* `id` - The static nat ID.
48+
* `network` - The network the public IP address is associated with.
49+
* `vm_guest_ip` - The IP address of the virtual machine that is used
50+
for the port forwarding rule.

0 commit comments

Comments
 (0)