Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default_external_network_ip computed field to resource and data source vcd_edge_gateway #389

Merged
merged 14 commits into from
Nov 12, 2019
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ IMPROVEMENTS:
* `resource/vcd_nsxv_firewall_rule` `rule_tag` must be int to avoid vCD internal exception
passthrough - [GH-384]
* Fix code warnings from `staticcheck` and add command `make static` to Travis tests
* `resource/vcd_edge_gateway` and `datasource/vcd_edge_gateway` add `default_external_network_ip`
field to export default edge gateway IP address - [GH-389]

BUG FIXES:

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.13

require (
github.com/hashicorp/terraform-plugin-sdk v1.0.0
github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.3
github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.4
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ github.com/ulikunitz/xz v0.5.5 h1:pFrO0lVpTBXLpYw+pnLj6TbvHuyjXMfjGeCwSqCVwok=
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/vmihailenco/msgpack v3.3.3+incompatible h1:wapg9xDUZDzGCNFlwc5SqI1rvcciqcxEHac4CYj89xI=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.3 h1:QAWHNctlYWrORrYRQiJcGtg+Aqt504oe6Q99iF3LrP0=
github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.3/go.mod h1:VqfkCixIzRmj4EzF2yFJKB+aKDW6GkXlLbFh5xZ+qqs=
github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.4 h1:O/gkoZ5aS7e/t3VeyJ4Emn02DTFZQzjGTHkXwXS03Lw=
github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.4/go.mod h1:VqfkCixIzRmj4EzF2yFJKB+aKDW6GkXlLbFh5xZ+qqs=
github.com/zclconf/go-cty v1.0.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v1.1.0 h1:uJwc9HiBOCpoKIObTQaLR+tsEXx1HBHnOsOOpcdhZgw=
github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
Expand Down
5 changes: 5 additions & 0 deletions vcd/datasource_vcd_edgegateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func datasourceVcdEdgeGateway() *schema.Resource {
Computed: true,
Description: "External network to be used as default gateway. Its name must be included in 'external_networks'. An empty value will skip the default gateway",
},
"default_external_network_ip": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "IP address of edge gateway interface which is used as default.",
},
"distributed_routing": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Expand Down
30 changes: 24 additions & 6 deletions vcd/resource_vcd_edgegateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func resourceVcdEdgeGateway() *schema.Resource {
ForceNew: true,
Description: "External network to be used as default gateway. Its name must be included in 'external_networks'. An empty value will skip the default gateway",
},
"default_external_network_ip": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "IP address of edge gateway interface which is used as default.",
},
"distributed_routing": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -371,7 +376,10 @@ func setEdgeGatewayValues(d *schema.ResourceData, egw govcd.EdgeGateway) error {
for _, net := range egw.EdgeGateway.Configuration.GatewayInterfaces.GatewayInterface {
if net.InterfaceType == "uplink" {
networks = append(networks, net.Network.Name)
gateways[net.SubnetParticipation.Gateway] = net.Network.Name

for _, subnet := range net.SubnetParticipation {
gateways[subnet.Gateway] = net.Network.Name
}
}
}
err = d.Set("external_networks", networks)
Expand All @@ -383,14 +391,24 @@ func setEdgeGatewayValues(d *schema.ResourceData, egw govcd.EdgeGateway) error {
_ = d.Set("ha_enabled", egw.EdgeGateway.Configuration.HaEnabled)

for _, gw := range egw.EdgeGateway.Configuration.GatewayInterfaces.GatewayInterface {
if gw.SubnetParticipation == nil || gw.SubnetParticipation.Gateway == "" {
if len(gw.SubnetParticipation) < 1 {
log.Printf("[DEBUG] [setEdgeGatewayValues] gateway %s is missing SubnetParticipation elements: %+#v",
egw.EdgeGateway.Name, gw)
return fmt.Errorf("[setEdgeGatewayValues] gateway %s is missing SubnetParticipation elements", egw.EdgeGateway.Name)

return fmt.Errorf("[setEdgeGatewayValues] gateway %s is missing SubnetParticipation elements",
egw.EdgeGateway.Name)
}
defaultGwNet, ok := gateways[gw.SubnetParticipation.Gateway]
if ok {
_ = d.Set("default_gateway_network", defaultGwNet)

for _, subnet := range gw.SubnetParticipation {
defaultGwNet, ok := gateways[subnet.Gateway]
if ok { // found default gateway network - set it
_ = d.Set("default_gateway_network", defaultGwNet)
}

// Check if this subnet is used as default gateway and set the IP
if subnet.UseForDefaultRoute {
_ = d.Set("default_external_network_ip", subnet.IPAddress)
}
}
}
// TODO: Enable this setting after we switch to a higher API version.
Expand Down
18 changes: 18 additions & 0 deletions vcd/resource_vcd_edgegateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package vcd
import (
"fmt"
"os"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand All @@ -14,6 +15,9 @@ import (
var (
edgeGatewayNameBasic string = "TestEdgeGatewayBasic"
edgeGatewayNameComplex string = "TestEdgeGatewayComplex"
// ipV4Regex matches any IP like format x.x.x.x and can be used to check if a returned value
// resembles an IP address
ipV4Regex = regexp.MustCompile(`^(?:\d+\.){3}\d+$`)
)

// Since we can't set the "advanced" property to false by default,
Expand Down Expand Up @@ -61,6 +65,7 @@ func TestAccVcdEdgeGatewayBasic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"vcd_edgegateway."+edgeGatewayNameBasic, "default_gateway_network", testConfig.Networking.ExternalNetwork),
resource.TestMatchResourceAttr("vcd_edgegateway."+edgeGatewayNameBasic, "default_external_network_ip", ipV4Regex),
),
},
resource.TestStep{
Expand Down Expand Up @@ -133,6 +138,9 @@ func TestAccVcdEdgeGatewayComplex(t *testing.T) {
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "lb_acceleration_enabled", "false"),
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "lb_logging_enabled", "false"),
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "lb_loglevel", "info"),
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "default_external_network_ip", "192.168.30.51"),

resourceFieldsEqual("vcd_edgegateway."+edgeGatewayNameComplex, "data.vcd_edgegateway.edge", []string{}),
),
},
resource.TestStep{
Expand All @@ -145,6 +153,7 @@ func TestAccVcdEdgeGatewayComplex(t *testing.T) {
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "lb_acceleration_enabled", "true"),
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "lb_logging_enabled", "true"),
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "lb_loglevel", "critical"),
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "default_external_network_ip", "192.168.30.51"),
),
},
resource.TestStep{
Expand All @@ -155,6 +164,7 @@ func TestAccVcdEdgeGatewayComplex(t *testing.T) {
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "fw_enabled", "true"),
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "fw_default_rule_logging_enabled", "true"),
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "fw_default_rule_action", "accept"),
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "default_external_network_ip", "192.168.30.51"),
),
},
resource.TestStep{ // step3
Expand All @@ -173,6 +183,7 @@ func TestAccVcdEdgeGatewayComplex(t *testing.T) {
"vcd_edgegateway."+edgeGatewayNameComplex, "default_gateway_network", newExternalNetworkVcd),
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "lb_enabled", "true"),
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "fw_enabled", "true"),
resource.TestCheckResourceAttr("vcd_edgegateway."+edgeGatewayNameComplex, "default_external_network_ip", "192.168.30.51"),
),
},
},
Expand Down Expand Up @@ -267,6 +278,13 @@ resource "vcd_edgegateway" "{{.EdgeGateway}}" {
advanced = {{.Advanced}}
external_networks = [ "{{.ExternalNetwork}}", "${vcd_external_network.{{.NewExternalNetwork}}.name}" ]
}

data "vcd_edgegateway" "edge" {
org = "{{.Org}}"
vdc = "{{.Vdc}}"

name = "${vcd_edgegateway.{{.EdgeGateway}}.name}"
}
`

const testAccEdgeGatewayComplexWithLb = testAccEdgeGatewayComplexNetwork + `
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 19 additions & 18 deletions vendor/github.com/vmware/go-vcloud-director/v2/types/v56/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/vmware/go-vcloud-director/v2/util/tar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ github.com/ulikunitz/xz/lzma
# github.com/vmihailenco/msgpack v3.3.3+incompatible
github.com/vmihailenco/msgpack
github.com/vmihailenco/msgpack/codes
# github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.3
# github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.4
github.com/vmware/go-vcloud-director/v2/govcd
github.com/vmware/go-vcloud-director/v2/types/v56
github.com/vmware/go-vcloud-director/v2/util
Expand Down
6 changes: 6 additions & 0 deletions website/docs/r/edgegateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ order) logging. Default `false`.
* `fw_default_rule_action` (Optional) Default firewall rule (last in the processing order) action.
One of `accept` or `deny`. Default `deny`.

## Attribute Reference

The following attributes are exported on this resource:

* `default_external_network_ip` (*v2.6+*) - IP address of edge gateway used for default network

## Importing

Supported in provider *v2.5+*
Expand Down
8 changes: 5 additions & 3 deletions website/docs/r/lb_virtual_server.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ variable "protocol" {
default = "http"
}

variable "edge_gateway_ip" {
default = "192.168.1.110" # IP address of edge gateway uplink interface
data "vcd_edgegateway" "mygw" {
org = "${var.org}"
vdc = "${var.vdc}"
name = "${var.edge_gateway.my-edge-gw}"
}

resource "vcd_lb_virtual_server" "http" {
Expand All @@ -67,7 +69,7 @@ resource "vcd_lb_virtual_server" "http" {
edge_gateway = "${var.edge_gateway}"

name = "my-virtual-server"
ip_address = "${var.edge_gateway_ip}"
ip_address = "${data.vcd_edgegateway.mygw.default_external_network_ip}"
protocol = "${var.protocol}"
port = 8888

Expand Down
10 changes: 8 additions & 2 deletions website/docs/r/nsxv_firewall_rule.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ used to create, modify, and delete firewall rules. Replaces
~> **Note:** This resource requires advanced edge gateway (NSX-V). For non-advanced edge gateways please
use the [`vcd_firewall_rules`](/docs/providers/vcd/r/firewall_rules.html) resource.

## Example Usage 1 (Minimal input)
## Example Usage 1 (Minimal input with dynamic edge gateway IP)

```hcl
data "vcd_edgegateway" "mygw" {
org = "my-org"
vdc = "my-vdc"
name = "my-edge-gateway-name"
}

resource "vcd_nsxv_firewall_rule" "my-rule-1" {
org = "my-org"
vdc = "my-vdc"
Expand All @@ -29,7 +35,7 @@ resource "vcd_nsxv_firewall_rule" "my-rule-1" {
}

destination {
ip_addresses = ["192.168.1.110"]
ip_addresses = ["${data.vcd_edgegateway.mygw.default_external_network_ip}"]
}

service {
Expand Down