Skip to content

Commit

Permalink
Add import test
Browse files Browse the repository at this point in the history
Signed-off-by: Dainius Serplis <[email protected]>
  • Loading branch information
Didainius committed Mar 1, 2022
1 parent fc2debf commit 3eba566
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ require (
github.com/vmware/go-vcloud-director/v2 v2.15.0-alpha.3
)

replace github.com/vmware/go-vcloud-director/v2 => github.com/Didainius/go-vcloud-director/v2 v2.14.0-rc.3.0.20220226193642-991c1925558d
replace github.com/vmware/go-vcloud-director/v2 => github.com/Didainius/go-vcloud-director/v2 v2.14.0-rc.3.0.20220301204642-b5b3544acf98
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
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.14.0-rc.3.0.20220226193642-991c1925558d h1:hNMNmiXDXZVzREppZv/r0dBBY89o5RU+7LReKnZLyvk=
github.com/Didainius/go-vcloud-director/v2 v2.14.0-rc.3.0.20220226193642-991c1925558d/go.mod h1:2BS1yw61VN34WI0/nUYoInFvBc3Zcuf84d4ESiAAl68=
github.com/Didainius/go-vcloud-director/v2 v2.14.0-rc.3.0.20220301204642-b5b3544acf98 h1:2LAwwJqO/vnpOu++kKOI51pUoI/SJqcFoHOKXZICJ8w=
github.com/Didainius/go-vcloud-director/v2 v2.14.0-rc.3.0.20220301204642-b5b3544acf98/go.mod h1:2BS1yw61VN34WI0/nUYoInFvBc3Zcuf84d4ESiAAl68=
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
Expand Down
15 changes: 15 additions & 0 deletions vcd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,21 @@ func importStateIdOrgNsxtVdcObject(vcd TestConfig, objectName string) resource.I
}
}

// importStateIdOrgNsxtVdcGroupObject can be used by all entities that depend on Org + NSX-T VDC
// Group (such as Vapp, networks, edge gateway) in NSX-T VDC
func importStateIdOrgNsxtVdcGroupObject(vcd TestConfig, vdcGroupName, objectName string) resource.ImportStateIdFunc {
return func(*terraform.State) (string, error) {
if testConfig.VCD.Org == "" || testConfig.Nsxt.Vdc == "" || objectName == "" {
return "", fmt.Errorf("missing information to generate import path")
}
return testConfig.VCD.Org +
ImportSeparator +
vdcGroupName +
ImportSeparator +
objectName, nil
}
}

// importStateIdNsxtManagerObject can be used by all entities that depend on NSX-T manager name + objectName
func importStateIdNsxtManagerObject(vcd TestConfig, objectName string) resource.ImportStateIdFunc {
return func(*terraform.State) (string, error) {
Expand Down
6 changes: 0 additions & 6 deletions vcd/datasource_vcd_nsxt_edgegateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,3 @@ func isBackedByNsxt(org *govcd.Org, vdcOrVdcGroupId string) (bool, error) {

return vdcOrGroup.IsNsxt(), nil
}

// vdcOrVdcGroupVerifier is an interface to access IsNsxt() on VDC or VDC Group method `IsNsxt`
// (used in isBackedByNsxt)
type vdcOrVdcGroupVerifier interface {
IsNsxt() bool
}
32 changes: 26 additions & 6 deletions vcd/resource_vcd_nsxt_edgegateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,22 +260,35 @@ func resourceVcdNsxtEdgeGatewayImport(ctx context.Context, d *schema.ResourceDat

resourceURI := strings.Split(d.Id(), ImportSeparator)
if len(resourceURI) != 3 {
return nil, fmt.Errorf("resource name must be specified as org-name.vdc-name.nsxt-edge-gw-name")
return nil, fmt.Errorf("resource name must be specified as org-name.vdc-name.nsxt-edge-gw-name or org-name.vdc-group-name.nsxt-edge-gw-name")
}
orgName, vdcName, edgeName := resourceURI[0], resourceURI[1], resourceURI[2]

vcdClient := meta.(*VCDClient)

_, vdc, err := vcdClient.GetOrgAndVdc(orgName, vdcName)
if err != nil {
return nil, fmt.Errorf("unable to find org %s: %s", vdcName, err)
// define an interface type to match VDC and VDC Groups
var vdcOrGroup vdcOrVdcGroupVerifier
_, vdcOrGroup, err := vcdClient.GetOrgAndVdc(orgName, vdcName)

// VDC was not found - attempt to find a VDC Group
if govcd.ContainsNotFound(err) {
adminOrg, err := vcdClient.GetAdminOrg(orgName)
if err != nil {
return nil, fmt.Errorf("error retrieving Admin Org for '%s': %s", orgName, err)
}

vdcOrGroup, err = adminOrg.GetVdcGroupByName(vdcName)
if err != nil {
return nil, fmt.Errorf("error finding VDC or VDC Group by name '%s': %s", vdcName, err)
}

}

if vdc.IsNsxv() {
if !vdcOrGroup.IsNsxt() {
return nil, fmt.Errorf("please use 'vcd_edgegateway' for NSX-V backed VDC")
}

edge, err := vdc.GetNsxtEdgeGatewayByName(edgeName)
edge, err := vdcOrGroup.GetNsxtEdgeGatewayByName(edgeName)
if err != nil {
return nil, fmt.Errorf("could not retrieve NSX-T Edge Gateway with ID '%s': %s", d.Id(), err)
}
Expand Down Expand Up @@ -590,3 +603,10 @@ func setNsxtEdgeGatewayData(edgeGateway *types.OpenAPIEdgeGateway, d *schema.Res

return nil
}

// vdcOrVdcGroupVerifier is an interface to access IsNsxt() and GetNsxtEdgeGatewayByName() on VDC or
// VDC Group method `IsNsxt` (used in isBackedByNsxt and resourceVcdNsxtEdgeGatewayImport)
type vdcOrVdcGroupVerifier interface {
IsNsxt() bool
GetNsxtEdgeGatewayByName(name string) (*govcd.NsxtEdgeGateway, error)
}
6 changes: 6 additions & 0 deletions vcd/resource_vcd_nsxt_edgegateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ func TestAccVcdNsxtEdgeGatewayVdcGroup(t *testing.T) {
resource.TestMatchResourceAttr("vcd_nsxt_edgegateway.nsxt-edge", "owner_id", regexp.MustCompile(`^urn:vcloud:vdcGroup:`)),
),
},
{
ResourceName: "vcd_nsxt_edgegateway.nsxt-edge",
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: importStateIdOrgNsxtVdcGroupObject(testConfig, "TestAccVcdVdcGroupResource", params["NsxtEdgeGatewayVcd"].(string)),
},
},
})
postTestChecks(t)
Expand Down
22 changes: 14 additions & 8 deletions website/docs/r/nsxt_edgegateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,10 @@ definition at provider level.
(random member of VDC Group or specified in `starting_vdc_id`). Main use case of `starting_vdc_id`
is to pick egress traffic origin for multi datacenter VDC Groups.

* `starting_vdc_id` - (Optional, *v3.6+*,*VCD 10.2+*) If `owner_id` is a VDC Group, this field
allows to specify initial VDC for Edge Gateway (this can define Egress location of traffic in the
VDC Group) **Note:** It can only be used when `owner_id` is a VDC Group.
* `starting_vdc_id` - (Optional, *v3.6+*,*VCD 10.2+*) If `owner_id` is a VDC Group, by default Edge
Gateway will be created in random member VDC and moved to destination VDC Group. This field allows
to specify initial VDC for Edge Gateway (this can define Egress location of traffic in the VDC
Group) **Note:** It can only be used when `owner_id` is a VDC Group.

* `name` - (Required) A unique name for the edge gateway.
* `description` - (Optional) A unique name for the edge gateway.
Expand Down Expand Up @@ -253,17 +254,22 @@ The following attributes are exported on this resource:

## Importing

~> **Note:** The current implementation of Terraform import can only import resources into the state. It does not generate
configuration. [More information.][docs-import]
~> **Note:** The current implementation of Terraform import can only import resources into the
state. It does not generate configuration. [More information.][docs-import]

An existing edge gateway can be [imported][docs-import] into this resource via supplying its path.
The path for this resource is made of org-name.vdc-name.nsxt-edge-name
For example, using this structure, representing an edge gateway that was **not** created using Terraform:
The path for this resource is made of `org-name.vdc-name.nsxt-edge-name` or
`org-name.vdc-group-name.nsxt-edge-name` For example, using this structure, representing an edge
gateway that was **not** created using Terraform:

```hcl
data "vcd_org_vdc" "vdc-1" {
name = "vdc-name"
}
resource "vcd_nsxt_edgegateway" "nsxt-edge" {
org = "my-org"
vdc = "nsxt-vdc"
owner_id = data.vcd_org_vdc.vdc-1.id
name = "nsxt-edge"
description = "Description"
Expand Down

0 comments on commit 3eba566

Please sign in to comment.