github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/ip_space_uplink_test.go (about)

     1  //go:build network || nsxt || functional || openapi || ALL
     2  
     3  package govcd
     4  
     5  import (
     6  	"fmt"
     7  	"time"
     8  
     9  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    10  	. "gopkg.in/check.v1"
    11  )
    12  
    13  func (vcd *TestVCD) Test_IpSpaceUplink(check *C) {
    14  	if vcd.skipAdminTests {
    15  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
    16  	}
    17  	skipNoNsxtConfiguration(vcd, check)
    18  	skipOpenApiEndpointTest(vcd, check, types.OpenApiPathVersion1_0_0+types.OpenApiEndpointIpSpaceUplinks)
    19  
    20  	// Create External Network (Provider Gateway)
    21  	extNet := createExternalNetwork(vcd, check)
    22  
    23  	// Create IP Space
    24  	ipSpace := createIpSpace(vcd, check)
    25  
    26  	// Create Uplink configuration
    27  	uplinkConfig := &types.IpSpaceUplink{
    28  		Name:               check.TestName(),
    29  		Description:        "IP SPace Uplink for External Network (Provider Gateway)",
    30  		ExternalNetworkRef: &types.OpenApiReference{ID: extNet.ExternalNetwork.ID},
    31  		IPSpaceRef:         &types.OpenApiReference{ID: ipSpace.IpSpace.ID},
    32  	}
    33  
    34  	createdIpSpaceUplink, err := vcd.client.CreateIpSpaceUplink(uplinkConfig)
    35  	check.Assert(err, IsNil)
    36  	check.Assert(createdIpSpaceUplink, NotNil)
    37  
    38  	openApiEndpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaceUplinks + createdIpSpaceUplink.IpSpaceUplink.ID
    39  	AddToCleanupListOpenApi(createdIpSpaceUplink.IpSpaceUplink.Name, check.TestName(), openApiEndpoint)
    40  
    41  	// Operations on IP Space related entities trigger a separate task
    42  	// 'ipSpaceUplinkRouteAdvertisementSync' which is better to finish before any other operations
    43  	// as it might cause an error: busy completing an operation IP_SPACE_UPLINK_ROUTE_ADVERTISEMENT_SYNC
    44  	// Sleeping a few seconds because the task is not immediately seen sometimes.
    45  	time.Sleep(3 * time.Second)
    46  	err = vcd.client.Client.WaitForRouteAdvertisementTasks()
    47  	check.Assert(err, IsNil)
    48  
    49  	// Get all IP Space Uplinks
    50  	allIpSpaceUplinks, err := vcd.client.GetAllIpSpaceUplinks(extNet.ExternalNetwork.ID, nil)
    51  	check.Assert(err, IsNil)
    52  	check.Assert(len(allIpSpaceUplinks) > 0, Equals, true)
    53  
    54  	// Get by ID
    55  	byId, err := vcd.client.GetIpSpaceUplinkById(createdIpSpaceUplink.IpSpaceUplink.ID)
    56  	check.Assert(err, IsNil)
    57  	check.Assert(byId, NotNil)
    58  	check.Assert(byId.IpSpaceUplink, DeepEquals, createdIpSpaceUplink.IpSpaceUplink)
    59  
    60  	// Get by Name
    61  	byName, err := vcd.client.GetIpSpaceUplinkByName(extNet.ExternalNetwork.ID, byId.IpSpaceUplink.Name)
    62  	check.Assert(err, IsNil)
    63  	check.Assert(byName, NotNil)
    64  	check.Assert(byName.IpSpaceUplink, DeepEquals, byId.IpSpaceUplink)
    65  
    66  	// Update
    67  	uplinkConfig.Name = check.TestName() + "updated"
    68  	uplinkConfig.Description = uplinkConfig.Description + "updated"
    69  	updatedUplinkConfig, err := createdIpSpaceUplink.Update(uplinkConfig)
    70  	check.Assert(err, IsNil)
    71  	check.Assert(updatedUplinkConfig.IpSpaceUplink.ID, Equals, byId.IpSpaceUplink.ID)
    72  	check.Assert(updatedUplinkConfig.IpSpaceUplink.ID, Equals, createdIpSpaceUplink.IpSpaceUplink.ID)
    73  	check.Assert(updatedUplinkConfig.IpSpaceUplink.Name, Equals, uplinkConfig.Name)
    74  	check.Assert(updatedUplinkConfig.IpSpaceUplink.Description, Equals, uplinkConfig.Description)
    75  	check.Assert(updatedUplinkConfig.IpSpaceUplink.ExternalNetworkRef.ID, Equals, createdIpSpaceUplink.IpSpaceUplink.ExternalNetworkRef.ID)
    76  	check.Assert(updatedUplinkConfig.IpSpaceUplink.IPSpaceRef.ID, Equals, createdIpSpaceUplink.IpSpaceUplink.IPSpaceRef.ID)
    77  
    78  	// Read-only variables
    79  	check.Assert(updatedUplinkConfig.IpSpaceUplink.IPSpaceType, Equals, types.IpSpacePublic)
    80  	check.Assert(updatedUplinkConfig.IpSpaceUplink.Status, Equals, "REALIZED")
    81  
    82  	time.Sleep(3 * time.Second)
    83  	err = vcd.client.Client.WaitForRouteAdvertisementTasks()
    84  	check.Assert(err, IsNil)
    85  
    86  	err = createdIpSpaceUplink.Delete()
    87  	check.Assert(err, IsNil)
    88  
    89  	// Check that IP Space Uplink was deleted
    90  	_, err = vcd.client.GetIpSpaceUplinkById(updatedUplinkConfig.IpSpaceUplink.ID)
    91  	check.Assert(ContainsNotFound(err), Equals, true)
    92  
    93  	err = extNet.Delete()
    94  	check.Assert(err, IsNil)
    95  
    96  	err = ipSpace.Delete()
    97  	check.Assert(err, IsNil)
    98  }
    99  
   100  func createIpSpace(vcd *TestVCD, check *C) *IpSpace {
   101  	ipSpaceConfig := &types.IpSpace{
   102  		Name:                      check.TestName(),
   103  		IPSpaceInternalScope:      []string{"22.0.0.0/24"},
   104  		IPSpaceExternalScope:      "200.0.0.1/24",
   105  		Type:                      types.IpSpacePublic,
   106  		RouteAdvertisementEnabled: false,
   107  		IPSpacePrefixes: []types.IPSpacePrefixes{
   108  			{
   109  				DefaultQuotaForPrefixLength: -1,
   110  				IPPrefixSequence: []types.IPPrefixSequence{
   111  					{
   112  						StartingPrefixIPAddress: "22.0.0.200",
   113  						PrefixLength:            31,
   114  						TotalPrefixCount:        3,
   115  					},
   116  				},
   117  			},
   118  			{
   119  				DefaultQuotaForPrefixLength: 2,
   120  				IPPrefixSequence: []types.IPPrefixSequence{
   121  					{
   122  						StartingPrefixIPAddress: "22.0.0.100",
   123  						PrefixLength:            30,
   124  						TotalPrefixCount:        3,
   125  					},
   126  				},
   127  			},
   128  		},
   129  		IPSpaceRanges: types.IPSpaceRanges{
   130  			DefaultFloatingIPQuota: 3,
   131  			IPRanges: []types.IpSpaceRangeValues{
   132  				{
   133  					StartIPAddress: "22.0.0.10",
   134  					EndIPAddress:   "22.0.0.30",
   135  				},
   136  				{
   137  					StartIPAddress: "22.0.0.32",
   138  					EndIPAddress:   "22.0.0.34",
   139  				},
   140  			},
   141  		},
   142  	}
   143  
   144  	createdIpSpace, err := vcd.client.CreateIpSpace(ipSpaceConfig)
   145  	check.Assert(err, IsNil)
   146  	check.Assert(createdIpSpace, NotNil)
   147  	openApiEndpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSpaces + createdIpSpace.IpSpace.ID
   148  	AddToCleanupListOpenApi(createdIpSpace.IpSpace.Name, check.TestName(), openApiEndpoint)
   149  
   150  	return createdIpSpace
   151  }
   152  
   153  func createExternalNetwork(vcd *TestVCD, check *C) *ExternalNetworkV2 {
   154  	// NSX-T details
   155  	man, err := vcd.client.QueryNsxtManagerByName(vcd.config.VCD.Nsxt.Manager)
   156  	check.Assert(err, IsNil)
   157  	nsxtManagerId, err := BuildUrnWithUuid("urn:vcloud:nsxtmanager:", extractUuid(man[0].HREF))
   158  	check.Assert(err, IsNil)
   159  
   160  	backingId := getBackingIdByNameAndType(check, vcd.config.VCD.Nsxt.Tier0router, types.ExternalNetworkBackingTypeNsxtTier0Router, vcd, nsxtManagerId)
   161  
   162  	net := &types.ExternalNetworkV2{
   163  		Name:        check.TestName(),
   164  		Description: "",
   165  		NetworkBackings: types.ExternalNetworkV2Backings{Values: []types.ExternalNetworkV2Backing{
   166  			{
   167  				BackingID: backingId,
   168  				NetworkProvider: types.NetworkProvider{
   169  					ID: nsxtManagerId,
   170  				},
   171  				BackingTypeValue: types.ExternalNetworkBackingTypeNsxtTier0Router,
   172  			},
   173  		}},
   174  		UsingIpSpace: addrOf(true),
   175  	}
   176  
   177  	createdNet, err := CreateExternalNetworkV2(vcd.client, net)
   178  	check.Assert(err, IsNil)
   179  	check.Assert(createdNet, NotNil)
   180  
   181  	// Use generic "OpenApiEntity" resource cleanup type
   182  	openApiEndpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointExternalNetworks + createdNet.ExternalNetwork.ID
   183  	AddToCleanupListOpenApi(createdNet.ExternalNetwork.Name, check.TestName(), openApiEndpoint)
   184  
   185  	return createdNet
   186  }