Skip to content

Commit bad3a87

Browse files
author
Sander van Harmelen
authored
provider/cloudstack: add support for multiple NICs with port forwards and set network_domain for networks (hashicorp#10638)
* Add support for multiple NICs with port forwards * Fix issue hashicorp#9801
1 parent 5016a56 commit bad3a87

6 files changed

Lines changed: 65 additions & 14 deletions

File tree

builtin/providers/cloudstack/resource_cloudstack_network.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ func resourceCloudStackNetwork() *schema.Resource {
7777
ForceNew: true,
7878
},
7979

80+
"network_domain": &schema.Schema{
81+
Type: schema.TypeString,
82+
Optional: true,
83+
Computed: true,
84+
},
85+
8086
"network_offering": &schema.Schema{
8187
Type: schema.TypeString,
8288
Required: true,
@@ -165,6 +171,11 @@ func resourceCloudStackNetworkCreate(d *schema.ResourceData, meta interface{}) e
165171
p.SetEndip(endip)
166172
}
167173

174+
// Set the network domain if we have one
175+
if networkDomain, ok := d.GetOk("network_domain"); ok {
176+
p.SetNetworkdomain(networkDomain.(string))
177+
}
178+
168179
if vlan, ok := d.GetOk("vlan"); ok {
169180
p.SetVlan(strconv.Itoa(vlan.(int)))
170181
}
@@ -225,6 +236,7 @@ func resourceCloudStackNetworkRead(d *schema.ResourceData, meta interface{}) err
225236
d.Set("display_text", n.Displaytext)
226237
d.Set("cidr", n.Cidr)
227238
d.Set("gateway", n.Gateway)
239+
d.Set("network_domain", n.Networkdomain)
228240
d.Set("vpc_id", n.Vpcid)
229241

230242
if n.Aclid == "" {
@@ -270,6 +282,11 @@ func resourceCloudStackNetworkUpdate(d *schema.ResourceData, meta interface{}) e
270282
p.SetGuestvmcidr(d.Get("cidr").(string))
271283
}
272284

285+
// Check if the network domain is changed
286+
if d.HasChange("network_domain") {
287+
p.SetNetworkdomain(d.Get("network_domain").(string))
288+
}
289+
273290
// Check if the network offering is changed
274291
if d.HasChange("network_offering") {
275292
// Retrieve the network_offering ID

builtin/providers/cloudstack/resource_cloudstack_port_forward.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ func resourceCloudStackPortForward() *schema.Resource {
6565
Required: true,
6666
},
6767

68+
"vm_guest_ip": &schema.Schema{
69+
Type: schema.TypeString,
70+
Optional: true,
71+
Computed: true,
72+
},
73+
6874
"uuid": &schema.Schema{
6975
Type: schema.TypeString,
7076
Computed: true,
@@ -154,9 +160,28 @@ func createPortForward(d *schema.ResourceData, meta interface{}, forward map[str
154160
p := cs.Firewall.NewCreatePortForwardingRuleParams(d.Id(), forward["private_port"].(int),
155161
forward["protocol"].(string), forward["public_port"].(int), vm.Id)
156162

157-
// Set the network ID, needed when the public IP address
158-
// is not associated with any network yet (VPC case)
159-
p.SetNetworkid(vm.Nic[0].Networkid)
163+
if vmGuestIP, ok := forward["vm_guest_ip"]; ok {
164+
p.SetVmguestip(vmGuestIP.(string))
165+
166+
// Set the network ID based on the guest IP, needed when the public IP address
167+
// is not associated with any network yet
168+
NICS:
169+
for _, nic := range vm.Nic {
170+
if vmGuestIP.(string) == nic.Ipaddress {
171+
p.SetNetworkid(nic.Networkid)
172+
break NICS
173+
}
174+
for _, ip := range nic.Secondaryip {
175+
if vmGuestIP.(string) == ip.Ipaddress {
176+
p.SetNetworkid(nic.Networkid)
177+
break NICS
178+
}
179+
}
180+
}
181+
} else {
182+
// If no guest IP is configured, use the primary NIC
183+
p.SetNetworkid(vm.Nic[0].Networkid)
184+
}
160185

161186
// Do not open the firewall automatically in any case
162187
p.SetOpenfirewall(false)
@@ -248,6 +273,7 @@ func resourceCloudStackPortForwardRead(d *schema.ResourceData, meta interface{})
248273
forward["private_port"] = privPort
249274
forward["public_port"] = pubPort
250275
forward["virtual_machine_id"] = f.Virtualmachineid
276+
forward["vm_guest_ip"] = f.Vmguestip
251277

252278
forwards.Add(forward)
253279
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,19 @@ The following arguments are supported:
3737
* `gateway` - (Optional) Gateway that will be provided to the instances in this
3838
network. Defaults to the first usable IP in the range.
3939

40-
* `startip` - (Optional) Start of the IP block that will be available on the
40+
* `startip` - (Optional) Start of the IP block that will be available on the
4141
network. Defaults to the second available IP in the range.
4242

43-
* `endip` - (Optional) End of the IP block that will be available on the
43+
* `endip` - (Optional) End of the IP block that will be available on the
4444
network. Defaults to the last available IP in the range.
4545

46+
* `network_domain` - (Optional) DNS domain for the network.
47+
4648
* `network_offering` - (Required) The name or ID of the network offering to use
4749
for this network.
4850

4951
* `vlan` - (Optional) The VLAN number (1-4095) the network will use. This might be
50-
required by the Network Offering if specifyVlan=true is set. Only the ROOT
52+
required by the Network Offering if specifyVlan=true is set. Only the ROOT
5153
admin can set this value.
5254

5355
* `vpc_id` - (Optional) The VPC ID in which to create this network. Changing
@@ -64,11 +66,12 @@ The following arguments are supported:
6466
* `zone` - (Required) The name or ID of the zone where this network will be
6567
available. Changing this forces a new resource to be created.
6668

67-
* `tags` - (Optional) A mapping of tags to assign to the resource.
69+
* `tags` - (Optional) A mapping of tags to assign to the resource.
6870

6971
## Attributes Reference
7072

7173
The following attributes are exported:
7274

7375
* `id` - The ID of the network.
7476
* `display_text` - The display text of the network.
77+
* `network_domain` - DNS domain for the network.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ The `forward` block supports:
5050

5151
* `virtual_machine_id` - (Required) The ID of the virtual machine to forward to.
5252

53+
* `vm_guest_ip` - (Optional) The virtual machine IP address for the port
54+
forwarding rule (useful when the virtual machine has secondairy NICs
55+
or IP addresses).
56+
5357
## Attributes Reference
5458

5559
The following attributes are exported:
5660

5761
* `id` - The ID of the IP address for which the port forwards are created.
58-
62+
* `vm_guest_ip` - The IP address of the virtual machine that is used
63+
for the port forwarding rule.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ The following arguments are supported:
2929
* `virtual_machine_id` - (Required) The virtual machine ID to enable the
3030
static NAT feature for. Changing this forces a new resource to be created.
3131

32-
* `vm_guest_ip` - (Optional) The virtual machine IP address for the port
33-
forwarding rule (useful when the virtual machine has a secondairy NIC).
34-
Changing this forces a new resource to be created.
32+
* `vm_guest_ip` - (Optional) The virtual machine IP address to forward the
33+
static NAT traffic to (useful when the virtual machine has secondary
34+
NICs or IP addresses). Changing this forces a new resource to be created.
3535

3636
* `project` - (Optional) The name or ID of the project to deploy this
3737
instance to. Changing this forces a new resource to be created.
@@ -42,4 +42,4 @@ The following attributes are exported:
4242

4343
* `id` - The static nat ID.
4444
* `vm_guest_ip` - The IP address of the virtual machine that is used
45-
for the port forwarding rule.
45+
to forward the static NAT traffic to.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ The following arguments are supported:
3737
* `vpc_offering` - (Required) The name or ID of the VPC offering to use for this VPC.
3838
Changing this forces a new resource to be created.
3939

40-
* `network_domain` - (Optional) DNS domain for guest
41-
networks. Changing this forces a new resource to be created.
40+
* `network_domain` - (Optional) The default DNS domain for networks created in
41+
this VPC. Changing this forces a new resource to be created.
4242

4343
* `project` - (Optional) The name or ID of the project to deploy this
4444
instance to. Changing this forces a new resource to be created.

0 commit comments

Comments
 (0)