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

     1  //go:build network || functional || ALL
     2  
     3  /*
     4   * Copyright 2019 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	"fmt"
    11  
    12  	. "gopkg.in/check.v1"
    13  
    14  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    15  )
    16  
    17  func (vcd *TestVCD) Test_NetRefresh(check *C) {
    18  	if vcd.config.VCD.Network.Net1 == "" {
    19  		check.Skip("Skipping test because no network was given")
    20  	}
    21  
    22  	fmt.Printf("Running: %s\n", check.TestName())
    23  
    24  	network, err := vcd.vdc.GetOrgVdcNetworkByName(vcd.config.VCD.Network.Net1, false)
    25  
    26  	check.Assert(err, IsNil)
    27  	check.Assert(network.OrgVDCNetwork.Name, Equals, vcd.config.VCD.Network.Net1)
    28  	save_network := network
    29  
    30  	err = network.Refresh()
    31  
    32  	check.Assert(err, IsNil)
    33  	check.Assert(network.OrgVDCNetwork.Name, Equals, save_network.OrgVDCNetwork.Name)
    34  	check.Assert(network.OrgVDCNetwork.HREF, Equals, save_network.OrgVDCNetwork.HREF)
    35  	check.Assert(network.OrgVDCNetwork.Type, Equals, save_network.OrgVDCNetwork.Type)
    36  	check.Assert(network.OrgVDCNetwork.ID, Equals, save_network.OrgVDCNetwork.ID)
    37  	check.Assert(network.OrgVDCNetwork.Description, Equals, save_network.OrgVDCNetwork.Description)
    38  	check.Assert(network.OrgVDCNetwork.EdgeGateway, DeepEquals, save_network.OrgVDCNetwork.EdgeGateway)
    39  	check.Assert(network.OrgVDCNetwork.Status, Equals, save_network.OrgVDCNetwork.Status)
    40  
    41  }
    42  
    43  func (vcd *TestVCD) Test_CreateUpdateOrgVdcNetworkRouted(check *C) {
    44  	vcd.testCreateUpdateOrgVdcNetworkRouted(check, "10.10.101", false, false)
    45  }
    46  
    47  func (vcd *TestVCD) Test_CreateUpdateOrgVdcNetworkRoutedSubInterface(check *C) {
    48  	vcd.testCreateUpdateOrgVdcNetworkRouted(check, "10.10.102", true, false)
    49  }
    50  
    51  func (vcd *TestVCD) Test_CreateUpdateOrgVdcNetworkRoutedDistributed(check *C) {
    52  	vcd.testCreateUpdateOrgVdcNetworkRouted(check, "10.10.103", false, true)
    53  }
    54  
    55  // Tests the creation and update of an Org VDC network connected to an Edge Gateway
    56  func (vcd *TestVCD) testCreateUpdateOrgVdcNetworkRouted(check *C, ipSubnet string, subInterface, distributed bool) {
    57  	fmt.Printf("Running: %s\n", check.TestName())
    58  	networkName := TestCreateOrgVdcNetworkRouted
    59  
    60  	gateway := ipSubnet + ".1"
    61  	startAddress := ipSubnet + ".2"
    62  	endAddress := ipSubnet + ".50"
    63  
    64  	if subInterface {
    65  		networkName += "-sub"
    66  	}
    67  	err := RemoveOrgVdcNetworkIfExists(*vcd.vdc, networkName)
    68  	if err != nil {
    69  		check.Skip(fmt.Sprintf("Error deleting network : %s", err))
    70  	}
    71  
    72  	edgeGWName := vcd.config.VCD.EdgeGateway
    73  	if edgeGWName == "" {
    74  		check.Skip("Edge Gateway not provided")
    75  	}
    76  	edgeGateway, err := vcd.vdc.GetEdgeGatewayByName(edgeGWName, false)
    77  	if err != nil {
    78  		check.Skip(fmt.Sprintf("Edge Gateway %s not found", edgeGWName))
    79  	}
    80  
    81  	networkDescription := "Created by govcd tests"
    82  	var networkConfig = types.OrgVDCNetwork{
    83  		Name:        networkName,
    84  		Description: networkDescription,
    85  		Configuration: &types.NetworkConfiguration{
    86  			FenceMode: types.FenceModeNAT,
    87  			IPScopes: &types.IPScopes{
    88  				IPScope: []*types.IPScope{&types.IPScope{
    89  					IsInherited: false,
    90  					Gateway:     gateway,
    91  					Netmask:     "255.255.255.0",
    92  					IPRanges: &types.IPRanges{
    93  						IPRange: []*types.IPRange{
    94  							&types.IPRange{
    95  								StartAddress: startAddress,
    96  								EndAddress:   endAddress,
    97  							},
    98  						},
    99  					},
   100  				},
   101  				},
   102  			},
   103  			BackwardCompatibilityMode: true,
   104  		},
   105  		EdgeGateway: &types.Reference{
   106  			HREF: edgeGateway.EdgeGateway.HREF,
   107  			ID:   edgeGateway.EdgeGateway.ID,
   108  			Name: edgeGateway.EdgeGateway.Name,
   109  			Type: edgeGateway.EdgeGateway.Type,
   110  		},
   111  		IsShared: false,
   112  	}
   113  	if subInterface && distributed {
   114  		check.Skip("A network can't be at the same time distributed and subInterface")
   115  	}
   116  	if subInterface {
   117  		networkConfig.Configuration.SubInterface = &subInterface
   118  	}
   119  
   120  	if distributed {
   121  		distributedRoutingEnabled := edgeGateway.EdgeGateway.Configuration.DistributedRoutingEnabled
   122  		if distributedRoutingEnabled != nil && *distributedRoutingEnabled {
   123  			networkConfig.Configuration.DistributedInterface = &distributed
   124  		} else {
   125  			check.Skip(fmt.Sprintf("edge gateway %s doesn't have distributed routing enabled", edgeGWName))
   126  		}
   127  	}
   128  
   129  	LogNetwork(networkConfig)
   130  	err = vcd.vdc.CreateOrgVDCNetworkWait(&networkConfig)
   131  	if err != nil {
   132  		fmt.Printf("error creating Network <%s>: %s\n", networkName, err)
   133  	}
   134  	check.Assert(err, IsNil)
   135  	AddToCleanupList(networkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, "Test_CreateOrgVdcNetworkRouted")
   136  	network, err := vcd.vdc.GetOrgVdcNetworkByName(networkName, true)
   137  	check.Assert(err, IsNil)
   138  	check.Assert(network, NotNil)
   139  	check.Assert(network.OrgVDCNetwork.Name, Equals, networkName)
   140  	check.Assert(network.OrgVDCNetwork.Description, Equals, networkDescription)
   141  	check.Assert(network.OrgVDCNetwork.Configuration, NotNil)
   142  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes, NotNil)
   143  	check.Assert(len(network.OrgVDCNetwork.Configuration.IPScopes.IPScope), Not(Equals), 0)
   144  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].Gateway, Equals, gateway)
   145  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange[0].StartAddress, Equals, startAddress)
   146  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange[0].EndAddress, Equals, endAddress)
   147  
   148  	if subInterface {
   149  		check.Assert(network.OrgVDCNetwork.Configuration.SubInterface, NotNil)
   150  		check.Assert(*network.OrgVDCNetwork.Configuration.SubInterface, Equals, true)
   151  	}
   152  
   153  	// Tests FindEdgeGatewayNameByNetwork
   154  	// Note: is should work without refreshing either VDC or edge gateway
   155  	connectedGw, err := vcd.vdc.FindEdgeGatewayNameByNetwork(networkName)
   156  	check.Assert(err, IsNil)
   157  	check.Assert(connectedGw, Equals, edgeGWName)
   158  
   159  	networkId := network.OrgVDCNetwork.ID
   160  	updatedNetworkName := networkName + "-updated"
   161  	updatedNetworkDescription := "Updated by govcd tests"
   162  	updatedStartAddress := ipSubnet + ".5"
   163  	updatedEndAddress := ipSubnet + ".30"
   164  	network.OrgVDCNetwork.Name = updatedNetworkName
   165  	network.OrgVDCNetwork.Description = updatedNetworkDescription
   166  	network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange[0].StartAddress = updatedStartAddress
   167  	network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange[0].EndAddress = updatedEndAddress
   168  
   169  	err = network.Update()
   170  	check.Assert(err, IsNil)
   171  	AddToCleanupList(updatedNetworkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, "Test_CreateOrgVdcNetworkRouted")
   172  
   173  	network, err = vcd.vdc.GetOrgVdcNetworkById(networkId, true)
   174  	check.Assert(err, IsNil)
   175  	check.Assert(network.OrgVDCNetwork.Name, Equals, updatedNetworkName)
   176  	check.Assert(network.OrgVDCNetwork.Description, Equals, updatedNetworkDescription)
   177  	check.Assert(network.OrgVDCNetwork.Configuration, NotNil)
   178  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes, NotNil)
   179  	check.Assert(len(network.OrgVDCNetwork.Configuration.IPScopes.IPScope), Not(Equals), 0)
   180  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].Gateway, Equals, gateway)
   181  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange[0].StartAddress, Equals, updatedStartAddress)
   182  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange[0].EndAddress, Equals, updatedEndAddress)
   183  
   184  	task, err := network.Delete()
   185  	check.Assert(err, IsNil)
   186  	err = task.WaitTaskCompletion()
   187  	check.Assert(err, IsNil)
   188  }
   189  
   190  // Tests that we can get a network list, and a known network is retrieved from that list
   191  func (vcd *TestVCD) Test_GetNetworkList(check *C) {
   192  	fmt.Printf("Running: %s\n", check.TestName())
   193  	networkName := vcd.config.VCD.Network.Net1
   194  	if networkName == "" {
   195  		check.Skip("no network name provided")
   196  	}
   197  	networks, err := vcd.vdc.GetNetworkList()
   198  	check.Assert(err, IsNil)
   199  	found := false
   200  	for _, net := range networks {
   201  		// Check that we don't get invalid fields
   202  		knownType := net.LinkType == 0 || net.LinkType == 1 || net.LinkType == 2
   203  		check.Assert(knownType, Equals, true)
   204  		// Check that the `ConnectTo` field is not empty
   205  		check.Assert(net.ConnectedTo, Not(Equals), "")
   206  		if net.Name == networkName {
   207  			found = true
   208  		}
   209  	}
   210  	check.Assert(found, Equals, true)
   211  }
   212  
   213  // Test_GetNetworkListLarge makes sure we can query a number of networks larger than the default query page length
   214  func (vcd *TestVCD) Test_GetNetworkListLarge(check *C) {
   215  	fmt.Printf("Running: %s\n", check.TestName())
   216  
   217  	defaultPagelength := 25
   218  	externalNetwork, err := vcd.client.GetExternalNetworkByName(vcd.config.VCD.ExternalNetwork)
   219  	if err != nil {
   220  		check.Skip("[Test_GetNetworkListLarge] parent network not found")
   221  		return
   222  	}
   223  	numOfNetworks := defaultPagelength + 1
   224  	baseName := vcd.config.VCD.Org
   225  	for i := 1; i <= numOfNetworks; i++ {
   226  		networkName := fmt.Sprintf("net-%s-d-%d", baseName, i)
   227  		if testVerbose {
   228  			fmt.Printf("creating network %s\n", networkName)
   229  		}
   230  		description := fmt.Sprintf("Created by govcd test - network n. %d", i)
   231  		var networkConfig = types.OrgVDCNetwork{
   232  			Xmlns:       types.XMLNamespaceVCloud,
   233  			Name:        networkName,
   234  			Description: description,
   235  			Configuration: &types.NetworkConfiguration{
   236  				FenceMode: types.FenceModeBridged,
   237  				ParentNetwork: &types.Reference{
   238  					HREF: externalNetwork.ExternalNetwork.HREF,
   239  					Name: externalNetwork.ExternalNetwork.Name,
   240  					Type: externalNetwork.ExternalNetwork.Type,
   241  				},
   242  				BackwardCompatibilityMode: true,
   243  			},
   244  			IsShared: false,
   245  		}
   246  		task, err := vcd.vdc.CreateOrgVDCNetwork(&networkConfig)
   247  		check.Assert(err, IsNil)
   248  
   249  		AddToCleanupList(networkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, "Test_GetNetworkListLarge")
   250  		err = task.WaitTaskCompletion()
   251  		check.Assert(err, IsNil)
   252  	}
   253  
   254  	err = vcd.vdc.Refresh()
   255  	check.Assert(err, IsNil)
   256  
   257  	knownNetworkName1 := fmt.Sprintf("net-%s-d", baseName)
   258  	knownNetworkName2 := fmt.Sprintf("net-%s-d-%d", baseName, numOfNetworks)
   259  	networks, err := vcd.vdc.GetNetworkList()
   260  	check.Assert(err, IsNil)
   261  	if testVerbose {
   262  		fmt.Printf("Number of networks: %d\n", len(networks))
   263  	}
   264  	check.Assert(len(networks) > defaultPagelength, Equals, true)
   265  	found1 := false
   266  	found2 := false
   267  	for _, net := range networks {
   268  		if net.Name == knownNetworkName1 {
   269  			found1 = true
   270  		}
   271  		if net.Name == knownNetworkName2 {
   272  			found2 = true
   273  		}
   274  	}
   275  	check.Assert(found1, Equals, true)
   276  	check.Assert(found2, Equals, true)
   277  
   278  	for i := 1; i <= numOfNetworks; i++ {
   279  		networkName := fmt.Sprintf("net-%s-d-%d", baseName, i)
   280  		if testVerbose {
   281  			fmt.Printf("Removing network %s\n", networkName)
   282  		}
   283  		network, err := vcd.vdc.GetOrgVdcNetworkByName(networkName, false)
   284  		check.Assert(err, IsNil)
   285  		_, err = network.Delete()
   286  		check.Assert(err, IsNil)
   287  	}
   288  	err = vcd.vdc.Refresh()
   289  	check.Assert(err, IsNil)
   290  }
   291  
   292  // Tests the creation and update of an isolated Org VDC network
   293  func (vcd *TestVCD) Test_CreateUpdateOrgVdcNetworkIso(check *C) {
   294  	fmt.Printf("Running: %s\n", check.TestName())
   295  	networkName := TestCreateOrgVdcNetworkIso
   296  
   297  	err := RemoveOrgVdcNetworkIfExists(*vcd.vdc, networkName)
   298  	if err != nil {
   299  		check.Skip(fmt.Sprintf("Error deleting network : %s", err))
   300  	}
   301  
   302  	var (
   303  		gateway             = "192.168.2.1"
   304  		updatedNetworkName  = TestCreateOrgVdcNetworkIso + "updated"
   305  		startAddress        = "192.168.2.2"
   306  		updatedStartAddress = "192.168.2.5"
   307  		endAddress          = "192.168.2.100"
   308  		updatedEndAddress   = "192.168.2.50"
   309  		netmask             = "255.255.255.0"
   310  		dns1                = gateway
   311  		dns2                = "8.8.8.8"
   312  		dnsSuffix           = "vcloud.org"
   313  		description         = "Created by govcd tests"
   314  		updatedDescription  = "Updated by govcd tests"
   315  		networkConfig       = types.OrgVDCNetwork{
   316  			Xmlns:       types.XMLNamespaceVCloud,
   317  			Name:        networkName,
   318  			Description: description,
   319  			Configuration: &types.NetworkConfiguration{
   320  				FenceMode: types.FenceModeIsolated,
   321  				/*One of:
   322  					bridged (connected directly to the ParentNetwork),
   323  				  isolated (not connected to any other network),
   324  				  natRouted (connected to the ParentNetwork via a NAT service)
   325  				  https://code.vmware.com/apis/287/vcloud#/doc/doc/types/OrgVdcNetworkType.html
   326  				*/
   327  				IPScopes: &types.IPScopes{
   328  					IPScope: []*types.IPScope{&types.IPScope{
   329  						IsInherited: false,
   330  						Gateway:     gateway,
   331  						Netmask:     netmask,
   332  						DNS1:        dns1,
   333  						DNS2:        dns2,
   334  						DNSSuffix:   dnsSuffix,
   335  						IPRanges: &types.IPRanges{
   336  							IPRange: []*types.IPRange{
   337  								&types.IPRange{
   338  									StartAddress: startAddress,
   339  									EndAddress:   endAddress,
   340  								},
   341  							},
   342  						},
   343  					},
   344  					},
   345  				},
   346  				BackwardCompatibilityMode: true,
   347  			},
   348  			IsShared: false,
   349  		}
   350  	)
   351  
   352  	LogNetwork(networkConfig)
   353  	err = vcd.vdc.CreateOrgVDCNetworkWait(&networkConfig)
   354  	if err != nil {
   355  		fmt.Printf("error creating Network <%s>: %s\n", networkName, err)
   356  	}
   357  	check.Assert(err, IsNil)
   358  	AddToCleanupList(networkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, "Test_CreateOrgVdcNetworkIso")
   359  
   360  	network, err := vcd.vdc.GetOrgVdcNetworkByName(networkName, true)
   361  	check.Assert(err, IsNil)
   362  	check.Assert(network.OrgVDCNetwork.Description, Equals, description)
   363  	check.Assert(network.OrgVDCNetwork.Configuration.FenceMode, Equals, types.FenceModeIsolated)
   364  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes, NotNil)
   365  	check.Assert(len(network.OrgVDCNetwork.Configuration.IPScopes.IPScope), Not(Equals), 0)
   366  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].Gateway, Equals, gateway)
   367  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].Netmask, Equals, netmask)
   368  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].DNS1, Equals, dns1)
   369  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].DNS2, Equals, dns2)
   370  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].DNSSuffix, Equals, dnsSuffix)
   371  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges, NotNil)
   372  	check.Assert(len(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange), Not(Equals), 0)
   373  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange[0].StartAddress, Equals, startAddress)
   374  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange[0].EndAddress, Equals, endAddress)
   375  
   376  	networkId := network.OrgVDCNetwork.ID
   377  	network.OrgVDCNetwork.Description = updatedDescription
   378  	network.OrgVDCNetwork.Name = updatedNetworkName
   379  	network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange[0].StartAddress = updatedStartAddress
   380  	network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange[0].EndAddress = updatedEndAddress
   381  	err = network.Update()
   382  	check.Assert(err, IsNil)
   383  	AddToCleanupList(updatedNetworkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, "Test_CreateOrgVdcNetworkIso")
   384  
   385  	network, err = vcd.vdc.GetOrgVdcNetworkById(networkId, true)
   386  	check.Assert(err, IsNil)
   387  	check.Assert(network.OrgVDCNetwork.Name, Equals, updatedNetworkName)
   388  	check.Assert(network.OrgVDCNetwork.Description, Equals, updatedDescription)
   389  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes, NotNil)
   390  	check.Assert(len(network.OrgVDCNetwork.Configuration.IPScopes.IPScope), Not(Equals), 0)
   391  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].Netmask, Equals, netmask)
   392  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].DNS2, Equals, dns2)
   393  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].DNSSuffix, Equals, dnsSuffix)
   394  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges, NotNil)
   395  	check.Assert(len(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange), Not(Equals), 0)
   396  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange[0].StartAddress, Equals, updatedStartAddress)
   397  	check.Assert(network.OrgVDCNetwork.Configuration.IPScopes.IPScope[0].IPRanges.IPRange[0].EndAddress, Equals, updatedEndAddress)
   398  	task, err := network.Delete()
   399  	check.Assert(err, IsNil)
   400  	err = task.WaitTaskCompletion()
   401  	check.Assert(err, IsNil)
   402  
   403  }
   404  
   405  // Tests the creation and update of a Org VDC network connected to an external network
   406  func (vcd *TestVCD) Test_CreateUpdateOrgVdcNetworkDirect(check *C) {
   407  	fmt.Printf("Running: %s\n", check.TestName())
   408  	networkName := TestCreateOrgVdcNetworkDirect
   409  
   410  	if vcd.skipAdminTests {
   411  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
   412  	}
   413  	err := RemoveOrgVdcNetworkIfExists(*vcd.vdc, networkName)
   414  	if err != nil {
   415  		check.Skip(fmt.Sprintf("Error deleting network : %s", err))
   416  	}
   417  
   418  	if vcd.config.VCD.ExternalNetwork == "" {
   419  		check.Skip("[Test_CreateOrgVdcNetworkDirect] external network not provided")
   420  	}
   421  	externalNetwork, err := vcd.client.GetExternalNetworkByName(vcd.config.VCD.ExternalNetwork)
   422  	if err != nil {
   423  		check.Skip("[Test_CreateOrgVdcNetworkDirect] parent network not found")
   424  		return
   425  	}
   426  	// Note that there is no IPScope for this type of network
   427  	description := "Created by govcd test"
   428  	var networkConfig = types.OrgVDCNetwork{
   429  		Xmlns:       types.XMLNamespaceVCloud,
   430  		Name:        networkName,
   431  		Description: description,
   432  		Configuration: &types.NetworkConfiguration{
   433  			FenceMode: types.FenceModeBridged,
   434  			ParentNetwork: &types.Reference{
   435  				HREF: externalNetwork.ExternalNetwork.HREF,
   436  				Name: externalNetwork.ExternalNetwork.Name,
   437  				Type: externalNetwork.ExternalNetwork.Type,
   438  			},
   439  			BackwardCompatibilityMode: true,
   440  		},
   441  		IsShared: false,
   442  	}
   443  	LogNetwork(networkConfig)
   444  
   445  	task, err := vcd.vdc.CreateOrgVDCNetwork(&networkConfig)
   446  	if err != nil {
   447  		fmt.Printf("error creating the network: %s", err)
   448  	}
   449  	check.Assert(err, IsNil)
   450  	if task == (Task{}) {
   451  		fmt.Printf("NULL task retrieved after network creation")
   452  	}
   453  	check.Assert(task.Task.HREF, Not(Equals), "")
   454  
   455  	AddToCleanupList(networkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, check.TestName())
   456  
   457  	// err = task.WaitTaskCompletion()
   458  	err = task.WaitInspectTaskCompletion(LogTask, 10)
   459  	if err != nil {
   460  		fmt.Printf("error performing task: %s", err)
   461  	}
   462  	check.Assert(err, IsNil)
   463  
   464  	// Retrieving the network
   465  	newNetwork, err := vcd.vdc.GetOrgVdcNetworkByName(networkName, true)
   466  	check.Assert(err, IsNil)
   467  	check.Assert(newNetwork, NotNil)
   468  	check.Assert(newNetwork.OrgVDCNetwork.Name, Equals, networkName)
   469  	check.Assert(newNetwork.OrgVDCNetwork.Description, Equals, description)
   470  	check.Assert(newNetwork.OrgVDCNetwork.Configuration, NotNil)
   471  	check.Assert(newNetwork.OrgVDCNetwork.Configuration.ParentNetwork, NotNil)
   472  	check.Assert(newNetwork.OrgVDCNetwork.Configuration.ParentNetwork.ID, Equals, externalNetwork.ExternalNetwork.ID)
   473  	check.Assert(newNetwork.OrgVDCNetwork.Configuration.ParentNetwork.Name, Equals, externalNetwork.ExternalNetwork.Name)
   474  
   475  	// Updating
   476  	updatedNetworkName := networkName + "-updated"
   477  	updatedDescription := "Updated by govcd tests"
   478  	newNetwork.OrgVDCNetwork.Description = updatedDescription
   479  	newNetwork.OrgVDCNetwork.Name = updatedNetworkName
   480  	err = newNetwork.Update()
   481  	check.Assert(err, IsNil)
   482  
   483  	AddToCleanupList(updatedNetworkName, "network", vcd.org.Org.Name+"|"+vcd.vdc.Vdc.Name, "Test_CreateOrgVdcNetworkDirect")
   484  
   485  	// Check the values of the updated entities. The new values should be available
   486  	// immediately.
   487  	check.Assert(newNetwork.OrgVDCNetwork.Name, Equals, updatedNetworkName)
   488  	check.Assert(newNetwork.OrgVDCNetwork.Description, Equals, updatedDescription)
   489  
   490  	// Gets a new copy of the network and check the values.
   491  	newNetwork, err = vcd.vdc.GetOrgVdcNetworkByName(updatedNetworkName, true)
   492  	check.Assert(err, IsNil)
   493  	check.Assert(newNetwork, NotNil)
   494  	check.Assert(newNetwork.OrgVDCNetwork.Name, Equals, updatedNetworkName)
   495  	check.Assert(newNetwork.OrgVDCNetwork.Description, Equals, updatedDescription)
   496  
   497  	// restoring original name
   498  	newNetwork.OrgVDCNetwork.Name = networkName
   499  	err = newNetwork.Update()
   500  	check.Assert(err, IsNil)
   501  	check.Assert(newNetwork.OrgVDCNetwork.Name, Equals, networkName)
   502  
   503  	// Testing RemoveOrgVdcNetworkIfExists:
   504  	// (1) Make sure the network exists
   505  	newNetwork, err = vcd.vdc.GetOrgVdcNetworkByName(networkName, true)
   506  	check.Assert(err, IsNil)
   507  	check.Assert(newNetwork, NotNil)
   508  
   509  	// (2) Removing the network. It should return nil, as a successful deletion
   510  	err = RemoveOrgVdcNetworkIfExists(*vcd.vdc, networkName)
   511  	check.Assert(err, IsNil)
   512  
   513  	// (3) Look for the network again. It should be deleted
   514  	_, err = vcd.vdc.GetOrgVdcNetworkByName(networkName, true)
   515  	check.Assert(err, NotNil)
   516  	check.Assert(IsNotFound(err), Equals, true)
   517  
   518  	// (4) Attempting a second conditional deletion. It should also return nil, as the network was not found
   519  	err = RemoveOrgVdcNetworkIfExists(*vcd.vdc, networkName)
   520  	check.Assert(err, IsNil)
   521  }
   522  
   523  func (vcd *TestVCD) Test_NetworkUpdateRename(check *C) {
   524  	if vcd.config.VCD.Network.Net1 == "" {
   525  		check.Skip("Skipping test because no network was given")
   526  	}
   527  
   528  	fmt.Printf("Running: %s\n", check.TestName())
   529  
   530  	network, err := vcd.vdc.GetOrgVdcNetworkByName(vcd.config.VCD.Network.Net1, false)
   531  
   532  	check.Assert(err, IsNil)
   533  	check.Assert(network.OrgVDCNetwork.Name, Equals, vcd.config.VCD.Network.Net1)
   534  	var saveNetwork = types.OrgVDCNetwork{
   535  		HREF:        network.OrgVDCNetwork.HREF,
   536  		Type:        network.OrgVDCNetwork.Type,
   537  		ID:          network.OrgVDCNetwork.ID,
   538  		Name:        network.OrgVDCNetwork.Name,
   539  		Status:      network.OrgVDCNetwork.Status,
   540  		Description: network.OrgVDCNetwork.Description,
   541  	}
   542  
   543  	// Change name and description
   544  	updatedNetworkName := "UpdatedNetwork"
   545  	updatedDescription := "Updated description"
   546  	network.OrgVDCNetwork.Description = updatedDescription
   547  	network.OrgVDCNetwork.Name = updatedNetworkName
   548  	err = network.Update()
   549  	check.Assert(err, IsNil)
   550  
   551  	// Retrieve the network again
   552  	network, err = vcd.vdc.GetOrgVdcNetworkById(saveNetwork.ID, false)
   553  	check.Assert(err, IsNil)
   554  
   555  	check.Assert(network.OrgVDCNetwork.Name, Equals, updatedNetworkName)
   556  	check.Assert(network.OrgVDCNetwork.Description, Equals, updatedDescription)
   557  	check.Assert(network.OrgVDCNetwork.HREF, Equals, saveNetwork.HREF)
   558  	check.Assert(network.OrgVDCNetwork.Type, Equals, saveNetwork.Type)
   559  	check.Assert(network.OrgVDCNetwork.ID, Equals, saveNetwork.ID)
   560  	check.Assert(network.OrgVDCNetwork.EdgeGateway, DeepEquals, saveNetwork.EdgeGateway)
   561  	check.Assert(network.OrgVDCNetwork.Status, Equals, saveNetwork.Status)
   562  
   563  	// Change back name and description to their original values
   564  	network.OrgVDCNetwork.Description = saveNetwork.Description
   565  	network.OrgVDCNetwork.Name = saveNetwork.Name
   566  
   567  	err = network.Update()
   568  	check.Assert(err, IsNil)
   569  	network, err = vcd.vdc.GetOrgVdcNetworkById(saveNetwork.ID, false)
   570  	check.Assert(err, IsNil)
   571  	check.Assert(network.OrgVDCNetwork.Description, Equals, saveNetwork.Description)
   572  	check.Assert(network.OrgVDCNetwork.Name, Equals, saveNetwork.Name)
   573  
   574  	// An update without any changes should run without producing side effects
   575  	err = network.Update()
   576  	check.Assert(err, IsNil)
   577  	network, err = vcd.vdc.GetOrgVdcNetworkById(saveNetwork.ID, false)
   578  	check.Assert(err, IsNil)
   579  	check.Assert(network.OrgVDCNetwork.Description, Equals, saveNetwork.Description)
   580  	check.Assert(network.OrgVDCNetwork.Name, Equals, saveNetwork.Name)
   581  
   582  	// We run some of the above operations using Rename instead of Update
   583  	err = network.Rename(updatedNetworkName)
   584  	check.Assert(err, IsNil)
   585  	network, err = vcd.vdc.GetOrgVdcNetworkById(saveNetwork.ID, false)
   586  	check.Assert(err, IsNil)
   587  	check.Assert(network.OrgVDCNetwork.Name, Equals, updatedNetworkName)
   588  
   589  	// Trying to rename with the same name should give an error
   590  	err = network.Rename(updatedNetworkName)
   591  	check.Assert(err, NotNil)
   592  
   593  	// Setting the original name again
   594  	err = network.Rename(saveNetwork.Name)
   595  	check.Assert(err, IsNil)
   596  	network, err = vcd.vdc.GetOrgVdcNetworkById(saveNetwork.ID, false)
   597  	check.Assert(err, IsNil)
   598  	check.Assert(network.OrgVDCNetwork.Name, Equals, saveNetwork.Name)
   599  
   600  }