-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce 'vcd_external_network_v2' and dependencies with NSX-T suppo…
…rt (#560) * Introduce 'vcd_external_network_v2' * vcd_vcenter * vcd_portgroup * vcd_nsxt_manager * vcd_nsxt_tier0_router
- Loading branch information
Showing
45 changed files
with
2,755 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
* **New Resource**: `vcd_external_network_v2` with NSX-T support [GH-560] | ||
* **New Data Source**: `vcd_external_network_v2` with NSX-T support [GH-560] | ||
* **New Data Source**: `vcd_vcenter` [GH-560] | ||
* **New Data Source**: `vcd_portgroup` [GH-560] | ||
* **New Data Source**: `vcd_nsxt_manager` [GH-560] | ||
* **New Data Source**: `vcd_nsxt_tier0_router` [GH-560] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// +build ALL nsxt functional | ||
|
||
package vcd | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccVcdDatasourceNsxtManager(t *testing.T) { | ||
|
||
if !usingSysAdmin() { | ||
t.Skip(t.Name() + " requires system admin privileges") | ||
return | ||
} | ||
|
||
skipNoNsxtConfiguration(t) | ||
|
||
var params = StringMap{ | ||
"FuncName": t.Name(), | ||
"NsxtManager": testConfig.Nsxt.Manager, | ||
"Tags": "nsxt", | ||
} | ||
|
||
configText := templateFill(testAccCheckVcdNsxtManager, params) | ||
|
||
if vcdShortTest { | ||
t.Skip(acceptanceTestsSkipped) | ||
return | ||
} | ||
debugPrintf("#[DEBUG] CONFIGURATION: %s", configText) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: configText, | ||
Check: resource.ComposeTestCheckFunc( | ||
// ID must match URN 'urn:vcloud:nsxtmanager:09722307-aee0-4623-af95-7f8e577c9ebc' | ||
resource.TestMatchResourceAttr("data.vcd_nsxt_manager.nsxt", "id", | ||
regexp.MustCompile(`urn:vcloud:nsxtmanager:[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$`)), | ||
resource.TestCheckResourceAttr("data.vcd_nsxt_manager.nsxt", "name", params["NsxtManager"].(string)), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccCheckVcdNsxtManager = ` | ||
data "vcd_nsxt_manager" "nsxt" { | ||
name = "{{.NsxtManager}}" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package vcd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/vmware/go-vcloud-director/v2/types/v56" | ||
|
||
"github.com/vmware/go-vcloud-director/v2/govcd" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func datasourceVcdNsxtTier0Router() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: datasourceNsxtTier0RouterRead, | ||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Name of NSX-T Tier-0 router.", | ||
}, | ||
"nsxt_manager_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "ID of NSX-T manager.", | ||
}, | ||
"is_assigned": &schema.Schema{ | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Defines if Tier-0 router is already assigned to external network.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// datasourceNsxtTier0RouterRead has special behavior. By default `GetImportableNsxtTier0RouterByName` which uses API | ||
// endpoint `1.0.0/nsxTResources/importableTier0Routers` does not return Tier-0 routers when they are used in external | ||
// networks. This causes a problem in regular Terraform flow - when user uses this datasource to reference Tier-0 router | ||
// for external network creation - next "apply" would fail with "Tier 0 router not found error". If original endpoint | ||
// does not find Tier-0 router - then this datasource queries all defined external networks and looks for Tier-0 router | ||
// backing by name. | ||
func datasourceNsxtTier0RouterRead(d *schema.ResourceData, meta interface{}) error { | ||
vcdClient := meta.(*VCDClient) | ||
nsxtManagerId := d.Get("nsxt_manager_id").(string) | ||
tier0RouterName := d.Get("name").(string) | ||
|
||
tier0Router, err := vcdClient.GetImportableNsxtTier0RouterByName(tier0RouterName, nsxtManagerId) | ||
if err != nil && !govcd.ContainsNotFound(err) { | ||
return fmt.Errorf("could not find NSX-T Tier-0 router by name '%s' in NSX-T manager %s: %s", | ||
tier0RouterName, nsxtManagerId, err) | ||
} | ||
|
||
// If unused Tier-0 router is found - set the ID and return | ||
if err == nil { | ||
d.Set("is_assigned", false) | ||
d.SetId(tier0Router.NsxtTier0Router.ID) | ||
return nil | ||
} | ||
|
||
// API endpoint for Tier-0 routers does not return Tier-0 routers which are already used in external networks | ||
// therefore we are searching for used Tier-0 router name in external networks. This should not cause any risks as | ||
// required permissions should be of the same level. | ||
if govcd.ContainsNotFound(err) { | ||
// Filtering by network backing is unsupported therefore queryParameters are nil | ||
extNets, err := govcd.GetAllExternalNetworksV2(vcdClient.VCDClient, nil) | ||
if err != nil { | ||
return fmt.Errorf("could not find external networks: %s", err) | ||
} | ||
|
||
for _, extNetwork := range extNets { | ||
for _, v := range extNetwork.ExternalNetwork.NetworkBackings.Values { | ||
// Very odd but when VRF Tier-0 router is used - BackingType can be UNKNOWN | ||
if v.Name == tier0RouterName && | ||
(v.BackingType == types.ExternalNetworkBackingTypeNsxtTier0Router || v.BackingType == "UNKNOWN") { | ||
d.Set("is_assigned", true) | ||
d.SetId(v.BackingID) | ||
return nil | ||
} | ||
} | ||
} | ||
} | ||
|
||
return fmt.Errorf("%s: could not find NSX-T Tier-0 router by name '%s' in NSX-T manager %s", | ||
govcd.ErrorEntityNotFound, tier0RouterName, nsxtManagerId) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// +build ALL nsxt functional | ||
|
||
package vcd | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
// TestAccVcdDatasourceNsxtTier0Router checks if datasource can find existing regular Tier-0 router | ||
// provided it is specified in configuration | ||
func TestAccVcdDatasourceNsxtTier0Router(t *testing.T) { | ||
testAccVcdDatasourceNsxtTier0Router(t, testConfig.Nsxt.Tier0router) | ||
} | ||
|
||
// TestAccVcdDatasourceNsxtTier0Router checks if datasource can find existing VRF Tier-0 router | ||
// provided it is specified in configuration | ||
func TestAccVcdDatasourceNsxtTier0RouterVrf(t *testing.T) { | ||
testAccVcdDatasourceNsxtTier0Router(t, testConfig.Nsxt.Tier0routerVrf) | ||
} | ||
|
||
func testAccVcdDatasourceNsxtTier0Router(t *testing.T, tier0RouterName string) { | ||
|
||
if !usingSysAdmin() { | ||
t.Skip(t.Name() + " requires system admin privileges") | ||
return | ||
} | ||
|
||
skipNoNsxtConfiguration(t) | ||
|
||
var params = StringMap{ | ||
"FuncName": t.Name(), | ||
"NsxtManager": testConfig.Nsxt.Manager, | ||
"NsxtTier0Router": tier0RouterName, | ||
"Tags": "nsxt", | ||
} | ||
|
||
configText := templateFill(testAccCheckVcdNsxtTier0Router, params) | ||
|
||
if vcdShortTest { | ||
t.Skip(acceptanceTestsSkipped) | ||
return | ||
} | ||
debugPrintf("#[DEBUG] CONFIGURATION: %s", configText) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: configText, | ||
Check: resource.ComposeTestCheckFunc( | ||
// ID must match URN 'urn:vcloud:nsxtmanager:09722307-aee0-4623-af95-7f8e577c9ebc' | ||
resource.TestMatchResourceAttr("data.vcd_nsxt_manager.nsxt", "id", | ||
regexp.MustCompile(`urn:vcloud:nsxtmanager:[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$`)), | ||
resource.TestCheckResourceAttr("data.vcd_nsxt_tier0_router.router", "name", params["NsxtTier0Router"].(string)), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccCheckVcdNsxtTier0Router = ` | ||
data "vcd_nsxt_manager" "nsxt" { | ||
name = "{{.NsxtManager}}" | ||
} | ||
data "vcd_nsxt_tier0_router" "router" { | ||
name = "{{.NsxtTier0Router}}" | ||
nsxt_manager_id = data.vcd_nsxt_manager.nsxt.id | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.