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

     1  //go:build network || nsxt || functional || openapi || ALL
     2  
     3  /*
     4   * Copyright 2021 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    13  	. "gopkg.in/check.v1"
    14  )
    15  
    16  func (vcd *TestVCD) Test_NsxtOrgVdcNetworkIsolated(check *C) {
    17  	skipOpenApiEndpointTest(vcd, check, types.OpenApiPathVersion1_0_0+types.OpenApiEndpointOrgVdcNetworks)
    18  	skipNoNsxtConfiguration(vcd, check)
    19  	vcd.skipIfNotSysAdmin(check) // this test uses GetNsxtEdgeClusterByName, which requires system administrator privileges
    20  
    21  	orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
    22  		Name:        check.TestName(),
    23  		Description: check.TestName() + "-description",
    24  		OwnerRef:    &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},
    25  		NetworkType: types.OrgVdcNetworkTypeIsolated,
    26  		Subnets: types.OrgVdcNetworkSubnets{
    27  			Values: []types.OrgVdcNetworkSubnetValues{
    28  				{
    29  					Gateway:      "2.1.1.1",
    30  					PrefixLength: 24,
    31  					DNSServer1:   "8.8.8.8",
    32  					DNSServer2:   "8.8.4.4",
    33  					DNSSuffix:    "bar.foo",
    34  					IPRanges: types.OrgVdcNetworkSubnetIPRanges{
    35  						Values: []types.OrgVdcNetworkSubnetIPRangeValues{
    36  							{
    37  								StartAddress: "2.1.1.20",
    38  								EndAddress:   "2.1.1.30",
    39  							},
    40  							{
    41  								StartAddress: "2.1.1.40",
    42  								EndAddress:   "2.1.1.50",
    43  							},
    44  							{
    45  								StartAddress: "2.1.1.88",
    46  								EndAddress:   "2.1.1.92",
    47  							},
    48  						}},
    49  				},
    50  			},
    51  		},
    52  	}
    53  
    54  	runOpenApiOrgVdcNetworkTestWithSegmentProfileTemplateEndpoint(check, vcd, vcd.nsxtVdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeIsolated)
    55  	runOpenApiOrgVdcNetworkTestWithSegmentProfileTemplate(check, vcd, vcd.nsxtVdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeIsolated)
    56  	runOpenApiOrgVdcNetworkTest(check, vcd, vcd.nsxtVdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeIsolated, []dhcpConfigFunc{nsxtDhcpConfigNetworkMode})
    57  	runOpenApiOrgVdcNetworkWithVdcGroupTest(check, vcd, orgVdcNetworkConfig, types.OrgVdcNetworkTypeIsolated, []dhcpConfigFunc{nsxtDhcpConfigNetworkMode})
    58  }
    59  
    60  func (vcd *TestVCD) Test_NsxtOrgVdcNetworkRouted(check *C) {
    61  	skipOpenApiEndpointTest(vcd, check, types.OpenApiPathVersion1_0_0+types.OpenApiEndpointOrgVdcNetworks)
    62  	skipNoNsxtConfiguration(vcd, check)
    63  	vcd.skipIfNotSysAdmin(check) // this test uses GetNsxtEdgeClusterByName, which requires system administrator privileges
    64  
    65  	egw, err := vcd.org.GetNsxtEdgeGatewayByName(vcd.config.VCD.Nsxt.EdgeGateway)
    66  	check.Assert(err, IsNil)
    67  
    68  	orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
    69  		Name:        check.TestName(),
    70  		Description: check.TestName() + "-description",
    71  		OwnerRef:    &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},
    72  		NetworkType: types.OrgVdcNetworkTypeRouted,
    73  
    74  		// Connection is used for "routed" network
    75  		Connection: &types.Connection{
    76  			RouterRef: types.OpenApiReference{
    77  				ID: egw.EdgeGateway.ID,
    78  			},
    79  			ConnectionType: "INTERNAL",
    80  		},
    81  		Subnets: types.OrgVdcNetworkSubnets{
    82  			Values: []types.OrgVdcNetworkSubnetValues{
    83  				{
    84  					Gateway:      "2.1.1.1",
    85  					PrefixLength: 24,
    86  					DNSServer1:   "8.8.8.8",
    87  					DNSServer2:   "8.8.4.4",
    88  					DNSSuffix:    "foo.bar",
    89  					IPRanges: types.OrgVdcNetworkSubnetIPRanges{
    90  						Values: []types.OrgVdcNetworkSubnetIPRangeValues{
    91  							{
    92  								StartAddress: "2.1.1.20",
    93  								EndAddress:   "2.1.1.30",
    94  							},
    95  							{
    96  								StartAddress: "2.1.1.40",
    97  								EndAddress:   "2.1.1.50",
    98  							},
    99  							{
   100  								StartAddress: "2.1.1.60",
   101  								EndAddress:   "2.1.1.62",
   102  							}, {
   103  								StartAddress: "2.1.1.72",
   104  								EndAddress:   "2.1.1.74",
   105  							}, {
   106  								StartAddress: "2.1.1.84",
   107  								EndAddress:   "2.1.1.85",
   108  							},
   109  						}},
   110  				},
   111  			},
   112  		},
   113  	}
   114  
   115  	runOpenApiOrgVdcNetworkTestWithSegmentProfileTemplateEndpoint(check, vcd, vcd.nsxtVdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeRouted)
   116  	runOpenApiOrgVdcNetworkTestWithSegmentProfileTemplate(check, vcd, vcd.nsxtVdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeRouted)
   117  	runOpenApiOrgVdcNetworkTest(check, vcd, vcd.nsxtVdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeRouted, []dhcpConfigFunc{nsxtRoutedDhcpConfigEdgeMode, nsxtDhcpConfigNetworkMode})
   118  	runOpenApiOrgVdcNetworkWithVdcGroupTest(check, vcd, orgVdcNetworkConfig, types.OrgVdcNetworkTypeRouted, []dhcpConfigFunc{nsxtRoutedDhcpConfigEdgeMode, nsxtDhcpConfigNetworkMode})
   119  }
   120  
   121  func (vcd *TestVCD) Test_NsxtOrgVdcNetworkImportedNsxtLogicalSwitch(check *C) {
   122  	if vcd.skipAdminTests {
   123  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
   124  	}
   125  	skipOpenApiEndpointTest(vcd, check, types.OpenApiPathVersion1_0_0+types.OpenApiEndpointOrgVdcNetworks)
   126  	skipNoNsxtConfiguration(vcd, check)
   127  
   128  	if vcd.config.VCD.Nsxt.NsxtImportSegment == "" {
   129  		check.Skip("Unused NSX-T segment was not provided")
   130  	}
   131  
   132  	logicalSwitch, err := vcd.nsxtVdc.GetNsxtImportableSwitchByName(vcd.config.VCD.Nsxt.NsxtImportSegment)
   133  	check.Assert(err, IsNil)
   134  
   135  	orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
   136  		Name:        check.TestName(),
   137  		Description: check.TestName() + "-description",
   138  
   139  		// On v35.0 orgVdc is not supported anymore. Using ownerRef instead.
   140  		OwnerRef: &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},
   141  
   142  		NetworkType: types.OrgVdcNetworkTypeOpaque,
   143  		// BackingNetworkId contains NSX-T logical switch ID for Imported networks
   144  		BackingNetworkId: logicalSwitch.NsxtImportableSwitch.ID,
   145  
   146  		Subnets: types.OrgVdcNetworkSubnets{
   147  			Values: []types.OrgVdcNetworkSubnetValues{
   148  				{
   149  					Gateway:      "2.1.1.1",
   150  					PrefixLength: 24,
   151  					DNSServer1:   "8.8.8.8",
   152  					DNSServer2:   "8.8.4.4",
   153  					DNSSuffix:    "foo.bar",
   154  					IPRanges: types.OrgVdcNetworkSubnetIPRanges{
   155  						Values: []types.OrgVdcNetworkSubnetIPRangeValues{
   156  							{
   157  								StartAddress: "2.1.1.20",
   158  								EndAddress:   "2.1.1.30",
   159  							},
   160  							{
   161  								StartAddress: "2.1.1.40",
   162  								EndAddress:   "2.1.1.50",
   163  							},
   164  						}},
   165  				},
   166  			},
   167  		},
   168  	}
   169  
   170  	runOpenApiOrgVdcNetworkTestWithSegmentProfileTemplateEndpoint(check, vcd, vcd.nsxtVdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeOpaque)
   171  	runOpenApiOrgVdcNetworkTestWithSegmentProfileTemplate(check, vcd, vcd.nsxtVdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeOpaque)
   172  	runOpenApiOrgVdcNetworkTest(check, vcd, vcd.nsxtVdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeOpaque, nil)
   173  	runOpenApiOrgVdcNetworkWithVdcGroupTest(check, vcd, orgVdcNetworkConfig, types.OrgVdcNetworkTypeOpaque, nil)
   174  }
   175  
   176  func (vcd *TestVCD) Test_NsxtOrgVdcNetworkImportedDistributedVirtualPortGroup(check *C) {
   177  	if vcd.skipAdminTests {
   178  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
   179  	}
   180  	skipOpenApiEndpointTest(vcd, check, types.OpenApiPathVersion1_0_0+types.OpenApiEndpointOrgVdcNetworks)
   181  	skipNoNsxtConfiguration(vcd, check)
   182  
   183  	if vcd.config.VCD.Nsxt.NsxtDvpg == "" {
   184  		check.Skip("Distributed Virtual Port Group was not provided")
   185  	}
   186  
   187  	dvpg, err := vcd.nsxtVdc.GetVcenterImportableDvpgByName(vcd.config.VCD.Nsxt.NsxtDvpg)
   188  	check.Assert(err, IsNil)
   189  
   190  	orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
   191  		Name:        check.TestName(),
   192  		Description: check.TestName() + "-description",
   193  
   194  		OwnerRef: &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},
   195  
   196  		NetworkType: types.OrgVdcNetworkTypeOpaque,
   197  		// BackingNetworkId contains Distributed Virtual Port Group ID for Imported networks
   198  		BackingNetworkId:   dvpg.VcenterImportableDvpg.BackingRef.ID,
   199  		BackingNetworkType: types.OrgVdcNetworkBackingTypeDvPortgroup,
   200  
   201  		Subnets: types.OrgVdcNetworkSubnets{
   202  			Values: []types.OrgVdcNetworkSubnetValues{
   203  				{
   204  					Gateway:      "2.1.1.1",
   205  					PrefixLength: 24,
   206  					DNSServer1:   "8.8.8.8",
   207  					DNSServer2:   "8.8.4.4",
   208  					DNSSuffix:    "foo.bar",
   209  					IPRanges: types.OrgVdcNetworkSubnetIPRanges{
   210  						Values: []types.OrgVdcNetworkSubnetIPRangeValues{
   211  							{
   212  								StartAddress: "2.1.1.20",
   213  								EndAddress:   "2.1.1.30",
   214  							},
   215  							{
   216  								StartAddress: "2.1.1.40",
   217  								EndAddress:   "2.1.1.50",
   218  							},
   219  						}},
   220  				},
   221  			},
   222  		},
   223  	}
   224  
   225  	// Org VDC network backed by Distributed Virtual Port Group can only be created in VDC (not VDC Group)
   226  	runOpenApiOrgVdcNetworkTest(check, vcd, vcd.nsxtVdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeOpaque, nil)
   227  }
   228  
   229  func (vcd *TestVCD) Test_NsxvOrgVdcNetworkIsolated(check *C) {
   230  	skipOpenApiEndpointTest(vcd, check, types.OpenApiPathVersion1_0_0+types.OpenApiEndpointOrgVdcNetworks)
   231  
   232  	orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
   233  		Name:        check.TestName(),
   234  		Description: check.TestName() + "-description",
   235  
   236  		// On v35.0 orgVdc is not supported anymore. Using ownerRef instead.
   237  		OwnerRef: &types.OpenApiReference{ID: vcd.vdc.Vdc.ID},
   238  
   239  		NetworkType: types.OrgVdcNetworkTypeIsolated,
   240  		Subnets: types.OrgVdcNetworkSubnets{
   241  			Values: []types.OrgVdcNetworkSubnetValues{
   242  				{
   243  					Gateway:      "4.1.1.1",
   244  					PrefixLength: 25,
   245  					DNSServer1:   "8.8.8.8",
   246  					DNSServer2:   "8.8.4.4",
   247  					DNSSuffix:    "bar.foo",
   248  					IPRanges: types.OrgVdcNetworkSubnetIPRanges{
   249  						Values: []types.OrgVdcNetworkSubnetIPRangeValues{
   250  							{
   251  								StartAddress: "4.1.1.20",
   252  								EndAddress:   "4.1.1.30",
   253  							},
   254  							{
   255  								StartAddress: "4.1.1.40",
   256  								EndAddress:   "4.1.1.50",
   257  							},
   258  							{
   259  								StartAddress: "4.1.1.88",
   260  								EndAddress:   "4.1.1.92",
   261  							},
   262  						}},
   263  				},
   264  			},
   265  		},
   266  	}
   267  
   268  	runOpenApiOrgVdcNetworkTest(check, vcd, vcd.vdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeIsolated, nil)
   269  }
   270  
   271  func (vcd *TestVCD) Test_NsxvOrgVdcNetworkRouted(check *C) {
   272  	skipOpenApiEndpointTest(vcd, check, types.OpenApiPathVersion1_0_0+types.OpenApiEndpointOrgVdcNetworks)
   273  
   274  	nsxvEdgeGateway, err := vcd.vdc.GetEdgeGatewayByName(vcd.config.VCD.EdgeGateway, true)
   275  	check.Assert(err, IsNil)
   276  
   277  	orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
   278  		Name:        check.TestName(),
   279  		Description: check.TestName() + "-description",
   280  
   281  		// On v35.0 orgVdc is not supported anymore. Using ownerRef instead.
   282  		OwnerRef: &types.OpenApiReference{ID: vcd.vdc.Vdc.ID},
   283  
   284  		NetworkType: types.OrgVdcNetworkTypeRouted,
   285  
   286  		// Connection is used for "routed" network
   287  		Connection: &types.Connection{
   288  			RouterRef: types.OpenApiReference{
   289  				ID: nsxvEdgeGateway.EdgeGateway.ID,
   290  			},
   291  			ConnectionType: "INTERNAL",
   292  		},
   293  		Subnets: types.OrgVdcNetworkSubnets{
   294  			Values: []types.OrgVdcNetworkSubnetValues{
   295  				{
   296  					Gateway:      "2.1.1.1",
   297  					PrefixLength: 24,
   298  					DNSServer1:   "8.8.8.8",
   299  					DNSServer2:   "8.8.4.4",
   300  					DNSSuffix:    "foo.bar",
   301  					IPRanges: types.OrgVdcNetworkSubnetIPRanges{
   302  						Values: []types.OrgVdcNetworkSubnetIPRangeValues{
   303  							{
   304  								StartAddress: "2.1.1.20",
   305  								EndAddress:   "2.1.1.30",
   306  							},
   307  							{
   308  								StartAddress: "2.1.1.40",
   309  								EndAddress:   "2.1.1.50",
   310  							},
   311  							{
   312  								StartAddress: "2.1.1.60",
   313  								EndAddress:   "2.1.1.62",
   314  							}, {
   315  								StartAddress: "2.1.1.72",
   316  								EndAddress:   "2.1.1.74",
   317  							}, {
   318  								StartAddress: "2.1.1.84",
   319  								EndAddress:   "2.1.1.85",
   320  							},
   321  						}},
   322  				},
   323  			},
   324  		},
   325  	}
   326  
   327  	runOpenApiOrgVdcNetworkTest(check, vcd, vcd.vdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeRouted, nil)
   328  }
   329  
   330  func (vcd *TestVCD) Test_NsxvOrgVdcNetworkDirect(check *C) {
   331  	skipOpenApiEndpointTest(vcd, check, types.OpenApiPathVersion1_0_0+types.OpenApiEndpointOrgVdcNetworks)
   332  	if vcd.skipAdminTests {
   333  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
   334  	}
   335  
   336  	externalNetwork, err := GetExternalNetworkV2ByName(vcd.client, vcd.config.VCD.ExternalNetwork)
   337  	check.Assert(err, IsNil)
   338  	check.Assert(externalNetwork, NotNil)
   339  
   340  	orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
   341  		Name:        check.TestName(),
   342  		Description: check.TestName() + "-description",
   343  
   344  		// On v35.0 orgVdc is not supported anymore. Using ownerRef instead.
   345  		OwnerRef: &types.OpenApiReference{ID: vcd.vdc.Vdc.ID},
   346  
   347  		NetworkType:   types.OrgVdcNetworkTypeDirect,
   348  		ParentNetwork: &types.OpenApiReference{ID: externalNetwork.ExternalNetwork.ID},
   349  
   350  		Subnets: types.OrgVdcNetworkSubnets{
   351  			Values: []types.OrgVdcNetworkSubnetValues{
   352  				{
   353  					Gateway:      "2.1.1.1",
   354  					PrefixLength: 24,
   355  					DNSServer1:   "8.8.8.8",
   356  					DNSServer2:   "8.8.4.4",
   357  					DNSSuffix:    "foo.bar",
   358  					IPRanges: types.OrgVdcNetworkSubnetIPRanges{
   359  						Values: []types.OrgVdcNetworkSubnetIPRangeValues{
   360  							{
   361  								StartAddress: "2.1.1.20",
   362  								EndAddress:   "2.1.1.30",
   363  							},
   364  							{
   365  								StartAddress: "2.1.1.40",
   366  								EndAddress:   "2.1.1.50",
   367  							},
   368  							{
   369  								StartAddress: "2.1.1.60",
   370  								EndAddress:   "2.1.1.62",
   371  							}, {
   372  								StartAddress: "2.1.1.72",
   373  								EndAddress:   "2.1.1.74",
   374  							}, {
   375  								StartAddress: "2.1.1.84",
   376  								EndAddress:   "2.1.1.85",
   377  							},
   378  						}},
   379  				},
   380  			},
   381  		},
   382  	}
   383  
   384  	runOpenApiOrgVdcNetworkTest(check, vcd, vcd.vdc, orgVdcNetworkConfig, types.OrgVdcNetworkTypeDirect, nil)
   385  }
   386  
   387  func runOpenApiOrgVdcNetworkTest(check *C, vcd *TestVCD, vdc *Vdc, orgVdcNetworkConfig *types.OpenApiOrgVdcNetwork, expectNetworkType string, dhcpFunc []dhcpConfigFunc) {
   388  	orgVdcNet, err := vdc.CreateOpenApiOrgVdcNetwork(orgVdcNetworkConfig)
   389  	check.Assert(err, IsNil)
   390  
   391  	// Use generic "OpenApiEntity" resource cleanup type
   392  	openApiEndpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworks + orgVdcNet.OpenApiOrgVdcNetwork.ID
   393  	AddToCleanupListOpenApi(orgVdcNet.OpenApiOrgVdcNetwork.Name, check.TestName(), openApiEndpoint)
   394  
   395  	check.Assert(orgVdcNet.GetType(), Equals, expectNetworkType)
   396  
   397  	// Check it can be found
   398  	orgVdcNetByIdInVdc, err := vdc.GetOpenApiOrgVdcNetworkById(orgVdcNet.OpenApiOrgVdcNetwork.ID)
   399  	check.Assert(err, IsNil)
   400  	orgVdcNetByName, err := vdc.GetOpenApiOrgVdcNetworkByName(orgVdcNet.OpenApiOrgVdcNetwork.Name)
   401  	check.Assert(err, IsNil)
   402  
   403  	check.Assert(orgVdcNetByIdInVdc.OpenApiOrgVdcNetwork.ID, Equals, orgVdcNet.OpenApiOrgVdcNetwork.ID)
   404  	check.Assert(orgVdcNetByName.OpenApiOrgVdcNetwork.ID, Equals, orgVdcNet.OpenApiOrgVdcNetwork.ID)
   405  
   406  	// Retrieve all networks in VDC and expect newly created network to be there
   407  	var foundNetInVdc bool
   408  	allOrgVdcNets, err := vdc.GetAllOpenApiOrgVdcNetworks(nil)
   409  	check.Assert(err, IsNil)
   410  	for _, net := range allOrgVdcNets {
   411  		if net.OpenApiOrgVdcNetwork.ID == orgVdcNet.OpenApiOrgVdcNetwork.ID {
   412  			foundNetInVdc = true
   413  		}
   414  	}
   415  	check.Assert(foundNetInVdc, Equals, true)
   416  
   417  	// Update
   418  	orgVdcNet.OpenApiOrgVdcNetwork.Description = check.TestName() + "updated description"
   419  	updatedOrgVdcNet, err := orgVdcNet.Update(orgVdcNet.OpenApiOrgVdcNetwork)
   420  	check.Assert(err, IsNil)
   421  
   422  	check.Assert(updatedOrgVdcNet.OpenApiOrgVdcNetwork.Name, Equals, orgVdcNet.OpenApiOrgVdcNetwork.Name)
   423  	check.Assert(updatedOrgVdcNet.OpenApiOrgVdcNetwork.ID, Equals, orgVdcNet.OpenApiOrgVdcNetwork.ID)
   424  	check.Assert(updatedOrgVdcNet.OpenApiOrgVdcNetwork.Description, Equals, orgVdcNet.OpenApiOrgVdcNetwork.Description)
   425  
   426  	// Configure DHCP if specified
   427  	for i := range dhcpFunc {
   428  		dhcpFunc[i](check, vcd, vdc, updatedOrgVdcNet.OpenApiOrgVdcNetwork.ID)
   429  	}
   430  	// Delete
   431  	err = orgVdcNet.Delete()
   432  	check.Assert(err, IsNil)
   433  
   434  	// Test again if it was deleted and expect it to contain ErrorEntityNotFound
   435  	_, err = vdc.GetOpenApiOrgVdcNetworkByName(orgVdcNet.OpenApiOrgVdcNetwork.Name)
   436  	check.Assert(ContainsNotFound(err), Equals, true)
   437  
   438  	_, err = vdc.GetOpenApiOrgVdcNetworkById(orgVdcNet.OpenApiOrgVdcNetwork.ID)
   439  	check.Assert(ContainsNotFound(err), Equals, true)
   440  }
   441  
   442  type dhcpConfigFunc func(check *C, vcd *TestVCD, vdc *Vdc, orgNetId string)
   443  
   444  func nsxtRoutedDhcpConfigEdgeMode(check *C, vcd *TestVCD, vdc *Vdc, orgNetId string) {
   445  	printVerbose("## Testing DHCP in EDGE mode\n")
   446  	dhcpDefinition := &types.OpenApiOrgVdcNetworkDhcp{
   447  		Enabled: addrOf(true),
   448  		DhcpPools: []types.OpenApiOrgVdcNetworkDhcpPools{
   449  			{
   450  				Enabled: addrOf(true),
   451  				IPRange: types.OpenApiOrgVdcNetworkDhcpIpRange{
   452  					StartAddress: "2.1.1.200",
   453  					EndAddress:   "2.1.1.201",
   454  				},
   455  			},
   456  		},
   457  		DnsServers: []string{
   458  			"8.8.8.8",
   459  			"8.8.4.4",
   460  		},
   461  	}
   462  
   463  	// In API versions lower than 36.1, dnsServers list does not exist
   464  	if vdc.client.APIVCDMaxVersionIs("< 36.1") {
   465  		dhcpDefinition.DnsServers = nil
   466  	}
   467  
   468  	orgVdcNetwork, err := vcd.org.GetOpenApiOrgVdcNetworkById(orgNetId)
   469  	check.Assert(err, IsNil)
   470  	check.Assert(orgVdcNetwork, NotNil)
   471  
   472  	// Check that DHCP is not enabled
   473  	check.Assert(orgVdcNetwork.IsDhcpEnabled(), Equals, false)
   474  
   475  	updatedDhcp, err := vdc.UpdateOpenApiOrgVdcNetworkDhcp(orgNetId, dhcpDefinition)
   476  	check.Assert(err, IsNil)
   477  
   478  	// Check that DHCP is enabled
   479  	check.Assert(orgVdcNetwork.IsDhcpEnabled(), Equals, true)
   480  	check.Assert(dhcpDefinition, DeepEquals, updatedDhcp.OpenApiOrgVdcNetworkDhcp)
   481  
   482  	if orgVdcNetwork.client.APIVCDMaxVersionIs(">= 36.1") {
   483  		printVerbose("### Testing DHCP Bindings - only supported in 10.3.1+\n")
   484  		testNsxtDhcpBinding(check, vcd, orgVdcNetwork)
   485  	}
   486  
   487  	err = vdc.DeleteOpenApiOrgVdcNetworkDhcp(orgNetId)
   488  	check.Assert(err, IsNil)
   489  
   490  	updatedDhcp2, err := orgVdcNetwork.UpdateDhcp(dhcpDefinition)
   491  	check.Assert(err, IsNil)
   492  	check.Assert(updatedDhcp2, NotNil)
   493  	check.Assert(dhcpDefinition, DeepEquals, updatedDhcp2.OpenApiOrgVdcNetworkDhcp)
   494  
   495  	err = orgVdcNetwork.DeletNetworkDhcp()
   496  	check.Assert(err, IsNil)
   497  
   498  	deletedDhcp, err := orgVdcNetwork.GetOpenApiOrgVdcNetworkDhcp()
   499  	check.Assert(err, IsNil)
   500  	check.Assert(len(deletedDhcp.OpenApiOrgVdcNetworkDhcp.DhcpPools), Equals, 0)
   501  	check.Assert(len(deletedDhcp.OpenApiOrgVdcNetworkDhcp.DnsServers), Equals, 0)
   502  
   503  	// Check that DHCP is not enabled
   504  	check.Assert(orgVdcNetwork.IsDhcpEnabled(), Equals, false)
   505  }
   506  
   507  // nsxtDhcpConfigNetworkMode checks DHCP functionality in NETWORK mode.
   508  // It requires that Edge Cluster is set at VDC level therefore this function does it for the
   509  // duration of this test and restores it back
   510  func nsxtDhcpConfigNetworkMode(check *C, vcd *TestVCD, vdc *Vdc, orgNetId string) {
   511  	// Only supported in 10.3.1+
   512  	if vdc.client.APIVCDMaxVersionIs("< 36.1") {
   513  		return
   514  	}
   515  
   516  	printVerbose("## Testing DHCP in NETWORK mode\n")
   517  
   518  	// DHCP in NETWORK mode requires Edge Cluster to be set for VDC and cleaned up afterward
   519  	edgeCluster, err := vdc.GetNsxtEdgeClusterByName(vcd.config.VCD.Nsxt.NsxtEdgeCluster)
   520  	check.Assert(err, IsNil)
   521  	vdcNetworkProfile := &types.VdcNetworkProfile{
   522  		ServicesEdgeCluster: &types.VdcNetworkProfileServicesEdgeCluster{
   523  			BackingID: edgeCluster.NsxtEdgeCluster.ID,
   524  		},
   525  	}
   526  	_, err = vdc.UpdateVdcNetworkProfile(vdcNetworkProfile)
   527  	check.Assert(err, IsNil)
   528  	defer func() {
   529  		err := vdc.DeleteVdcNetworkProfile()
   530  		if err != nil {
   531  			check.Errorf("error cleaning up VDC Network Profile: %s", err)
   532  		}
   533  	}()
   534  
   535  	dhcpDefinition := &types.OpenApiOrgVdcNetworkDhcp{
   536  		Enabled:   addrOf(true),
   537  		Mode:      "NETWORK",
   538  		IPAddress: "2.1.1.252",
   539  		DhcpPools: []types.OpenApiOrgVdcNetworkDhcpPools{
   540  			{
   541  				Enabled: addrOf(true),
   542  				IPRange: types.OpenApiOrgVdcNetworkDhcpIpRange{
   543  					StartAddress: "2.1.1.200",
   544  					EndAddress:   "2.1.1.201",
   545  				},
   546  			},
   547  		},
   548  		DnsServers: []string{
   549  			"8.8.8.8",
   550  			"8.8.4.4",
   551  		},
   552  	}
   553  
   554  	orgVdcNetwork, err := vcd.org.GetOpenApiOrgVdcNetworkById(orgNetId)
   555  	check.Assert(err, IsNil)
   556  	check.Assert(orgVdcNetwork, NotNil)
   557  
   558  	// Check that DHCP is not enabled
   559  	check.Assert(orgVdcNetwork.IsDhcpEnabled(), Equals, false)
   560  
   561  	updatedDhcp, err := vdc.UpdateOpenApiOrgVdcNetworkDhcp(orgNetId, dhcpDefinition)
   562  	check.Assert(err, IsNil)
   563  
   564  	// Check that DHCP is enabled
   565  	check.Assert(orgVdcNetwork.IsDhcpEnabled(), Equals, true)
   566  	check.Assert(dhcpDefinition, DeepEquals, updatedDhcp.OpenApiOrgVdcNetworkDhcp)
   567  
   568  	if orgVdcNetwork.client.APIVCDMaxVersionIs(">= 36.1") {
   569  		printVerbose("### Testing DHCP Bindings - only supported in 10.3.1+\n")
   570  		testNsxtDhcpBinding(check, vcd, orgVdcNetwork)
   571  	}
   572  
   573  	err = vdc.DeleteOpenApiOrgVdcNetworkDhcp(orgNetId)
   574  	check.Assert(err, IsNil)
   575  
   576  	updatedDhcp2, err := orgVdcNetwork.UpdateDhcp(dhcpDefinition)
   577  	check.Assert(err, IsNil)
   578  	check.Assert(updatedDhcp2, NotNil)
   579  
   580  	check.Assert(dhcpDefinition, DeepEquals, updatedDhcp2.OpenApiOrgVdcNetworkDhcp)
   581  
   582  	err = orgVdcNetwork.DeletNetworkDhcp()
   583  	check.Assert(err, IsNil)
   584  
   585  	deletedDhcp, err := orgVdcNetwork.GetOpenApiOrgVdcNetworkDhcp()
   586  	check.Assert(err, IsNil)
   587  	check.Assert(len(deletedDhcp.OpenApiOrgVdcNetworkDhcp.DhcpPools), Equals, 0)
   588  	check.Assert(len(deletedDhcp.OpenApiOrgVdcNetworkDhcp.DnsServers), Equals, 0)
   589  
   590  	// Check that DHCP is not enabled
   591  	check.Assert(orgVdcNetwork.IsDhcpEnabled(), Equals, false)
   592  }
   593  
   594  func runOpenApiOrgVdcNetworkWithVdcGroupTest(check *C, vcd *TestVCD, orgVdcNetworkConfig *types.OpenApiOrgVdcNetwork, expectNetworkType string, dhcpFunc []dhcpConfigFunc) {
   595  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
   596  	check.Assert(err, IsNil)
   597  
   598  	nsxtExternalNetwork, err := GetExternalNetworkV2ByName(vcd.client, vcd.config.VCD.Nsxt.ExternalNetwork)
   599  	check.Assert(err, IsNil)
   600  	check.Assert(nsxtExternalNetwork, NotNil)
   601  
   602  	vdc, vdcGroup := test_CreateVdcGroup(check, adminOrg, vcd)
   603  	egwDefinition := &types.OpenAPIEdgeGateway{
   604  		Name:        "nsx-for-org-network-edge",
   605  		Description: "nsx-for-org-network-edge-description",
   606  		OwnerRef: &types.OpenApiReference{
   607  			ID: vdc.Vdc.ID,
   608  		},
   609  		EdgeGatewayUplinks: []types.EdgeGatewayUplinks{{
   610  			UplinkID: nsxtExternalNetwork.ExternalNetwork.ID,
   611  			Subnets: types.OpenAPIEdgeGatewaySubnets{Values: []types.OpenAPIEdgeGatewaySubnetValue{{
   612  				Gateway:      "10.10.10.10",
   613  				PrefixLength: 24,
   614  				Enabled:      true,
   615  			}}},
   616  			Connected: true,
   617  			Dedicated: false,
   618  		}},
   619  	}
   620  
   621  	// Create Edge Gateway in VDC Group
   622  	createdEdge, err := adminOrg.CreateNsxtEdgeGateway(egwDefinition)
   623  	check.Assert(err, IsNil)
   624  	check.Assert(createdEdge.EdgeGateway.OwnerRef.ID, Matches, `^urn:vcloud:vdc:.*`)
   625  	openApiEndpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointEdgeGateways + createdEdge.EdgeGateway.ID
   626  	PrependToCleanupListOpenApi(createdEdge.EdgeGateway.Name, check.TestName(), openApiEndpoint)
   627  
   628  	check.Assert(createdEdge.EdgeGateway.Name, Equals, egwDefinition.Name)
   629  	check.Assert(createdEdge.EdgeGateway.OwnerRef.ID, Equals, egwDefinition.OwnerRef.ID)
   630  
   631  	movedGateway, err := createdEdge.MoveToVdcOrVdcGroup(vdcGroup.VdcGroup.Id)
   632  	check.Assert(err, IsNil)
   633  	check.Assert(movedGateway.EdgeGateway.OwnerRef.ID, Equals, vdcGroup.VdcGroup.Id)
   634  	check.Assert(movedGateway.EdgeGateway.OwnerRef.ID, Matches, `^urn:vcloud:vdcGroup:.*`)
   635  
   636  	orgVdcNetworkConfig.OwnerRef.ID = vdcGroup.VdcGroup.Id
   637  	if orgVdcNetworkConfig.Connection != nil {
   638  		orgVdcNetworkConfig.Connection.RouterRef.ID = movedGateway.EdgeGateway.ID
   639  	}
   640  	orgVdcNet, err := vdcGroup.CreateOpenApiOrgVdcNetwork(orgVdcNetworkConfig)
   641  	check.Assert(err, IsNil)
   642  
   643  	// Use generic "OpenApiEntity" resource cleanup type
   644  	openApiEndpoint = types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworks + orgVdcNet.OpenApiOrgVdcNetwork.ID
   645  	AddToCleanupListOpenApi(orgVdcNet.OpenApiOrgVdcNetwork.Name, check.TestName(), openApiEndpoint)
   646  
   647  	check.Assert(orgVdcNet.GetType(), Equals, expectNetworkType)
   648  
   649  	// Check it can be found
   650  	orgVdcNetByIdInVdc, err := vdcGroup.GetOpenApiOrgVdcNetworkById(orgVdcNet.OpenApiOrgVdcNetwork.ID)
   651  	check.Assert(err, IsNil)
   652  	check.Assert(orgVdcNetByIdInVdc, NotNil)
   653  	orgVdcNetByName, err := vdcGroup.GetOpenApiOrgVdcNetworkByName(orgVdcNet.OpenApiOrgVdcNetwork.Name)
   654  	check.Assert(err, IsNil)
   655  	check.Assert(orgVdcNetByName, NotNil)
   656  
   657  	check.Assert(orgVdcNetByIdInVdc.OpenApiOrgVdcNetwork.ID, Equals, orgVdcNet.OpenApiOrgVdcNetwork.ID)
   658  	check.Assert(orgVdcNetByName.OpenApiOrgVdcNetwork.ID, Equals, orgVdcNet.OpenApiOrgVdcNetwork.ID)
   659  
   660  	// Retrieve all networks in VDC and expect newly created network to be there
   661  	var foundNetInVdc bool
   662  	allOrgVdcNets, err := vdcGroup.GetAllOpenApiOrgVdcNetworks(nil)
   663  	check.Assert(err, IsNil)
   664  	for _, net := range allOrgVdcNets {
   665  		if net.OpenApiOrgVdcNetwork.ID == orgVdcNet.OpenApiOrgVdcNetwork.ID {
   666  			foundNetInVdc = true
   667  		}
   668  	}
   669  	check.Assert(foundNetInVdc, Equals, true)
   670  
   671  	// Update
   672  	orgVdcNet.OpenApiOrgVdcNetwork.Description = check.TestName() + "updated description"
   673  	updatedOrgVdcNet, err := orgVdcNet.Update(orgVdcNet.OpenApiOrgVdcNetwork)
   674  	check.Assert(err, IsNil)
   675  
   676  	check.Assert(updatedOrgVdcNet.OpenApiOrgVdcNetwork.Name, Equals, orgVdcNet.OpenApiOrgVdcNetwork.Name)
   677  	check.Assert(updatedOrgVdcNet.OpenApiOrgVdcNetwork.ID, Equals, orgVdcNet.OpenApiOrgVdcNetwork.ID)
   678  	check.Assert(updatedOrgVdcNet.OpenApiOrgVdcNetwork.Description, Equals, orgVdcNet.OpenApiOrgVdcNetwork.Description)
   679  
   680  	// Configure DHCP if specified
   681  	for i := range dhcpFunc {
   682  		dhcpFunc[i](check, vcd, vdc, updatedOrgVdcNet.OpenApiOrgVdcNetwork.ID)
   683  	}
   684  	// Delete
   685  	err = orgVdcNet.Delete()
   686  	check.Assert(err, IsNil)
   687  
   688  	// Test again if it was deleted and expect it to contain ErrorEntityNotFound
   689  	_, err = vdcGroup.GetOpenApiOrgVdcNetworkByName(orgVdcNet.OpenApiOrgVdcNetwork.Name)
   690  	check.Assert(ContainsNotFound(err), Equals, true)
   691  
   692  	_, err = vdcGroup.GetOpenApiOrgVdcNetworkById(orgVdcNet.OpenApiOrgVdcNetwork.ID)
   693  	check.Assert(ContainsNotFound(err), Equals, true)
   694  
   695  	//cleanup
   696  	err = movedGateway.Delete()
   697  	check.Assert(err, IsNil)
   698  
   699  	// Remove VDC group and VDC
   700  	err = vdcGroup.Delete()
   701  	check.Assert(err, IsNil)
   702  	task, err := vdc.Delete(true, true)
   703  	check.Assert(err, IsNil)
   704  	err = task.WaitTaskCompletion()
   705  	check.Assert(err, IsNil)
   706  }
   707  
   708  func testNsxtDhcpBinding(check *C, vcd *TestVCD, orgNet *OpenApiOrgVdcNetwork) {
   709  	// define DHCP binding configuration
   710  	dhcpBindingConfig := &types.OpenApiOrgVdcNetworkDhcpBinding{
   711  		Name:        check.TestName() + "-dhcp-binding",
   712  		Description: "dhcp binding description",
   713  		IpAddress:   "2.1.1.231",
   714  		MacAddress:  "00:11:22:33:44:55",
   715  		BindingType: types.NsxtDhcpBindingTypeIpv4,
   716  		DhcpV4BindingConfig: &types.DhcpV4BindingConfig{
   717  			HostName:         "dhcp-binding-hostname",
   718  			GatewayIPAddress: "2.1.1.244",
   719  		},
   720  	}
   721  
   722  	// create DHCP binding
   723  	createdDhcpBinding, err := orgNet.CreateOpenApiOrgVdcNetworkDhcpBinding(dhcpBindingConfig)
   724  	check.Assert(err, IsNil)
   725  	check.Assert(createdDhcpBinding, NotNil)
   726  
   727  	// Add binding to cleanup list
   728  	openApiEndpoint := fmt.Sprintf(types.OpenApiPathVersion1_0_0+types.OpenApiEndpointOrgVdcNetworksDhcpBindings+"%s",
   729  		orgNet.OpenApiOrgVdcNetwork.ID, createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.ID)
   730  	PrependToCleanupListOpenApi(createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.Name, check.TestName(), openApiEndpoint)
   731  
   732  	// Validate DHCP binding fields
   733  	check.Assert(createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.Name, Equals, dhcpBindingConfig.Name)
   734  	check.Assert(createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.Description, Equals, dhcpBindingConfig.Description)
   735  	check.Assert(createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.IpAddress, Equals, dhcpBindingConfig.IpAddress)
   736  	check.Assert(createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.MacAddress, Equals, dhcpBindingConfig.MacAddress)
   737  	check.Assert(createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.BindingType, Equals, dhcpBindingConfig.BindingType)
   738  
   739  	// Get DHCP binding by ID
   740  	getDhcpBinding, err := orgNet.GetOpenApiOrgVdcNetworkDhcpBindingById(createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.ID)
   741  	check.Assert(err, IsNil)
   742  	check.Assert(getDhcpBinding, NotNil)
   743  	check.Assert(getDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.Name, Equals, dhcpBindingConfig.Name)
   744  
   745  	// Get DHCP binding by Name
   746  	getDhcpBindingByName, err := orgNet.GetOpenApiOrgVdcNetworkDhcpBindingByName(createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.Name)
   747  	check.Assert(err, IsNil)
   748  	check.Assert(getDhcpBindingByName, NotNil)
   749  	check.Assert(getDhcpBindingByName.OpenApiOrgVdcNetworkDhcpBinding.ID, Equals, createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.ID)
   750  
   751  	// Get all DHCP bindings
   752  	allDhcpBindings, err := orgNet.GetAllOpenApiOrgVdcNetworkDhcpBindings(nil)
   753  	check.Assert(err, IsNil)
   754  	check.Assert(allDhcpBindings, NotNil)
   755  	check.Assert(len(allDhcpBindings), Equals, 1)
   756  	check.Assert(allDhcpBindings[0].OpenApiOrgVdcNetworkDhcpBinding.ID, Equals, createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.ID)
   757  
   758  	// Update DHCP binding
   759  	dhcpBindingConfig.Description = "updated description"
   760  	dhcpBindingConfig.IpAddress = "2.1.1.232"
   761  	dhcpBindingConfig.MacAddress = "00:11:22:33:33:33"
   762  	dhcpBindingConfig.ID = createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.ID
   763  
   764  	updatedDhcpBinding, err := createdDhcpBinding.Update(dhcpBindingConfig)
   765  	check.Assert(err, IsNil)
   766  	check.Assert(updatedDhcpBinding, NotNil)
   767  	check.Assert(updatedDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.Description, Equals, dhcpBindingConfig.Description)
   768  	check.Assert(updatedDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.IpAddress, Equals, dhcpBindingConfig.IpAddress)
   769  	check.Assert(updatedDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.MacAddress, Equals, dhcpBindingConfig.MacAddress)
   770  
   771  	// Attempt to refresh originally created binding and see if it got these new updates values as well
   772  	err = createdDhcpBinding.Refresh()
   773  	check.Assert(err, IsNil)
   774  	check.Assert(createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.Description, Equals, dhcpBindingConfig.Description)
   775  	check.Assert(createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.IpAddress, Equals, dhcpBindingConfig.IpAddress)
   776  	check.Assert(createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.MacAddress, Equals, dhcpBindingConfig.MacAddress)
   777  
   778  	// Delete DHCP binding
   779  	err = createdDhcpBinding.Delete()
   780  	check.Assert(err, IsNil)
   781  
   782  	// Ensure the binding is removed
   783  	bindingShouldBeNil, err := orgNet.GetOpenApiOrgVdcNetworkDhcpBindingById(createdDhcpBinding.OpenApiOrgVdcNetworkDhcpBinding.ID)
   784  	check.Assert(err, NotNil)
   785  	check.Assert(bindingShouldBeNil, IsNil)
   786  }
   787  
   788  func runOpenApiOrgVdcNetworkTestWithSegmentProfileTemplateEndpoint(check *C, vcd *TestVCD, vdc *Vdc, orgVdcNetworkConfig *types.OpenApiOrgVdcNetwork, expectNetworkType string) {
   789  	printVerbose("## Testing Segment Profile assignment in explicit Segment Profile endpoint\n")
   790  
   791  	nsxtManager, err := vcd.client.GetNsxtManagerByName(vcd.config.VCD.Nsxt.Manager)
   792  	check.Assert(err, IsNil)
   793  	check.Assert(nsxtManager, NotNil)
   794  	nsxtManagerUrn, err := nsxtManager.Urn()
   795  	check.Assert(err, IsNil)
   796  
   797  	// Filter by NSX-T Manager
   798  	queryParams := copyOrNewUrlValues(nil)
   799  	queryParams = queryParameterFilterAnd(fmt.Sprintf("nsxTManagerRef.id==%s", nsxtManagerUrn), queryParams)
   800  
   801  	// Lookup prerequisite profiles for Segment Profile template creation
   802  	ipDiscoveryProfile, err := vcd.client.GetIpDiscoveryProfileByName(vcd.config.VCD.Nsxt.IpDiscoveryProfile, queryParams)
   803  	check.Assert(err, IsNil)
   804  	macDiscoveryProfile, err := vcd.client.GetMacDiscoveryProfileByName(vcd.config.VCD.Nsxt.MacDiscoveryProfile, queryParams)
   805  	check.Assert(err, IsNil)
   806  	spoofGuardProfile, err := vcd.client.GetSpoofGuardProfileByName(vcd.config.VCD.Nsxt.SpoofGuardProfile, queryParams)
   807  	check.Assert(err, IsNil)
   808  	qosProfile, err := vcd.client.GetQoSProfileByName(vcd.config.VCD.Nsxt.QosProfile, queryParams)
   809  	check.Assert(err, IsNil)
   810  	segmentSecurityProfile, err := vcd.client.GetSegmentSecurityProfileByName(vcd.config.VCD.Nsxt.SegmentSecurityProfile, queryParams)
   811  	check.Assert(err, IsNil)
   812  
   813  	orgVdcNet, err := vdc.CreateOpenApiOrgVdcNetwork(orgVdcNetworkConfig)
   814  	check.Assert(err, IsNil)
   815  
   816  	// Use generic "OpenApiEntity" resource cleanup type
   817  	openApiEndpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworks + orgVdcNet.OpenApiOrgVdcNetwork.ID
   818  	AddToCleanupListOpenApi(orgVdcNet.OpenApiOrgVdcNetwork.Name, check.TestName(), openApiEndpoint)
   819  
   820  	// Set segment profiles explicitly without using templates
   821  	entitySegmentProfileCfg := &types.OrgVdcNetworkSegmentProfiles{
   822  		IPDiscoveryProfile:     &types.Reference{ID: ipDiscoveryProfile.ID},
   823  		MacDiscoveryProfile:    &types.Reference{ID: macDiscoveryProfile.ID},
   824  		SpoofGuardProfile:      &types.Reference{ID: spoofGuardProfile.ID},
   825  		QosProfile:             &types.Reference{ID: qosProfile.ID},
   826  		SegmentSecurityProfile: &types.Reference{ID: segmentSecurityProfile.ID},
   827  	}
   828  
   829  	updatedSegmentProfiles, err := orgVdcNet.UpdateSegmentProfile(entitySegmentProfileCfg)
   830  	check.Assert(err, IsNil)
   831  	check.Assert(updatedSegmentProfiles, NotNil)
   832  
   833  	check.Assert(updatedSegmentProfiles.IPDiscoveryProfile.ID, Equals, ipDiscoveryProfile.ID)
   834  	check.Assert(updatedSegmentProfiles.MacDiscoveryProfile.ID, Equals, macDiscoveryProfile.ID)
   835  	check.Assert(updatedSegmentProfiles.SpoofGuardProfile.ID, Equals, spoofGuardProfile.ID)
   836  	check.Assert(updatedSegmentProfiles.QosProfile.ID, Equals, qosProfile.ID)
   837  	check.Assert(updatedSegmentProfiles.SegmentSecurityProfile.ID, Equals, segmentSecurityProfile.ID)
   838  
   839  	retrievedSegmentProfile, err := orgVdcNet.GetSegmentProfile()
   840  	check.Assert(err, IsNil)
   841  	check.Assert(retrievedSegmentProfile, NotNil)
   842  
   843  	check.Assert(retrievedSegmentProfile.IPDiscoveryProfile.ID, Equals, ipDiscoveryProfile.ID)
   844  	check.Assert(retrievedSegmentProfile.MacDiscoveryProfile.ID, Equals, macDiscoveryProfile.ID)
   845  	check.Assert(retrievedSegmentProfile.SpoofGuardProfile.ID, Equals, spoofGuardProfile.ID)
   846  	check.Assert(retrievedSegmentProfile.QosProfile.ID, Equals, qosProfile.ID)
   847  	check.Assert(retrievedSegmentProfile.SegmentSecurityProfile.ID, Equals, segmentSecurityProfile.ID)
   848  
   849  	// Delete
   850  	err = orgVdcNet.Delete()
   851  	check.Assert(err, IsNil)
   852  }
   853  
   854  func runOpenApiOrgVdcNetworkTestWithSegmentProfileTemplate(check *C, vcd *TestVCD, vdc *Vdc, orgVdcNetworkConfig *types.OpenApiOrgVdcNetwork, expectNetworkType string) {
   855  	printVerbose("## Testing Segment Profile Template assignment in Org VDC Network Structure\n")
   856  	// Precreate two segment profile templates
   857  	spt1 := preCreateSegmentProfileTemplate(vcd, check, "1")
   858  	spt2 := preCreateSegmentProfileTemplate(vcd, check, "2")
   859  	orgVdcNetworkConfig.SegmentProfileTemplate = &types.OpenApiReference{ID: spt1.NsxtSegmentProfileTemplate.ID}
   860  	defer func() { // Cleanup Segment Profile Template configuration to prevent altering other tests
   861  		orgVdcNetworkConfig.SegmentProfileTemplate = nil
   862  	}()
   863  
   864  	orgVdcNet, err := vdc.CreateOpenApiOrgVdcNetwork(orgVdcNetworkConfig)
   865  	check.Assert(err, IsNil)
   866  
   867  	// Use generic "OpenApiEntity" resource cleanup type
   868  	openApiEndpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointOrgVdcNetworks + orgVdcNet.OpenApiOrgVdcNetwork.ID
   869  	AddToCleanupListOpenApi(orgVdcNet.OpenApiOrgVdcNetwork.Name, check.TestName(), openApiEndpoint)
   870  
   871  	// Segment Profile Templates are not returned in GET by API definition which is not convenient,
   872  	// but this check will work as a fuse to detect if anything changes in that regard in future
   873  	check.Assert(orgVdcNet.OpenApiOrgVdcNetwork.SegmentProfileTemplate, IsNil)
   874  
   875  	// Retrieve Segment Profile Template using its dedicated endpoint
   876  	segmentProfileConfig, err := orgVdcNet.GetSegmentProfile()
   877  	check.Assert(err, IsNil)
   878  	check.Assert(segmentProfileConfig, NotNil)
   879  	check.Assert(segmentProfileConfig.SegmentProfileTemplate.TemplateRef.ID, Equals, spt1.NsxtSegmentProfileTemplate.ID)
   880  
   881  	// Update Segment Profile Template
   882  	orgVdcNet.OpenApiOrgVdcNetwork.SegmentProfileTemplate = &types.OpenApiReference{ID: spt2.NsxtSegmentProfileTemplate.ID}
   883  
   884  	updatedOrgVdcNet, err := orgVdcNet.Update(orgVdcNet.OpenApiOrgVdcNetwork)
   885  	check.Assert(err, IsNil)
   886  
   887  	check.Assert(updatedOrgVdcNet.OpenApiOrgVdcNetwork.Name, Equals, orgVdcNet.OpenApiOrgVdcNetwork.Name)
   888  	check.Assert(updatedOrgVdcNet.OpenApiOrgVdcNetwork.ID, Equals, orgVdcNet.OpenApiOrgVdcNetwork.ID)
   889  	check.Assert(updatedOrgVdcNet.OpenApiOrgVdcNetwork.Description, Equals, orgVdcNet.OpenApiOrgVdcNetwork.Description)
   890  
   891  	// Segment Profile Templates are not returned in GET by API definition which is not convenient,
   892  	// but this check will work as a fuse to detect if anything changes in that regard in future
   893  	check.Assert(updatedOrgVdcNet.OpenApiOrgVdcNetwork.SegmentProfileTemplate, IsNil)
   894  	// Retrieve Segment Profile Template using its dedicated endpoint
   895  	segmentProfileConfig, err = orgVdcNet.GetSegmentProfile()
   896  	check.Assert(err, IsNil)
   897  	check.Assert(segmentProfileConfig, NotNil)
   898  	check.Assert(segmentProfileConfig.SegmentProfileTemplate.TemplateRef.ID, Equals, spt2.NsxtSegmentProfileTemplate.ID)
   899  
   900  	// Delete
   901  	err = orgVdcNet.Delete()
   902  	check.Assert(err, IsNil)
   903  
   904  	// Delete Segment Profile Templates
   905  
   906  	err = spt1.Delete()
   907  	check.Assert(err, IsNil)
   908  	err = spt2.Delete()
   909  	check.Assert(err, IsNil)
   910  }
   911  
   912  func preCreateSegmentProfileTemplate(vcd *TestVCD, check *C, sptNameSuffix string) *NsxtSegmentProfileTemplate {
   913  	skipNoNsxtConfiguration(vcd, check)
   914  	vcd.skipIfNotSysAdmin(check)
   915  
   916  	nsxtManager, err := vcd.client.GetNsxtManagerByName(vcd.config.VCD.Nsxt.Manager)
   917  	check.Assert(err, IsNil)
   918  	check.Assert(nsxtManager, NotNil)
   919  	nsxtManagerUrn, err := nsxtManager.Urn()
   920  	check.Assert(err, IsNil)
   921  
   922  	// Filter by NSX-T Manager
   923  	queryParams := copyOrNewUrlValues(nil)
   924  	queryParams = queryParameterFilterAnd(fmt.Sprintf("nsxTManagerRef.id==%s", nsxtManagerUrn), queryParams)
   925  
   926  	// Lookup prerequisite profiles for Segment Profile template creation
   927  	ipDiscoveryProfile, err := vcd.client.GetIpDiscoveryProfileByName(vcd.config.VCD.Nsxt.IpDiscoveryProfile, queryParams)
   928  	check.Assert(err, IsNil)
   929  	macDiscoveryProfile, err := vcd.client.GetMacDiscoveryProfileByName(vcd.config.VCD.Nsxt.MacDiscoveryProfile, queryParams)
   930  	check.Assert(err, IsNil)
   931  	spoofGuardProfile, err := vcd.client.GetSpoofGuardProfileByName(vcd.config.VCD.Nsxt.SpoofGuardProfile, queryParams)
   932  	check.Assert(err, IsNil)
   933  	qosProfile, err := vcd.client.GetQoSProfileByName(vcd.config.VCD.Nsxt.QosProfile, queryParams)
   934  	check.Assert(err, IsNil)
   935  	segmentSecurityProfile, err := vcd.client.GetSegmentSecurityProfileByName(vcd.config.VCD.Nsxt.SegmentSecurityProfile, queryParams)
   936  	check.Assert(err, IsNil)
   937  
   938  	config := &types.NsxtSegmentProfileTemplate{
   939  		Name:                   check.TestName() + "-" + sptNameSuffix,
   940  		Description:            check.TestName() + "-description",
   941  		IPDiscoveryProfile:     &types.Reference{ID: ipDiscoveryProfile.ID},
   942  		MacDiscoveryProfile:    &types.Reference{ID: macDiscoveryProfile.ID},
   943  		QosProfile:             &types.Reference{ID: qosProfile.ID},
   944  		SegmentSecurityProfile: &types.Reference{ID: segmentSecurityProfile.ID},
   945  		SpoofGuardProfile:      &types.Reference{ID: spoofGuardProfile.ID},
   946  		SourceNsxTManagerRef:   &types.OpenApiReference{ID: nsxtManager.NsxtManager.ID},
   947  	}
   948  
   949  	createdSegmentProfileTemplate, err := vcd.client.CreateSegmentProfileTemplate(config)
   950  	check.Assert(err, IsNil)
   951  	check.Assert(createdSegmentProfileTemplate, NotNil)
   952  
   953  	// Add to cleanup list
   954  	openApiEndpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointNsxtSegmentProfileTemplates + createdSegmentProfileTemplate.NsxtSegmentProfileTemplate.ID
   955  	AddToCleanupListOpenApi(config.Name, check.TestName(), openApiEndpoint)
   956  
   957  	return createdSegmentProfileTemplate
   958  }