Skip to content

Commit fbf2771

Browse files
author
Sander van Harmelen
authored
Remove the need for specifying a network ID (hashicorp#10204)
When using the static NAT resource, you no longer have to specify a `network_id`. This can be inferred from the choosen `virtual_machine_id` and/or the `vm_guest_ip`.
1 parent 3920460 commit fbf2771

4 files changed

Lines changed: 29 additions & 20 deletions

File tree

builtin/providers/cloudstack/resource_cloudstack_port_forward.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ func createPortForward(d *schema.ResourceData, meta interface{}, forward map[str
154154
p := cs.Firewall.NewCreatePortForwardingRuleParams(d.Id(), forward["private_port"].(int),
155155
forward["protocol"].(string), forward["public_port"].(int), vm.Id)
156156

157-
// Set the network ID of the default network, needed when public IP address
158-
// is not associated with any Guest network yet (VPC case)
157+
// Set the network ID, needed when the public IP address
158+
// is not associated with any network yet (VPC case)
159159
p.SetNetworkid(vm.Nic[0].Networkid)
160160

161161
// Do not open the firewall automatically in any case

builtin/providers/cloudstack/resource_cloudstack_static_nat.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ func resourceCloudStackStaticNAT() *schema.Resource {
2424
},
2525

2626
"network_id": &schema.Schema{
27-
Type: schema.TypeString,
28-
Optional: true,
29-
Computed: true,
30-
ForceNew: true,
27+
Type: schema.TypeString,
28+
Optional: true,
29+
ForceNew: true,
30+
Deprecated: "network_id is deprecated and can be safely omitted",
3131
},
3232

3333
"virtual_machine_id": &schema.Schema{
@@ -57,20 +57,34 @@ func resourceCloudStackStaticNATCreate(d *schema.ResourceData, meta interface{})
5757
cs := meta.(*cloudstack.CloudStackClient)
5858

5959
ipaddressid := d.Get("ip_address_id").(string)
60-
virtualmachineid := d.Get("virtual_machine_id").(string)
6160

62-
// Create a new parameter struct
63-
p := cs.NAT.NewEnableStaticNatParams(ipaddressid, virtualmachineid)
64-
65-
if networkid, ok := d.GetOk("network_id"); ok {
66-
p.SetNetworkid(networkid.(string))
61+
vm, _, err := cs.VirtualMachine.GetVirtualMachineByID(
62+
d.Get("virtual_machine_id").(string),
63+
cloudstack.WithProject(d.Get("project").(string)),
64+
)
65+
if err != nil {
66+
return err
6767
}
6868

69+
// Create a new parameter struct
70+
p := cs.NAT.NewEnableStaticNatParams(ipaddressid, vm.Id)
71+
6972
if vmGuestIP, ok := d.GetOk("vm_guest_ip"); ok {
7073
p.SetVmguestip(vmGuestIP.(string))
74+
75+
// Set the network ID based on the guest IP, needed when the public IP address
76+
// is not associated with any network yet (VPC case)
77+
for _, nic := range vm.Nic {
78+
if vmGuestIP.(string) == nic.Ipaddress {
79+
p.SetNetworkid(nic.Networkid)
80+
}
81+
}
82+
} else {
83+
// If no guest IP is configured, use the primary NIC
84+
p.SetNetworkid(vm.Nic[0].Networkid)
7185
}
7286

73-
_, err := cs.NAT.EnableStaticNat(p)
87+
_, err = cs.NAT.EnableStaticNat(p)
7488
if err != nil {
7589
return fmt.Errorf("Error enabling static NAT: %s", err)
7690
}
@@ -124,7 +138,6 @@ func resourceCloudStackStaticNATRead(d *schema.ResourceData, meta interface{}) e
124138
return nil
125139
}
126140

127-
d.Set("network_id", ip.Associatednetworkid)
128141
d.Set("virtual_machine_id", ip.Virtualmachineid)
129142
d.Set("vm_guest_ip", ip.Vmipaddress)
130143

builtin/providers/cloudstack/resource_cloudstack_static_nat_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ resource "cloudstack_ipaddress" "foo" {
113113
114114
resource "cloudstack_static_nat" "foo" {
115115
ip_address_id = "${cloudstack_ipaddress.foo.id}"
116-
network_id = "${cloudstack_ipaddress.foo.network_id}"
117116
virtual_machine_id = "${cloudstack_instance.foobar.id}"
118117
}`,
119118
CLOUDSTACK_SERVICE_OFFERING_1,

website/source/docs/providers/cloudstack/r/static_nat.html.markdown

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ The following arguments are supported:
2626
* `ip_address_id` - (Required) The public IP address ID for which static
2727
NAT will be enabled. Changing this forces a new resource to be created.
2828

29-
* `network_id` - (Optional) The network ID of the VM the static NAT will be
30-
enabled for. Required when public IP address is not associated with any
31-
guest network yet (VPC case). Changing this forces a new resource to be
32-
created.
29+
* `network_id` - (Deprecated) The network ID of the VM the static NAT will be
30+
enabled for. This argument is no longer needed and can be safely omitted.
3331

3432
* `virtual_machine_id` - (Required) The virtual machine ID to enable the
3533
static NAT feature for. Changing this forces a new resource to be created.
@@ -46,6 +44,5 @@ The following arguments are supported:
4644
The following attributes are exported:
4745

4846
* `id` - The static nat ID.
49-
* `network` - The network the public IP address is associated with.
5047
* `vm_guest_ip` - The IP address of the virtual machine that is used
5148
for the port forwarding rule.

0 commit comments

Comments
 (0)