From d09d560d6437a0c3f2cf46ac33d08c5605e9ceba Mon Sep 17 00:00:00 2001 From: Dainius S Date: Thu, 5 Dec 2019 16:28:40 +0200 Subject: [PATCH 01/19] Acceptance test passes --- go.mod | 2 + go.sum | 4 +- vcd/provider.go | 1 + vcd/resource_vcd_nsxv_dhcp_relay.go | 255 +++++++++++++++++++++++ vcd/resource_vcd_nsxv_dhcp_relay_test.go | 65 ++++++ vcd/structure.go | 5 + 6 files changed, 330 insertions(+), 2 deletions(-) create mode 100644 vcd/resource_vcd_nsxv_dhcp_relay.go create mode 100644 vcd/resource_vcd_nsxv_dhcp_relay_test.go diff --git a/go.mod b/go.mod index c6e9c9ba1..e9b3ba868 100644 --- a/go.mod +++ b/go.mod @@ -7,3 +7,5 @@ require ( github.com/hashicorp/terraform-plugin-sdk v1.3.0 github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.9 ) + +replace github.com/vmware/go-vcloud-director/v2 => github.com/Didainius/go-vcloud-director/v2 v2.5.0-alpha.9.0.20191205142452-25e41fadb7f5 diff --git a/go.sum b/go.sum index 853f90b81..99b18d013 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbf cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/Didainius/go-vcloud-director/v2 v2.5.0-alpha.9.0.20191205142452-25e41fadb7f5 h1:YYDvsY/2VKMxBKewGtNfNJ6lKHah1IHFpH+jCzW+ISQ= +github.com/Didainius/go-vcloud-director/v2 v2.5.0-alpha.9.0.20191205142452-25e41fadb7f5/go.mod h1:zjondbeyTfZlzhwxOzyF4K2sWWYgMEv5H91dp5dPbU8= github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8= github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE= @@ -201,8 +203,6 @@ github.com/vmihailenco/msgpack v3.3.3+incompatible h1:wapg9xDUZDzGCNFlwc5SqI1rvc github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/vmihailenco/msgpack v4.0.1+incompatible h1:RMF1enSPeKTlXrXdOcqjFUElywVZjjC6pqse21bKbEU= github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= -github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.9 h1:jHcYDaWZtSbHdgO6Q/th0fDZxP86S0oRgM79r02BPno= -github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.9/go.mod h1:zjondbeyTfZlzhwxOzyF4K2sWWYgMEv5H91dp5dPbU8= 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= diff --git a/vcd/provider.go b/vcd/provider.go index 795a832e7..9ee8c57d8 100644 --- a/vcd/provider.go +++ b/vcd/provider.go @@ -129,6 +129,7 @@ func Provider() terraform.ResourceProvider { "vcd_nsxv_snat": resourceVcdNsxvSnat(), // 2.5 "vcd_nsxv_firewall_rule": resourceVcdNsxvFirewallRule(), // 2.5 "vcd_ipset": resourceVcdIpSet(), // 2.6 + "vcd_nsxv_dhcp_relay": resourceVcdNsxvDhcpRelay(), // 2.6 }, DataSourcesMap: map[string]*schema.Resource{ diff --git a/vcd/resource_vcd_nsxv_dhcp_relay.go b/vcd/resource_vcd_nsxv_dhcp_relay.go new file mode 100644 index 000000000..ee5bf1962 --- /dev/null +++ b/vcd/resource_vcd_nsxv_dhcp_relay.go @@ -0,0 +1,255 @@ +package vcd + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/vmware/go-vcloud-director/v2/govcd" + "github.com/vmware/go-vcloud-director/v2/types/v56" +) + +func resourceVcdNsxvDhcpRelay() *schema.Resource { + return &schema.Resource{ + Create: resourceVcdNsxvDhcpRelayCreate, + Read: resourceVcdNsxvDhcpRelayRead, + // Update: resourceVcdNsxvDhcpRelayUpdate, + Delete: resourceVcdNsxvDhcpRelayDelete, + // Importer: &schema.ResourceImporter{ + // State: resourceVcdNsxvDhcpRelayImport, + // }, + + Schema: map[string]*schema.Schema{ + "org": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Description: "The name of organization to use, optional if defined at provider " + + "level. Useful when connected as sysadmin working across different organizations", + }, + "vdc": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Description: "The name of VDC to use, optional if defined at provider level", + }, + "edge_gateway": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "Edge gateway name for DHCP relay settings", + }, + "ip_addresses": { + ForceNew: true, + Optional: true, + Type: schema.TypeSet, + Description: "IP addresses ", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "domain_names": { + ForceNew: true, + Optional: true, + Type: schema.TypeSet, + Description: "IP addresses ", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "ip_sets": { + ForceNew: true, + Optional: true, + Type: schema.TypeSet, + Description: "IP addresses ", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "relay_agent": { + ForceNew: true, + Required: true, + MinItems: 1, + Type: schema.TypeSet, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "org_network": { + Required: true, + Type: schema.TypeString, + }, + "gateway_ip_address": { + Optional: true, + Computed: true, + Type: schema.TypeString, + }, + }, + }, + }, + }, + } +} + +// resourceVcdNsxvDhcpRelayCreate +func resourceVcdNsxvDhcpRelayCreate(d *schema.ResourceData, meta interface{}) error { + vcdClient := meta.(*VCDClient) + vcdClient.lockParentEdgeGtw(d) + defer vcdClient.unLockParentEdgeGtw(d) + + edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d, "edge_gateway") + if err != nil { + return fmt.Errorf(errorUnableToFindEdgeGateway, err) + } + + _, vdc, err := vcdClient.GetOrgAndVdcFromResource(d) + if err != nil { + return fmt.Errorf(errorRetrievingOrgAndVdc, err) + } + + dhcpRelayConfig, err := getDhcpRelayType(d, edgeGateway, vdc) + if err != nil { + return fmt.Errorf("could not process DHCP relay settings: %s", err) + } + + _, err = edgeGateway.UpdateDhcpRelay(dhcpRelayConfig) + if err != nil { + return fmt.Errorf("unable to update DHCP relay settings for Edge Gateway %s: %s", edgeGateway.EdgeGateway.Name, err) + } + + // Because this is not a real object but a settings property - let create a fake composite ID + fakeId := edgeGateway.EdgeGateway.ID + ":dhcpRelaySettings" + d.SetId(fakeId) + + return nil +} + +// resourceVcdNsxvDhcpRelayUpdate +func resourceVcdNsxvDhcpRelayUpdate(d *schema.ResourceData, meta interface{}) error { + return nil +} + +// resourceVcdNsxvDhcpRelayRead +func resourceVcdNsxvDhcpRelayRead(d *schema.ResourceData, meta interface{}) error { + return nil +} + +// resourceVcdNsxvDhcpRelayDelete +func resourceVcdNsxvDhcpRelayDelete(d *schema.ResourceData, meta interface{}) error { + vcdClient := meta.(*VCDClient) + vcdClient.lockParentEdgeGtw(d) + defer vcdClient.unLockParentEdgeGtw(d) + + edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d, "edge_gateway") + if err != nil { + return fmt.Errorf(errorUnableToFindEdgeGateway, err) + } + + err = edgeGateway.ResetDhcpRelay() + if err != nil { + return fmt.Errorf("could not reset DHCP relay settings: %s", err) + } + + return nil +} + +// resourceVcdNsxvDhcpRelayImport +func resourceVcdNsxvDhcpRelayImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + return []*schema.ResourceData{d}, nil +} + +func getDhcpRelayType(d *schema.ResourceData, edge *govcd.EdgeGateway, vdc *govcd.Vdc) (*types.EdgeDhcpRelay, error) { + + dhcpRelayConfig := &types.EdgeDhcpRelay{} + + // Relay server part + var ( + listOfIps []string + listOfDomainNames []string + listOfIpSetNames []string + listOfIpSetIds []string + err error + ) + + if ipAddresses, ok := d.GetOk("ip_addresses"); ok { + listOfIps = convertSchemaSetToSliceOfStrings(ipAddresses.(*schema.Set)) + } + + if domainNames, ok := d.GetOk("domain_names"); ok { + listOfDomainNames = convertSchemaSetToSliceOfStrings(domainNames.(*schema.Set)) + } + + if ipSetNames, ok := d.GetOk("ipsets"); ok { + listOfIpSetNames = convertSchemaSetToSliceOfStrings(ipSetNames.(*schema.Set)) + listOfIpSetIds, err = ipSetNamesToIds(listOfIpSetNames, vdc) + if err != nil { + return nil, fmt.Errorf("could not lookup supplied IP set IDs by their names: %s", err) + } + } + + dhcpRelayServer := &types.EdgeDhcpRelayServer{ + IpAddress: listOfIps, + Fqdns: listOfDomainNames, + GroupingObjectId: listOfIpSetIds, + } + + // Add DHCP relay server part to struct + dhcpRelayConfig.RelayServer = dhcpRelayServer + + // Relay agent part + if relayAgent, ok := d.GetOk("relay_agent"); ok { + relayAgentsSet := relayAgent.(*schema.Set) + relayAgentsSlice := relayAgentsSet.List() + relayAgentsStruct := make([]types.EdgeDhcpRelayAgent, len(relayAgentsSlice)) + if len(relayAgentsSlice) > 0 { + for index, relayAgent := range relayAgentsSlice { + relayAgentMap := convertToStringMap(relayAgent.(map[string]interface{})) + + // Lookup vNic index by network name + orgNetworkName := relayAgentMap["org_network"] + vNicIndex, _, err := edge.GetAnyVnicIndexByNetworkName(orgNetworkName) + if err != nil { + return nil, fmt.Errorf("could not lookup edge gateway interface (vNic) index by network name for network %s: %s", orgNetworkName, err) + } + + oneRelayAgent := types.EdgeDhcpRelayAgent{ + VnicIndex: vNicIndex, + } + + if gatewayIp, isSet := relayAgentMap["gateway_ip_address"]; isSet { + oneRelayAgent.GatewayInterfaceAddress = gatewayIp + } + + relayAgentsStruct[index] = oneRelayAgent + } + } + // Add all relay agent values to struct + dhcpRelayConfig.RelayAgents = &types.EdgeDhcpRelayAgents{Agents: relayAgentsStruct} + } + + return dhcpRelayConfig, nil + +} + +// To be removed when other PR is merged +func ipSetNamesToIds(ipSetNames []string, vdc *govcd.Vdc) ([]string, error) { + ipSetIds := make([]string, len(ipSetNames)) + + allIpSets, err := vdc.GetAllNsxvIpSets() + if err != nil { + return nil, fmt.Errorf("unable to fetch all IP sets in vDC %s: %s", vdc.Vdc.Name, err) + } + + for index, ipSetName := range ipSetNames { + var ipSetFound bool + for _, ipSet := range allIpSets { + if ipSet.Name == ipSetName { + ipSetIds[index] = ipSet.ID + ipSetFound = true + } + } + // If ID was not found - fail early + if !ipSetFound { + return nil, fmt.Errorf("could not find IP set with Name %s", ipSetName) + } + } + + return ipSetIds, nil +} diff --git a/vcd/resource_vcd_nsxv_dhcp_relay_test.go b/vcd/resource_vcd_nsxv_dhcp_relay_test.go new file mode 100644 index 000000000..f34026992 --- /dev/null +++ b/vcd/resource_vcd_nsxv_dhcp_relay_test.go @@ -0,0 +1,65 @@ +// +build gateway ALL functional + +package vcd + +import ( + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccVcdNsxvDhcpRelay(t *testing.T) { + + // String map to fill the template + var params = StringMap{ + "Org": testConfig.VCD.Org, + "Vdc": testConfig.VCD.Vdc, + "EdgeGateway": testConfig.Networking.EdgeGateway, + // "OrgNetwork": testConfig.Networking., + "Tags": "gateway", + } + + configText := templateFill(testAccVcdNsxvDhcpRelay, params) + debugPrintf("#[DEBUG] CONFIGURATION for step 0: %s", configText) + + if vcdShortTest { + t.Skip(acceptanceTestsSkipped) + return + } + + if !edgeGatewayIsAdvanced() { + t.Skip(t.Name() + "requires advanced edge gateway to work") + } + + resource.ParallelTest(t, resource.TestCase{ + Providers: testAccProviders, + PreCheck: func() { testAccPreCheck(t) }, + // CheckDestroy: testAccCheckVcdLbServiceMonitorDestroy(params["ServiceMonitorName"].(string)), + Steps: []resource.TestStep{ + resource.TestStep{ + Config: configText, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestMatchResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "id", regexp.MustCompile(`^.*:dhcpRelaySettings`)), + ), + }, + }, + }) +} + +const testAccVcdNsxvDhcpRelay = ` +resource "vcd_nsxv_dhcp_relay" "relay_config" { + org = "{{.Org}}" + vdc = "{{.Vdc}}" + edge_gateway = "{{.EdgeGateway}}" + + ip_addresses = ["1.1.1.1", "2.2.2.2"] + domain_names = ["servergroups.domainname.com", "other.domain.com"] + ip_sets = ["myset1", "myset2"] + relay_agent { + org_network = "my-vdc-int-net" + # gateway_ip_address = "10.10.10.5" # optional + } + +} +` diff --git a/vcd/structure.go b/vcd/structure.go index 66a284976..0c78a083d 100644 --- a/vcd/structure.go +++ b/vcd/structure.go @@ -151,3 +151,8 @@ func convertToTypeSet(param []string) []interface{} { func takeBoolPointer(value bool) *bool { return &value } + +// takeBoolPointer accepts an int and returns a pointer to this value. +func takeIntPointer(value int) *int { + return &value +} From 3b63debb1d6a7b4a619c708b9b7658dcb2249554 Mon Sep 17 00:00:00 2001 From: Dainius S Date: Thu, 5 Dec 2019 17:32:10 +0200 Subject: [PATCH 02/19] WIP --- vcd/resource_vcd_nsxv_dhcp_relay.go | 215 ++++++++++++++++++----- vcd/resource_vcd_nsxv_dhcp_relay_test.go | 21 ++- 2 files changed, 191 insertions(+), 45 deletions(-) diff --git a/vcd/resource_vcd_nsxv_dhcp_relay.go b/vcd/resource_vcd_nsxv_dhcp_relay.go index ee5bf1962..a3760c42d 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay.go @@ -2,6 +2,7 @@ package vcd import ( "fmt" + "strings" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/vmware/go-vcloud-director/v2/govcd" @@ -12,11 +13,11 @@ func resourceVcdNsxvDhcpRelay() *schema.Resource { return &schema.Resource{ Create: resourceVcdNsxvDhcpRelayCreate, Read: resourceVcdNsxvDhcpRelayRead, - // Update: resourceVcdNsxvDhcpRelayUpdate, + Update: resourceVcdNsxvDhcpRelayUpdate, Delete: resourceVcdNsxvDhcpRelayDelete, - // Importer: &schema.ResourceImporter{ - // State: resourceVcdNsxvDhcpRelayImport, - // }, + Importer: &schema.ResourceImporter{ + State: resourceVcdNsxvDhcpRelayImport, + }, Schema: map[string]*schema.Schema{ "org": { @@ -39,7 +40,6 @@ func resourceVcdNsxvDhcpRelay() *schema.Resource { Description: "Edge gateway name for DHCP relay settings", }, "ip_addresses": { - ForceNew: true, Optional: true, Type: schema.TypeSet, Description: "IP addresses ", @@ -48,7 +48,6 @@ func resourceVcdNsxvDhcpRelay() *schema.Resource { }, }, "domain_names": { - ForceNew: true, Optional: true, Type: schema.TypeSet, Description: "IP addresses ", @@ -57,7 +56,6 @@ func resourceVcdNsxvDhcpRelay() *schema.Resource { }, }, "ip_sets": { - ForceNew: true, Optional: true, Type: schema.TypeSet, Description: "IP addresses ", @@ -66,7 +64,6 @@ func resourceVcdNsxvDhcpRelay() *schema.Resource { }, }, "relay_agent": { - ForceNew: true, Required: true, MinItems: 1, Type: schema.TypeSet, @@ -88,7 +85,8 @@ func resourceVcdNsxvDhcpRelay() *schema.Resource { } } -// resourceVcdNsxvDhcpRelayCreate +// resourceVcdNsxvDhcpRelayCreate sets up DHCP relay configuration as per supplied schema +// configuration func resourceVcdNsxvDhcpRelayCreate(d *schema.ResourceData, meta interface{}) error { vcdClient := meta.(*VCDClient) vcdClient.lockParentEdgeGtw(d) @@ -114,24 +112,51 @@ func resourceVcdNsxvDhcpRelayCreate(d *schema.ResourceData, meta interface{}) er return fmt.Errorf("unable to update DHCP relay settings for Edge Gateway %s: %s", edgeGateway.EdgeGateway.Name, err) } - // Because this is not a real object but a settings property - let create a fake composite ID - fakeId := edgeGateway.EdgeGateway.ID + ":dhcpRelaySettings" + // This is not a real object but a settings property on Edge gateway - creating a fake composite + // ID + fakeId, err := getDhclRelaySettingsId(edgeGateway) + if err != nil { + return fmt.Errorf("could not construct DHCP relay settings ID: %s", err) + } + d.SetId(fakeId) return nil } -// resourceVcdNsxvDhcpRelayUpdate +// resourceVcdNsxvDhcpRelayUpdate is in fact exactly the same as create because there is no object, +// just settings to modify func resourceVcdNsxvDhcpRelayUpdate(d *schema.ResourceData, meta interface{}) error { - return nil + return resourceVcdNsxvDhcpRelayCreate(d, meta) } // resourceVcdNsxvDhcpRelayRead func resourceVcdNsxvDhcpRelayRead(d *schema.ResourceData, meta interface{}) error { + vcdClient := meta.(*VCDClient) + vcdClient.lockParentEdgeGtw(d) + defer vcdClient.unLockParentEdgeGtw(d) + + edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d, "edge_gateway") + if err != nil { + return fmt.Errorf(errorUnableToFindEdgeGateway, err) + } + + _, vdc, err := vcdClient.GetOrgAndVdcFromResource(d) + if err != nil { + return fmt.Errorf(errorRetrievingOrgAndVdc, err) + } + + dhcpRelaySettings, err := edgeGateway.GetDhcpRelay() + if err != nil { + return fmt.Errorf("could not read DHCP relay settings: %s", err) + } + + getDhcpRelayData(d, dhcpRelaySettings, vdc) + return nil } -// resourceVcdNsxvDhcpRelayDelete +// resourceVcdNsxvDhcpRelayDelete removes DHCP relay configuration by triggering ResetDhcpRelay() func resourceVcdNsxvDhcpRelayDelete(d *schema.ResourceData, meta interface{}) error { vcdClient := meta.(*VCDClient) vcdClient.lockParentEdgeGtw(d) @@ -150,13 +175,35 @@ func resourceVcdNsxvDhcpRelayDelete(d *schema.ResourceData, meta interface{}) er return nil } -// resourceVcdNsxvDhcpRelayImport +// resourceVcdNsxvDhcpRelayImport imports DHCP relay configuration. Because DHCP relay is just a +// settings on edge gateway and not a separate object - the ID actually does not represent any object func resourceVcdNsxvDhcpRelayImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + resourceURI := strings.Split(d.Id(), ImportSeparator) + if len(resourceURI) != 3 { + return nil, fmt.Errorf("resource name must be specified in such way org.vdc.edge-gw") + } + orgName, vdcName, edgeName := resourceURI[0], resourceURI[1], resourceURI[2] + + vcdClient := meta.(*VCDClient) + edgeGateway, err := vcdClient.GetEdgeGateway(orgName, vdcName, edgeName) + if err != nil { + return nil, fmt.Errorf(errorUnableToFindEdgeGateway, err) + } + + fakeId, err := getDhclRelaySettingsId(edgeGateway) + if err != nil { + return nil, fmt.Errorf("could not construct DHCP relay settings ID: %s", err) + } + + d.Set("org", orgName) + d.Set("vdc", vdcName) + d.Set("edge_gateway", edgeName) + d.SetId(fakeId) return []*schema.ResourceData{d}, nil } +// getDhcpRelayType converts resource schema to *types.EdgeDhcpRelay func getDhcpRelayType(d *schema.ResourceData, edge *govcd.EdgeGateway, vdc *govcd.Vdc) (*types.EdgeDhcpRelay, error) { - dhcpRelayConfig := &types.EdgeDhcpRelay{} // Relay server part @@ -194,41 +241,96 @@ func getDhcpRelayType(d *schema.ResourceData, edge *govcd.EdgeGateway, vdc *govc dhcpRelayConfig.RelayServer = dhcpRelayServer // Relay agent part - if relayAgent, ok := d.GetOk("relay_agent"); ok { - relayAgentsSet := relayAgent.(*schema.Set) - relayAgentsSlice := relayAgentsSet.List() - relayAgentsStruct := make([]types.EdgeDhcpRelayAgent, len(relayAgentsSlice)) - if len(relayAgentsSlice) > 0 { - for index, relayAgent := range relayAgentsSlice { - relayAgentMap := convertToStringMap(relayAgent.(map[string]interface{})) - - // Lookup vNic index by network name - orgNetworkName := relayAgentMap["org_network"] - vNicIndex, _, err := edge.GetAnyVnicIndexByNetworkName(orgNetworkName) - if err != nil { - return nil, fmt.Errorf("could not lookup edge gateway interface (vNic) index by network name for network %s: %s", orgNetworkName, err) - } - - oneRelayAgent := types.EdgeDhcpRelayAgent{ - VnicIndex: vNicIndex, - } - - if gatewayIp, isSet := relayAgentMap["gateway_ip_address"]; isSet { - oneRelayAgent.GatewayInterfaceAddress = gatewayIp - } - - relayAgentsStruct[index] = oneRelayAgent + relayAgent := d.Get("relay_agent") + relayAgentsStruct, err := getDhcpRelayAgentsType(relayAgent.(*schema.Set), edge) + if err != nil { + return nil, fmt.Errorf("could not process relay agents: %s", err) + } + // Add all relay agent values to struct + dhcpRelayConfig.RelayAgents = &types.EdgeDhcpRelayAgents{Agents: relayAgentsStruct} + + return dhcpRelayConfig, nil + +} + +// getDhcpRelayAgentsType converts relay_agent configuration blocks to []types.EdgeDhcpRelayAgent +func getDhcpRelayAgentsType(relayAgentsSet *schema.Set, edge *govcd.EdgeGateway) ([]types.EdgeDhcpRelayAgent, error) { + relayAgentsSlice := relayAgentsSet.List() + relayAgentsStruct := make([]types.EdgeDhcpRelayAgent, len(relayAgentsSlice)) + if len(relayAgentsSlice) > 0 { + for index, relayAgent := range relayAgentsSlice { + relayAgentMap := convertToStringMap(relayAgent.(map[string]interface{})) + + // Lookup vNic index by network name + orgNetworkName := relayAgentMap["org_network"] + vNicIndex, _, err := edge.GetAnyVnicIndexByNetworkName(orgNetworkName) + if err != nil { + return nil, fmt.Errorf("could not lookup edge gateway interface (vNic) index by network name for network %s: %s", orgNetworkName, err) } + + oneRelayAgent := types.EdgeDhcpRelayAgent{ + VnicIndex: vNicIndex, + } + + if gatewayIp, isSet := relayAgentMap["gateway_ip_address"]; isSet { + oneRelayAgent.GatewayInterfaceAddress = gatewayIp + } + + relayAgentsStruct[index] = oneRelayAgent } - // Add all relay agent values to struct - dhcpRelayConfig.RelayAgents = &types.EdgeDhcpRelayAgents{Agents: relayAgentsStruct} } - return dhcpRelayConfig, nil + return relayAgentsStruct, nil +} + +func getDhcpRelayData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRelay, vdc *govcd.Vdc) error { + + // DHCP relay server settings + relayServer := edgeRelay.RelayServer + // relayServer. + + relayServerIpAddresses := convertToTypeSet(relayServer.IpAddress) + relayServerIpAddressesSet := schema.NewSet(schema.HashSchema(&schema.Schema{Type: schema.TypeString}), relayServerIpAddresses) + err := d.Set("ip_addresses", relayServerIpAddressesSet) + if err != nil { + return fmt.Errorf("could not save ip_addresses to schema: %s", err) + } + + relayServerDomainNames := convertToTypeSet(relayServer.Fqdns) + relayServerDomainNamesSet := schema.NewSet(schema.HashSchema(&schema.Schema{Type: schema.TypeString}), relayServerDomainNames) + err = d.Set("domain_names", relayServerDomainNamesSet) + if err != nil { + return fmt.Errorf("could not save domain_names to schema: %s", err) + } + + ipSetNames, err := ipSetIdsToNames(relayServer.GroupingObjectId, vdc) + if err != nil { + return fmt.Errorf("could not find names for all IP set IDs: %s", err) + } + + relayServerIpSetNames := convertToTypeSet(ipSetNames) + relayServerIpSetNamesSet := schema.NewSet(schema.HashSchema(&schema.Schema{Type: schema.TypeString}), relayServerIpSetNames) + err = d.Set("ip_sets", relayServerIpSetNamesSet) + if err != nil { + return fmt.Errorf("could not save ip_sets to schema: %s", err) + } + + return nil +} + +// getDhclRelaySettingsId constructs a fake DHCP relay configuration ID which is needed for +// Terraform The ID is in format "edgeGateway.ID:dhcpRelaySettings". Edge Gateway ID is left here +// just in case we ever want to refer this object somewhere. +func getDhclRelaySettingsId(edge *govcd.EdgeGateway) (string, error) { + if edge.EdgeGateway.ID == "" { + return "", fmt.Errorf("edge gateway does not have ID populated") + } + id := edge.EdgeGateway.ID + ":dhcpRelaySettings" + return id, nil } -// To be removed when other PR is merged +// BELOW THIS LINE To be removed when other PR is merged func ipSetNamesToIds(ipSetNames []string, vdc *govcd.Vdc) ([]string, error) { ipSetIds := make([]string, len(ipSetNames)) @@ -253,3 +355,28 @@ func ipSetNamesToIds(ipSetNames []string, vdc *govcd.Vdc) ([]string, error) { return ipSetIds, nil } + +func ipSetIdsToNames(ipSetIds []string, vdc *govcd.Vdc) ([]string, error) { + ipSetNames := make([]string, len(ipSetIds)) + + allIpSets, err := vdc.GetAllNsxvIpSets() + if err != nil { + return nil, fmt.Errorf("unable to fetch all IP sets in vDC %s: %s", vdc.Vdc.Name, err) + } + + for index, ipSetId := range ipSetIds { + var ipSetFound bool + for _, ipSet := range allIpSets { + if ipSet.ID == ipSetId { + ipSetNames[index] = ipSet.Name + ipSetFound = true + } + } + // If ID was not found - fail early + if !ipSetFound { + return nil, fmt.Errorf("could not find IP set with ID %s", ipSetId) + } + } + + return ipSetNames, nil +} diff --git a/vcd/resource_vcd_nsxv_dhcp_relay_test.go b/vcd/resource_vcd_nsxv_dhcp_relay_test.go index f34026992..1f0365a56 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay_test.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay_test.go @@ -3,10 +3,13 @@ package vcd import ( + "fmt" "regexp" "testing" + "time" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" ) func TestAccVcdNsxvDhcpRelay(t *testing.T) { @@ -41,12 +44,27 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { Config: configText, Check: resource.ComposeAggregateTestCheckFunc( resource.TestMatchResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "id", regexp.MustCompile(`^.*:dhcpRelaySettings`)), + // sleepTester(), ), }, + resource.TestStep{ + ResourceName: "vcd_nsxv_dhcp_relay.imported", + ImportState: true, + ImportStateVerify: true, + ImportStateId: testConfig.VCD.Org + "." + testConfig.VCD.Vdc + "." + testConfig.Networking.EdgeGateway, + }, }, }) } +func sleepTester() resource.TestCheckFunc { + return func(s *terraform.State) error { + fmt.Println("sleeping") + time.Sleep(1 * time.Minute) + return nil + } +} + const testAccVcdNsxvDhcpRelay = ` resource "vcd_nsxv_dhcp_relay" "relay_config" { org = "{{.Org}}" @@ -56,7 +74,8 @@ resource "vcd_nsxv_dhcp_relay" "relay_config" { ip_addresses = ["1.1.1.1", "2.2.2.2"] domain_names = ["servergroups.domainname.com", "other.domain.com"] ip_sets = ["myset1", "myset2"] - relay_agent { + + relay_agent { org_network = "my-vdc-int-net" # gateway_ip_address = "10.10.10.5" # optional } From 517810da89d86752aa686842f79ba83c0b373bd0 Mon Sep 17 00:00:00 2001 From: Dainius S Date: Fri, 6 Dec 2019 06:51:49 +0200 Subject: [PATCH 03/19] WIP2 --- go.mod | 1 + vcd/resource_vcd_nsxv_dhcp_relay.go | 5 +++-- vcd/resource_vcd_nsxv_dhcp_relay_test.go | 21 ++++++++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index e9b3ba868..c22304f6d 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/terraform-providers/terraform-provider-vcd/v2 go 1.13 require ( + github.com/davecgh/go-spew v1.1.1 github.com/hashicorp/go-version v1.2.0 github.com/hashicorp/terraform-plugin-sdk v1.3.0 github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.9 diff --git a/vcd/resource_vcd_nsxv_dhcp_relay.go b/vcd/resource_vcd_nsxv_dhcp_relay.go index a3760c42d..ee46db0c9 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay.go @@ -2,6 +2,7 @@ package vcd import ( "fmt" + "github.com/davecgh/go-spew/spew" "strings" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -223,7 +224,7 @@ func getDhcpRelayType(d *schema.ResourceData, edge *govcd.EdgeGateway, vdc *govc listOfDomainNames = convertSchemaSetToSliceOfStrings(domainNames.(*schema.Set)) } - if ipSetNames, ok := d.GetOk("ipsets"); ok { + if ipSetNames, ok := d.GetOk("ip_sets"); ok { listOfIpSetNames = convertSchemaSetToSliceOfStrings(ipSetNames.(*schema.Set)) listOfIpSetIds, err = ipSetNamesToIds(listOfIpSetNames, vdc) if err != nil { @@ -302,7 +303,7 @@ func getDhcpRelayData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRelay, vd if err != nil { return fmt.Errorf("could not save domain_names to schema: %s", err) } - + spew.Dump(relayServer.GroupingObjectId) ipSetNames, err := ipSetIdsToNames(relayServer.GroupingObjectId, vdc) if err != nil { return fmt.Errorf("could not find names for all IP set IDs: %s", err) diff --git a/vcd/resource_vcd_nsxv_dhcp_relay_test.go b/vcd/resource_vcd_nsxv_dhcp_relay_test.go index 1f0365a56..adb1e43ba 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay_test.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay_test.go @@ -8,6 +8,7 @@ import ( "testing" "time" + "github.com/davecgh/go-spew/spew" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" ) @@ -65,6 +66,13 @@ func sleepTester() resource.TestCheckFunc { } } +func stateDumper() resource.TestCheckFunc { + return func(s *terraform.State) error { + spew.Dump(s) + return nil + } +} + const testAccVcdNsxvDhcpRelay = ` resource "vcd_nsxv_dhcp_relay" "relay_config" { org = "{{.Org}}" @@ -73,12 +81,23 @@ resource "vcd_nsxv_dhcp_relay" "relay_config" { ip_addresses = ["1.1.1.1", "2.2.2.2"] domain_names = ["servergroups.domainname.com", "other.domain.com"] - ip_sets = ["myset1", "myset2"] + ip_sets = [vcd_nsxv_ip_set.myset1.name, vcd_nsxv_ip_set.myset2.name] relay_agent { org_network = "my-vdc-int-net" # gateway_ip_address = "10.10.10.5" # optional } +} +resource "vcd_nsxv_ip_set" "myset1" { + name = "test-set1" + ip_addresses = ["192.168.1.1"] } + +resource "vcd_nsxv_ip_set" "myset2" { + name = "test-set2" + ip_addresses = ["192.168.1.1"] + } + + ` From 5d0223a6d413be71337eeacf8dd58e02275d09e2 Mon Sep 17 00:00:00 2001 From: Dainius S Date: Fri, 6 Dec 2019 08:58:42 +0200 Subject: [PATCH 04/19] Add website, tests --- vcd/datasource_vcd_nsxv_dhcp_relay.go | 72 ++++++++++ vcd/provider.go | 2 + vcd/resource_vcd_nsxv_dhcp_relay.go | 95 +++++++++---- vcd/resource_vcd_nsxv_dhcp_relay_test.go | 162 ++++++++++++++++++++--- website/docs/d/nsxv_dhcp_relay.markdown | 44 ++++++ website/docs/r/nsxv_dhcp_relay.markdown | 97 ++++++++++++++ website/vcd.erb | 6 + 7 files changed, 436 insertions(+), 42 deletions(-) create mode 100644 vcd/datasource_vcd_nsxv_dhcp_relay.go create mode 100644 website/docs/d/nsxv_dhcp_relay.markdown create mode 100644 website/docs/r/nsxv_dhcp_relay.markdown diff --git a/vcd/datasource_vcd_nsxv_dhcp_relay.go b/vcd/datasource_vcd_nsxv_dhcp_relay.go new file mode 100644 index 000000000..224a08bb6 --- /dev/null +++ b/vcd/datasource_vcd_nsxv_dhcp_relay.go @@ -0,0 +1,72 @@ +package vcd + +import ( + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func datasourceVcdNsxvDhcpRelay() *schema.Resource { + return &schema.Resource{ + Read: resourceVcdNsxvDhcpRelayRead, + Schema: map[string]*schema.Schema{ + "org": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Description: "The name of organization to use, optional if defined at provider " + + "level. Useful when connected as sysadmin working across different organizations", + }, + "vdc": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Description: "The name of VDC to use, optional if defined at provider level", + }, + "edge_gateway": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "Edge gateway name for DHCP relay settings", + }, + "ip_addresses": { + Computed: true, + Type: schema.TypeSet, + Description: "IP addresses ", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "domain_names": { + Computed: true, + Type: schema.TypeSet, + Description: "IP addresses ", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "ip_sets": { + Computed: true, + Type: schema.TypeSet, + Description: "IP addresses ", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "relay_agent": { + Computed: true, + Type: schema.TypeSet, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "org_network": { + Computed: true, + Type: schema.TypeString, + }, + "gateway_ip_address": { + Computed: true, + Type: schema.TypeString, + }, + }, + }, + }, + }, + } +} diff --git a/vcd/provider.go b/vcd/provider.go index 9ee8c57d8..38edcbd86 100644 --- a/vcd/provider.go +++ b/vcd/provider.go @@ -155,6 +155,8 @@ func Provider() terraform.ResourceProvider { "vcd_nsxv_snat": datasourceVcdNsxvSnat(), // 2.5 "vcd_nsxv_firewall_rule": datasourceVcdNsxvFirewallRule(), // 2.5 "vcd_ipset": datasourceVcdIpSet(), // 2.6 + "vcd_nsxv_dhcp_relay": datasourceVcdNsxvDhcpRelay(), // 2.6 + }, ConfigureFunc: providerConfigure, diff --git a/vcd/resource_vcd_nsxv_dhcp_relay.go b/vcd/resource_vcd_nsxv_dhcp_relay.go index ee46db0c9..54fde3afc 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay.go @@ -2,7 +2,6 @@ package vcd import ( "fmt" - "github.com/davecgh/go-spew/spew" "strings" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -10,6 +9,20 @@ import ( "github.com/vmware/go-vcloud-director/v2/types/v56" ) +var relayAgentResource = &schema.Resource{ + Schema: map[string]*schema.Schema{ + "org_network": { + Required: true, + Type: schema.TypeString, + }, + "gateway_ip_address": { + Optional: true, + Computed: true, + Type: schema.TypeString, + }, + }, +} + func resourceVcdNsxvDhcpRelay() *schema.Resource { return &schema.Resource{ Create: resourceVcdNsxvDhcpRelayCreate, @@ -68,19 +81,7 @@ func resourceVcdNsxvDhcpRelay() *schema.Resource { Required: true, MinItems: 1, Type: schema.TypeSet, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "org_network": { - Required: true, - Type: schema.TypeString, - }, - "gateway_ip_address": { - Optional: true, - Computed: true, - Type: schema.TypeString, - }, - }, - }, + Elem: relayAgentResource, }, }, } @@ -122,7 +123,7 @@ func resourceVcdNsxvDhcpRelayCreate(d *schema.ResourceData, meta interface{}) er d.SetId(fakeId) - return nil + return resourceVcdNsxvDhcpRelayRead(d, meta) } // resourceVcdNsxvDhcpRelayUpdate is in fact exactly the same as create because there is no object, @@ -131,11 +132,9 @@ func resourceVcdNsxvDhcpRelayUpdate(d *schema.ResourceData, meta interface{}) er return resourceVcdNsxvDhcpRelayCreate(d, meta) } -// resourceVcdNsxvDhcpRelayRead +// resourceVcdNsxvDhcpRelayRead reads DHCP relay configuration and persists to statefile func resourceVcdNsxvDhcpRelayRead(d *schema.ResourceData, meta interface{}) error { vcdClient := meta.(*VCDClient) - vcdClient.lockParentEdgeGtw(d) - defer vcdClient.unLockParentEdgeGtw(d) edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d, "edge_gateway") if err != nil { @@ -152,7 +151,19 @@ func resourceVcdNsxvDhcpRelayRead(d *schema.ResourceData, meta interface{}) erro return fmt.Errorf("could not read DHCP relay settings: %s", err) } - getDhcpRelayData(d, dhcpRelaySettings, vdc) + err = getDhcpRelayData(d, dhcpRelaySettings, edgeGateway, vdc) + if err != nil { + return fmt.Errorf("could not read DHCP relay settings: %s", err) + } + + // This is not a real object but a settings property on Edge gateway - creating a fake composite + // ID + fakeId, err := getDhclRelaySettingsId(edgeGateway) + if err != nil { + return fmt.Errorf("could not construct DHCP relay settings ID: %s", err) + } + + d.SetId(fakeId) return nil } @@ -177,7 +188,8 @@ func resourceVcdNsxvDhcpRelayDelete(d *schema.ResourceData, meta interface{}) er } // resourceVcdNsxvDhcpRelayImport imports DHCP relay configuration. Because DHCP relay is just a -// settings on edge gateway and not a separate object - the ID actually does not represent any object +// settings on edge gateway and not a separate object - the ID actually does not represent any +// object func resourceVcdNsxvDhcpRelayImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { resourceURI := strings.Split(d.Id(), ImportSeparator) if len(resourceURI) != 3 { @@ -284,11 +296,10 @@ func getDhcpRelayAgentsType(relayAgentsSet *schema.Set, edge *govcd.EdgeGateway) return relayAgentsStruct, nil } -func getDhcpRelayData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRelay, vdc *govcd.Vdc) error { +func getDhcpRelayData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRelay, edge *govcd.EdgeGateway, vdc *govcd.Vdc) error { // DHCP relay server settings relayServer := edgeRelay.RelayServer - // relayServer. relayServerIpAddresses := convertToTypeSet(relayServer.IpAddress) relayServerIpAddressesSet := schema.NewSet(schema.HashSchema(&schema.Schema{Type: schema.TypeString}), relayServerIpAddresses) @@ -303,7 +314,6 @@ func getDhcpRelayData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRelay, vd if err != nil { return fmt.Errorf("could not save domain_names to schema: %s", err) } - spew.Dump(relayServer.GroupingObjectId) ipSetNames, err := ipSetIdsToNames(relayServer.GroupingObjectId, vdc) if err != nil { return fmt.Errorf("could not find names for all IP set IDs: %s", err) @@ -316,22 +326,53 @@ func getDhcpRelayData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRelay, vd return fmt.Errorf("could not save ip_sets to schema: %s", err) } + // DHCP relay agent settings + relayAgents := edgeRelay.RelayAgents + + relayAgentSlice := make([]interface{}, len(relayAgents.Agents)) + + for index, agent := range relayAgents.Agents { + relayAgentMap := make(map[string]interface{}) + if agent.VnicIndex == nil { + return fmt.Errorf("DHCP relay agent configuration does not have vNic specified: %s", err) + } + // Lookup org network name by edge gateway vNic index + orgNetworkName, _, err := edge.GetNetworkNameAndTypeByVnicIndex(*agent.VnicIndex) + if err != nil { + return fmt.Errorf("could not find network name for edge gateway vNic %d: %s ", agent.VnicIndex, err) + } + + relayAgentMap["org_network"] = orgNetworkName + relayAgentMap["gateway_ip_address"] = agent.GatewayInterfaceAddress + + relayAgentSlice[index] = relayAgentMap + } + + relayAgentSet := schema.NewSet(schema.HashResource(relayAgentResource), relayAgentSlice) + err = d.Set("relay_agent", relayAgentSet) + if err != nil { + return fmt.Errorf("could not save relay_agent to schema: %s", err) + } + return nil } // getDhclRelaySettingsId constructs a fake DHCP relay configuration ID which is needed for -// Terraform The ID is in format "edgeGateway.ID:dhcpRelaySettings". Edge Gateway ID is left here -// just in case we ever want to refer this object somewhere. +// Terraform. The ID is in format "edgeGateway.ID:dhcpRelaySettings" +// (eg.: "urn:vcloud:gateway:77ccbdcd-ac04-4111-bf08-8ac294a3185b:dhcpRelay"). Edge Gateway ID is +// left here just in case we ever want to refer this object somewhere but still be able to +// distinguish it from the real edge gateway resource. func getDhclRelaySettingsId(edge *govcd.EdgeGateway) (string, error) { if edge.EdgeGateway.ID == "" { return "", fmt.Errorf("edge gateway does not have ID populated") } - id := edge.EdgeGateway.ID + ":dhcpRelaySettings" + id := edge.EdgeGateway.ID + ":dhcpRelay" return id, nil } -// BELOW THIS LINE To be removed when other PR is merged +// NOT WORTH REVIEWING BELOW THIS LINE - will be removed once +// https://github.com/terraform-providers/terraform-provider-vcd/pull/411 is merged func ipSetNamesToIds(ipSetNames []string, vdc *govcd.Vdc) ([]string, error) { ipSetIds := make([]string, len(ipSetNames)) diff --git a/vcd/resource_vcd_nsxv_dhcp_relay_test.go b/vcd/resource_vcd_nsxv_dhcp_relay_test.go index adb1e43ba..69a11df19 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay_test.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay_test.go @@ -27,6 +27,10 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { configText := templateFill(testAccVcdNsxvDhcpRelay, params) debugPrintf("#[DEBUG] CONFIGURATION for step 0: %s", configText) + params["FuncName"] = t.Name() + "-step1" + configText1 := templateFill(testAccVcdNsxvDhcpRelayUpdate, params) + debugPrintf("#[DEBUG] CONFIGURATION for step 1: %s", configText1) + if vcdShortTest { t.Skip(acceptanceTestsSkipped) return @@ -37,15 +41,37 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { } resource.ParallelTest(t, resource.TestCase{ - Providers: testAccProviders, - PreCheck: func() { testAccPreCheck(t) }, - // CheckDestroy: testAccCheckVcdLbServiceMonitorDestroy(params["ServiceMonitorName"].(string)), + Providers: testAccProviders, + PreCheck: func() { testAccPreCheck(t) }, + CheckDestroy: testAccCheckVcdDhcpRelaySettingsEmpty(), Steps: []resource.TestStep{ resource.TestStep{ Config: configText, Check: resource.ComposeAggregateTestCheckFunc( - resource.TestMatchResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "id", regexp.MustCompile(`^.*:dhcpRelaySettings`)), - // sleepTester(), + resource.TestMatchResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "id", regexp.MustCompile(`^.*:dhcpRelay$`)), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "domain_names.#", "2"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "domain_names.2956203856", "servergroups.domainname.com"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "domain_names.4048773415", "other.domain.com"), + + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_addresses.#", "2"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_addresses.1048647934", "2.2.2.2"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_addresses.251826590", "1.1.1.1"), + + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_sets.#", "2"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_sets.489836264", "test-set1"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_sets.908008747", "test-set2"), + + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.#", "2"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3348209499.org_network", "dhcp-relay-0"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3348209499.gateway_ip_address", "10.201.0.1"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3180164926.org_network", "dhcp-relay-1"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3180164926.gateway_ip_address", "10.201.1.1"), + + // Validate that data source has all fields except the hashed IP set because it is turned into slice in data source + // and only one due to outstanding problem in Terraform plugin SDK - https://github.com/hashicorp/terraform-plugin-sdk/pull/197 + resourceFieldsEqual("vcd_nsxv_dhcp_relay.relay_config", "data.vcd_nsxv_dhcp_relay.relay", + []string{"relay_agent.3348209499.gateway_ip_address", "relay_agent.3348209499.org_network", "relay_agent.#", + "relay_agent.3180164926.org_network", "relay_agent.3180164926.gateway_ip_address"}), ), }, resource.TestStep{ @@ -54,10 +80,55 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { ImportStateVerify: true, ImportStateId: testConfig.VCD.Org + "." + testConfig.VCD.Vdc + "." + testConfig.Networking.EdgeGateway, }, + resource.TestStep{ + Config: configText1, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestMatchResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "id", regexp.MustCompile(`^urn:vcloud:gateway:.*:dhcpRelay$`)), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "domain_names.#", "0"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_addresses.#", "0"), + + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_sets.#", "2"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_sets.489836264", "test-set1"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_sets.908008747", "test-set2"), + + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.#", "1"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3348209499.org_network", "dhcp-relay-0"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3348209499.gateway_ip_address", "10.201.0.1"), + + // Validate that data source has all fields except the hashed IP set because it is turned into slice in data source + // and only one due to outstanding problem in Terraform plugin SDK - https://github.com/hashicorp/terraform-plugin-sdk/pull/197 + resourceFieldsEqual("vcd_nsxv_dhcp_relay.relay_config", "data.vcd_nsxv_dhcp_relay.relay", + []string{"relay_agent.3348209499.gateway_ip_address", "relay_agent.3348209499.org_network", "relay_agent.#"}), + ), + }, }, }) } +// testAccCheckVcdDhcpRelaySettingsEmpty reads DHCP relay configuration and ensure it has no +// settings set. +func testAccCheckVcdDhcpRelaySettingsEmpty() resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := testAccProvider.Meta().(*VCDClient) + edgeGateway, err := conn.GetEdgeGateway(testConfig.VCD.Org, testConfig.VCD.Vdc, testConfig.Networking.EdgeGateway) + if err != nil { + return fmt.Errorf(errorUnableToFindEdgeGateway, err) + } + + dhcpRelaySettings, err := edgeGateway.GetDhcpRelay() + if err != nil { + return fmt.Errorf("could not read DHCP relay settings: %s", err) + } + + // Validate that DHCP relay settings are empty + if dhcpRelaySettings.RelayServer != nil || dhcpRelaySettings.RelayAgents != nil { + return fmt.Errorf("DHCP relay settings were not cleaned up") + } + + return nil + } +} + func sleepTester() resource.TestCheckFunc { return func(s *terraform.State) error { fmt.Println("sleeping") @@ -73,7 +144,30 @@ func stateDumper() resource.TestCheckFunc { } } -const testAccVcdNsxvDhcpRelay = ` +const testAccRoutedNet = ` +variable "network_types" { + type = list(string) + default = ["internal", "subinterface"] +} + +resource "vcd_network_routed" "test-routed" { + count = 2 + name = "dhcp-relay-${count.index}" + org = "{{.Org}}" + vdc = "{{.Vdc}}" + edge_gateway = "{{.EdgeGateway}}" + gateway = "10.201.${count.index}.1" + netmask = "255.255.255.0" + interface_type = var.network_types[count.index] + + static_ip_pool { + start_address = "10.201.${count.index}.10" + end_address = "10.201.${count.index}.20" + } +} +` + +const testAccVcdNsxvDhcpRelay = testAccRoutedNet + ` resource "vcd_nsxv_dhcp_relay" "relay_config" { org = "{{.Org}}" vdc = "{{.Vdc}}" @@ -81,23 +175,61 @@ resource "vcd_nsxv_dhcp_relay" "relay_config" { ip_addresses = ["1.1.1.1", "2.2.2.2"] domain_names = ["servergroups.domainname.com", "other.domain.com"] - ip_sets = [vcd_nsxv_ip_set.myset1.name, vcd_nsxv_ip_set.myset2.name] + ip_sets = [vcd_ipset.myset1.name, vcd_ipset.myset2.name] relay_agent { - org_network = "my-vdc-int-net" - # gateway_ip_address = "10.10.10.5" # optional + org_network = vcd_network_routed.test-routed[0].name + } + + relay_agent { + org_network = vcd_network_routed.test-routed[1].name + gateway_ip_address = "10.201.1.1" } } -resource "vcd_nsxv_ip_set" "myset1" { - name = "test-set1" - ip_addresses = ["192.168.1.1"] +data "vcd_nsxv_dhcp_relay" "relay" { + org = "{{.Org}}" + vdc = "{{.Vdc}}" + edge_gateway = vcd_nsxv_dhcp_relay.relay_config.edge_gateway } -resource "vcd_nsxv_ip_set" "myset2" { +resource "vcd_ipset" "myset1" { + name = "test-set1" + ip_addresses = ["192.168.1.1"] +} + +resource "vcd_ipset" "myset2" { name = "test-set2" ip_addresses = ["192.168.1.1"] - } - +} +` + +const testAccVcdNsxvDhcpRelayUpdate = testAccRoutedNet + ` +resource "vcd_nsxv_dhcp_relay" "relay_config" { + org = "{{.Org}}" + vdc = "{{.Vdc}}" + edge_gateway = "{{.EdgeGateway}}" + + ip_sets = [vcd_ipset.myset1.name, vcd_ipset.myset2.name] + + relay_agent { + org_network = vcd_network_routed.test-routed[0].name + } +} + +data "vcd_nsxv_dhcp_relay" "relay" { + org = "{{.Org}}" + vdc = "{{.Vdc}}" + edge_gateway = vcd_nsxv_dhcp_relay.relay_config.edge_gateway +} + +resource "vcd_ipset" "myset1" { + name = "test-set1" + ip_addresses = ["192.168.1.1"] +} +resource "vcd_ipset" "myset2" { + name = "test-set2" + ip_addresses = ["192.168.1.1"] +} ` diff --git a/website/docs/d/nsxv_dhcp_relay.markdown b/website/docs/d/nsxv_dhcp_relay.markdown new file mode 100644 index 000000000..22d397680 --- /dev/null +++ b/website/docs/d/nsxv_dhcp_relay.markdown @@ -0,0 +1,44 @@ +--- +layout: "vcd" +page_title: "vCloudDirector: vcd_nsxv_dhcp_relay" +sidebar_current: "docs-vcd-datasource-nsxv-dhcp-relay" +description: |- + Provides an NSX edge gateway DHCP relay configuration data source. +--- + +# vcd\_nsxv\_dhcp\_relay + +Provides a vCloud Director Edge Gateway DHCP relay configuration data source. The DHCP relay +capability provided by NSX in vCloud Director environment allows to leverage existing DHCP +infrastructure from within vCloud Director environment without any interruption to the IP address +management in existing DHCP infrastructure. DHCP messages are relayed from virtual machines to the +designated DHCP servers in your physical DHCP infrastructure, which allows IP addresses controlled +by the NSX software to continue to be in synch with IP addresses in the rest of your DHCP-controlled +environments. + +Supported in provider *v2.6+* + +## Example Usage 1 + +```hcl +data "vcd_nsxv_dhcp_relay" "relay_config" { + org = "my-org" + vdc = "my-org-vdc" + edge_gateway = "my-edge-gw" +} +``` + + +## Argument Reference + +The following arguments are supported: + +* `org` - (Optional) The name of organization to use, optional if defined at provider level. Useful + when connected as sysadmin working across different organisations. +* `vdc` - (Optional) The name of VDC to use, optional if defined at provider level. +* `edge_gateway` - (Required) The name of the edge gateway on which DHCP relay is to be configured. + +## Attribute Reference + +All the attributes defined in [`vcd_nsxv_dhcp_relay`](/docs/providers/vcd/r/nsxv_dhcp_relay.html) +resource are available. diff --git a/website/docs/r/nsxv_dhcp_relay.markdown b/website/docs/r/nsxv_dhcp_relay.markdown new file mode 100644 index 000000000..096325aaa --- /dev/null +++ b/website/docs/r/nsxv_dhcp_relay.markdown @@ -0,0 +1,97 @@ +--- +layout: "vcd" +page_title: "vCloudDirector: vcd_nsxv_dhcp_relay" +sidebar_current: "docs-vcd-resource-nsxv-dhcp-relay" +description: |- + Provides an NSX edge gateway DHCP relay configuration resource. +--- + +# vcd\_nsxv\_dhcp\_relay + +Provides a vCloud Director Edge Gateway DHCP relay configuration resource. The DHCP relay capability +provided by NSX in vCloud Director environment allows to leverage existing DHCP infrastructure from +within vCloud Director environment without any interruption to the IP address management in existing +DHCP infrastructure. DHCP messages are relayed from virtual machines to the designated DHCP servers +in your physical DHCP infrastructure, which allows IP addresses controlled by the NSX software to +continue to be in synch with IP addresses in the rest of your DHCP-controlled environments. + +Supported in provider *v2.6+* + +## Example Usage 1 (Minimal configuration) + +```hcl +resource "vcd_nsxv_dhcp_relay" "relay_config" { + org = "my-org" + vdc = "my-org-vdc" + edge_gateway = "my-edge-gw" + + ip_addresses = ["1.1.1.1"] + + relay_agent { + org_network = vcd_network_routed.test-routed[0].name + } +} +``` + +## Example Usage 2 (Example of configuration with multiple relay agents) + +```hcl +resource "vcd_nsxv_dhcp_relay" "relay_config" { + org = "my-org" + vdc = "my-org-vdc" + edge_gateway = "my-edge-gw" + + ip_addresses = ["1.1.1.1", "2.2.2.2"] + domain_names = ["servergroups.domainname.com", "other.domain.com"] + ip_sets = [vcd_ipset.myset1.name, vcd_ipset.myset2.name] + + relay_agent { + org_network = "my-routed-network-1" + } + + relay_agent { + org_network = vcd_network_routed.db-network.name + gateway_ip_address = "10.201.1.1" + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `org` - (Optional) The name of organization to use, optional if defined at provider level. Useful + when connected as sysadmin working across different organisations. +* `vdc` - (Optional) The name of VDC to use, optional if defined at provider level. +* `edge_gateway` - (Required) The name of the edge gateway on which DHCP relay is to be configured. +* `ip_addresses` - (Optional) A set of IP addresses. +* `domain_names` - (Optional) A set of domain names. +* `ip_sets` - (Optional) A set of IP set names. +* `relay_agent` - (Required) One or more blocks to define Org network and optional IP address of + edge gateway interfaces from which DHCP messages are to be relayed to the external DHCP relay + server(s). See [Relay Agent](#relay-agent) and example for usage details. + + +## Relay Agent + +* `org_network` - (Required) An existing Org network name from which DHCP messages are to be relayed. +* `gateway_ip_address` - (Optional) IP address on edge gateway to be used for relaying messages. + Primary address of edge gateway interface will be picked if not specified. + +## Importing + +~> **Note:** The current implementation of Terraform import can only import resources into the state. +It does not generate configuration. [More information.](https://www.terraform.io/docs/import/) + +An existing DHCP relay configuration can be [imported][docs-import] into this resource +via supplying the full dot separated path for your edge gateway. An example is +below: + +[docs-import]: https://www.terraform.io/docs/import/ + +``` +terraform import vcd_nsxv_dhcp_relay.imported my-org.my-org-vdc.my-edge-gw +``` + +The above would import the DHCP relay settings that are defined on edge +gateway `my-edge-gw` which is configured in organization named `my-org` and vDC named `my-org-vdc`. diff --git a/website/vcd.erb b/website/vcd.erb index 2f3d91de0..b8a1b76d5 100644 --- a/website/vcd.erb +++ b/website/vcd.erb @@ -79,6 +79,9 @@ > vcd_ipset + > + vcd_nsxv_dhcp_relay + @@ -175,6 +178,9 @@ > vcd_ipset + > + vcd_nsxv_dhcp_relay + From 427dc095b9c3fe8b05d171dd366a5c1d5b057d8f Mon Sep 17 00:00:00 2001 From: Dainius S Date: Fri, 6 Dec 2019 09:09:32 +0200 Subject: [PATCH 05/19] Add field descriptions --- go.mod | 1 - vcd/datasource_vcd_nsxv_dhcp_relay.go | 16 +++++---- vcd/resource_vcd_nsxv_dhcp_relay.go | 44 +++++++++++++++--------- vcd/resource_vcd_nsxv_dhcp_relay_test.go | 19 +--------- vcd/structure.go | 5 --- 5 files changed, 37 insertions(+), 48 deletions(-) diff --git a/go.mod b/go.mod index c22304f6d..e9b3ba868 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/terraform-providers/terraform-provider-vcd/v2 go 1.13 require ( - github.com/davecgh/go-spew v1.1.1 github.com/hashicorp/go-version v1.2.0 github.com/hashicorp/terraform-plugin-sdk v1.3.0 github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.9 diff --git a/vcd/datasource_vcd_nsxv_dhcp_relay.go b/vcd/datasource_vcd_nsxv_dhcp_relay.go index 224a08bb6..beb819d39 100644 --- a/vcd/datasource_vcd_nsxv_dhcp_relay.go +++ b/vcd/datasource_vcd_nsxv_dhcp_relay.go @@ -30,7 +30,7 @@ func datasourceVcdNsxvDhcpRelay() *schema.Resource { "ip_addresses": { Computed: true, Type: schema.TypeSet, - Description: "IP addresses ", + Description: "A set of IP address of DHCP servers", Elem: &schema.Schema{ Type: schema.TypeString, }, @@ -38,7 +38,7 @@ func datasourceVcdNsxvDhcpRelay() *schema.Resource { "domain_names": { Computed: true, Type: schema.TypeSet, - Description: "IP addresses ", + Description: "A set of IP domain names of DHCP servers", Elem: &schema.Schema{ Type: schema.TypeString, }, @@ -46,7 +46,7 @@ func datasourceVcdNsxvDhcpRelay() *schema.Resource { "ip_sets": { Computed: true, Type: schema.TypeSet, - Description: "IP addresses ", + Description: "A set of IP set names which consist DHCP servers", Elem: &schema.Schema{ Type: schema.TypeString, }, @@ -57,12 +57,14 @@ func datasourceVcdNsxvDhcpRelay() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "org_network": { - Computed: true, - Type: schema.TypeString, + Computed: true, + Type: schema.TypeString, + Description: "Org network which is to be used for relaying DHCP message to specified servers", }, "gateway_ip_address": { - Computed: true, - Type: schema.TypeString, + Computed: true, + Type: schema.TypeString, + Description: "Optional gateway IP address of org network which is to be used for relaying DHCP message to specified servers", }, }, }, diff --git a/vcd/resource_vcd_nsxv_dhcp_relay.go b/vcd/resource_vcd_nsxv_dhcp_relay.go index 54fde3afc..bff0415ab 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay.go @@ -12,13 +12,15 @@ import ( var relayAgentResource = &schema.Resource{ Schema: map[string]*schema.Schema{ "org_network": { - Required: true, - Type: schema.TypeString, + Required: true, + Type: schema.TypeString, + Description: "Org network which is to be used for relaying DHCP message to specified servers", }, "gateway_ip_address": { - Optional: true, - Computed: true, - Type: schema.TypeString, + Optional: true, + Computed: true, + Type: schema.TypeString, + Description: "Optional gateway IP address of org network which is to be used for relaying DHCP message to specified servers", }, }, } @@ -56,7 +58,7 @@ func resourceVcdNsxvDhcpRelay() *schema.Resource { "ip_addresses": { Optional: true, Type: schema.TypeSet, - Description: "IP addresses ", + Description: "A set of IP address of DHCP servers", Elem: &schema.Schema{ Type: schema.TypeString, }, @@ -64,7 +66,7 @@ func resourceVcdNsxvDhcpRelay() *schema.Resource { "domain_names": { Optional: true, Type: schema.TypeSet, - Description: "IP addresses ", + Description: "A set of IP domain names of DHCP servers", Elem: &schema.Schema{ Type: schema.TypeString, }, @@ -72,7 +74,7 @@ func resourceVcdNsxvDhcpRelay() *schema.Resource { "ip_sets": { Optional: true, Type: schema.TypeSet, - Description: "IP addresses ", + Description: "A set of IP set names which consist DHCP servers", Elem: &schema.Schema{ Type: schema.TypeString, }, @@ -151,9 +153,14 @@ func resourceVcdNsxvDhcpRelayRead(d *schema.ResourceData, meta interface{}) erro return fmt.Errorf("could not read DHCP relay settings: %s", err) } - err = getDhcpRelayData(d, dhcpRelaySettings, edgeGateway, vdc) + err = setDhcpRelayServerData(d, dhcpRelaySettings, edgeGateway, vdc) if err != nil { - return fmt.Errorf("could not read DHCP relay settings: %s", err) + return fmt.Errorf("could not set DHCP relay server settings: %s", err) + } + + err = setDhcpRelayAgentData(d, dhcpRelaySettings, edgeGateway, vdc) + if err != nil { + return fmt.Errorf("could not set DHCP relay agent settings: %s", err) } // This is not a real object but a settings property on Edge gateway - creating a fake composite @@ -296,9 +303,8 @@ func getDhcpRelayAgentsType(relayAgentsSet *schema.Set, edge *govcd.EdgeGateway) return relayAgentsStruct, nil } -func getDhcpRelayData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRelay, edge *govcd.EdgeGateway, vdc *govcd.Vdc) error { - - // DHCP relay server settings +// setDhcpRelayServerData sets DHCP relay server related fields into statefile +func setDhcpRelayServerData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRelay, edge *govcd.EdgeGateway, vdc *govcd.Vdc) error { relayServer := edgeRelay.RelayServer relayServerIpAddresses := convertToTypeSet(relayServer.IpAddress) @@ -326,7 +332,11 @@ func getDhcpRelayData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRelay, ed return fmt.Errorf("could not save ip_sets to schema: %s", err) } - // DHCP relay agent settings + return nil +} + +// setDhcpRelayAgentData sets DHCP relay agent related fields into statefile +func setDhcpRelayAgentData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRelay, edge *govcd.EdgeGateway, vdc *govcd.Vdc) error { relayAgents := edgeRelay.RelayAgents relayAgentSlice := make([]interface{}, len(relayAgents.Agents)) @@ -334,7 +344,7 @@ func getDhcpRelayData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRelay, ed for index, agent := range relayAgents.Agents { relayAgentMap := make(map[string]interface{}) if agent.VnicIndex == nil { - return fmt.Errorf("DHCP relay agent configuration does not have vNic specified: %s", err) + return fmt.Errorf("DHCP relay agent configuration does not have vNic specified") } // Lookup org network name by edge gateway vNic index orgNetworkName, _, err := edge.GetNetworkNameAndTypeByVnicIndex(*agent.VnicIndex) @@ -349,7 +359,7 @@ func getDhcpRelayData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRelay, ed } relayAgentSet := schema.NewSet(schema.HashResource(relayAgentResource), relayAgentSlice) - err = d.Set("relay_agent", relayAgentSet) + err := d.Set("relay_agent", relayAgentSet) if err != nil { return fmt.Errorf("could not save relay_agent to schema: %s", err) } @@ -358,7 +368,7 @@ func getDhcpRelayData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRelay, ed } // getDhclRelaySettingsId constructs a fake DHCP relay configuration ID which is needed for -// Terraform. The ID is in format "edgeGateway.ID:dhcpRelaySettings" +// Terraform. The ID is in format "edgeGateway.ID:dhcpRelay" // (eg.: "urn:vcloud:gateway:77ccbdcd-ac04-4111-bf08-8ac294a3185b:dhcpRelay"). Edge Gateway ID is // left here just in case we ever want to refer this object somewhere but still be able to // distinguish it from the real edge gateway resource. diff --git a/vcd/resource_vcd_nsxv_dhcp_relay_test.go b/vcd/resource_vcd_nsxv_dhcp_relay_test.go index 69a11df19..12f7092e5 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay_test.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay_test.go @@ -6,9 +6,7 @@ import ( "fmt" "regexp" "testing" - "time" - "github.com/davecgh/go-spew/spew" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" ) @@ -129,21 +127,6 @@ func testAccCheckVcdDhcpRelaySettingsEmpty() resource.TestCheckFunc { } } -func sleepTester() resource.TestCheckFunc { - return func(s *terraform.State) error { - fmt.Println("sleeping") - time.Sleep(1 * time.Minute) - return nil - } -} - -func stateDumper() resource.TestCheckFunc { - return func(s *terraform.State) error { - spew.Dump(s) - return nil - } -} - const testAccRoutedNet = ` variable "network_types" { type = list(string) @@ -159,7 +142,7 @@ resource "vcd_network_routed" "test-routed" { gateway = "10.201.${count.index}.1" netmask = "255.255.255.0" interface_type = var.network_types[count.index] - + static_ip_pool { start_address = "10.201.${count.index}.10" end_address = "10.201.${count.index}.20" diff --git a/vcd/structure.go b/vcd/structure.go index 0c78a083d..66a284976 100644 --- a/vcd/structure.go +++ b/vcd/structure.go @@ -151,8 +151,3 @@ func convertToTypeSet(param []string) []interface{} { func takeBoolPointer(value bool) *bool { return &value } - -// takeBoolPointer accepts an int and returns a pointer to this value. -func takeIntPointer(value int) *int { - return &value -} From 2e7f8bf2710417ee4575b784a264aaac614cf8ef Mon Sep 17 00:00:00 2001 From: Dainius S Date: Fri, 6 Dec 2019 09:11:45 +0200 Subject: [PATCH 06/19] Add changelog note --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe9669900..3a78b6df1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,8 +3,10 @@ FEATURES: * **New Resource:** `vcd_ipset` IP set - [GH-406] +* **New Resource:** `vcd_nsxv_dhcp_relay` Edge gateway DHCP relay configuration - [GH-416] * **New Data Source:** `vcd_vapp_vm` VM - [GH-218] * **New Data Source:** `vcd_ipset` IP set - [GH-406] +* **New Data Source:** `vcd_nsxv_dhcp_relay` Edge gateway DHCP relay configuration - [GH-416] * **New build command:** `make test-upgrade` to run an upgrade test from the previous released version IMPROVEMENTS: From 361c60ce948ccbfd7965536ef15ad7374c92b39f Mon Sep 17 00:00:00 2001 From: Dainius S Date: Fri, 6 Dec 2019 09:15:27 +0200 Subject: [PATCH 07/19] Add vendor --- .../v2/govcd/nsxv_dhcprelay.go | 81 +++++++++++++++++++ .../v2/types/v56/constants.go | 1 + .../v2/types/v56/nsxv_types.go | 44 ++++++++++ vendor/modules.txt | 2 +- 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 vendor/github.com/vmware/go-vcloud-director/v2/govcd/nsxv_dhcprelay.go diff --git a/vendor/github.com/vmware/go-vcloud-director/v2/govcd/nsxv_dhcprelay.go b/vendor/github.com/vmware/go-vcloud-director/v2/govcd/nsxv_dhcprelay.go new file mode 100644 index 000000000..4a9be007a --- /dev/null +++ b/vendor/github.com/vmware/go-vcloud-director/v2/govcd/nsxv_dhcprelay.go @@ -0,0 +1,81 @@ +/* + * Copyright 2019 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. + */ + +package govcd + +import ( + "fmt" + "net/http" + + "github.com/vmware/go-vcloud-director/v2/types/v56" +) + +// UpdateDhcpRelay updates DHCP relay settings for a particular edge gateway and returns them. The +// feature itself enables you to leverage your existing DHCP infrastructure from within NSX without +// any interruption to the IP address management in your environment. DHCP messages are relayed from +// virtual machine(s) to the designated DHCP server(s) in the physical world. This enables IP +// addresses within NSX to continue to be in sync with IP addresses in other environments. +func (egw *EdgeGateway) UpdateDhcpRelay(dhcpRelayConfig *types.EdgeDhcpRelay) (*types.EdgeDhcpRelay, error) { + if !egw.HasAdvancedNetworking() { + return nil, fmt.Errorf("only advanced edge gateways support DHCP relay") + } + + httpPath, err := egw.buildProxiedEdgeEndpointURL(types.EdgeDhcpRelayPath) + if err != nil { + return nil, fmt.Errorf("could not get Edge Gateway API endpoint: %s", err) + } + // We expect to get http.StatusNoContent or if not an error of type types.NSXError + _, err = egw.client.ExecuteRequestWithCustomError(httpPath, http.MethodPut, types.AnyXMLMime, + "error setting DHCP relay settings: %s", dhcpRelayConfig, &types.NSXError{}) + if err != nil { + return nil, err + } + + return egw.GetDhcpRelay() +} + +// GetDhcpRelay retrieves a structure of *types.EdgeDhcpRelay with all DHCP relay settings present +// on a particular edge gateway. +func (egw *EdgeGateway) GetDhcpRelay() (*types.EdgeDhcpRelay, error) { + if !egw.HasAdvancedNetworking() { + return nil, fmt.Errorf("only advanced edge gateways support DHCP relay") + } + response := &types.EdgeDhcpRelay{} + + httpPath, err := egw.buildProxiedEdgeEndpointURL(types.EdgeDhcpRelayPath) + if err != nil { + return nil, fmt.Errorf("could not get Edge Gateway API endpoint: %s", err) + } + + // This query Edge gaateway DHCP relay using proxied NSX-V API + _, err = egw.client.ExecuteRequest(httpPath, http.MethodGet, types.AnyXMLMime, + "unable to read edge gateway DHCP relay configuration: %s", nil, response) + if err != nil { + return nil, err + } + + return response, nil +} + +// ResetDhcpRelay resets all fields to empty which is equivalent of what edge gateway has upon +// creation. +func (egw *EdgeGateway) ResetDhcpRelay() error { + if !egw.HasAdvancedNetworking() { + return fmt.Errorf("only advanced edge gateways support DHCP relay") + } + + httpPath, err := egw.buildProxiedEdgeEndpointURL(types.EdgeDhcpRelayPath) + if err != nil { + return fmt.Errorf("could not get Edge Gateway API endpoint: %s", err) + } + + // Send a DELETE request to DHCP relay configuration endpoint + _, err = egw.client.ExecuteRequestWithCustomError(httpPath, http.MethodDelete, types.AnyXMLMime, + "unable to reset edge gateway DHCP relay configuration: %s", nil, &types.NSXError{}) + if err != nil { + return err + } + + return nil +} diff --git a/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/constants.go b/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/constants.go index 85bd1e94d..60c8001d6 100644 --- a/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/constants.go +++ b/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/constants.go @@ -161,6 +161,7 @@ const ( EdgeFirewallPath = "/firewall/config" EdgeCreateFirewallPath = "/firewall/config/rules" EdgeVnicConfig = "/vnics" + EdgeDhcpRelayPath = "/dhcp/config/relay" LbConfigPath = "/loadbalancer/config/" LbMonitorPath = "/loadbalancer/config/monitors/" LbServerPoolPath = "/loadbalancer/config/pools/" diff --git a/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/nsxv_types.go b/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/nsxv_types.go index 645306df3..6151d04b0 100644 --- a/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/nsxv_types.go +++ b/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/nsxv_types.go @@ -284,3 +284,47 @@ type EdgeIpSet struct { // EdgeIpSets is a slice of pointers to EdgeIpSet type EdgeIpSets []*EdgeIpSet + +// EdgeDhcpRelay - Dynamic Host Configuration Protocol (DHCP) relay enables you to leverage your +// existing DHCP infrastructure from within NSX without any interruption to the IP address +// management in your environment. DHCP messages are relayed from virtual machine(s) to the +// designated DHCP server(s) in the physical world. This enables IP addresses within NSX to continue +// to be in sync with IP addresses in other environments. +type EdgeDhcpRelay struct { + XMLName xml.Name `xml:"relay"` + // RelayServer specifies external relay server(s) to which DHCP messages are to be relayed to. + // The relay server can be an IP set, IP address block, domain, or a combination of all of + // these. Messages are relayed to each listed DHCP server. + RelayServer *EdgeDhcpRelayServer `xml:"relayServer"` + // EdgeDhcRelayAgents specifies a list of edge gateway interfaces (vNics) from which DHCP + // messages are to be relayed to the external DHCP relay server(s) with optional gateway + // interface addresses. + RelayAgents *EdgeDhcpRelayAgents `xml:"relayAgents"` +} + +type EdgeDhcpRelayServer struct { + // GroupingObjectIds is a general concept in NSX which allows to pass in many types of objects + // (like VM IDs, IP set IDs, org networks, security groups) howether in this case it accepts + // only IP sets which have IDs specified as 'f9daf2da-b4f9-4921-a2f4-d77a943a381c:ipset-2' where + // first part is vDC ID and the second part is unique IP set ID + GroupingObjectId []string `xml:"groupingObjectId,omitempty"` + // IpAddresses holds a list of IP addresses for DHCP servers + IpAddress []string `xml:"ipAddress,omitempty"` + // Fqdn holds a list of FQDNs (fully qualified domain names) + Fqdns []string `xml:"fqdn,omitempty"` +} + +// EdgeDhcpRelayAgent specifies which edge gateway interface (vNic) from which DHCP messages are to +// be relayed to the external DHCP relay server(s) with an optional gateway interface address. +type EdgeDhcpRelayAgent struct { + // VnicIndex must specify vNic adapter index on the edge gateway + VnicIndex *int `xml:"vnicIndex"` + // GatewayInterfaceAddress holds a gateway interface address. Optional, defaults to the vNic + // primary address. + GatewayInterfaceAddress string `xml:"giAddress,omitempty"` +} + +// EdgeDhcpRelayAgents holds a slice of EdgeDhcpRelayAgent +type EdgeDhcpRelayAgents struct { + Agents []EdgeDhcpRelayAgent `xml:"relayAgent"` +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 46f312d09..43f4c6472 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -224,7 +224,7 @@ github.com/ulikunitz/xz/lzma # github.com/vmihailenco/msgpack v4.0.1+incompatible github.com/vmihailenco/msgpack github.com/vmihailenco/msgpack/codes -# github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.9 +# github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.9 => github.com/Didainius/go-vcloud-director/v2 v2.5.0-alpha.9.0.20191205142452-25e41fadb7f5 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 From 95e94bf2dd4b2ce954a7ff70beb01ac78d77cb84 Mon Sep 17 00:00:00 2001 From: Dainius S Date: Fri, 6 Dec 2019 11:24:04 +0200 Subject: [PATCH 08/19] Do not check data source in update stage --- vcd/resource_vcd_nsxv_dhcp_relay_test.go | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/vcd/resource_vcd_nsxv_dhcp_relay_test.go b/vcd/resource_vcd_nsxv_dhcp_relay_test.go index 12f7092e5..2d66c4591 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay_test.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay_test.go @@ -72,12 +72,6 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { "relay_agent.3180164926.org_network", "relay_agent.3180164926.gateway_ip_address"}), ), }, - resource.TestStep{ - ResourceName: "vcd_nsxv_dhcp_relay.imported", - ImportState: true, - ImportStateVerify: true, - ImportStateId: testConfig.VCD.Org + "." + testConfig.VCD.Vdc + "." + testConfig.Networking.EdgeGateway, - }, resource.TestStep{ Config: configText1, Check: resource.ComposeAggregateTestCheckFunc( @@ -92,13 +86,14 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.#", "1"), resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3348209499.org_network", "dhcp-relay-0"), resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3348209499.gateway_ip_address", "10.201.0.1"), - - // Validate that data source has all fields except the hashed IP set because it is turned into slice in data source - // and only one due to outstanding problem in Terraform plugin SDK - https://github.com/hashicorp/terraform-plugin-sdk/pull/197 - resourceFieldsEqual("vcd_nsxv_dhcp_relay.relay_config", "data.vcd_nsxv_dhcp_relay.relay", - []string{"relay_agent.3348209499.gateway_ip_address", "relay_agent.3348209499.org_network", "relay_agent.#"}), ), }, + resource.TestStep{ + ResourceName: "vcd_nsxv_dhcp_relay.imported", + ImportState: true, + ImportStateVerify: true, + ImportStateId: testConfig.VCD.Org + "." + testConfig.VCD.Vdc + "." + testConfig.Networking.EdgeGateway, + }, }, }) } @@ -200,12 +195,6 @@ resource "vcd_nsxv_dhcp_relay" "relay_config" { } } -data "vcd_nsxv_dhcp_relay" "relay" { - org = "{{.Org}}" - vdc = "{{.Vdc}}" - edge_gateway = vcd_nsxv_dhcp_relay.relay_config.edge_gateway -} - resource "vcd_ipset" "myset1" { name = "test-set1" ip_addresses = ["192.168.1.1"] From 525ce8d8c3a5c7d64e63adfc5fb2b172dc9c1808 Mon Sep 17 00:00:00 2001 From: Dainius S Date: Fri, 6 Dec 2019 11:36:18 +0200 Subject: [PATCH 09/19] Remove commented field --- vcd/resource_vcd_nsxv_dhcp_relay_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vcd/resource_vcd_nsxv_dhcp_relay_test.go b/vcd/resource_vcd_nsxv_dhcp_relay_test.go index 2d66c4591..8a9ea08a0 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay_test.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay_test.go @@ -18,8 +18,7 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { "Org": testConfig.VCD.Org, "Vdc": testConfig.VCD.Vdc, "EdgeGateway": testConfig.Networking.EdgeGateway, - // "OrgNetwork": testConfig.Networking., - "Tags": "gateway", + "Tags": "gateway", } configText := templateFill(testAccVcdNsxvDhcpRelay, params) From dfcd5053525c63022651928d32dc3d3f2ed0c209 Mon Sep 17 00:00:00 2001 From: Dainius S Date: Mon, 9 Dec 2019 12:47:57 +0200 Subject: [PATCH 10/19] Address comments --- vcd/resource_vcd_nsxv_dhcp_relay.go | 10 +-- vcd/resource_vcd_nsxv_dhcp_relay_test.go | 102 +++++++++++------------ website/docs/d/nsxv_dhcp_relay.markdown | 2 +- website/docs/r/nsxv_dhcp_relay.markdown | 23 ++++- 4 files changed, 78 insertions(+), 59 deletions(-) diff --git a/vcd/resource_vcd_nsxv_dhcp_relay.go b/vcd/resource_vcd_nsxv_dhcp_relay.go index bff0415ab..3b809a30a 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay.go @@ -118,7 +118,7 @@ func resourceVcdNsxvDhcpRelayCreate(d *schema.ResourceData, meta interface{}) er // This is not a real object but a settings property on Edge gateway - creating a fake composite // ID - fakeId, err := getDhclRelaySettingsId(edgeGateway) + fakeId, err := getDhcpRelaySettingsId(edgeGateway) if err != nil { return fmt.Errorf("could not construct DHCP relay settings ID: %s", err) } @@ -165,7 +165,7 @@ func resourceVcdNsxvDhcpRelayRead(d *schema.ResourceData, meta interface{}) erro // This is not a real object but a settings property on Edge gateway - creating a fake composite // ID - fakeId, err := getDhclRelaySettingsId(edgeGateway) + fakeId, err := getDhcpRelaySettingsId(edgeGateway) if err != nil { return fmt.Errorf("could not construct DHCP relay settings ID: %s", err) } @@ -210,7 +210,7 @@ func resourceVcdNsxvDhcpRelayImport(d *schema.ResourceData, meta interface{}) ([ return nil, fmt.Errorf(errorUnableToFindEdgeGateway, err) } - fakeId, err := getDhclRelaySettingsId(edgeGateway) + fakeId, err := getDhcpRelaySettingsId(edgeGateway) if err != nil { return nil, fmt.Errorf("could not construct DHCP relay settings ID: %s", err) } @@ -367,12 +367,12 @@ func setDhcpRelayAgentData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRela return nil } -// getDhclRelaySettingsId constructs a fake DHCP relay configuration ID which is needed for +// getDhcpRelaySettingsId constructs a fake DHCP relay configuration ID which is needed for // Terraform. The ID is in format "edgeGateway.ID:dhcpRelay" // (eg.: "urn:vcloud:gateway:77ccbdcd-ac04-4111-bf08-8ac294a3185b:dhcpRelay"). Edge Gateway ID is // left here just in case we ever want to refer this object somewhere but still be able to // distinguish it from the real edge gateway resource. -func getDhclRelaySettingsId(edge *govcd.EdgeGateway) (string, error) { +func getDhcpRelaySettingsId(edge *govcd.EdgeGateway) (string, error) { if edge.EdgeGateway.ID == "" { return "", fmt.Errorf("edge gateway does not have ID populated") } diff --git a/vcd/resource_vcd_nsxv_dhcp_relay_test.go b/vcd/resource_vcd_nsxv_dhcp_relay_test.go index 8a9ea08a0..4c7c8c8e7 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay_test.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay_test.go @@ -123,84 +123,84 @@ func testAccCheckVcdDhcpRelaySettingsEmpty() resource.TestCheckFunc { const testAccRoutedNet = ` variable "network_types" { - type = list(string) - default = ["internal", "subinterface"] + type = list(string) + default = ["internal", "subinterface"] } resource "vcd_network_routed" "test-routed" { - count = 2 - name = "dhcp-relay-${count.index}" - org = "{{.Org}}" - vdc = "{{.Vdc}}" - edge_gateway = "{{.EdgeGateway}}" - gateway = "10.201.${count.index}.1" - netmask = "255.255.255.0" - interface_type = var.network_types[count.index] - - static_ip_pool { - start_address = "10.201.${count.index}.10" - end_address = "10.201.${count.index}.20" - } + count = 2 + name = "dhcp-relay-${count.index}" + org = "{{.Org}}" + vdc = "{{.Vdc}}" + edge_gateway = "{{.EdgeGateway}}" + gateway = "10.201.${count.index}.1" + netmask = "255.255.255.0" + interface_type = var.network_types[count.index] + + static_ip_pool { + start_address = "10.201.${count.index}.10" + end_address = "10.201.${count.index}.20" + } } ` const testAccVcdNsxvDhcpRelay = testAccRoutedNet + ` resource "vcd_nsxv_dhcp_relay" "relay_config" { - org = "{{.Org}}" - vdc = "{{.Vdc}}" - edge_gateway = "{{.EdgeGateway}}" - - ip_addresses = ["1.1.1.1", "2.2.2.2"] - domain_names = ["servergroups.domainname.com", "other.domain.com"] - ip_sets = [vcd_ipset.myset1.name, vcd_ipset.myset2.name] - - relay_agent { - org_network = vcd_network_routed.test-routed[0].name - } - - relay_agent { - org_network = vcd_network_routed.test-routed[1].name - gateway_ip_address = "10.201.1.1" - } + org = "{{.Org}}" + vdc = "{{.Vdc}}" + edge_gateway = "{{.EdgeGateway}}" + + ip_addresses = ["1.1.1.1", "2.2.2.2"] + domain_names = ["servergroups.domainname.com", "other.domain.com"] + ip_sets = [vcd_ipset.myset1.name, vcd_ipset.myset2.name] + + relay_agent { + org_network = vcd_network_routed.test-routed[0].name + } + + relay_agent { + org_network = vcd_network_routed.test-routed[1].name + gateway_ip_address = "10.201.1.1" + } } data "vcd_nsxv_dhcp_relay" "relay" { - org = "{{.Org}}" - vdc = "{{.Vdc}}" - edge_gateway = vcd_nsxv_dhcp_relay.relay_config.edge_gateway + org = "{{.Org}}" + vdc = "{{.Vdc}}" + edge_gateway = vcd_nsxv_dhcp_relay.relay_config.edge_gateway } resource "vcd_ipset" "myset1" { - name = "test-set1" - ip_addresses = ["192.168.1.1"] + name = "test-set1" + ip_addresses = ["192.168.1.1"] } resource "vcd_ipset" "myset2" { - name = "test-set2" - ip_addresses = ["192.168.1.1"] + name = "test-set2" + ip_addresses = ["192.168.1.1"] } ` const testAccVcdNsxvDhcpRelayUpdate = testAccRoutedNet + ` resource "vcd_nsxv_dhcp_relay" "relay_config" { - org = "{{.Org}}" - vdc = "{{.Vdc}}" - edge_gateway = "{{.EdgeGateway}}" - - ip_sets = [vcd_ipset.myset1.name, vcd_ipset.myset2.name] - - relay_agent { - org_network = vcd_network_routed.test-routed[0].name - } + org = "{{.Org}}" + vdc = "{{.Vdc}}" + edge_gateway = "{{.EdgeGateway}}" + + ip_sets = [vcd_ipset.myset1.name, vcd_ipset.myset2.name] + + relay_agent { + org_network = vcd_network_routed.test-routed[0].name + } } resource "vcd_ipset" "myset1" { - name = "test-set1" - ip_addresses = ["192.168.1.1"] + name = "test-set1" + ip_addresses = ["192.168.1.1"] } resource "vcd_ipset" "myset2" { - name = "test-set2" - ip_addresses = ["192.168.1.1"] + name = "test-set2" + ip_addresses = ["192.168.1.1"] } ` diff --git a/website/docs/d/nsxv_dhcp_relay.markdown b/website/docs/d/nsxv_dhcp_relay.markdown index 22d397680..f1a97f084 100644 --- a/website/docs/d/nsxv_dhcp_relay.markdown +++ b/website/docs/d/nsxv_dhcp_relay.markdown @@ -13,7 +13,7 @@ capability provided by NSX in vCloud Director environment allows to leverage exi infrastructure from within vCloud Director environment without any interruption to the IP address management in existing DHCP infrastructure. DHCP messages are relayed from virtual machines to the designated DHCP servers in your physical DHCP infrastructure, which allows IP addresses controlled -by the NSX software to continue to be in synch with IP addresses in the rest of your DHCP-controlled +by the NSX software to continue to be in sync with IP addresses in the rest of your DHCP-controlled environments. Supported in provider *v2.6+* diff --git a/website/docs/r/nsxv_dhcp_relay.markdown b/website/docs/r/nsxv_dhcp_relay.markdown index 096325aaa..5dea71c49 100644 --- a/website/docs/r/nsxv_dhcp_relay.markdown +++ b/website/docs/r/nsxv_dhcp_relay.markdown @@ -13,7 +13,10 @@ provided by NSX in vCloud Director environment allows to leverage existing DHCP within vCloud Director environment without any interruption to the IP address management in existing DHCP infrastructure. DHCP messages are relayed from virtual machines to the designated DHCP servers in your physical DHCP infrastructure, which allows IP addresses controlled by the NSX software to -continue to be in synch with IP addresses in the rest of your DHCP-controlled environments. +continue to be in sync with IP addresses in the rest of your DHCP-controlled environments. + +~> **Note:** This resource is a "singleton". Because DHCP relay settings are just edge gateway +properties - only one resource per Edge Gateway is useful. Supported in provider *v2.6+* @@ -43,7 +46,7 @@ resource "vcd_nsxv_dhcp_relay" "relay_config" { ip_addresses = ["1.1.1.1", "2.2.2.2"] domain_names = ["servergroups.domainname.com", "other.domain.com"] - ip_sets = [vcd_ipset.myset1.name, vcd_ipset.myset2.name] + ip_sets = [vcd_nsxv_ip_set.myset1.name, vcd_nsxv_ip_set.myset2.name] relay_agent { org_network = "my-routed-network-1" @@ -54,6 +57,22 @@ resource "vcd_nsxv_dhcp_relay" "relay_config" { gateway_ip_address = "10.201.1.1" } } + +resource "vcd_nsxv_ip_set" "myset1" { + org = "my-org" + vdc = "my-org-vdc" + + name = "ipset-one" + ip_addresses = ["10.10.10.1/24"] +} + +resource "vcd_nsxv_ip_set" "myset2" { + org = "my-org" + vdc = "my-org-vdc" + + name = "ipset-two" + ip_addresses = ["20.20.20.1/24"] +} ``` ## Argument Reference From 9543a60f6973d8fc1218c181b747d31bd3b5a7a9 Mon Sep 17 00:00:00 2001 From: Dainius S Date: Mon, 9 Dec 2019 14:55:22 +0200 Subject: [PATCH 11/19] Bump govcd --- go.mod | 4 +--- go.sum | 4 ++-- .../go-vcloud-director/v2/govcd/nsxv_dhcprelay.go | 10 +++------- vendor/modules.txt | 2 +- 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index e9b3ba868..400877e6c 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,5 @@ go 1.13 require ( github.com/hashicorp/go-version v1.2.0 github.com/hashicorp/terraform-plugin-sdk v1.3.0 - github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.9 + github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.10 ) - -replace github.com/vmware/go-vcloud-director/v2 => github.com/Didainius/go-vcloud-director/v2 v2.5.0-alpha.9.0.20191205142452-25e41fadb7f5 diff --git a/go.sum b/go.sum index 99b18d013..90dc39af4 100644 --- a/go.sum +++ b/go.sum @@ -9,8 +9,6 @@ cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbf cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/Didainius/go-vcloud-director/v2 v2.5.0-alpha.9.0.20191205142452-25e41fadb7f5 h1:YYDvsY/2VKMxBKewGtNfNJ6lKHah1IHFpH+jCzW+ISQ= -github.com/Didainius/go-vcloud-director/v2 v2.5.0-alpha.9.0.20191205142452-25e41fadb7f5/go.mod h1:zjondbeyTfZlzhwxOzyF4K2sWWYgMEv5H91dp5dPbU8= github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8= github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE= @@ -203,6 +201,8 @@ github.com/vmihailenco/msgpack v3.3.3+incompatible h1:wapg9xDUZDzGCNFlwc5SqI1rvc github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/vmihailenco/msgpack v4.0.1+incompatible h1:RMF1enSPeKTlXrXdOcqjFUElywVZjjC6pqse21bKbEU= github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= +github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.10 h1:8ofElneVQwL4oGN/6GzuDSvFLORrGqgU2SQMZtWP4c0= +github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.10/go.mod h1:zjondbeyTfZlzhwxOzyF4K2sWWYgMEv5H91dp5dPbU8= 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= diff --git a/vendor/github.com/vmware/go-vcloud-director/v2/govcd/nsxv_dhcprelay.go b/vendor/github.com/vmware/go-vcloud-director/v2/govcd/nsxv_dhcprelay.go index 4a9be007a..f1511043a 100644 --- a/vendor/github.com/vmware/go-vcloud-director/v2/govcd/nsxv_dhcprelay.go +++ b/vendor/github.com/vmware/go-vcloud-director/v2/govcd/nsxv_dhcprelay.go @@ -58,8 +58,8 @@ func (egw *EdgeGateway) GetDhcpRelay() (*types.EdgeDhcpRelay, error) { return response, nil } -// ResetDhcpRelay resets all fields to empty which is equivalent of what edge gateway has upon -// creation. +// ResetDhcpRelay removes all configuration by sending a DELETE request for DHCP relay configuration +// endpoint func (egw *EdgeGateway) ResetDhcpRelay() error { if !egw.HasAdvancedNetworking() { return fmt.Errorf("only advanced edge gateways support DHCP relay") @@ -73,9 +73,5 @@ func (egw *EdgeGateway) ResetDhcpRelay() error { // Send a DELETE request to DHCP relay configuration endpoint _, err = egw.client.ExecuteRequestWithCustomError(httpPath, http.MethodDelete, types.AnyXMLMime, "unable to reset edge gateway DHCP relay configuration: %s", nil, &types.NSXError{}) - if err != nil { - return err - } - - return nil + return err } diff --git a/vendor/modules.txt b/vendor/modules.txt index 43f4c6472..20665c953 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -224,7 +224,7 @@ github.com/ulikunitz/xz/lzma # github.com/vmihailenco/msgpack v4.0.1+incompatible github.com/vmihailenco/msgpack github.com/vmihailenco/msgpack/codes -# github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.9 => github.com/Didainius/go-vcloud-director/v2 v2.5.0-alpha.9.0.20191205142452-25e41fadb7f5 +# github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.10 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 From 19e4ac9549c72df5e86b10b328bc613c7c470950 Mon Sep 17 00:00:00 2001 From: Dainius S Date: Mon, 9 Dec 2019 16:55:58 +0200 Subject: [PATCH 12/19] Address comments --- vcd/resource_vcd_nsxv_dhcp_relay.go | 38 ++++++++++++++--------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/vcd/resource_vcd_nsxv_dhcp_relay.go b/vcd/resource_vcd_nsxv_dhcp_relay.go index 3b809a30a..ad3ffd1c6 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay.go @@ -118,12 +118,12 @@ func resourceVcdNsxvDhcpRelayCreate(d *schema.ResourceData, meta interface{}) er // This is not a real object but a settings property on Edge gateway - creating a fake composite // ID - fakeId, err := getDhcpRelaySettingsId(edgeGateway) + compositeId, err := getDhcpRelaySettingsId(edgeGateway) if err != nil { return fmt.Errorf("could not construct DHCP relay settings ID: %s", err) } - d.SetId(fakeId) + d.SetId(compositeId) return resourceVcdNsxvDhcpRelayRead(d, meta) } @@ -200,7 +200,7 @@ func resourceVcdNsxvDhcpRelayDelete(d *schema.ResourceData, meta interface{}) er func resourceVcdNsxvDhcpRelayImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { resourceURI := strings.Split(d.Id(), ImportSeparator) if len(resourceURI) != 3 { - return nil, fmt.Errorf("resource name must be specified in such way org.vdc.edge-gw") + return nil, fmt.Errorf("resource name must be specified in such way org-name.vdc-name.edge-gw-name") } orgName, vdcName, edgeName := resourceURI[0], resourceURI[1], resourceURI[2] @@ -277,27 +277,25 @@ func getDhcpRelayType(d *schema.ResourceData, edge *govcd.EdgeGateway, vdc *govc func getDhcpRelayAgentsType(relayAgentsSet *schema.Set, edge *govcd.EdgeGateway) ([]types.EdgeDhcpRelayAgent, error) { relayAgentsSlice := relayAgentsSet.List() relayAgentsStruct := make([]types.EdgeDhcpRelayAgent, len(relayAgentsSlice)) - if len(relayAgentsSlice) > 0 { - for index, relayAgent := range relayAgentsSlice { - relayAgentMap := convertToStringMap(relayAgent.(map[string]interface{})) - - // Lookup vNic index by network name - orgNetworkName := relayAgentMap["org_network"] - vNicIndex, _, err := edge.GetAnyVnicIndexByNetworkName(orgNetworkName) - if err != nil { - return nil, fmt.Errorf("could not lookup edge gateway interface (vNic) index by network name for network %s: %s", orgNetworkName, err) - } + for index, relayAgent := range relayAgentsSlice { + relayAgentMap := convertToStringMap(relayAgent.(map[string]interface{})) - oneRelayAgent := types.EdgeDhcpRelayAgent{ - VnicIndex: vNicIndex, - } + // Lookup vNic index by network name + orgNetworkName := relayAgentMap["org_network"] + vNicIndex, _, err := edge.GetAnyVnicIndexByNetworkName(orgNetworkName) + if err != nil { + return nil, fmt.Errorf("could not lookup edge gateway interface (vNic) index by network name for network %s: %s", orgNetworkName, err) + } - if gatewayIp, isSet := relayAgentMap["gateway_ip_address"]; isSet { - oneRelayAgent.GatewayInterfaceAddress = gatewayIp - } + oneRelayAgent := types.EdgeDhcpRelayAgent{ + VnicIndex: vNicIndex, + } - relayAgentsStruct[index] = oneRelayAgent + if gatewayIp, isSet := relayAgentMap["gateway_ip_address"]; isSet { + oneRelayAgent.GatewayInterfaceAddress = gatewayIp } + + relayAgentsStruct[index] = oneRelayAgent } return relayAgentsStruct, nil From 9a81246fa3b7a3ce853a95ad2cac40365ed87719 Mon Sep 17 00:00:00 2001 From: Dainius S Date: Mon, 9 Dec 2019 17:16:43 +0200 Subject: [PATCH 13/19] Migrate vdc lookup --- vcd/resource_vcd_nsxv_dhcp_relay.go | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/vcd/resource_vcd_nsxv_dhcp_relay.go b/vcd/resource_vcd_nsxv_dhcp_relay.go index ad3ffd1c6..a7093afd6 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay.go @@ -101,12 +101,7 @@ func resourceVcdNsxvDhcpRelayCreate(d *schema.ResourceData, meta interface{}) er return fmt.Errorf(errorUnableToFindEdgeGateway, err) } - _, vdc, err := vcdClient.GetOrgAndVdcFromResource(d) - if err != nil { - return fmt.Errorf(errorRetrievingOrgAndVdc, err) - } - - dhcpRelayConfig, err := getDhcpRelayType(d, edgeGateway, vdc) + dhcpRelayConfig, err := getDhcpRelayType(d, edgeGateway, vcdClient) if err != nil { return fmt.Errorf("could not process DHCP relay settings: %s", err) } @@ -165,12 +160,12 @@ func resourceVcdNsxvDhcpRelayRead(d *schema.ResourceData, meta interface{}) erro // This is not a real object but a settings property on Edge gateway - creating a fake composite // ID - fakeId, err := getDhcpRelaySettingsId(edgeGateway) + compositeId, err := getDhcpRelaySettingsId(edgeGateway) if err != nil { return fmt.Errorf("could not construct DHCP relay settings ID: %s", err) } - d.SetId(fakeId) + d.SetId(compositeId) return nil } @@ -210,7 +205,7 @@ func resourceVcdNsxvDhcpRelayImport(d *schema.ResourceData, meta interface{}) ([ return nil, fmt.Errorf(errorUnableToFindEdgeGateway, err) } - fakeId, err := getDhcpRelaySettingsId(edgeGateway) + compositeId, err := getDhcpRelaySettingsId(edgeGateway) if err != nil { return nil, fmt.Errorf("could not construct DHCP relay settings ID: %s", err) } @@ -218,12 +213,17 @@ func resourceVcdNsxvDhcpRelayImport(d *schema.ResourceData, meta interface{}) ([ d.Set("org", orgName) d.Set("vdc", vdcName) d.Set("edge_gateway", edgeName) - d.SetId(fakeId) + d.SetId(compositeId) return []*schema.ResourceData{d}, nil } // getDhcpRelayType converts resource schema to *types.EdgeDhcpRelay -func getDhcpRelayType(d *schema.ResourceData, edge *govcd.EdgeGateway, vdc *govcd.Vdc) (*types.EdgeDhcpRelay, error) { +func getDhcpRelayType(d *schema.ResourceData, edge *govcd.EdgeGateway, vcdClient *VCDClient) (*types.EdgeDhcpRelay, error) { + _, vdc, err := vcdClient.GetOrgAndVdcFromResource(d) + if err != nil { + return nil, fmt.Errorf(errorRetrievingOrgAndVdc, err) + } + dhcpRelayConfig := &types.EdgeDhcpRelay{} // Relay server part @@ -232,7 +232,6 @@ func getDhcpRelayType(d *schema.ResourceData, edge *govcd.EdgeGateway, vdc *govc listOfDomainNames []string listOfIpSetNames []string listOfIpSetIds []string - err error ) if ipAddresses, ok := d.GetOk("ip_addresses"); ok { From 580d9887e95986a18a8297e7f2296d9e9a4141f2 Mon Sep 17 00:00:00 2001 From: Dainius S Date: Tue, 10 Dec 2019 14:20:53 +0200 Subject: [PATCH 14/19] Fix errors, bump govcd --- go.mod | 3 +- go.sum | 2 + vcd/resource_vcd_nsxv_dhcp_relay_test.go | 55 ++++++++++++++++-------- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index 400877e6c..b21be4c3f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,8 @@ module github.com/terraform-providers/terraform-provider-vcd/v2 go 1.13 require ( + github.com/davecgh/go-spew v1.1.1 github.com/hashicorp/go-version v1.2.0 github.com/hashicorp/terraform-plugin-sdk v1.3.0 - github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.10 + github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.11 ) diff --git a/go.sum b/go.sum index 90dc39af4..9428385c6 100644 --- a/go.sum +++ b/go.sum @@ -203,6 +203,8 @@ github.com/vmihailenco/msgpack v4.0.1+incompatible h1:RMF1enSPeKTlXrXdOcqjFUElyw github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.10 h1:8ofElneVQwL4oGN/6GzuDSvFLORrGqgU2SQMZtWP4c0= github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.10/go.mod h1:zjondbeyTfZlzhwxOzyF4K2sWWYgMEv5H91dp5dPbU8= +github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.11 h1:VyJTHIPwwQZkCbIX2TPQ4ddnhJZIWZ5HCkSy/cHiSkc= +github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.11/go.mod h1:zjondbeyTfZlzhwxOzyF4K2sWWYgMEv5H91dp5dPbU8= 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= diff --git a/vcd/resource_vcd_nsxv_dhcp_relay_test.go b/vcd/resource_vcd_nsxv_dhcp_relay_test.go index 4c7c8c8e7..4c13d4e09 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay_test.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay_test.go @@ -6,7 +6,9 @@ import ( "fmt" "regexp" "testing" + "time" + "github.com/davecgh/go-spew/spew" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" ) @@ -59,16 +61,15 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_sets.908008747", "test-set2"), resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.#", "2"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3348209499.org_network", "dhcp-relay-0"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3348209499.gateway_ip_address", "10.201.0.1"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3180164926.org_network", "dhcp-relay-1"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3180164926.gateway_ip_address", "10.201.1.1"), - + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3772586107.org_network", "dhcp-relay-0"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3772586107.gateway_ip_address", "210.201.0.1"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.2596401182.org_network", "dhcp-relay-1"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.2596401182.gateway_ip_address", "210.201.1.1"), // Validate that data source has all fields except the hashed IP set because it is turned into slice in data source // and only one due to outstanding problem in Terraform plugin SDK - https://github.com/hashicorp/terraform-plugin-sdk/pull/197 resourceFieldsEqual("vcd_nsxv_dhcp_relay.relay_config", "data.vcd_nsxv_dhcp_relay.relay", - []string{"relay_agent.3348209499.gateway_ip_address", "relay_agent.3348209499.org_network", "relay_agent.#", - "relay_agent.3180164926.org_network", "relay_agent.3180164926.gateway_ip_address"}), + []string{"relay_agent.3772586107.gateway_ip_address", "relay_agent.3772586107.org_network", "relay_agent.#", + "relay_agent.2596401182.org_network", "relay_agent.2596401182.gateway_ip_address"}), ), }, resource.TestStep{ @@ -83,8 +84,9 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_sets.908008747", "test-set2"), resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.#", "1"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3348209499.org_network", "dhcp-relay-0"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3348209499.gateway_ip_address", "10.201.0.1"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3772586107.org_network", "dhcp-relay-0"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3772586107.gateway_ip_address", "210.201.0.1"), + // stateDumper(), ), }, resource.TestStep{ @@ -97,6 +99,21 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { }) } +func sleepTester() resource.TestCheckFunc { + return func(s *terraform.State) error { + fmt.Println("sleeping") + time.Sleep(1 * time.Minute) + return nil + } +} + +func stateDumper() resource.TestCheckFunc { + return func(s *terraform.State) error { + spew.Dump(s) + return nil + } +} + // testAccCheckVcdDhcpRelaySettingsEmpty reads DHCP relay configuration and ensure it has no // settings set. func testAccCheckVcdDhcpRelaySettingsEmpty() resource.TestCheckFunc { @@ -133,13 +150,13 @@ resource "vcd_network_routed" "test-routed" { org = "{{.Org}}" vdc = "{{.Vdc}}" edge_gateway = "{{.EdgeGateway}}" - gateway = "10.201.${count.index}.1" + gateway = "210.201.${count.index}.1" netmask = "255.255.255.0" interface_type = var.network_types[count.index] static_ip_pool { - start_address = "10.201.${count.index}.10" - end_address = "10.201.${count.index}.20" + start_address = "210.201.${count.index}.10" + end_address = "210.201.${count.index}.20" } } ` @@ -152,7 +169,7 @@ resource "vcd_nsxv_dhcp_relay" "relay_config" { ip_addresses = ["1.1.1.1", "2.2.2.2"] domain_names = ["servergroups.domainname.com", "other.domain.com"] - ip_sets = [vcd_ipset.myset1.name, vcd_ipset.myset2.name] + ip_sets = [vcd_nsxv_ip_set.myset1.name, vcd_nsxv_ip_set.myset2.name] relay_agent { org_network = vcd_network_routed.test-routed[0].name @@ -160,7 +177,7 @@ resource "vcd_nsxv_dhcp_relay" "relay_config" { relay_agent { org_network = vcd_network_routed.test-routed[1].name - gateway_ip_address = "10.201.1.1" + gateway_ip_address = "210.201.1.1" } } @@ -170,12 +187,12 @@ data "vcd_nsxv_dhcp_relay" "relay" { edge_gateway = vcd_nsxv_dhcp_relay.relay_config.edge_gateway } -resource "vcd_ipset" "myset1" { +resource "vcd_nsxv_ip_set" "myset1" { name = "test-set1" ip_addresses = ["192.168.1.1"] } -resource "vcd_ipset" "myset2" { +resource "vcd_nsxv_ip_set" "myset2" { name = "test-set2" ip_addresses = ["192.168.1.1"] } @@ -187,19 +204,19 @@ resource "vcd_nsxv_dhcp_relay" "relay_config" { vdc = "{{.Vdc}}" edge_gateway = "{{.EdgeGateway}}" - ip_sets = [vcd_ipset.myset1.name, vcd_ipset.myset2.name] + ip_sets = [vcd_nsxv_ip_set.myset1.name, vcd_nsxv_ip_set.myset2.name] relay_agent { org_network = vcd_network_routed.test-routed[0].name } } -resource "vcd_ipset" "myset1" { +resource "vcd_nsxv_ip_set" "myset1" { name = "test-set1" ip_addresses = ["192.168.1.1"] } -resource "vcd_ipset" "myset2" { +resource "vcd_nsxv_ip_set" "myset2" { name = "test-set2" ip_addresses = ["192.168.1.1"] } From 8ca5fff4c6bc7f64e9614d4c6dfa278e2b4763d8 Mon Sep 17 00:00:00 2001 From: Dainius S Date: Tue, 10 Dec 2019 14:25:11 +0200 Subject: [PATCH 15/19] Remove unused functions --- vcd/resource_vcd_nsxv_dhcp_relay_test.go | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/vcd/resource_vcd_nsxv_dhcp_relay_test.go b/vcd/resource_vcd_nsxv_dhcp_relay_test.go index 4c13d4e09..beb716715 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay_test.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay_test.go @@ -6,9 +6,7 @@ import ( "fmt" "regexp" "testing" - "time" - "github.com/davecgh/go-spew/spew" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" ) @@ -99,21 +97,6 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { }) } -func sleepTester() resource.TestCheckFunc { - return func(s *terraform.State) error { - fmt.Println("sleeping") - time.Sleep(1 * time.Minute) - return nil - } -} - -func stateDumper() resource.TestCheckFunc { - return func(s *terraform.State) error { - spew.Dump(s) - return nil - } -} - // testAccCheckVcdDhcpRelaySettingsEmpty reads DHCP relay configuration and ensure it has no // settings set. func testAccCheckVcdDhcpRelaySettingsEmpty() resource.TestCheckFunc { From aec95c73802cee62e071404abda444fc76474974 Mon Sep 17 00:00:00 2001 From: Dainius S Date: Tue, 10 Dec 2019 17:03:50 +0200 Subject: [PATCH 16/19] go mod vendor --- go.mod | 1 - .../v2/govcd/edgegateway.go | 49 ++++-------- .../v2/types/v56/constants.go | 1 + .../v2/types/v56/nsxv_types.go | 78 +++++++++++++++++++ .../go-vcloud-director/v2/types/v56/types.go | 49 +----------- vendor/modules.txt | 2 +- 6 files changed, 95 insertions(+), 85 deletions(-) diff --git a/go.mod b/go.mod index b21be4c3f..337834496 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/terraform-providers/terraform-provider-vcd/v2 go 1.13 require ( - github.com/davecgh/go-spew v1.1.1 github.com/hashicorp/go-version v1.2.0 github.com/hashicorp/terraform-plugin-sdk v1.3.0 github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.11 diff --git a/vendor/github.com/vmware/go-vcloud-director/v2/govcd/edgegateway.go b/vendor/github.com/vmware/go-vcloud-director/v2/govcd/edgegateway.go index 02f1adef8..72c4a4c14 100644 --- a/vendor/github.com/vmware/go-vcloud-director/v2/govcd/edgegateway.go +++ b/vendor/github.com/vmware/go-vcloud-director/v2/govcd/edgegateway.go @@ -1254,19 +1254,19 @@ func validateUpdateLBGeneralParams(logLevel string) error { return nil } -// getVnics retrieves a structure of type EdgeGatewayVnics which contains network interfaces -// available in Edge Gateway -func (egw *EdgeGateway) getVnics() (*types.EdgeGatewayVnics, error) { +// getVdcNetworks retrieves a structure of type EdgeGatewayInterfaces which contains network +// interfaces available in Edge Gateway (uses "/vdcNetworks" endpoint) +func (egw *EdgeGateway) getVdcNetworks() (*types.EdgeGatewayInterfaces, error) { if !egw.HasAdvancedNetworking() { return nil, fmt.Errorf("only advanced edge gateway supports vNics") } - httpPath, err := egw.buildProxiedEdgeEndpointURL(types.EdgeVnicConfig) + httpPath, err := egw.buildProxiedEdgeEndpointURL("/vdcNetworks") if err != nil { return nil, fmt.Errorf("could not get Edge Gateway API endpoint: %s", err) } - vnicConfig := &types.EdgeGatewayVnics{} + vnicConfig := &types.EdgeGatewayInterfaces{} _, err = egw.client.ExecuteRequest(httpPath, http.MethodGet, types.AnyXMLMime, "unable to edge gateway vnic configuration: %s", nil, vnicConfig) @@ -1281,7 +1281,7 @@ func (egw *EdgeGateway) getVnics() (*types.EdgeGatewayVnics, error) { // networkType one of: 'internal', 'uplink', 'trunk', 'subinterface' // networkName cannot be empty func (egw *EdgeGateway) GetVnicIndexByNetworkNameAndType(networkName, networkType string) (*int, error) { - vnics, err := egw.getVnics() + vnics, err := egw.getVdcNetworks() if err != nil { return nil, fmt.Errorf("cannot retrieve vNic configuration: %s", err) } @@ -1296,7 +1296,7 @@ func (egw *EdgeGateway) GetVnicIndexByNetworkNameAndType(networkName, networkTyp // Warning: this function assumes that there are no duplicate network names attached. If it is so // this function will return the first network func (egw *EdgeGateway) GetAnyVnicIndexByNetworkName(networkName string) (*int, string, error) { - vnics, err := egw.getVnics() + vnics, err := egw.getVdcNetworks() if err != nil { return nil, "", fmt.Errorf("cannot retrieve vNic configuration: %s", err) } @@ -1325,7 +1325,7 @@ func (egw *EdgeGateway) GetAnyVnicIndexByNetworkName(networkName string) (*int, // GetNetworkNameAndTypeByVnicIndex returns network name and network type for given vNic index // returned networkType can be one of: 'internal', 'uplink', 'trunk', 'subinterface' func (egw *EdgeGateway) GetNetworkNameAndTypeByVnicIndex(vNicIndex int) (string, string, error) { - vnics, err := egw.getVnics() + vnics, err := egw.getVdcNetworks() if err != nil { return "", "", fmt.Errorf("cannot retrieve vNic configuration: %s", err) } @@ -1333,7 +1333,7 @@ func (egw *EdgeGateway) GetNetworkNameAndTypeByVnicIndex(vNicIndex int) (string, } // getVnicIndexByNetworkNameAndType is wrapped and used by public function GetVnicIndexByNetworkNameAndType -func getVnicIndexByNetworkNameAndType(networkName, networkType string, vnics *types.EdgeGatewayVnics) (*int, error) { +func getVnicIndexByNetworkNameAndType(networkName, networkType string, vnics *types.EdgeGatewayInterfaces) (*int, error) { if networkName == "" { return nil, fmt.Errorf("network name cannot be empty") } @@ -1347,22 +1347,12 @@ func getVnicIndexByNetworkNameAndType(networkName, networkType string, vnics *ty var foundIndex *int foundCount := 0 - for _, vnic := range vnics.Vnic { + for _, vnic := range vnics.EdgeInterface { // Look for matching portgroup name and network type - if networkType != types.EdgeGatewayVnicTypeSubinterface && vnic.PortgroupName == networkName && vnic.Type == networkType { + if vnic.Name == networkName && vnic.PortgroupName == networkName && vnic.Type == networkType { foundIndex = vnic.Index foundCount++ } - - // if looking for subinterface - check if they are defined and search for logicalSwitchName - if networkType == types.EdgeGatewayVnicTypeSubinterface && len(vnic.SubInterfaces.SubInterface) > 0 { - for _, subInterface := range vnic.SubInterfaces.SubInterface { - if subInterface.LogicalSwitchName == networkName { - foundIndex = subInterface.Index - foundCount++ - } - } - } } if foundCount > 1 { @@ -1378,7 +1368,7 @@ func getVnicIndexByNetworkNameAndType(networkName, networkType string, vnics *ty } // getNetworkNameAndTypeByVnicIndex looks up network type and name in list of edge gateway interfaces -func getNetworkNameAndTypeByVnicIndex(vNicIndex int, vnics *types.EdgeGatewayVnics) (string, string, error) { +func getNetworkNameAndTypeByVnicIndex(vNicIndex int, vnics *types.EdgeGatewayInterfaces) (string, string, error) { if vNicIndex < 0 { return "", "", fmt.Errorf("vNic index cannot be negative") } @@ -1386,23 +1376,12 @@ func getNetworkNameAndTypeByVnicIndex(vNicIndex int, vnics *types.EdgeGatewayVni foundCount := 0 var networkName, networkType string - for _, vnic := range vnics.Vnic { + for _, vnic := range vnics.EdgeInterface { if vnic.Index != nil && *vnic.Index == vNicIndex { foundCount++ - networkName = vnic.PortgroupName + networkName = vnic.Name networkType = vnic.Type } - - // Search inside "subinterface tree" - if vnic.Type == types.EdgeGatewayVnicTypeTrunk && len(vnic.SubInterfaces.SubInterface) > 0 { - for _, subInterface := range vnic.SubInterfaces.SubInterface { - if subInterface.Index != nil && *subInterface.Index == vNicIndex { - foundCount++ - networkName = subInterface.LogicalSwitchName - networkType = types.EdgeGatewayVnicTypeSubinterface - } - } - } } if foundCount > 1 { diff --git a/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/constants.go b/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/constants.go index 60c8001d6..f5a1a1163 100644 --- a/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/constants.go +++ b/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/constants.go @@ -161,6 +161,7 @@ const ( EdgeFirewallPath = "/firewall/config" EdgeCreateFirewallPath = "/firewall/config/rules" EdgeVnicConfig = "/vnics" + EdgeVdcVnicConfig = "/vdcNetworks" EdgeDhcpRelayPath = "/dhcp/config/relay" LbConfigPath = "/loadbalancer/config/" LbMonitorPath = "/loadbalancer/config/monitors/" diff --git a/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/nsxv_types.go b/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/nsxv_types.go index 6151d04b0..5ff411ab1 100644 --- a/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/nsxv_types.go +++ b/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/nsxv_types.go @@ -285,6 +285,84 @@ type EdgeIpSet struct { // EdgeIpSets is a slice of pointers to EdgeIpSet type EdgeIpSets []*EdgeIpSet +// EdgeGatewayVnics is a data structure holding information of vNic configuration in NSX-V edge +// gateway using "/network/edges/edge_id/vnics" endpoint +type EdgeGatewayVnics struct { + XMLName xml.Name `xml:"vnics"` + Vnic []struct { + Label string `xml:"label"` + Name string `xml:"name"` + AddressGroups struct { + AddressGroup struct { + PrimaryAddress string `xml:"primaryAddress,omitempty"` + SecondaryAddresses struct { + IpAddress []string `xml:"ipAddress,omitempty"` + } `xml:"secondaryAddresses,omitempty"` + SubnetMask string `xml:"subnetMask,omitempty"` + SubnetPrefixLength string `xml:"subnetPrefixLength,omitempty"` + } `xml:"addressGroup,omitempty"` + } `xml:"addressGroups,omitempty"` + Mtu string `xml:"mtu,omitempty"` + Type string `xml:"type,omitempty"` + IsConnected string `xml:"isConnected,omitempty"` + Index *int `xml:"index"` + PortgroupId string `xml:"portgroupId,omitempty"` + PortgroupName string `xml:"portgroupName,omitempty"` + EnableProxyArp string `xml:"enableProxyArp,omitempty"` + EnableSendRedirects string `xml:"enableSendRedirects,omitempty"` + SubInterfaces struct { + SubInterface []struct { + IsConnected string `xml:"isConnected,omitempty"` + Label string `xml:"label,omitempty"` + Name string `xml:"name,omitempty"` + Index *int `xml:"index,omitempty"` + TunnelId string `xml:"tunnelId,omitempty"` + LogicalSwitchId string `xml:"logicalSwitchId,omitempty"` + LogicalSwitchName string `xml:"logicalSwitchName,omitempty"` + EnableSendRedirects string `xml:"enableSendRedirects,omitempty"` + Mtu string `xml:"mtu,omitempty"` + AddressGroups struct { + AddressGroup struct { + PrimaryAddress string `xml:"primaryAddress,omitempty"` + SubnetMask string `xml:"subnetMask,omitempty"` + SubnetPrefixLength string `xml:"subnetPrefixLength,omitempty"` + } `xml:"addressGroup,omitempty"` + } `xml:"addressGroups,omitempty"` + } `xml:"subInterface,omitempty"` + } `xml:"subInterfaces,omitempty"` + } `xml:"vnic,omitempty"` +} + +// EdgeGatewayInterfaces is a data structure holding information of vNic configuration in NSX-V edge +// gateway using "/network/edges/edge_id/vdcNetworks" endpoint +type EdgeGatewayInterfaces struct { + XMLName xml.Name `xml:"edgeInterfaces"` + EdgeInterface []struct { + Name string `xml:"name"` + Type string `xml:"type"` + Index *int `xml:"index"` + NetworkReference struct { + ID string `xml:"id"` + Name string `xml:"name"` + Type string `xml:"type"` + } `xml:"networkReference"` + AddressGroups struct { + AddressGroup struct { + PrimaryAddress string `xml:"primaryAddress"` + SubnetMask string `xml:"subnetMask"` + SubnetPrefixLength string `xml:"subnetPrefixLength"` + SecondaryAddresses struct { + IpAddress []string `xml:"ipAddress"` + } `xml:"secondaryAddresses"` + } `xml:"addressGroup"` + } `xml:"addressGroups"` + PortgroupId string `xml:"portgroupId"` + PortgroupName string `xml:"portgroupName"` + IsConnected string `xml:"isConnected"` + TunnelId string `xml:"tunnelId"` + } `xml:"edgeInterface"` +} + // EdgeDhcpRelay - Dynamic Host Configuration Protocol (DHCP) relay enables you to leverage your // existing DHCP infrastructure from within NSX without any interruption to the IP address // management in your environment. DHCP messages are relayed from virtual machine(s) to the diff --git a/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/types.go b/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/types.go index 114112933..738f3e775 100644 --- a/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/types.go +++ b/vendor/github.com/vmware/go-vcloud-director/v2/types/v56/types.go @@ -1877,6 +1877,7 @@ type FirewallService struct { type NatService struct { Xmlns string `xml:"xmlns,attr,omitempty"` // Elements + IsEnabled bool `xml:"IsEnabled"` // Enable or disable the service using this flag NatType string `xml:"NatType,omitempty"` // One of: ipTranslation (use IP translation), portForwarding (use port forwarding) Policy string `xml:"Policy,omitempty"` // One of: allowTraffic (Allow all traffic), allowTrafficIn (Allow inbound traffic only) @@ -2566,51 +2567,3 @@ type AdminCatalogRecord struct { Link *Link `xml:"Link,omitempty"` Vdc *Metadata `xml:"Metadata,omitempty"` } - -// EdgeGatewayVnics is a data structure holding information of vNic configuration in NSX-V edge -// gateway -type EdgeGatewayVnics struct { - XMLName xml.Name `xml:"vnics"` - Vnic []struct { - Label string `xml:"label"` - Name string `xml:"name"` - AddressGroups struct { - AddressGroup struct { - PrimaryAddress string `xml:"primaryAddress,omitempty"` - SecondaryAddresses struct { - IpAddress []string `xml:"ipAddress,omitempty"` - } `xml:"secondaryAddresses,omitempty"` - SubnetMask string `xml:"subnetMask,omitempty"` - SubnetPrefixLength string `xml:"subnetPrefixLength,omitempty"` - } `xml:"addressGroup,omitempty"` - } `xml:"addressGroups,omitempty"` - Mtu string `xml:"mtu,omitempty"` - Type string `xml:"type,omitempty"` - IsConnected string `xml:"isConnected,omitempty"` - Index *int `xml:"index"` - PortgroupId string `xml:"portgroupId,omitempty"` - PortgroupName string `xml:"portgroupName,omitempty"` - EnableProxyArp string `xml:"enableProxyArp,omitempty"` - EnableSendRedirects string `xml:"enableSendRedirects,omitempty"` - SubInterfaces struct { - SubInterface []struct { - IsConnected string `xml:"isConnected,omitempty"` - Label string `xml:"label,omitempty"` - Name string `xml:"name,omitempty"` - Index *int `xml:"index,omitempty"` - TunnelId string `xml:"tunnelId,omitempty"` - LogicalSwitchId string `xml:"logicalSwitchId,omitempty"` - LogicalSwitchName string `xml:"logicalSwitchName,omitempty"` - EnableSendRedirects string `xml:"enableSendRedirects,omitempty"` - Mtu string `xml:"mtu,omitempty"` - AddressGroups struct { - AddressGroup struct { - PrimaryAddress string `xml:"primaryAddress,omitempty"` - SubnetMask string `xml:"subnetMask,omitempty"` - SubnetPrefixLength string `xml:"subnetPrefixLength,omitempty"` - } `xml:"addressGroup,omitempty"` - } `xml:"addressGroups,omitempty"` - } `xml:"subInterface,omitempty"` - } `xml:"subInterfaces,omitempty"` - } `xml:"vnic,omitempty"` -} diff --git a/vendor/modules.txt b/vendor/modules.txt index 20665c953..5d1a09f7b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -224,7 +224,7 @@ github.com/ulikunitz/xz/lzma # github.com/vmihailenco/msgpack v4.0.1+incompatible github.com/vmihailenco/msgpack github.com/vmihailenco/msgpack/codes -# github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.10 +# github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.11 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 From c1cc3bc3c1fe09fccfda8a32d3ae618dd0c3918d Mon Sep 17 00:00:00 2001 From: Dainius S Date: Tue, 10 Dec 2019 17:22:06 +0200 Subject: [PATCH 17/19] go mod tidy --- go.sum | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.sum b/go.sum index 9428385c6..b0696c0ee 100644 --- a/go.sum +++ b/go.sum @@ -201,8 +201,6 @@ github.com/vmihailenco/msgpack v3.3.3+incompatible h1:wapg9xDUZDzGCNFlwc5SqI1rvc github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/vmihailenco/msgpack v4.0.1+incompatible h1:RMF1enSPeKTlXrXdOcqjFUElywVZjjC6pqse21bKbEU= github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= -github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.10 h1:8ofElneVQwL4oGN/6GzuDSvFLORrGqgU2SQMZtWP4c0= -github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.10/go.mod h1:zjondbeyTfZlzhwxOzyF4K2sWWYgMEv5H91dp5dPbU8= github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.11 h1:VyJTHIPwwQZkCbIX2TPQ4ddnhJZIWZ5HCkSy/cHiSkc= github.com/vmware/go-vcloud-director/v2 v2.5.0-alpha.11/go.mod h1:zjondbeyTfZlzhwxOzyF4K2sWWYgMEv5H91dp5dPbU8= github.com/zclconf/go-cty v1.0.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s= From 1962917a3b1c573ba74f2b37f226d50b9109d9ea Mon Sep 17 00:00:00 2001 From: Dainius S Date: Tue, 10 Dec 2019 18:20:38 +0200 Subject: [PATCH 18/19] Rename field 'org_network' -> 'network_name' --- vcd/resource_vcd_nsxv_dhcp_relay.go | 6 +++--- vcd/resource_vcd_nsxv_dhcp_relay_test.go | 16 ++++++++-------- website/docs/r/nsxv_dhcp_relay.markdown | 8 ++++---- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/vcd/resource_vcd_nsxv_dhcp_relay.go b/vcd/resource_vcd_nsxv_dhcp_relay.go index 02cd7538a..f324a902b 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay.go @@ -11,7 +11,7 @@ import ( var relayAgentResource = &schema.Resource{ Schema: map[string]*schema.Schema{ - "org_network": { + "network_name": { Required: true, Type: schema.TypeString, Description: "Org network which is to be used for relaying DHCP message to specified servers", @@ -280,7 +280,7 @@ func getDhcpRelayAgentsType(relayAgentsSet *schema.Set, edge *govcd.EdgeGateway) relayAgentMap := convertToStringMap(relayAgent.(map[string]interface{})) // Lookup vNic index by network name - orgNetworkName := relayAgentMap["org_network"] + orgNetworkName := relayAgentMap["network_name"] vNicIndex, _, err := edge.GetAnyVnicIndexByNetworkName(orgNetworkName) if err != nil { return nil, fmt.Errorf("could not lookup edge gateway interface (vNic) index by network name for network %s: %s", orgNetworkName, err) @@ -349,7 +349,7 @@ func setDhcpRelayAgentData(d *schema.ResourceData, edgeRelay *types.EdgeDhcpRela return fmt.Errorf("could not find network name for edge gateway vNic %d: %s ", agent.VnicIndex, err) } - relayAgentMap["org_network"] = orgNetworkName + relayAgentMap["network_name"] = orgNetworkName relayAgentMap["gateway_ip_address"] = agent.GatewayInterfaceAddress relayAgentSlice[index] = relayAgentMap diff --git a/vcd/resource_vcd_nsxv_dhcp_relay_test.go b/vcd/resource_vcd_nsxv_dhcp_relay_test.go index beb716715..4e661443e 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay_test.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay_test.go @@ -59,15 +59,15 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_sets.908008747", "test-set2"), resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.#", "2"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3772586107.org_network", "dhcp-relay-0"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3772586107.network_name", "dhcp-relay-0"), resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3772586107.gateway_ip_address", "210.201.0.1"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.2596401182.org_network", "dhcp-relay-1"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.2596401182.network_name", "dhcp-relay-1"), resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.2596401182.gateway_ip_address", "210.201.1.1"), // Validate that data source has all fields except the hashed IP set because it is turned into slice in data source // and only one due to outstanding problem in Terraform plugin SDK - https://github.com/hashicorp/terraform-plugin-sdk/pull/197 resourceFieldsEqual("vcd_nsxv_dhcp_relay.relay_config", "data.vcd_nsxv_dhcp_relay.relay", - []string{"relay_agent.3772586107.gateway_ip_address", "relay_agent.3772586107.org_network", "relay_agent.#", - "relay_agent.2596401182.org_network", "relay_agent.2596401182.gateway_ip_address"}), + []string{"relay_agent.3772586107.gateway_ip_address", "relay_agent.3772586107.network_name", "relay_agent.#", + "relay_agent.2596401182.network_name", "relay_agent.2596401182.gateway_ip_address"}), ), }, resource.TestStep{ @@ -82,7 +82,7 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_sets.908008747", "test-set2"), resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.#", "1"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3772586107.org_network", "dhcp-relay-0"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3772586107.network_name", "dhcp-relay-0"), resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3772586107.gateway_ip_address", "210.201.0.1"), // stateDumper(), ), @@ -155,11 +155,11 @@ resource "vcd_nsxv_dhcp_relay" "relay_config" { ip_sets = [vcd_nsxv_ip_set.myset1.name, vcd_nsxv_ip_set.myset2.name] relay_agent { - org_network = vcd_network_routed.test-routed[0].name + network_name = vcd_network_routed.test-routed[0].name } relay_agent { - org_network = vcd_network_routed.test-routed[1].name + network_name = vcd_network_routed.test-routed[1].name gateway_ip_address = "210.201.1.1" } } @@ -190,7 +190,7 @@ resource "vcd_nsxv_dhcp_relay" "relay_config" { ip_sets = [vcd_nsxv_ip_set.myset1.name, vcd_nsxv_ip_set.myset2.name] relay_agent { - org_network = vcd_network_routed.test-routed[0].name + network_name = vcd_network_routed.test-routed[0].name } } diff --git a/website/docs/r/nsxv_dhcp_relay.markdown b/website/docs/r/nsxv_dhcp_relay.markdown index 5dea71c49..e0970638a 100644 --- a/website/docs/r/nsxv_dhcp_relay.markdown +++ b/website/docs/r/nsxv_dhcp_relay.markdown @@ -31,7 +31,7 @@ resource "vcd_nsxv_dhcp_relay" "relay_config" { ip_addresses = ["1.1.1.1"] relay_agent { - org_network = vcd_network_routed.test-routed[0].name + network_name = vcd_network_routed.test-routed[0].name } } ``` @@ -49,11 +49,11 @@ resource "vcd_nsxv_dhcp_relay" "relay_config" { ip_sets = [vcd_nsxv_ip_set.myset1.name, vcd_nsxv_ip_set.myset2.name] relay_agent { - org_network = "my-routed-network-1" + network_name = "my-routed-network-1" } relay_agent { - org_network = vcd_network_routed.db-network.name + network_name = vcd_network_routed.db-network.name gateway_ip_address = "10.201.1.1" } } @@ -93,7 +93,7 @@ The following arguments are supported: ## Relay Agent -* `org_network` - (Required) An existing Org network name from which DHCP messages are to be relayed. +* `network_name` - (Required) An existing Org network name from which DHCP messages are to be relayed. * `gateway_ip_address` - (Optional) IP address on edge gateway to be used for relaying messages. Primary address of edge gateway interface will be picked if not specified. From c9a172e6623da2f36f37e80e399be9c574156af6 Mon Sep 17 00:00:00 2001 From: Dainius S Date: Tue, 10 Dec 2019 18:38:42 +0200 Subject: [PATCH 19/19] Field rename test cleanup --- vcd/datasource_vcd_nsxv_dhcp_relay.go | 2 +- vcd/resource_vcd_nsxv_dhcp_relay_test.go | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/vcd/datasource_vcd_nsxv_dhcp_relay.go b/vcd/datasource_vcd_nsxv_dhcp_relay.go index beb819d39..b3a05bba9 100644 --- a/vcd/datasource_vcd_nsxv_dhcp_relay.go +++ b/vcd/datasource_vcd_nsxv_dhcp_relay.go @@ -56,7 +56,7 @@ func datasourceVcdNsxvDhcpRelay() *schema.Resource { Type: schema.TypeSet, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ - "org_network": { + "network_name": { Computed: true, Type: schema.TypeString, Description: "Org network which is to be used for relaying DHCP message to specified servers", diff --git a/vcd/resource_vcd_nsxv_dhcp_relay_test.go b/vcd/resource_vcd_nsxv_dhcp_relay_test.go index 4e661443e..044e43243 100644 --- a/vcd/resource_vcd_nsxv_dhcp_relay_test.go +++ b/vcd/resource_vcd_nsxv_dhcp_relay_test.go @@ -59,15 +59,15 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_sets.908008747", "test-set2"), resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.#", "2"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3772586107.network_name", "dhcp-relay-0"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3772586107.gateway_ip_address", "210.201.0.1"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.2596401182.network_name", "dhcp-relay-1"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.2596401182.gateway_ip_address", "210.201.1.1"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.958630993.network_name", "dhcp-relay-0"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.958630993.gateway_ip_address", "210.201.0.1"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.475553304.network_name", "dhcp-relay-1"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.475553304.gateway_ip_address", "210.201.1.1"), // Validate that data source has all fields except the hashed IP set because it is turned into slice in data source // and only one due to outstanding problem in Terraform plugin SDK - https://github.com/hashicorp/terraform-plugin-sdk/pull/197 resourceFieldsEqual("vcd_nsxv_dhcp_relay.relay_config", "data.vcd_nsxv_dhcp_relay.relay", - []string{"relay_agent.3772586107.gateway_ip_address", "relay_agent.3772586107.network_name", "relay_agent.#", - "relay_agent.2596401182.network_name", "relay_agent.2596401182.gateway_ip_address"}), + []string{"relay_agent.958630993.gateway_ip_address", "relay_agent.958630993.network_name", "relay_agent.#", + "relay_agent.475553304.network_name", "relay_agent.475553304.gateway_ip_address"}), ), }, resource.TestStep{ @@ -82,9 +82,8 @@ func TestAccVcdNsxvDhcpRelay(t *testing.T) { resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "ip_sets.908008747", "test-set2"), resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.#", "1"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3772586107.network_name", "dhcp-relay-0"), - resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.3772586107.gateway_ip_address", "210.201.0.1"), - // stateDumper(), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.958630993.network_name", "dhcp-relay-0"), + resource.TestCheckResourceAttr("vcd_nsxv_dhcp_relay.relay_config", "relay_agent.958630993.gateway_ip_address", "210.201.0.1"), ), }, resource.TestStep{