Skip to content

Commit

Permalink
Fix struct docs
Browse files Browse the repository at this point in the history
Signed-off-by: Dainius Serplis <dserplis@vmware.com>
  • Loading branch information
Didainius committed Jun 16, 2021
1 parent e5337d0 commit f8d2c19
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 126 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
[#368](https://github.com/vmware/go-vcloud-director/pull/368)
* Added `NsxtAppPortProfile` and `types.NsxtAppPortProfile` for NSX-T Application Port Profile management
[#378](https://github.com/vmware/go-vcloud-director/pull/378)

* Added `NsxtIpSecVpnTunnel` and `types.NsxtIpSecVpnTunnel` for NSX-T IPsec VPN Tunnel configuration
[#385](https://github.com/vmware/go-vcloud-director/pull/385)

BREAKING CHANGES:
* Added parameter `description` to method `vdc.ComposeRawVapp` [#372](https://github.com/vmware/go-vcloud-director/pull/372)
Expand Down
116 changes: 62 additions & 54 deletions govcd/nsxt_ipsec_vpn_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,24 @@ import (
"github.com/vmware/go-vcloud-director/v2/util"
)

// NsxtIpSecVpnTunnel supports site-to-site policy-based IPsec VPN between an NSX-T Data Center Edge Gateway instance
// and a remote site.
// IPsec VPN offers site-to-site connectivity between an Edge Gateway and remote sites which also use NSX-T Data Center
// or which have either third-party hardware routers or VPN gateways that support IPsec.
// Policy-based IPsec VPN requires a VPN policy to be applied to packets to determine which traffic is to be protected
// by IPsec before being passed through a VPN tunnel. This type of VPN is considered static because when a local network
// topology and configuration change, the VPN policy settings must also be updated to accommodate the changes.
// NSX-T Data Center Edge Gateways support split tunnel configuration, with IPsec traffic taking routing precedence.
// VMware Cloud Director supports automatic route redistribution when you use IPsec VPN on an NSX-T edge gateway.
// NsxtIpSecVpnTunnel offers site-to-site connectivity between an Edge Gateway and remote sites which also use NSX-T
// Data Center or which have either third-party hardware routers or VPN gateways that support IPsec. Policy-based IPsec
// VPN requires a VPN policy to be applied to packets to determine which traffic is to be protected by IPsec before
// being passed through a VPN tunnel. This type of VPN is considered static because when a local network topology and
// configuration change, the VPN policy settings must also be updated to accommodate the changes. NSX-T Data Center Edge
// Gateways support split tunnel configuration, with IPsec traffic taking routing precedence. VMware Cloud Director
// supports automatic route redistribution when you use IPsec VPN on an NSX-T edge gateway.
type NsxtIpSecVpnTunnel struct {
NsxtIpSecVpn *types.NsxtIpSecVpnTunnel
client *Client
// edgeGatewayId is stored here so that pointer receiver functions can embed edge gateway ID into path
edgeGatewayId string
}

// GetAllIpSecVpns returns all IPsec VPN configurations
func (egw *NsxtEdgeGateway) GetAllIpSecVpns(queryParameters url.Values) ([]*NsxtIpSecVpnTunnel, error) {
// GetAllIpSecVpnTunnels returns all IPsec VPN Tunnel configurations
func (egw *NsxtEdgeGateway) GetAllIpSecVpnTunnels(queryParameters url.Values) ([]*NsxtIpSecVpnTunnel, error) {
client := egw.client
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpn
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpnTunnel
apiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
if err != nil {
return nil, err
Expand Down Expand Up @@ -61,13 +59,14 @@ func (egw *NsxtEdgeGateway) GetAllIpSecVpns(queryParameters url.Values) ([]*Nsxt
return wrappedResponses, nil
}

func (egw *NsxtEdgeGateway) GetIpSecVpnById(id string) (*NsxtIpSecVpnTunnel, error) {
//GetIpSecVpnTunnelById retrieves single IPsec VPN Tunnel by ID
func (egw *NsxtEdgeGateway) GetIpSecVpnTunnelById(id string) (*NsxtIpSecVpnTunnel, error) {
if id == "" {
return nil, fmt.Errorf("canot find NSX-T IPsec VPN configuration without ID")
return nil, fmt.Errorf("canot find NSX-T IPsec VPN Tunnel configuration without ID")
}

client := egw.client
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpn
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpnTunnel
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
if err != nil {
return nil, err
Expand All @@ -92,14 +91,18 @@ func (egw *NsxtEdgeGateway) GetIpSecVpnById(id string) (*NsxtIpSecVpnTunnel, err
return returnObject, nil
}

func (egw *NsxtEdgeGateway) GetIpSecVpnByName(name string) (*NsxtIpSecVpnTunnel, error) {
// GetIpSecVpnTunnelByName retrieves single IPsec VPN Tunnel by Name.
//
// Note. Name uniqueness is not enforced therefore it there might exists a few IPsec VPN Tunnels with the same name.
// An error will be returned in that case.
func (egw *NsxtEdgeGateway) GetIpSecVpnTunnelByName(name string) (*NsxtIpSecVpnTunnel, error) {
if name == "" {
return nil, fmt.Errorf("canot find NSX-T IPsec VPN configuration without Name")
return nil, fmt.Errorf("canot find NSX-T IPsec VPN Tunnel configuration without Name")
}

allVpns, err := egw.GetAllIpSecVpns(nil)
allVpns, err := egw.GetAllIpSecVpnTunnels(nil)
if err != nil {
return nil, fmt.Errorf("error retrieving all NSX-T IPsec VPN configurations: %s", err)
return nil, fmt.Errorf("error retrieving all NSX-T IPsec VPN Tunnel configurations: %s", err)
}

var allResults []*NsxtIpSecVpnTunnel
Expand All @@ -111,20 +114,22 @@ func (egw *NsxtEdgeGateway) GetIpSecVpnByName(name string) (*NsxtIpSecVpnTunnel,
}

if len(allResults) > 1 {
return nil, fmt.Errorf("error - found %d NSX-T IPsec VPN configuratios with Name '%s'. Expected 1", len(allResults), name)
return nil, fmt.Errorf("error - found %d NSX-T IPsec VPN Tunnel configuratios with Name '%s'. Expected 1",
len(allResults), name)
}

if len(allResults) == 0 {
return nil, ErrorEntityNotFound
}

// Retrieving the object by ID, because only it includes Pre-shared Key
return egw.GetIpSecVpnById(allResults[0].NsxtIpSecVpn.ID)
// Retrieving again the object by ID, because only it includes Pre-shared Key
return egw.GetIpSecVpnTunnelById(allResults[0].NsxtIpSecVpn.ID)
}

func (egw *NsxtEdgeGateway) CreateIpSecVpn(ipSecVpnConfig *types.NsxtIpSecVpnTunnel) (*NsxtIpSecVpnTunnel, error) {
// CreateIpSecVpnTunnel creates IPsec VPN Tunnel and returns it
func (egw *NsxtEdgeGateway) CreateIpSecVpnTunnel(ipSecVpnConfig *types.NsxtIpSecVpnTunnel) (*NsxtIpSecVpnTunnel, error) {
client := egw.client
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpn
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpnTunnel
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
if err != nil {
return nil, err
Expand All @@ -137,45 +142,46 @@ func (egw *NsxtEdgeGateway) CreateIpSecVpn(ipSecVpnConfig *types.NsxtIpSecVpnTun

task, err := client.OpenApiPostItemAsync(minimumApiVersion, urlRef, nil, ipSecVpnConfig)
if err != nil {
return nil, fmt.Errorf("error creating NSX-T IPsec VPN configuration: %s", err)
return nil, fmt.Errorf("error creating NSX-T IPsec VPN Tunnel configuration: %s", err)
}

err = task.WaitTaskCompletion()
if err != nil {
return nil, fmt.Errorf("task failed while creating NSX-T IPsec VPN configuration: %s", err)
return nil, fmt.Errorf("task failed while creating NSX-T IPsec VPN Tunnel configuration: %s", err)
}

// filtering even by Name is not supported
allVpns, err := egw.GetAllIpSecVpns(nil)
allVpns, err := egw.GetAllIpSecVpnTunnels(nil)
if err != nil {
return nil, fmt.Errorf("error retrieving all NSX-T IPsec VPN configuration after creation: %s", err)
return nil, fmt.Errorf("error retrieving all NSX-T IPsec VPN Tunnel configuration after creation: %s", err)
}

for index, singleConfig := range allVpns {
if singleConfig.IsEqualTo(ipSecVpnConfig) {
// retrieve exact value by ID, because only this endpoint includes private key
ipSecVpn, err := egw.GetIpSecVpnById(allVpns[index].NsxtIpSecVpn.ID)
ipSecVpn, err := egw.GetIpSecVpnTunnelById(allVpns[index].NsxtIpSecVpn.ID)
if err != nil {
return nil, fmt.Errorf("error retrieving NSX-T IPsec VPN configuration: %s", err)
return nil, fmt.Errorf("error retrieving NSX-T IPsec VPN Tunnel configuration: %s", err)
}

return ipSecVpn, nil
}
}

return nil, fmt.Errorf("error finding NSX-T IPsec VPN configuration after creation: %s", ErrorEntityNotFound)
return nil, fmt.Errorf("error finding NSX-T IPsec VPN Tunnel configuration after creation: %s", ErrorEntityNotFound)
}

// Update updates NSX-T IPsec VPN Tunnel configuration with newly supplied data.
func (ipSecVpn *NsxtIpSecVpnTunnel) Update(ipSecVpnConfig *types.NsxtIpSecVpnTunnel) (*NsxtIpSecVpnTunnel, error) {
client := ipSecVpn.client
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpn
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpnTunnel
apiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
if err != nil {
return nil, err
}

if ipSecVpn.NsxtIpSecVpn.ID == "" {
return nil, fmt.Errorf("cannot update NSX-T IPsec VPN configuration without ID")
return nil, fmt.Errorf("cannot update NSX-T IPsec VPN Tunnel configuration without ID")
}

urlRef, err := client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, ipSecVpn.edgeGatewayId), ipSecVpn.NsxtIpSecVpn.ID)
Expand All @@ -191,23 +197,23 @@ func (ipSecVpn *NsxtIpSecVpnTunnel) Update(ipSecVpnConfig *types.NsxtIpSecVpnTun

err = client.OpenApiPutItem(apiVersion, urlRef, nil, ipSecVpnConfig, returnObject.NsxtIpSecVpn)
if err != nil {
return nil, fmt.Errorf("error updating NSX-T IPsec VPN configuration: %s", err)
return nil, fmt.Errorf("error updating NSX-T IPsec VPN Tunnel configuration: %s", err)
}

return returnObject, nil
}

// Delete allows users to delete NSX-T Application Port Profile
// Delete allows users to delete NSX-T IPsec VPN Tunnel
func (ipSecVpn *NsxtIpSecVpnTunnel) Delete() error {
client := ipSecVpn.client
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpn
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpnTunnel
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
if err != nil {
return err
}

if ipSecVpn.NsxtIpSecVpn.ID == "" {
return fmt.Errorf("cannot delete NSX-T IPsec VPN configuration without ID")
return fmt.Errorf("cannot delete NSX-T IPsec VPN Tunnel configuration without ID")
}

urlRef, err := ipSecVpn.client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, ipSecVpn.edgeGatewayId), ipSecVpn.NsxtIpSecVpn.ID)
Expand All @@ -216,27 +222,27 @@ func (ipSecVpn *NsxtIpSecVpnTunnel) Delete() error {
}

err = ipSecVpn.client.OpenApiDeleteItem(minimumApiVersion, urlRef, nil)

if err != nil {
return fmt.Errorf("error deleting NSX-T IPsec VPN configuration: %s", err)
return fmt.Errorf("error deleting NSX-T IPsec VPN Tunnel configuration: %s", err)
}

return nil
}

// GetStatus returns status of IPsec VPN Tunnel.
//
// Note. This is not being immediately populated and may appear after some time
// Note. This is not being immediately populated and may appear after some time depending on
// NsxtIpSecVpnTunnelSecurityProfile.DpdConfiguration
func (ipSecVpn *NsxtIpSecVpnTunnel) GetStatus() (*types.NsxtIpSecVpnTunnelStatus, error) {
client := ipSecVpn.client
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpnStatus
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpnTunnelStatus
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
if err != nil {
return nil, err
}

if ipSecVpn.NsxtIpSecVpn.ID == "" {
return nil, fmt.Errorf("cannot get NSX-T IPsec VPN status without ID")
return nil, fmt.Errorf("cannot get NSX-T IPsec VPN Tunnel status without ID")
}

urlRef, err := ipSecVpn.client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, ipSecVpn.edgeGatewayId, ipSecVpn.NsxtIpSecVpn.ID))
Expand All @@ -247,17 +253,18 @@ func (ipSecVpn *NsxtIpSecVpnTunnel) GetStatus() (*types.NsxtIpSecVpnTunnelStatus
ipSecVpnTunnelStatus := &types.NsxtIpSecVpnTunnelStatus{}

err = ipSecVpn.client.OpenApiGetItem(minimumApiVersion, urlRef, nil, ipSecVpnTunnelStatus)

if err != nil {
return nil, fmt.Errorf("error deleting NSX-T IPsec VPN configuration: %s", err)
return nil, fmt.Errorf("error deleting NSX-T IPsec VPN Tunnel configuration: %s", err)
}

return ipSecVpnTunnelStatus, nil
}

// UpdateTunnelConnectionProperties allows user to customize IPsec VPN Tunnel Security Profile when the default one
// does not fit requirements.
func (ipSecVpn *NsxtIpSecVpnTunnel) UpdateTunnelConnectionProperties(ipSecVpnTunnelConnectionProperties *types.NsxtIpSecVpnTunnelSecurityProfile) (*types.NsxtIpSecVpnTunnelSecurityProfile, error) {
client := ipSecVpn.client
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpnConnectionProperties
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpnTunnelConnectionProperties
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
if err != nil {
return nil, err
Expand All @@ -281,9 +288,10 @@ func (ipSecVpn *NsxtIpSecVpnTunnel) UpdateTunnelConnectionProperties(ipSecVpnTun
return ipSecVpnTunnelProfile, nil
}

// GetTunnelConnectionProperties retrieves IPsec VPN Tunnel Security Profile
func (ipSecVpn *NsxtIpSecVpnTunnel) GetTunnelConnectionProperties() (*types.NsxtIpSecVpnTunnelSecurityProfile, error) {
client := ipSecVpn.client
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpnConnectionProperties
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpnTunnelConnectionProperties
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
if err != nil {
return nil, err
Expand All @@ -307,8 +315,8 @@ func (ipSecVpn *NsxtIpSecVpnTunnel) GetTunnelConnectionProperties() (*types.Nsxt
return ipSecVpnTunnelProfile, nil
}

// IsEqualTo helps to find NSX-T IPsec Configuration
// Combination of LocalAddress and RemoteAddress has to be unique. This is a list of fields compared:
// IsEqualTo helps to find NSX-T IPsec VPN Tunnel Configuration
// Combination of LocalAddress and RemoteAddress has to be unique (enforced by API). This is a list of fields compared:
// * Name
// * Description
// * Enabled
Expand All @@ -318,12 +326,12 @@ func (ipSecVpn *NsxtIpSecVpnTunnel) IsEqualTo(vpnConfig *types.NsxtIpSecVpnTunne
return ipSetVpnRulesEqual(ipSecVpn.NsxtIpSecVpn, vpnConfig)
}

// ipSetVpnRulesEqual performs comparison of two rules to ease lookup. This is a list of fields compared:
//// * Name
//// * Description
//// * Enabled
//// * LocalEndpoint.LocalAddress
//// * RemoteEndpoint.RemoteAddress
// ipSetVpnRulesEqual performs comparison of two NSX-T IPsec VPN Tunnels to ease lookup. This is a list of fields compared:
// * Name
// * Description
// * Enabled
// * LocalEndpoint.LocalAddress
// * RemoteEndpoint.RemoteAddress
func ipSetVpnRulesEqual(first, second *types.NsxtIpSecVpnTunnel) bool {
util.Logger.Println("comparing NSX-T IP Sev VPN configuration:")
util.Logger.Printf("%+v\n", first)
Expand Down
20 changes: 10 additions & 10 deletions govcd/nsxt_ipsec_vpn_tunnel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ func (vcd *TestVCD) Test_NsxtIpSecVpnCustomSecurityProfile(check *C) {
Logging: false,
}

createdIpSecVpn, err := edge.CreateIpSecVpn(ipSecDef)
createdIpSecVpn, err := edge.CreateIpSecVpnTunnel(ipSecDef)
check.Assert(err, IsNil)
openApiEndpoint := types.OpenApiPathVersion1_0_0 + fmt.Sprintf(types.OpenApiEndpointIpSecVpn, createdIpSecVpn.edgeGatewayId) + createdIpSecVpn.NsxtIpSecVpn.ID
openApiEndpoint := types.OpenApiPathVersion1_0_0 + fmt.Sprintf(types.OpenApiEndpointIpSecVpnTunnel, createdIpSecVpn.edgeGatewayId) + createdIpSecVpn.NsxtIpSecVpn.ID
AddToCleanupListOpenApi(createdIpSecVpn.NsxtIpSecVpn.Name, check.TestName(), openApiEndpoint)

foundIpSecVpnById, err := edge.GetIpSecVpnById(createdIpSecVpn.NsxtIpSecVpn.ID)
foundIpSecVpnById, err := edge.GetIpSecVpnTunnelById(createdIpSecVpn.NsxtIpSecVpn.ID)
check.Assert(err, IsNil)
check.Assert(foundIpSecVpnById.NsxtIpSecVpn, DeepEquals, createdIpSecVpn.NsxtIpSecVpn)

foundIpSecVpnByName, err := edge.GetIpSecVpnByName(createdIpSecVpn.NsxtIpSecVpn.Name)
foundIpSecVpnByName, err := edge.GetIpSecVpnTunnelByName(createdIpSecVpn.NsxtIpSecVpn.Name)
check.Assert(err, IsNil)
check.Assert(foundIpSecVpnByName.NsxtIpSecVpn, DeepEquals, createdIpSecVpn.NsxtIpSecVpn)
check.Assert(foundIpSecVpnByName.NsxtIpSecVpn, DeepEquals, foundIpSecVpnById.NsxtIpSecVpn)
Expand All @@ -104,7 +104,7 @@ func (vcd *TestVCD) Test_NsxtIpSecVpnCustomSecurityProfile(check *C) {
check.Assert(err, IsNil)

// Ensure rule does not exist in the list
allVpnConfigs, err := edge.GetAllIpSecVpns(nil)
allVpnConfigs, err := edge.GetAllIpSecVpnTunnels(nil)
check.Assert(err, IsNil)
for _, vpnConfig := range allVpnConfigs {
check.Assert(vpnConfig.IsEqualTo(updatedIpSecVpn.NsxtIpSecVpn), Equals, false)
Expand All @@ -113,16 +113,16 @@ func (vcd *TestVCD) Test_NsxtIpSecVpnCustomSecurityProfile(check *C) {
}

func runIpSecVpnTests(check *C, edge *NsxtEdgeGateway, ipSecDef *types.NsxtIpSecVpnTunnel) {
createdIpSecVpn, err := edge.CreateIpSecVpn(ipSecDef)
createdIpSecVpn, err := edge.CreateIpSecVpnTunnel(ipSecDef)
check.Assert(err, IsNil)
openApiEndpoint := types.OpenApiPathVersion1_0_0 + fmt.Sprintf(types.OpenApiEndpointIpSecVpn, createdIpSecVpn.edgeGatewayId) + createdIpSecVpn.NsxtIpSecVpn.ID
openApiEndpoint := types.OpenApiPathVersion1_0_0 + fmt.Sprintf(types.OpenApiEndpointIpSecVpnTunnel, createdIpSecVpn.edgeGatewayId) + createdIpSecVpn.NsxtIpSecVpn.ID
AddToCleanupListOpenApi(createdIpSecVpn.NsxtIpSecVpn.Name, check.TestName(), openApiEndpoint)

foundIpSecVpnById, err := edge.GetIpSecVpnById(createdIpSecVpn.NsxtIpSecVpn.ID)
foundIpSecVpnById, err := edge.GetIpSecVpnTunnelById(createdIpSecVpn.NsxtIpSecVpn.ID)
check.Assert(err, IsNil)
check.Assert(foundIpSecVpnById.NsxtIpSecVpn, DeepEquals, createdIpSecVpn.NsxtIpSecVpn)

foundIpSecVpnByName, err := edge.GetIpSecVpnByName(createdIpSecVpn.NsxtIpSecVpn.Name)
foundIpSecVpnByName, err := edge.GetIpSecVpnTunnelByName(createdIpSecVpn.NsxtIpSecVpn.Name)
check.Assert(err, IsNil)
check.Assert(foundIpSecVpnByName.NsxtIpSecVpn, DeepEquals, createdIpSecVpn.NsxtIpSecVpn)
check.Assert(foundIpSecVpnByName.NsxtIpSecVpn, DeepEquals, foundIpSecVpnById.NsxtIpSecVpn)
Expand All @@ -143,7 +143,7 @@ func runIpSecVpnTests(check *C, edge *NsxtEdgeGateway, ipSecDef *types.NsxtIpSec
check.Assert(err, IsNil)

// Ensure rule does not exist in the list
allVpnConfigs, err := edge.GetAllIpSecVpns(nil)
allVpnConfigs, err := edge.GetAllIpSecVpnTunnels(nil)
check.Assert(err, IsNil)
for _, vpnConfig := range allVpnConfigs {
check.Assert(vpnConfig.IsEqualTo(updatedIpSecVpn.NsxtIpSecVpn), Equals, false)
Expand Down
Loading

0 comments on commit f8d2c19

Please sign in to comment.