Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add VDC Group support for NSX-T Edge Gateways #440

Merged
merged 8 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/v2.15.0/440-improvements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* NSX-T Edge Gateway now supports VDC Groups by switching from `OrgVdc` to `OwnerRef` field.
Additional methods `NsxtEdgeGateway.MoveToVdc()`, `Org.GetNsxtEdgeGatewayByNameAndOwnerId()`,
vbauzys marked this conversation as resolved.
Show resolved Hide resolved
`VdcGroup.GetNsxtEdgeGatewayByName()`, `VdcGroup.GetAllNsxtEdgeGateways()`, `org.GetVdcGroupById`
[GH-440]
* Additional helper functions `OwnerIsVdcGroup()`, `OwnerIsVdc()`, `VdcGroup.GetCapabilities()`,
`VdcGroup.IsNsxt()` [GH-440]
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# These owners will be the default owners for everything in the repo. Unless a later match takes
# precedence all these users will be requested for review when someone opens a pull request.
* @lvirbalas @dataclouder @Didainius @vbauzysvmware
* @lvirbalas @dataclouder @Didainius @vbauzysvmware @mikeletux @adambarreiro
3 changes: 2 additions & 1 deletion govcd/api_vcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ package govcd
import (
"crypto/tls"
"fmt"
semver "github.com/hashicorp/go-version"
"net/http"
"net/url"
"os"
"strings"
"time"

semver "github.com/hashicorp/go-version"

"github.com/vmware/go-vcloud-director/v2/types/v56"
"github.com/vmware/go-vcloud-director/v2/util"
)
Expand Down
Empty file removed govcd/graph_plan.png
Empty file.
89 changes: 74 additions & 15 deletions govcd/nsxt_edgegateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,34 @@ type NsxtEdgeGateway struct {
client *Client
}

// GetNsxtEdgeGatewayById allows to retrieve NSX-T edge gateway by ID for Org admins
// GetNsxtEdgeGatewayById allows retrieving NSX-T edge gateway by ID for Org admins
func (adminOrg *AdminOrg) GetNsxtEdgeGatewayById(id string) (*NsxtEdgeGateway, error) {
return getNsxtEdgeGatewayById(adminOrg.client, id, nil)
}

// GetNsxtEdgeGatewayById allows to retrieve NSX-T edge gateway by ID for Org users
// GetNsxtEdgeGatewayById allows retrieving NSX-T edge gateway by ID for Org users
func (org *Org) GetNsxtEdgeGatewayById(id string) (*NsxtEdgeGateway, error) {
return getNsxtEdgeGatewayById(org.client, id, nil)
}

// GetNsxtEdgeGatewayById allows to retrieve NSX-T edge gateway by ID for specific VDC
// GetNsxtEdgeGatewayById allows retrieving NSX-T edge gateway by ID for specific VDC
func (vdc *Vdc) GetNsxtEdgeGatewayById(id string) (*NsxtEdgeGateway, error) {
params := url.Values{}
filterParams := queryParameterFilterAnd("orgVdc.id=="+vdc.Vdc.ID, params)
filterParams := queryParameterFilterAnd("ownerRef.id=="+vdc.Vdc.ID, params)
egw, err := getNsxtEdgeGatewayById(vdc.client, id, filterParams)
if err != nil {
return nil, err
}

if egw.EdgeGateway.OrgVdc.ID != vdc.Vdc.ID {
if egw.EdgeGateway.OwnerRef.ID != vdc.Vdc.ID {
return nil, fmt.Errorf("%s: no NSX-T Edge Gateway with ID '%s' found in VDC '%s'",
ErrorEntityNotFound, id, vdc.Vdc.ID)
}

return egw, nil
}

// GetNsxtEdgeGatewayByName allows to retrieve NSX-T edge gateway by Name for Org admins
// GetNsxtEdgeGatewayByName allows retrieving NSX-T edge gateway by Name for Org admins
func (adminOrg *AdminOrg) GetNsxtEdgeGatewayByName(name string) (*NsxtEdgeGateway, error) {
queryParameters := url.Values{}
queryParameters.Add("filter", "name=="+name)
Expand All @@ -59,7 +59,7 @@ func (adminOrg *AdminOrg) GetNsxtEdgeGatewayByName(name string) (*NsxtEdgeGatewa
return returnSingleNsxtEdgeGateway(name, onlyNsxtEdges)
}

// GetNsxtEdgeGatewayByName allows to retrieve NSX-T edge gateway by Name for Org admins
// GetNsxtEdgeGatewayByName allows retrieving NSX-T edge gateway by Name for Org admins
func (org *Org) GetNsxtEdgeGatewayByName(name string) (*NsxtEdgeGateway, error) {
queryParameters := url.Values{}
queryParameters.Add("filter", "name=="+name)
Expand All @@ -74,6 +74,26 @@ func (org *Org) GetNsxtEdgeGatewayByName(name string) (*NsxtEdgeGateway, error)
return returnSingleNsxtEdgeGateway(name, onlyNsxtEdges)
}

// GetNsxtEdgeGatewayByNameAndOwnerId looks up NSX-T Edge Gateway by name and its owner ID (owner
// can be VDC or VDC Group).
func (org *Org) GetNsxtEdgeGatewayByNameAndOwnerId(edgeGatewayName, ownerId string) (*NsxtEdgeGateway, error) {
vbauzys marked this conversation as resolved.
Show resolved Hide resolved
if edgeGatewayName == "" || ownerId == "" {
return nil, fmt.Errorf("'edgeGatewayName' and 'ownerId' must both be specified")
}

queryParameters := url.Values{}
queryParameters.Add("filter", fmt.Sprintf("ownerRef.id==%s;name==%s", ownerId, edgeGatewayName))

allEdges, err := org.GetAllNsxtEdgeGateways(queryParameters)
if err != nil {
return nil, fmt.Errorf("unable to retrieve Edge Gateway by name '%s': %s", edgeGatewayName, err)
}

onlyNsxtEdges := filterOnlyNsxtEdges(allEdges)

return returnSingleNsxtEdgeGateway(edgeGatewayName, onlyNsxtEdges)
}

// GetNsxtEdgeGatewayByName allows to retrieve NSX-T edge gateway by Name for specific VDC
func (vdc *Vdc) GetNsxtEdgeGatewayByName(name string) (*NsxtEdgeGateway, error) {
queryParameters := url.Values{}
Expand All @@ -87,6 +107,23 @@ func (vdc *Vdc) GetNsxtEdgeGatewayByName(name string) (*NsxtEdgeGateway, error)
return returnSingleNsxtEdgeGateway(name, allEdges)
}

// GetNsxtEdgeGatewayByName allows to retrieve NSX-T edge gateway by Name for specific VDC Group
func (vdcGroup *VdcGroup) GetNsxtEdgeGatewayByName(name string) (*NsxtEdgeGateway, error) {
if name == "" {
return nil, fmt.Errorf("'name' must be specified")
}

queryParameters := url.Values{}
queryParameters.Add("filter", "name=="+name)
vbauzys marked this conversation as resolved.
Show resolved Hide resolved

allEdges, err := vdcGroup.GetAllNsxtEdgeGateways(queryParameters)
if err != nil {
return nil, fmt.Errorf("unable to retrieve Edge Gateway by name '%s': %s", name, err)
}

return returnSingleNsxtEdgeGateway(name, allEdges)
}

// GetAllNsxtEdgeGateways allows to retrieve all NSX-T edge gateways for Org Admins
func (adminOrg *AdminOrg) GetAllNsxtEdgeGateways(queryParameters url.Values) ([]*NsxtEdgeGateway, error) {
return getAllNsxtEdgeGateways(adminOrg.client, queryParameters)
Expand All @@ -99,18 +136,24 @@ func (org *Org) GetAllNsxtEdgeGateways(queryParameters url.Values) ([]*NsxtEdgeG

// GetAllNsxtEdgeGateways allows to retrieve all NSX-T edge gateways for specific VDC
func (vdc *Vdc) GetAllNsxtEdgeGateways(queryParameters url.Values) ([]*NsxtEdgeGateway, error) {
filteredQueryParams := queryParameterFilterAnd("orgVdc.id=="+vdc.Vdc.ID, queryParameters)
filteredQueryParams := queryParameterFilterAnd("ownerRef.id=="+vdc.Vdc.ID, queryParameters)
return getAllNsxtEdgeGateways(vdc.client, filteredQueryParams)
}

// GetAllNsxtEdgeGateways allows to retrieve all NSX-T edge gateways for specific VDC
func (vdcGroup *VdcGroup) GetAllNsxtEdgeGateways(queryParameters url.Values) ([]*NsxtEdgeGateway, error) {
filteredQueryParams := queryParameterFilterAnd("ownerRef.id=="+vdcGroup.VdcGroup.Id, queryParameters)
return getAllNsxtEdgeGateways(vdcGroup.client, filteredQueryParams)
}

// CreateNsxtEdgeGateway allows to create NSX-T edge gateway for Org admins
func (adminOrg *AdminOrg) CreateNsxtEdgeGateway(edgeGatewayConfig *types.OpenAPIEdgeGateway) (*NsxtEdgeGateway, error) {
if !adminOrg.client.IsSysAdmin {
return nil, fmt.Errorf("only System Administrator can create Edge Gateway")
}

endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointEdgeGateways
minimumApiVersion, err := adminOrg.client.checkOpenApiEndpointCompatibility(endpoint)
minimumApiVersion, err := adminOrg.client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
Expand All @@ -133,14 +176,14 @@ func (adminOrg *AdminOrg) CreateNsxtEdgeGateway(edgeGatewayConfig *types.OpenAPI
return returnEgw, nil
}

// Update allows to update NSX-T edge gateway for Org admins
// Update allows updating NSX-T edge gateway for Org admins
func (egw *NsxtEdgeGateway) Update(edgeGatewayConfig *types.OpenAPIEdgeGateway) (*NsxtEdgeGateway, error) {
if !egw.client.IsSysAdmin {
return nil, fmt.Errorf("only System Administrator can update Edge Gateway")
}

endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointEdgeGateways
minimumApiVersion, err := egw.client.checkOpenApiEndpointCompatibility(endpoint)
minimumApiVersion, err := egw.client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
Expand All @@ -167,14 +210,14 @@ func (egw *NsxtEdgeGateway) Update(edgeGatewayConfig *types.OpenAPIEdgeGateway)
return returnEgw, nil
}

// Delete allows to delete NSX-T edge gateway for sysadmins
// Delete allows deleting NSX-T edge gateway for sysadmins
func (egw *NsxtEdgeGateway) Delete() error {
if !egw.client.IsSysAdmin {
return fmt.Errorf("only Provider can delete Edge Gateway")
}

endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointEdgeGateways
minimumApiVersion, err := egw.client.checkOpenApiEndpointCompatibility(endpoint)
minimumApiVersion, err := egw.client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return err
}
Expand All @@ -197,13 +240,29 @@ func (egw *NsxtEdgeGateway) Delete() error {
return nil
}

// MoveToVdcOrVdcGroup moves NSX-T Edge Gateway to another VDC. This can cover such scenarios:
// * Move from VDC to VDC Group
// * Move from VDC Group to VDC (which is part of that VDC Group)
//
// This function is just an Update operation with OwnerRef changed to vdcGroupId, but it is more
// convenient to use it.
// Note. NSX-T Edge Gateway cannot be moved directly from one VDC to another
func (egw *NsxtEdgeGateway) MoveToVdcOrVdcGroup(vdcOrVdcGroupId string) (*NsxtEdgeGateway, error) {
edgeGatewayConfig := egw.EdgeGateway
edgeGatewayConfig.OwnerRef = &types.OpenApiReference{ID: vdcOrVdcGroupId}
// Explicitly unset VDC field because using it fails
edgeGatewayConfig.OrgVdc = nil

return egw.Update(edgeGatewayConfig)
}

// getNsxtEdgeGatewayById is a private parent for wrapped functions:
// func (adminOrg *AdminOrg) GetNsxtEdgeGatewayByName(id string) (*NsxtEdgeGateway, error)
// func (org *Org) GetNsxtEdgeGatewayByName(id string) (*NsxtEdgeGateway, error)
// func (vdc *Vdc) GetNsxtEdgeGatewayById(id string) (*NsxtEdgeGateway, error)
func getNsxtEdgeGatewayById(client *Client, id string, queryParameters url.Values) (*NsxtEdgeGateway, error) {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointEdgeGateways
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
minimumApiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -255,7 +314,7 @@ func returnSingleNsxtEdgeGateway(name string, allEdges []*NsxtEdgeGateway) (*Nsx
// func (vdc *Vdc) GetAllNsxtEdgeGateways(queryParameters url.Values) ([]*NsxtEdgeGateway, error)
func getAllNsxtEdgeGateways(client *Client, queryParameters url.Values) ([]*NsxtEdgeGateway, error) {
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointEdgeGateways
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
minimumApiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint)
if err != nil {
return nil, err
}
Expand Down
85 changes: 85 additions & 0 deletions govcd/nsxt_edgegateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,88 @@ func (vcd *TestVCD) Test_NsxtEdgeCreate(check *C) {
err = updatedEdge.Delete()
check.Assert(err, IsNil)
}

func (vcd *TestVCD) Test_NsxtEdgeVdcGroup(check *C) {
skipNoNsxtConfiguration(vcd, check)
skipOpenApiEndpointTest(vcd, check, types.OpenApiPathVersion1_0_0+types.OpenApiEndpointEdgeGateways)

adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
vbauzys marked this conversation as resolved.
Show resolved Hide resolved
check.Assert(err, IsNil)

org, err := vcd.client.GetOrgByName(vcd.config.VCD.Org)
check.Assert(err, IsNil)

nsxtExternalNetwork, err := GetExternalNetworkV2ByName(vcd.client, vcd.config.VCD.Nsxt.ExternalNetwork)
check.Assert(err, IsNil)

vdc, vdcGroup := test_CreateVdcGroup(check, adminOrg, vcd)

egwDefinition := &types.OpenAPIEdgeGateway{
Name: "nsx-t-edge",
Description: "nsx-t-edge-description",
OwnerRef: &types.OpenApiReference{
ID: vdc.Vdc.ID,
},
EdgeGatewayUplinks: []types.EdgeGatewayUplinks{{
UplinkID: nsxtExternalNetwork.ExternalNetwork.ID,
Subnets: types.OpenAPIEdgeGatewaySubnets{Values: []types.OpenAPIEdgeGatewaySubnetValue{{
Gateway: "1.1.1.1",
PrefixLength: 24,
Enabled: true,
}}},
Connected: true,
Dedicated: false,
}},
}

// Create Edge Gateway in VDC
createdEdge, err := adminOrg.CreateNsxtEdgeGateway(egwDefinition)
check.Assert(err, IsNil)
check.Assert(createdEdge.EdgeGateway.OwnerRef.ID, Matches, `^urn:vcloud:vdc:.*`)
openApiEndpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointEdgeGateways + createdEdge.EdgeGateway.ID
PrependToCleanupListOpenApi(createdEdge.EdgeGateway.Name, check.TestName(), openApiEndpoint)

check.Assert(createdEdge.EdgeGateway.Name, Equals, egwDefinition.Name)
check.Assert(createdEdge.EdgeGateway.OwnerRef.ID, Equals, egwDefinition.OwnerRef.ID)

// Move Edge Gateway to VDC Group
movedGateway, err := createdEdge.MoveToVdcOrVdcGroup(vdcGroup.VdcGroup.Id)
check.Assert(err, IsNil)
check.Assert(movedGateway.EdgeGateway.OwnerRef.ID, Equals, vdcGroup.VdcGroup.Id)
check.Assert(movedGateway.EdgeGateway.OwnerRef.ID, Matches, `^urn:vcloud:vdcGroup:.*`)

// Get by name and owner ID
edgeByNameAndOwnerId, err := org.GetNsxtEdgeGatewayByNameAndOwnerId(createdEdge.EdgeGateway.Name, vdcGroup.VdcGroup.Id)
check.Assert(err, IsNil)

// Check lookup of Edge Gateways in VDC Groups
edgeInVdcGroup, err := vdcGroup.GetNsxtEdgeGatewayByName(createdEdge.EdgeGateway.Name)
check.Assert(err, IsNil)

// Ensure both methods for retrieval get the same object
check.Assert(edgeByNameAndOwnerId.EdgeGateway, DeepEquals, edgeInVdcGroup.EdgeGateway)

// Masking known variables that have change for deep check
edgeInVdcGroup.EdgeGateway.OwnerRef.Name = ""
check.Assert(edgeInVdcGroup.EdgeGateway, DeepEquals, createdEdge.EdgeGateway)

// Move Edge Gateway back to VDC from VDC Group
egwDefinition.OwnerRef.ID = vdc.Vdc.ID
egwDefinition.ID = movedGateway.EdgeGateway.ID

movedBackToVdcEdge, err := movedGateway.Update(egwDefinition)
check.Assert(err, IsNil)
check.Assert(movedBackToVdcEdge.EdgeGateway.OwnerRef.ID, Matches, `^urn:vcloud:vdc:.*`)

// Ignore known to differ fields, but check that whole Edge Gateway structure remains the same
// as it is important to perform update operations without impacting configuration itself

// Fields to ignore on both sides
createdEdge.EdgeGateway.OrgVdc = movedBackToVdcEdge.EdgeGateway.OrgVdc
createdEdge.EdgeGateway.OwnerRef = movedBackToVdcEdge.EdgeGateway.OwnerRef
check.Assert(movedBackToVdcEdge.EdgeGateway, DeepEquals, createdEdge.EdgeGateway)

// Remove Edge Gateway
err = movedBackToVdcEdge.Delete()
check.Assert(err, IsNil)
}
4 changes: 2 additions & 2 deletions govcd/openapi_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ func (client *Client) checkOpenApiEndpointCompatibility(endpoint string) (string
return minimumApiVersion, nil
}

// getOpenApiHighestElevatedVersion returns highest supported API version for particular endpoint
// getOpenApiHighestElevatedVersion returns the highest supported API version for particular endpoint
// These API versions must be defined in endpointElevatedApiVersions. If none are there - it will return minimum
// supported API versions just like client.checkOpenApiEndpointCompatibility().
//
// The advantage of this functions is that it provides a controlled API elevation instead of just picking the highest
// The advantage of this function is that it provides a controlled API elevation instead of just picking the highest
// which could be risky and untested (especially if new API version is released after release of package consuming this
// SDK)
func (client *Client) getOpenApiHighestElevatedVersion(endpoint string) (string, error) {
Expand Down
Loading