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

     1  //go:build org || 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  	"math"
    12  
    13  	. "gopkg.in/check.v1"
    14  
    15  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    16  )
    17  
    18  // Tests Refresh for Org by updating the org and then asserting if the
    19  // variable is updated.
    20  func (vcd *TestVCD) Test_RefreshOrg(check *C) {
    21  
    22  	if vcd.skipAdminTests {
    23  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
    24  	}
    25  	adminOrg, err := vcd.client.GetAdminOrgByName(TestRefreshOrg)
    26  	if adminOrg != nil {
    27  		check.Assert(err, IsNil)
    28  		err := adminOrg.Delete(true, true)
    29  		check.Assert(err, IsNil)
    30  	}
    31  	task, err := CreateOrg(vcd.client, TestRefreshOrg, TestRefreshOrg, TestRefreshOrg, &types.OrgSettings{
    32  		OrgLdapSettings: &types.OrgLdapSettingsType{OrgLdapMode: "NONE"},
    33  	}, true)
    34  	check.Assert(err, IsNil)
    35  	// After a successful creation, the entity is added to the cleanup list.
    36  	// If something fails after this point, the entity will be removed
    37  	AddToCleanupList(TestRefreshOrg, "org", "", "Test_RefreshOrg")
    38  
    39  	err = task.WaitTaskCompletion()
    40  	check.Assert(err, IsNil)
    41  
    42  	// fetch newly created org
    43  	org, err := vcd.client.GetOrgByName(TestRefreshOrg)
    44  	check.Assert(err, IsNil)
    45  	check.Assert(org.Org.Name, Equals, TestRefreshOrg)
    46  	// fetch admin version of org for updating
    47  	adminOrg, err = vcd.client.GetAdminOrgByName(TestRefreshOrg)
    48  	check.Assert(err, IsNil)
    49  	check.Assert(adminOrg.AdminOrg.Name, Equals, TestRefreshOrg)
    50  	adminOrg.AdminOrg.FullName = TestRefreshOrgFullName
    51  	task, err = adminOrg.Update()
    52  	check.Assert(err, IsNil)
    53  	// Wait until update is complete
    54  	err = task.WaitTaskCompletion()
    55  	check.Assert(err, IsNil)
    56  	// Test Refresh on normal org
    57  	err = org.Refresh()
    58  	check.Assert(err, IsNil)
    59  	check.Assert(org.Org.FullName, Equals, TestRefreshOrgFullName)
    60  	// Test Refresh on admin org
    61  	err = adminOrg.Refresh()
    62  	check.Assert(err, IsNil)
    63  	check.Assert(adminOrg.AdminOrg.FullName, Equals, TestRefreshOrgFullName)
    64  	// Delete, with force and recursive true
    65  	err = adminOrg.Delete(true, true)
    66  	check.Assert(err, IsNil)
    67  }
    68  
    69  // Creates an org DELETEORG and then deletes it to test functionality of
    70  // delete org. Fails if org still exists
    71  func (vcd *TestVCD) Test_DeleteOrg(check *C) {
    72  	if vcd.skipAdminTests {
    73  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
    74  	}
    75  	org, _ := vcd.client.GetAdminOrgByName(TestDeleteOrg)
    76  	if org != nil {
    77  		err := org.Delete(true, true)
    78  		check.Assert(err, IsNil)
    79  	}
    80  	task, err := CreateOrg(vcd.client, TestDeleteOrg, TestDeleteOrg, TestDeleteOrg, &types.OrgSettings{}, true)
    81  	check.Assert(err, IsNil)
    82  	// After a successful creation, the entity is added to the cleanup list.
    83  	// If something fails after this point, the entity will be removed
    84  	AddToCleanupList(TestDeleteOrg, "org", "", "Test_DeleteOrg")
    85  	// fetch newly created org
    86  	err = task.WaitTaskCompletion()
    87  	check.Assert(err, IsNil)
    88  
    89  	org, err = vcd.client.GetAdminOrgByName(TestDeleteOrg)
    90  	check.Assert(err, IsNil)
    91  	check.Assert(org.AdminOrg.Name, Equals, TestDeleteOrg)
    92  	// Delete, with force and recursive true
    93  	err = org.Delete(true, true)
    94  	check.Assert(err, IsNil)
    95  	doesOrgExist(check, vcd)
    96  }
    97  
    98  // Creates a org UPDATEORG, changes the deployed vm quota on the org,
    99  // and tests the update functionality of the org. Then it deletes the org.
   100  // Fails if the deployedvmquota variable is not changed when the org is
   101  // refetched.
   102  func (vcd *TestVCD) Test_UpdateOrg(check *C) {
   103  	if vcd.skipAdminTests {
   104  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
   105  	}
   106  	type updateSet struct {
   107  		orgName              string
   108  		enabled              bool
   109  		canPublishCatalogs   bool
   110  		canPublishExternally bool
   111  		canSubscribe         bool
   112  	}
   113  
   114  	// Tests a combination of enabled and canPublishCatalogs to see
   115  	// whether they are updated correctly
   116  	var updateOrgs = []updateSet{
   117  		{TestUpdateOrg + "1", true, false, false, false},
   118  		{TestUpdateOrg + "2", false, false, false, false},
   119  		{TestUpdateOrg + "3", true, true, true, false},
   120  		{TestUpdateOrg + "4", false, true, false, true},
   121  	}
   122  
   123  	for _, uo := range updateOrgs {
   124  		if vcd.client.Client.APIVCDMaxVersionIs("= 37.2") && !uo.enabled {
   125  			// TODO revisit once bug is fixed in VCD
   126  			fmt.Println("[INFO] VCD 10.4.2 has a bug that prevents creating a disabled Org - Changing 'enabled' parameter to 'true'")
   127  			uo.enabled = true
   128  		}
   129  		fmt.Printf("Org %s - enabled %v - catalogs %v\n", uo.orgName, uo.enabled, uo.canPublishCatalogs)
   130  		task, err := CreateOrg(vcd.client, uo.orgName, uo.orgName, uo.orgName, &types.OrgSettings{
   131  			OrgGeneralSettings: &types.OrgGeneralSettings{
   132  				CanPublishCatalogs:   uo.canPublishCatalogs,
   133  				CanPublishExternally: uo.canPublishExternally,
   134  				CanSubscribe:         uo.canSubscribe,
   135  			},
   136  			OrgLdapSettings: &types.OrgLdapSettingsType{OrgLdapMode: "NONE"},
   137  		}, uo.enabled)
   138  
   139  		check.Assert(err, IsNil)
   140  		check.Assert(task, Not(Equals), Task{})
   141  
   142  		err = task.WaitTaskCompletion()
   143  		check.Assert(err, IsNil)
   144  
   145  		AddToCleanupList(uo.orgName, "org", "", "TestUpdateOrg")
   146  
   147  		// fetch newly created org
   148  		adminOrg, err := vcd.client.GetAdminOrgByName(uo.orgName)
   149  		check.Assert(err, IsNil)
   150  		check.Assert(adminOrg, NotNil)
   151  
   152  		check.Assert(adminOrg.AdminOrg.Name, Equals, uo.orgName)
   153  		check.Assert(adminOrg.AdminOrg.Description, Equals, uo.orgName)
   154  		updatedDescription := "description_changed"
   155  		updatedFullName := "full_name_changed"
   156  		adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.DeployedVMQuota = 100
   157  		adminOrg.AdminOrg.Description = updatedDescription
   158  		adminOrg.AdminOrg.FullName = updatedFullName
   159  		adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanPublishCatalogs = !uo.canPublishCatalogs
   160  		adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanPublishExternally = !uo.canPublishExternally
   161  		adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanSubscribe = !uo.canSubscribe
   162  
   163  		adminOrg.AdminOrg.IsEnabled = !uo.enabled
   164  
   165  		task, err = adminOrg.Update()
   166  		check.Assert(err, IsNil)
   167  		check.Assert(task, Not(Equals), Task{})
   168  		// Wait until update is complete
   169  		err = task.WaitTaskCompletion()
   170  		check.Assert(err, IsNil)
   171  
   172  		// Get the Org again
   173  		updatedAdminOrg, err := vcd.client.GetAdminOrgByName(uo.orgName)
   174  		check.Assert(err, IsNil)
   175  		check.Assert(updatedAdminOrg, NotNil)
   176  
   177  		check.Assert(updatedAdminOrg.AdminOrg.IsEnabled, Equals, !uo.enabled)
   178  		check.Assert(updatedAdminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanPublishCatalogs, Equals, !uo.canPublishCatalogs)
   179  		check.Assert(updatedAdminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanPublishExternally, Equals, !uo.canPublishExternally)
   180  		check.Assert(updatedAdminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanSubscribe, Equals, !uo.canSubscribe)
   181  		if testVerbose {
   182  			fmt.Printf("[updated] Org %s - enabled %v (expected %v) - catalogs %v (expected %v)\n",
   183  				updatedAdminOrg.AdminOrg.Name,
   184  				updatedAdminOrg.AdminOrg.IsEnabled, !uo.enabled,
   185  				adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanPublishCatalogs, !uo.canPublishCatalogs)
   186  		}
   187  		check.Assert(err, IsNil)
   188  		check.Assert(updatedAdminOrg.AdminOrg.Description, Equals, updatedDescription)
   189  		check.Assert(updatedAdminOrg.AdminOrg.FullName, Equals, updatedFullName)
   190  		check.Assert(updatedAdminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.DeployedVMQuota, Equals, 100)
   191  		// Delete, with force and recursive true
   192  		err = updatedAdminOrg.Delete(true, true)
   193  		check.Assert(err, IsNil)
   194  		doesOrgExist(check, vcd)
   195  	}
   196  }
   197  
   198  // Tests org function GetVDCByName with the vdc specified
   199  // in the config file. Then tests with a vdc that doesn't exist.
   200  // Fails if the config file name doesn't match with the found VDC, or
   201  // if the invalid vdc is found by the function.  Also tests a VDC
   202  // that doesn't exist. Asserts an error if the function finds it or
   203  // if the error is not nil.
   204  func (vcd *TestVCD) Test_GetVdcByName(check *C) {
   205  	vdc, err := vcd.org.GetVDCByName(vcd.config.VCD.Vdc, false)
   206  	check.Assert(err, IsNil)
   207  	check.Assert(vdc, NotNil)
   208  	check.Assert(vdc.Vdc.Name, Equals, vcd.config.VCD.Vdc)
   209  	// Try a vdc that doesn't exist
   210  	vdc, err = vcd.org.GetVDCByName(INVALID_NAME, false)
   211  	check.Assert(err, NotNil)
   212  	check.Assert(vdc, IsNil)
   213  }
   214  
   215  // Tests org function Admin version of GetVDCByName with the vdc
   216  // specified in the config file. Fails if the names don't match
   217  // or the function returns an error.  Also tests a vdc
   218  // that doesn't exist. Asserts an error if the function finds it or
   219  // if the error is not nil.
   220  func (vcd *TestVCD) Test_Admin_GetVdcByName(check *C) {
   221  	if vcd.skipAdminTests {
   222  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
   223  	}
   224  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
   225  	check.Assert(err, IsNil)
   226  	check.Assert(adminOrg, NotNil)
   227  	vdc, err := adminOrg.GetVDCByName(vcd.config.VCD.Vdc, false)
   228  	check.Assert(err, IsNil)
   229  	check.Assert(vdc, NotNil)
   230  	check.Assert(vdc.Vdc.Name, Equals, vcd.config.VCD.Vdc)
   231  	// Try a vdc that doesn't exist
   232  	vdc, err = adminOrg.GetVDCByName(INVALID_NAME, false)
   233  	check.Assert(vdc, IsNil)
   234  	check.Assert(err, NotNil)
   235  }
   236  
   237  // Tests org function GetVDCByName with the vdc specified
   238  // in the config file. Then tests with a vdc that doesn't exist.
   239  // Fails if the config file name doesn't match with the found VDC, or
   240  // if the invalid vdc is found by the function.  Also tests a VDC
   241  // that doesn't exist. Asserts an error if the function finds it or
   242  // if the error is not nil.
   243  func (vcd *TestVCD) Test_CreateVdc(check *C) {
   244  	if vcd.skipAdminTests {
   245  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
   246  	}
   247  
   248  	if vcd.config.VCD.ProviderVdc.Name == "" {
   249  		check.Skip("No Provider VDC name given for VDC tests")
   250  	}
   251  	if vcd.config.VCD.ProviderVdc.StorageProfile == "" {
   252  		check.Skip("No Storage Profile given for VDC tests")
   253  	}
   254  	if vcd.config.VCD.ProviderVdc.NetworkPool == "" {
   255  		check.Skip("No Network Pool given for VDC tests")
   256  	}
   257  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
   258  	check.Assert(err, IsNil)
   259  	check.Assert(adminOrg, NotNil)
   260  
   261  	results, err := vcd.client.QueryWithNotEncodedParams(nil, map[string]string{
   262  		"type":   "providerVdc",
   263  		"filter": fmt.Sprintf("name==%s", vcd.config.VCD.ProviderVdc.Name),
   264  	})
   265  	check.Assert(err, IsNil)
   266  	if len(results.Results.VMWProviderVdcRecord) == 0 {
   267  		check.Skip(fmt.Sprintf("No Provider VDC found with name '%s'", vcd.config.VCD.ProviderVdc.Name))
   268  	}
   269  	providerVdcHref := results.Results.VMWProviderVdcRecord[0].HREF
   270  
   271  	storageProfile, err := vcd.client.QueryProviderVdcStorageProfileByName(vcd.config.VCD.ProviderVdc.StorageProfile, providerVdcHref)
   272  	check.Assert(err, IsNil)
   273  
   274  	results, err = vcd.client.QueryWithNotEncodedParams(nil, map[string]string{
   275  		"type":   "networkPool",
   276  		"filter": fmt.Sprintf("name==%s", vcd.config.VCD.ProviderVdc.NetworkPool),
   277  	})
   278  	check.Assert(err, IsNil)
   279  	if len(results.Results.NetworkPoolRecord) == 0 {
   280  		check.Skip(fmt.Sprintf("No network pool found with name '%s'", vcd.config.VCD.ProviderVdc.NetworkPool))
   281  	}
   282  	networkPoolHref := results.Results.NetworkPoolRecord[0].HREF
   283  
   284  	allocationModels := []string{"AllocationVApp", "AllocationPool", "ReservationPool"}
   285  	for i, allocationModel := range allocationModels {
   286  		vdcConfiguration := &types.VdcConfiguration{
   287  			Name:            fmt.Sprintf("%s%d", TestCreateOrgVdc, i),
   288  			AllocationModel: allocationModel,
   289  			ComputeCapacity: []*types.ComputeCapacity{
   290  				&types.ComputeCapacity{
   291  					CPU: &types.CapacityWithUsage{
   292  						Units:     "MHz",
   293  						Allocated: 1024,
   294  						Limit:     1024,
   295  					},
   296  					Memory: &types.CapacityWithUsage{
   297  						Allocated: 1024,
   298  						Limit:     1024,
   299  					},
   300  				},
   301  			},
   302  			VdcStorageProfile: []*types.VdcStorageProfileConfiguration{&types.VdcStorageProfileConfiguration{
   303  				Enabled: addrOf(true),
   304  				Units:   "MB",
   305  				Limit:   1024,
   306  				Default: true,
   307  				ProviderVdcStorageProfile: &types.Reference{
   308  					HREF: storageProfile.HREF,
   309  				},
   310  			},
   311  			},
   312  			NetworkPoolReference: &types.Reference{
   313  				HREF: networkPoolHref,
   314  			},
   315  			ProviderVdcReference: &types.Reference{
   316  				HREF: providerVdcHref,
   317  			},
   318  			IsEnabled:            true,
   319  			IsThinProvision:      true,
   320  			UsesFastProvisioning: true,
   321  		}
   322  
   323  		vdc, _ := adminOrg.GetVDCByName(vdcConfiguration.Name, false)
   324  		if vdc != nil {
   325  			err = vdc.DeleteWait(true, true)
   326  			check.Assert(err, IsNil)
   327  		}
   328  
   329  		task, err := adminOrg.CreateVdc(vdcConfiguration)
   330  		check.Assert(err, NotNil)
   331  		check.Assert(task, Equals, Task{})
   332  		check.Assert(err.Error(), Equals, "VdcConfiguration missing required field: ComputeCapacity[0].Memory.Units")
   333  		vdcConfiguration.ComputeCapacity[0].Memory.Units = "MB"
   334  
   335  		err = adminOrg.CreateVdcWait(vdcConfiguration)
   336  		check.Assert(err, IsNil)
   337  
   338  		AddToCleanupList(vdcConfiguration.Name, "vdc", vcd.org.Org.Name, "Test_CreateVdc")
   339  
   340  		vdc, err = adminOrg.GetVDCByName(vdcConfiguration.Name, true)
   341  		check.Assert(err, IsNil)
   342  		check.Assert(vdc, NotNil)
   343  		check.Assert(vdc.Vdc.Name, Equals, vdcConfiguration.Name)
   344  		check.Assert(vdc.Vdc.IsEnabled, Equals, vdcConfiguration.IsEnabled)
   345  		check.Assert(vdc.Vdc.AllocationModel, Equals, vdcConfiguration.AllocationModel)
   346  
   347  		err = vdc.DeleteWait(true, true)
   348  		check.Assert(err, IsNil)
   349  
   350  		vdc, err = adminOrg.GetVDCByName(vdcConfiguration.Name, true)
   351  		check.Assert(err, NotNil)
   352  		check.Assert(vdc, IsNil)
   353  	}
   354  }
   355  
   356  // Tests FindCatalog with Catalog in config file. Fails if the name and
   357  // description don't match the catalog elements in the config file or if
   358  // function returns an error.  Also tests an catalog
   359  // that doesn't exist. Asserts an error if the function finds it or
   360  // if the error is not nil.
   361  func (vcd *TestVCD) Test_FindCatalog(check *C) {
   362  	// Find Catalog
   363  	cat, err := vcd.org.GetCatalogByName(vcd.config.VCD.Catalog.Name, false)
   364  	check.Assert(err, IsNil)
   365  	check.Assert(cat, NotNil)
   366  	check.Assert(cat.Catalog.Name, Equals, vcd.config.VCD.Catalog.Name)
   367  	// checks if user gave a catalog description in config file
   368  	if vcd.config.VCD.Catalog.Description != "" {
   369  		check.Assert(cat.Catalog.Description, Equals, vcd.config.VCD.Catalog.Description)
   370  	}
   371  	// Check Invalid Catalog
   372  	cat, err = vcd.org.GetCatalogByName(INVALID_NAME, false)
   373  	check.Assert(err, NotNil)
   374  	check.Assert(cat, IsNil)
   375  }
   376  
   377  // Tests Admin version of FindCatalog with Catalog in config file. Asserts an
   378  // error if the name and description don't match the catalog elements in
   379  // the config file or if function returns an error.  Also tests an catalog
   380  // that doesn't exist. Asserts an error if the function finds it or
   381  // if the error is not nil.
   382  func (vcd *TestVCD) Test_Admin_FindCatalog(check *C) {
   383  	if vcd.skipAdminTests {
   384  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
   385  	}
   386  	// Fetch admin org version of current test org
   387  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
   388  	check.Assert(err, IsNil)
   389  	check.Assert(adminOrg, NotNil)
   390  	// Find Catalog
   391  	cat, err := adminOrg.GetCatalogByName(vcd.config.VCD.Catalog.Name, false)
   392  	check.Assert(cat, Not(Equals), Catalog{})
   393  	check.Assert(err, IsNil)
   394  	check.Assert(cat.Catalog.Name, Equals, vcd.config.VCD.Catalog.Name)
   395  	// checks if user gave a catalog description in config file
   396  	if vcd.config.VCD.Catalog.Description != "" {
   397  		check.Assert(cat.Catalog.Description, Equals, vcd.config.VCD.Catalog.Description)
   398  	}
   399  	// Check Invalid Catalog
   400  	cat, err = adminOrg.GetCatalogByName(INVALID_NAME, false)
   401  	check.Assert(err, NotNil)
   402  	check.Assert(cat, IsNil)
   403  }
   404  
   405  // Tests CreateCatalog by creating a catalog using an AdminOrg and
   406  // asserts that the catalog returned contains the right contents or if it fails.
   407  // Then Deletes the catalog.
   408  func (vcd *TestVCD) Test_AdminOrgCreateCatalog(check *C) {
   409  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
   410  	check.Assert(err, IsNil)
   411  	check.Assert(adminOrg, NotNil)
   412  	oldAdminCatalog, _ := adminOrg.GetAdminCatalogByName(TestCreateCatalog, false)
   413  	if oldAdminCatalog != nil {
   414  		err = oldAdminCatalog.Delete(true, true)
   415  		check.Assert(err, IsNil)
   416  	}
   417  	adminCatalog, err := adminOrg.CreateCatalog(TestCreateCatalog, TestCreateCatalogDesc)
   418  	check.Assert(err, IsNil)
   419  	AddToCleanupList(TestCreateCatalog, "catalog", vcd.org.Org.Name, "Test_CreateCatalog")
   420  	check.Assert(adminCatalog.AdminCatalog.Name, Equals, TestCreateCatalog)
   421  	check.Assert(adminCatalog.AdminCatalog.Description, Equals, TestCreateCatalogDesc)
   422  	// Immediately after the catalog creation, the creation task should be already complete
   423  	check.Assert(ResourceComplete(adminCatalog.AdminCatalog.Tasks), Equals, true)
   424  
   425  	adminOrg, err = vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
   426  	check.Assert(err, IsNil)
   427  	copyAdminCatalog, err := adminOrg.GetAdminCatalogByName(TestCreateCatalog, false)
   428  	check.Assert(err, IsNil)
   429  	check.Assert(copyAdminCatalog, NotNil)
   430  	check.Assert(adminCatalog.AdminCatalog.Name, Equals, copyAdminCatalog.AdminCatalog.Name)
   431  	check.Assert(adminCatalog.AdminCatalog.Description, Equals, copyAdminCatalog.AdminCatalog.Description)
   432  	check.Assert(adminCatalog.AdminCatalog.IsPublished, Equals, false)
   433  	check.Assert(adminCatalog.AdminCatalog.CatalogStorageProfiles, NotNil)
   434  	check.Assert(adminCatalog.AdminCatalog.CatalogStorageProfiles.VdcStorageProfile, IsNil)
   435  	err = adminCatalog.Delete(true, true)
   436  	check.Assert(err, IsNil)
   437  }
   438  
   439  func (vcd *TestVCD) Test_AdminOrgCreateCatalogWithStorageProfile(check *C) {
   440  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
   441  	check.Assert(err, IsNil)
   442  	check.Assert(adminOrg, NotNil)
   443  	oldAdminCatalog, _ := adminOrg.GetAdminCatalogByName(check.TestName(), false)
   444  	if oldAdminCatalog != nil {
   445  		err = oldAdminCatalog.Delete(true, true)
   446  		check.Assert(err, IsNil)
   447  	}
   448  
   449  	// Lookup storage profile to use in catalog
   450  	storageProfile, err := vcd.vdc.FindStorageProfileReference(vcd.config.VCD.StorageProfile.SP1)
   451  	check.Assert(err, IsNil)
   452  	createStorageProfiles := &types.CatalogStorageProfiles{VdcStorageProfile: []*types.Reference{&storageProfile}}
   453  
   454  	adminCatalog, err := adminOrg.CreateCatalogWithStorageProfile(check.TestName(), TestCreateCatalogDesc, createStorageProfiles)
   455  	check.Assert(err, IsNil)
   456  	AddToCleanupList(check.TestName(), "catalog", vcd.org.Org.Name, check.TestName())
   457  	check.Assert(adminCatalog.AdminCatalog.Name, Equals, check.TestName())
   458  	check.Assert(adminCatalog.AdminCatalog.Description, Equals, TestCreateCatalogDesc)
   459  	// Accessing the task directly with `adminCatalog.AdminCatalog.Tasks.Task[0]` is not safe for Org user
   460  	err = adminCatalog.WaitForTasks()
   461  	check.Assert(err, IsNil)
   462  	adminOrg, err = vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
   463  	check.Assert(err, IsNil)
   464  	copyAdminCatalog, err := adminOrg.GetAdminCatalogByName(check.TestName(), false)
   465  	check.Assert(err, IsNil)
   466  	check.Assert(copyAdminCatalog, NotNil)
   467  	check.Assert(adminCatalog.AdminCatalog.Name, Equals, copyAdminCatalog.AdminCatalog.Name)
   468  	check.Assert(adminCatalog.AdminCatalog.Description, Equals, copyAdminCatalog.AdminCatalog.Description)
   469  	check.Assert(adminCatalog.AdminCatalog.IsPublished, Equals, false)
   470  
   471  	// Try to update storage profile for catalog if secondary profile is defined
   472  	if vcd.config.VCD.StorageProfile.SP2 != "" {
   473  		updateStorageProfile, err := vcd.vdc.FindStorageProfileReference(vcd.config.VCD.StorageProfile.SP2)
   474  		check.Assert(err, IsNil)
   475  		updateStorageProfiles := &types.CatalogStorageProfiles{VdcStorageProfile: []*types.Reference{&updateStorageProfile}}
   476  		adminCatalog.AdminCatalog.CatalogStorageProfiles = updateStorageProfiles
   477  		err = adminCatalog.Update()
   478  		check.Assert(err, IsNil)
   479  	} else {
   480  		fmt.Printf("# Skipping storage profile update for %s because secondary storage profile is not provided",
   481  			adminCatalog.AdminCatalog.Name)
   482  	}
   483  
   484  	err = adminCatalog.Delete(true, true)
   485  	check.Assert(err, IsNil)
   486  }
   487  
   488  // Tests CreateCatalog by creating a catalog using an Org and
   489  // asserts that the catalog returned contains the right contents or if it fails.
   490  // Then Deletes the catalog.
   491  func (vcd *TestVCD) Test_OrgCreateCatalog(check *C) {
   492  	org, err := vcd.client.GetOrgByName(vcd.org.Org.Name)
   493  	check.Assert(err, IsNil)
   494  	check.Assert(org, NotNil)
   495  	oldCatalog, _ := org.GetCatalogByName(TestCreateCatalog, false)
   496  	if oldCatalog != nil {
   497  		err = oldCatalog.Delete(true, true)
   498  		check.Assert(err, IsNil)
   499  	}
   500  	catalog, err := org.CreateCatalog(TestCreateCatalog, TestCreateCatalogDesc)
   501  	check.Assert(err, IsNil)
   502  	AddToCleanupList(TestCreateCatalog, "catalog", vcd.org.Org.Name, "Test_CreateCatalog")
   503  	check.Assert(catalog.Catalog.Name, Equals, TestCreateCatalog)
   504  	check.Assert(catalog.Catalog.Description, Equals, TestCreateCatalogDesc)
   505  	// Immediately after the catalog creation, the creation task should be already complete
   506  	check.Assert(ResourceComplete(catalog.Catalog.Tasks), Equals, true)
   507  	org, err = vcd.client.GetOrgByName(vcd.org.Org.Name)
   508  	check.Assert(err, IsNil)
   509  	copyCatalog, err := org.GetCatalogByName(TestCreateCatalog, false)
   510  	check.Assert(err, IsNil)
   511  	check.Assert(copyCatalog, NotNil)
   512  	check.Assert(catalog.Catalog.Name, Equals, copyCatalog.Catalog.Name)
   513  	check.Assert(catalog.Catalog.Description, Equals, copyCatalog.Catalog.Description)
   514  	check.Assert(catalog.Catalog.IsPublished, Equals, false)
   515  	err = catalog.Delete(true, true)
   516  	check.Assert(err, IsNil)
   517  }
   518  
   519  func (vcd *TestVCD) Test_OrgCreateCatalogWithStorageProfile(check *C) {
   520  	org, err := vcd.client.GetOrgByName(vcd.org.Org.Name)
   521  	check.Assert(err, IsNil)
   522  	check.Assert(org, NotNil)
   523  	oldCatalog, _ := org.GetCatalogByName(check.TestName(), false)
   524  	if oldCatalog != nil {
   525  		err = oldCatalog.Delete(true, true)
   526  		check.Assert(err, IsNil)
   527  	}
   528  
   529  	// Lookup storage profile to use in catalog
   530  	storageProfile, err := vcd.vdc.FindStorageProfileReference(vcd.config.VCD.StorageProfile.SP1)
   531  	check.Assert(err, IsNil)
   532  	storageProfiles := &types.CatalogStorageProfiles{VdcStorageProfile: []*types.Reference{&storageProfile}}
   533  
   534  	catalog, err := org.CreateCatalogWithStorageProfile(check.TestName(), TestCreateCatalogDesc, storageProfiles)
   535  	check.Assert(err, IsNil)
   536  	AddToCleanupList(check.TestName(), "catalog", vcd.org.Org.Name, check.TestName())
   537  	check.Assert(catalog.Catalog.Name, Equals, check.TestName())
   538  	check.Assert(catalog.Catalog.Description, Equals, TestCreateCatalogDesc)
   539  	err = catalog.WaitForTasks()
   540  	check.Assert(err, IsNil)
   541  	org, err = vcd.client.GetOrgByName(vcd.org.Org.Name)
   542  	check.Assert(err, IsNil)
   543  	copyCatalog, err := org.GetCatalogByName(check.TestName(), false)
   544  	check.Assert(err, IsNil)
   545  	check.Assert(copyCatalog, NotNil)
   546  	check.Assert(catalog.Catalog.Name, Equals, copyCatalog.Catalog.Name)
   547  	check.Assert(catalog.Catalog.Description, Equals, copyCatalog.Catalog.Description)
   548  	check.Assert(catalog.Catalog.IsPublished, Equals, false)
   549  	err = catalog.Delete(true, true)
   550  	check.Assert(err, IsNil)
   551  }
   552  
   553  // Test for GetAdminCatalog. Gets admin version of Catalog, and asserts that
   554  // the names and description match, and that no error is returned
   555  func (vcd *TestVCD) Test_GetAdminCatalog(check *C) {
   556  	if vcd.skipAdminTests {
   557  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
   558  	}
   559  	// Fetch admin org version of current test org
   560  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
   561  	check.Assert(err, IsNil)
   562  	// Find Catalog
   563  	adminCatalog, err := adminOrg.GetAdminCatalogByName(vcd.config.VCD.Catalog.Name, false)
   564  	check.Assert(err, IsNil)
   565  	check.Assert(adminCatalog, NotNil)
   566  	check.Assert(adminCatalog.AdminCatalog.Name, Equals, vcd.config.VCD.Catalog.Name)
   567  	// checks if user gave a catalog description in config file
   568  	if vcd.config.VCD.Catalog.Description != "" {
   569  		check.Assert(adminCatalog.AdminCatalog.Description, Equals, vcd.config.VCD.Catalog.Description)
   570  	}
   571  }
   572  
   573  // Tests Refresh for VDC by updating it and then asserting if the
   574  // variable is updated.
   575  func (vcd *TestVCD) Test_RefreshVdc(check *C) {
   576  
   577  	adminOrg, vdcConfiguration, err := setupVdc(vcd, check, "AllocationPool")
   578  	check.Assert(err, IsNil)
   579  
   580  	// Refresh so the new VDC shows up in the org's list
   581  	err = adminOrg.Refresh()
   582  	check.Assert(err, IsNil)
   583  
   584  	adminVdc, err := adminOrg.GetAdminVDCByName(vdcConfiguration.Name, false)
   585  
   586  	check.Assert(err, IsNil)
   587  	check.Assert(adminVdc, NotNil)
   588  	check.Assert(adminVdc.AdminVdc.Name, Equals, vdcConfiguration.Name)
   589  	check.Assert(adminVdc.AdminVdc.IsEnabled, Equals, vdcConfiguration.IsEnabled)
   590  	check.Assert(adminVdc.AdminVdc.AllocationModel, Equals, vdcConfiguration.AllocationModel)
   591  
   592  	adminVdc.AdminVdc.Name = TestRefreshOrgVdc
   593  	_, err = adminVdc.Update()
   594  	check.Assert(err, IsNil)
   595  	AddToCleanupList(TestRefreshOrgVdc, "vdc", vcd.org.Org.Name, check.TestName())
   596  
   597  	// Test Refresh on vdc
   598  	err = adminVdc.Refresh()
   599  	check.Assert(err, IsNil)
   600  	check.Assert(adminVdc.AdminVdc.Name, Equals, TestRefreshOrgVdc)
   601  
   602  	//cleanup
   603  	vdc, err := adminOrg.GetVDCByName(vdcConfiguration.Name, false)
   604  	check.Assert(err, IsNil)
   605  	check.Assert(vdc, NotNil)
   606  	err = vdc.DeleteWait(true, true)
   607  	check.Assert(err, IsNil)
   608  }
   609  
   610  func setupVdc(vcd *TestVCD, check *C, allocationModel string) (AdminOrg, *types.VdcConfiguration, error) {
   611  	if vcd.skipAdminTests {
   612  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
   613  	}
   614  	if vcd.config.VCD.ProviderVdc.Name == "" {
   615  		check.Skip("No Provider VDC name given for VDC tests")
   616  	}
   617  	if vcd.config.VCD.ProviderVdc.StorageProfile == "" {
   618  		check.Skip("No Storage Profile given for VDC tests")
   619  	}
   620  	if vcd.config.VCD.ProviderVdc.NetworkPool == "" {
   621  		check.Skip("No Network Pool given for VDC tests")
   622  	}
   623  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
   624  	check.Assert(err, IsNil)
   625  	check.Assert(adminOrg, NotNil)
   626  	results, err := vcd.client.QueryWithNotEncodedParams(nil, map[string]string{
   627  		"type":   "providerVdc",
   628  		"filter": fmt.Sprintf("name==%s", vcd.config.VCD.ProviderVdc.Name),
   629  	})
   630  	check.Assert(err, IsNil)
   631  	if len(results.Results.VMWProviderVdcRecord) == 0 {
   632  		check.Skip(fmt.Sprintf("No Provider VDC found with name '%s'", vcd.config.VCD.ProviderVdc.Name))
   633  	}
   634  	providerVdcHref := results.Results.VMWProviderVdcRecord[0].HREF
   635  	storageProfile, err := vcd.client.QueryProviderVdcStorageProfileByName(vcd.config.VCD.ProviderVdc.StorageProfile, providerVdcHref)
   636  	check.Assert(err, IsNil)
   637  
   638  	check.Assert(storageProfile.HREF, Not(Equals), "")
   639  	results, err = vcd.client.QueryWithNotEncodedParams(nil, map[string]string{
   640  		"type":   "networkPool",
   641  		"filter": fmt.Sprintf("name==%s", vcd.config.VCD.ProviderVdc.NetworkPool),
   642  	})
   643  	check.Assert(err, IsNil)
   644  	if len(results.Results.NetworkPoolRecord) == 0 {
   645  		check.Skip(fmt.Sprintf("No network pool found with name '%s'", vcd.config.VCD.ProviderVdc.NetworkPool))
   646  	}
   647  	networkPoolHref := results.Results.NetworkPoolRecord[0].HREF
   648  	vdcConfiguration := &types.VdcConfiguration{
   649  		Name:            check.TestName(),
   650  		AllocationModel: allocationModel,
   651  		ComputeCapacity: []*types.ComputeCapacity{
   652  			&types.ComputeCapacity{
   653  				CPU: &types.CapacityWithUsage{
   654  					Units:     "MHz",
   655  					Allocated: 1024,
   656  					Limit:     1024,
   657  				},
   658  				Memory: &types.CapacityWithUsage{
   659  					Allocated: 1024,
   660  					Limit:     1024,
   661  					Units:     "MB",
   662  				},
   663  			},
   664  		},
   665  		VdcStorageProfile: []*types.VdcStorageProfileConfiguration{&types.VdcStorageProfileConfiguration{
   666  			Enabled: addrOf(true),
   667  			Units:   "MB",
   668  			Limit:   1024,
   669  			Default: true,
   670  			ProviderVdcStorageProfile: &types.Reference{
   671  				HREF: storageProfile.HREF,
   672  			},
   673  		},
   674  		},
   675  		NetworkPoolReference: &types.Reference{
   676  			HREF: networkPoolHref,
   677  		},
   678  		ProviderVdcReference: &types.Reference{
   679  			HREF: providerVdcHref,
   680  		},
   681  		IsEnabled:            true,
   682  		IsThinProvision:      true,
   683  		UsesFastProvisioning: true,
   684  	}
   685  	trueValue := true
   686  	falseValue := true
   687  	if allocationModel == "Flex" {
   688  		vdcConfiguration.IsElastic = &falseValue
   689  		vdcConfiguration.IncludeMemoryOverhead = &trueValue
   690  	}
   691  
   692  	vdc, _ := adminOrg.GetVDCByName(vdcConfiguration.Name, false)
   693  	if vdc != nil {
   694  		err = vdc.DeleteWait(true, true)
   695  		check.Assert(err, IsNil)
   696  	}
   697  	_, err = adminOrg.CreateOrgVdc(vdcConfiguration)
   698  	check.Assert(err, IsNil)
   699  	AddToCleanupList(vdcConfiguration.Name, "vdc", vcd.org.Org.Name, check.TestName())
   700  	return *adminOrg, vdcConfiguration, err
   701  }
   702  
   703  func (vcd *TestVCD) Test_QueryStorageProfiles(check *C) {
   704  
   705  	// retrieve Org and VDC
   706  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
   707  	check.Assert(err, IsNil)
   708  	adminVdc, err := adminOrg.GetAdminVDCByName(vcd.config.VCD.Vdc, false)
   709  	check.Assert(err, IsNil)
   710  
   711  	if adminVdc.AdminVdc.ProviderVdcReference == nil {
   712  		check.Skip(fmt.Sprintf("test %s requires system administrator privileges", check.TestName()))
   713  	}
   714  	// Gets the Provider VDC from the AdminVdc structure
   715  	providerVdcName := adminVdc.AdminVdc.ProviderVdcReference.Name
   716  	check.Assert(providerVdcName, Not(Equals), "")
   717  	providerVdcHref := adminVdc.AdminVdc.ProviderVdcReference.HREF
   718  	check.Assert(providerVdcHref, Not(Equals), "")
   719  
   720  	// Gets the full list of storage profilers
   721  	rawSpList, err := vcd.client.Client.QueryAllProviderVdcStorageProfiles()
   722  	check.Assert(err, IsNil)
   723  
   724  	// Manually select the storage profiles that belong to the current provider VDC
   725  	var spList []*types.QueryResultProviderVdcStorageProfileRecordType
   726  	var duplicateNames = make(map[string]bool)
   727  	var notLocalStorageProfile string
   728  	var used = make(map[string]bool)
   729  	for _, sp := range rawSpList {
   730  		if sp.ProviderVdcHREF == providerVdcHref {
   731  			spList = append(spList, sp)
   732  		}
   733  		_, seen := used[sp.Name]
   734  		if seen {
   735  			duplicateNames[sp.Name] = true
   736  		}
   737  		used[sp.Name] = true
   738  	}
   739  	// Find a storage profile from a different provider VDC
   740  	for _, sp := range rawSpList {
   741  		if sp.ProviderVdcHREF != providerVdcHref {
   742  			_, isDuplicate := duplicateNames[sp.Name]
   743  			if !isDuplicate {
   744  				notLocalStorageProfile = sp.Name
   745  			}
   746  		}
   747  	}
   748  
   749  	// Get the list of local storage profiles (belonging to the Provider VDC that the adminVdc depends on)
   750  	localSpList, err := vcd.client.Client.QueryProviderVdcStorageProfiles(providerVdcHref)
   751  	check.Assert(err, IsNil)
   752  	// Make sure the automated list and the manual list match
   753  	check.Assert(spList, DeepEquals, localSpList)
   754  
   755  	// Get the same list using the AdminVdc method and check that the result matches
   756  	compatibleSpList, err := adminVdc.QueryCompatibleStorageProfiles()
   757  	check.Assert(err, IsNil)
   758  	check.Assert(compatibleSpList, DeepEquals, localSpList)
   759  
   760  	for _, sp := range compatibleSpList {
   761  		fullSp, err := vcd.client.QueryProviderVdcStorageProfileByName(sp.Name, providerVdcHref)
   762  		check.Assert(err, IsNil)
   763  		check.Assert(sp.HREF, Equals, fullSp.HREF)
   764  		check.Assert(fullSp.ProviderVdcHREF, Equals, providerVdcHref)
   765  	}
   766  
   767  	// When we have duplicate names, we also check the effectiveness of the retrieval function with Provider VDC filter
   768  	for name := range duplicateNames {
   769  		// Duplicate name with specific provider VDC HREF will succeed
   770  		fullSp, err := vcd.client.QueryProviderVdcStorageProfileByName(name, providerVdcHref)
   771  		check.Assert(err, IsNil)
   772  		check.Assert(fullSp.ProviderVdcHREF, Equals, providerVdcHref)
   773  		// Duplicate name with empty provider VDC HREF will fail
   774  		faultySp, err := vcd.client.QueryProviderVdcStorageProfileByName(name, "")
   775  		check.Assert(err, NotNil)
   776  		check.Assert(faultySp, IsNil)
   777  	}
   778  
   779  	// Search explicitly for a storage profile not present in current provider VDC
   780  	if notLocalStorageProfile != "" {
   781  		fullSp, err := vcd.client.QueryProviderVdcStorageProfileByName(notLocalStorageProfile, providerVdcHref)
   782  		check.Assert(err, NotNil)
   783  		check.Assert(fullSp, IsNil)
   784  	}
   785  }
   786  
   787  func (vcd *TestVCD) Test_AddRemoveVdcStorageProfiles(check *C) {
   788  	vcd.skipIfNotSysAdmin(check)
   789  	if vcd.config.VCD.ProviderVdc.Name == "" {
   790  		check.Skip("No provider VDC found in configuration")
   791  	}
   792  	providerVDCs, err := QueryProviderVdcByName(vcd.client, vcd.config.VCD.ProviderVdc.Name)
   793  	check.Assert(err, IsNil)
   794  	check.Assert(len(providerVDCs), Equals, 1)
   795  
   796  	rawSpList, err := vcd.client.Client.QueryAllProviderVdcStorageProfiles()
   797  	check.Assert(err, IsNil)
   798  	var spList []*types.QueryResultProviderVdcStorageProfileRecordType
   799  	for _, sp := range rawSpList {
   800  		if sp.ProviderVdcHREF == providerVDCs[0].HREF {
   801  			spList = append(spList, sp)
   802  		}
   803  	}
   804  
   805  	localSpList, err := vcd.client.Client.QueryProviderVdcStorageProfiles(providerVDCs[0].HREF)
   806  	check.Assert(err, IsNil)
   807  	check.Assert(spList, DeepEquals, localSpList)
   808  
   809  	const minSp = 2
   810  	if len(spList) < minSp {
   811  		check.Skip(fmt.Sprintf("At least %d  storage profiles are needed for this test", minSp))
   812  	}
   813  	var defaultSp *types.QueryResultProviderVdcStorageProfileRecordType
   814  	var sp2 *types.QueryResultProviderVdcStorageProfileRecordType
   815  
   816  	for i := 0; i < minSp; i++ {
   817  		if spList[i].Name == vcd.config.VCD.ProviderVdc.StorageProfile {
   818  			if defaultSp == nil {
   819  				defaultSp = spList[i]
   820  			}
   821  		} else {
   822  			if sp2 == nil {
   823  				sp2 = spList[i]
   824  			}
   825  		}
   826  	}
   827  
   828  	check.Assert(defaultSp, NotNil)
   829  	check.Assert(sp2, NotNil)
   830  
   831  	// Create the VDC
   832  	adminOrg, vdcConfiguration, err := setupVdc(vcd, check, "AllocationPool")
   833  	check.Assert(err, IsNil)
   834  
   835  	adminVdc, err := adminOrg.GetAdminVDCByName(vdcConfiguration.Name, true)
   836  	check.Assert(err, IsNil)
   837  
   838  	// Add another storage profile
   839  	err = adminVdc.AddStorageProfileWait(&types.VdcStorageProfileConfiguration{
   840  		Enabled: addrOf(true),
   841  		Units:   "MB",
   842  		Limit:   1024,
   843  		Default: false,
   844  		ProviderVdcStorageProfile: &types.Reference{
   845  			HREF: sp2.HREF,
   846  			Name: sp2.Name,
   847  		},
   848  	}, "new sp 2")
   849  	check.Assert(err, IsNil)
   850  	check.Assert(len(adminVdc.AdminVdc.VdcStorageProfiles.VdcStorageProfile), Equals, 2)
   851  
   852  	// Find the default storage profile and makes sure it matches with the one we know to be the default
   853  	defaultSpRef, err := adminVdc.GetDefaultStorageProfileReference()
   854  	check.Assert(err, IsNil)
   855  	check.Assert(defaultSp.Name, Equals, defaultSpRef.Name)
   856  
   857  	// Remove the second storage profile
   858  	err = adminVdc.RemoveStorageProfileWait(sp2.Name)
   859  	check.Assert(err, IsNil)
   860  	check.Assert(len(adminVdc.AdminVdc.VdcStorageProfiles.VdcStorageProfile), Equals, 1)
   861  
   862  	// Add the second storage profile again
   863  	err = adminVdc.AddStorageProfileWait(&types.VdcStorageProfileConfiguration{
   864  		Enabled: addrOf(true),
   865  		Units:   "MB",
   866  		Limit:   1024,
   867  		Default: false,
   868  		ProviderVdcStorageProfile: &types.Reference{
   869  			HREF: sp2.HREF,
   870  			Name: sp2.Name,
   871  		},
   872  	}, "new sp 2")
   873  
   874  	check.Assert(err, IsNil)
   875  	check.Assert(len(adminVdc.AdminVdc.VdcStorageProfiles.VdcStorageProfile), Equals, 2)
   876  
   877  	// Change default storage profile from the original one to the second one
   878  	err = adminVdc.SetDefaultStorageProfile(sp2.Name)
   879  	check.Assert(err, IsNil)
   880  
   881  	// Check that the default storage profile was changed
   882  	defaultSpRef, err = adminVdc.GetDefaultStorageProfileReference()
   883  	check.Assert(err, IsNil)
   884  	check.Assert(defaultSpRef.Name, Equals, sp2.Name)
   885  
   886  	// Set the default storage profile again to the same item.
   887  	// This proves that SetDefaultStorageProfile is idempotent
   888  	err = adminVdc.SetDefaultStorageProfile(sp2.Name)
   889  	check.Assert(err, IsNil)
   890  	defaultSpRef, err = adminVdc.GetDefaultStorageProfileReference()
   891  	check.Assert(err, IsNil)
   892  	check.Assert(defaultSpRef.Name, Equals, sp2.Name)
   893  
   894  	// Remove the former default storage profile
   895  	err = adminVdc.RemoveStorageProfileWait(defaultSp.Name)
   896  	check.Assert(err, IsNil)
   897  	check.Assert(len(adminVdc.AdminVdc.VdcStorageProfiles.VdcStorageProfile), Equals, 1)
   898  
   899  	// Delete the VDC
   900  	vdc, err := adminOrg.GetVDCByName(adminVdc.AdminVdc.Name, false)
   901  	check.Assert(err, IsNil)
   902  	err = vdc.DeleteWait(true, true)
   903  	check.Assert(err, IsNil)
   904  }
   905  
   906  // Tests VDC by updating it and then asserting if the
   907  // variable is updated.
   908  func (vcd *TestVCD) Test_UpdateVdc(check *C) {
   909  	adminOrg, vdcConfiguration, err := setupVdc(vcd, check, "AllocationPool")
   910  	check.Assert(err, IsNil)
   911  
   912  	// Refresh so the new VDC shows up in the org's list
   913  	err = adminOrg.Refresh()
   914  	check.Assert(err, IsNil)
   915  
   916  	adminVdc, err := adminOrg.GetAdminVDCByName(vdcConfiguration.Name, false)
   917  
   918  	check.Assert(err, IsNil)
   919  	check.Assert(adminVdc, NotNil)
   920  	check.Assert(adminVdc.AdminVdc.Name, Equals, vdcConfiguration.Name)
   921  	check.Assert(adminVdc.AdminVdc.IsEnabled, Equals, vdcConfiguration.IsEnabled)
   922  	check.Assert(adminVdc.AdminVdc.AllocationModel, Equals, vdcConfiguration.AllocationModel)
   923  
   924  	updateDescription := "updateDescription"
   925  	computeCapacity := []*types.ComputeCapacity{
   926  		&types.ComputeCapacity{
   927  			CPU: &types.CapacityWithUsage{
   928  				Units:     "MHz",
   929  				Allocated: 2024,
   930  				Limit:     2024,
   931  			},
   932  			Memory: &types.CapacityWithUsage{
   933  				Allocated: 2024,
   934  				Limit:     2024,
   935  				Units:     "MB",
   936  			},
   937  		},
   938  	}
   939  	quota := 111
   940  	vCpu := int64(1000)
   941  	guaranteed := float64(0.6)
   942  	adminVdc.AdminVdc.Description = updateDescription
   943  	adminVdc.AdminVdc.ComputeCapacity = computeCapacity
   944  	adminVdc.AdminVdc.IsEnabled = false
   945  	falseRef := false
   946  	adminVdc.AdminVdc.IsThinProvision = &falseRef
   947  	adminVdc.AdminVdc.NetworkQuota = quota
   948  	adminVdc.AdminVdc.VMQuota = quota
   949  	adminVdc.AdminVdc.OverCommitAllowed = false
   950  	adminVdc.AdminVdc.VCpuInMhz = &vCpu
   951  	adminVdc.AdminVdc.UsesFastProvisioning = &falseRef
   952  	adminVdc.AdminVdc.ResourceGuaranteedCpu = &guaranteed
   953  	adminVdc.AdminVdc.ResourceGuaranteedMemory = &guaranteed
   954  
   955  	updatedVdc, err := adminVdc.Update()
   956  	check.Assert(err, IsNil)
   957  	check.Assert(updatedVdc, Not(IsNil))
   958  	check.Assert(updatedVdc.AdminVdc.Description, Equals, updateDescription)
   959  	check.Assert(updatedVdc.AdminVdc.ComputeCapacity[0].CPU.Allocated, Equals, computeCapacity[0].CPU.Allocated)
   960  	check.Assert(updatedVdc.AdminVdc.IsEnabled, Equals, false)
   961  	check.Assert(*updatedVdc.AdminVdc.IsThinProvision, Equals, false)
   962  	check.Assert(updatedVdc.AdminVdc.NetworkQuota, Equals, quota)
   963  	check.Assert(updatedVdc.AdminVdc.VMQuota, Equals, quota)
   964  	check.Assert(updatedVdc.AdminVdc.OverCommitAllowed, Equals, false)
   965  	check.Assert(*updatedVdc.AdminVdc.VCpuInMhz, Equals, vCpu)
   966  	check.Assert(*updatedVdc.AdminVdc.UsesFastProvisioning, Equals, false)
   967  	check.Assert(math.Abs(*updatedVdc.AdminVdc.ResourceGuaranteedCpu-guaranteed) < 0.001, Equals, true)
   968  	check.Assert(math.Abs(*updatedVdc.AdminVdc.ResourceGuaranteedMemory-guaranteed) < 0.001, Equals, true)
   969  	vdc, err := adminOrg.GetVDCByName(updatedVdc.AdminVdc.Name, true)
   970  	check.Assert(err, IsNil)
   971  	task, err := vdc.Delete(true, true)
   972  	check.Assert(err, IsNil)
   973  	err = task.WaitTaskCompletion()
   974  	check.Assert(err, IsNil)
   975  }
   976  
   977  // Tests org function GetAdminVdcByName with the vdc specified
   978  // in the config file. Then tests with a vdc that doesn't exist.
   979  // Fails if the config file name doesn't match with the found VDC, or
   980  // if the invalid VDC is found by the function.  Also tests a VDC
   981  // that doesn't exist. Asserts an error if the function finds it or
   982  // if the error is not nil.
   983  func (vcd *TestVCD) Test_GetAdminVdcByName(check *C) {
   984  	if vcd.skipAdminTests {
   985  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
   986  	}
   987  
   988  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
   989  	check.Assert(err, IsNil)
   990  	check.Assert(adminOrg, NotNil)
   991  
   992  	adminVdc, err := adminOrg.GetAdminVDCByName(vcd.config.VCD.Vdc, false)
   993  	check.Assert(err, IsNil)
   994  	check.Assert(adminVdc, NotNil)
   995  	check.Assert(adminVdc.AdminVdc.Name, Equals, vcd.config.VCD.Vdc)
   996  	// Try a vdc that doesn't exist
   997  	adminVdc, err = adminOrg.GetAdminVDCByName(INVALID_NAME, false)
   998  	check.Assert(err, NotNil)
   999  	check.Assert(adminVdc, IsNil)
  1000  }
  1001  
  1002  // Tests catalog retrieval by name, by ID, and by a combination of name and ID
  1003  func (vcd *TestVCD) Test_OrgGetCatalog(check *C) {
  1004  
  1005  	getByName := func(name string, refresh bool) (genericEntity, error) {
  1006  		return vcd.org.GetCatalogByName(name, refresh)
  1007  	}
  1008  	getById := func(id string, refresh bool) (genericEntity, error) { return vcd.org.GetCatalogById(id, refresh) }
  1009  	getByNameOrId := func(id string, refresh bool) (genericEntity, error) {
  1010  		return vcd.org.GetCatalogByNameOrId(id, refresh)
  1011  	}
  1012  
  1013  	var def = getterTestDefinition{
  1014  		parentType:    "Org",
  1015  		parentName:    vcd.config.VCD.Org,
  1016  		entityType:    "Catalog",
  1017  		entityName:    vcd.config.VCD.Catalog.Name,
  1018  		getByName:     getByName,
  1019  		getById:       getById,
  1020  		getByNameOrId: getByNameOrId,
  1021  	}
  1022  
  1023  	vcd.testFinderGetGenericEntity(def, check)
  1024  }
  1025  
  1026  // Tests admin catalog retrieval by name, by ID, and by a combination of name and ID
  1027  func (vcd *TestVCD) Test_AdminOrgGetAdminCatalog(check *C) {
  1028  	catalogName := vcd.config.VCD.Catalog.Name
  1029  	if vcd.config.VCD.Org == "" {
  1030  		check.Skip("Test_AdminOrgGetAdminCatalog: Org name not given.")
  1031  		return
  1032  	}
  1033  
  1034  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
  1035  	check.Assert(err, IsNil)
  1036  	check.Assert(adminOrg, NotNil)
  1037  
  1038  	getByName := func(name string, refresh bool) (genericEntity, error) {
  1039  		return adminOrg.GetAdminCatalogByName(name, refresh)
  1040  	}
  1041  	getById := func(id string, refresh bool) (genericEntity, error) {
  1042  		return adminOrg.GetAdminCatalogById(id, refresh)
  1043  	}
  1044  	getByNameOrId := func(id string, refresh bool) (genericEntity, error) {
  1045  		return adminOrg.GetAdminCatalogByNameOrId(id, refresh)
  1046  	}
  1047  
  1048  	var def = getterTestDefinition{
  1049  		parentType:    "AdminOrg",
  1050  		parentName:    vcd.config.VCD.Org,
  1051  		entityType:    "AdminCatalog",
  1052  		entityName:    catalogName,
  1053  		getByName:     getByName,
  1054  		getById:       getById,
  1055  		getByNameOrId: getByNameOrId,
  1056  	}
  1057  
  1058  	vcd.testFinderGetGenericEntity(def, check)
  1059  
  1060  }
  1061  
  1062  // Tests catalog retrieval by name, by ID, and by a combination of name and ID
  1063  func (vcd *TestVCD) Test_AdminOrgGetCatalog(check *C) {
  1064  	catalogName := vcd.config.VCD.Catalog.Name
  1065  
  1066  	if vcd.config.VCD.Org == "" {
  1067  		check.Skip("Test_AdminOrgGetCatalog: Org name not given.")
  1068  		return
  1069  	}
  1070  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
  1071  	check.Assert(err, IsNil)
  1072  	check.Assert(adminOrg, NotNil)
  1073  
  1074  	getByName := func(name string, refresh bool) (genericEntity, error) {
  1075  		return adminOrg.GetCatalogByName(name, refresh)
  1076  	}
  1077  	getById := func(id string, refresh bool) (genericEntity, error) { return adminOrg.GetCatalogById(id, refresh) }
  1078  	getByNameOrId := func(id string, refresh bool) (genericEntity, error) {
  1079  		return adminOrg.GetCatalogByNameOrId(id, refresh)
  1080  	}
  1081  
  1082  	var def = getterTestDefinition{
  1083  		parentType:    "AdminOrg",
  1084  		parentName:    vcd.config.VCD.Org,
  1085  		entityType:    "Catalog",
  1086  		entityName:    catalogName,
  1087  		getByName:     getByName,
  1088  		getById:       getById,
  1089  		getByNameOrId: getByNameOrId,
  1090  	}
  1091  
  1092  	vcd.testFinderGetGenericEntity(def, check)
  1093  
  1094  }
  1095  
  1096  // Tests VDC retrieval by name, by ID, and by a combination of name and ID
  1097  func (vcd *TestVCD) Test_AdminOrgGetVdc(check *C) {
  1098  
  1099  	if vcd.config.VCD.Org == "" {
  1100  		check.Skip("Test_AdminOrgGetVdc: Org name not given.")
  1101  		return
  1102  	}
  1103  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
  1104  	check.Assert(err, IsNil)
  1105  	check.Assert(adminOrg, NotNil)
  1106  
  1107  	getByName := func(name string, refresh bool) (genericEntity, error) { return adminOrg.GetVDCByName(name, refresh) }
  1108  	getById := func(id string, refresh bool) (genericEntity, error) { return adminOrg.GetVDCById(id, refresh) }
  1109  	getByNameOrId := func(id string, refresh bool) (genericEntity, error) { return adminOrg.GetVDCByNameOrId(id, refresh) }
  1110  
  1111  	var def = getterTestDefinition{
  1112  		parentType:    "AdminOrg",
  1113  		parentName:    vcd.config.VCD.Org,
  1114  		entityType:    "Vdc",
  1115  		getterPrefix:  "VDC",
  1116  		entityName:    vcd.config.VCD.Vdc,
  1117  		getByName:     getByName,
  1118  		getById:       getById,
  1119  		getByNameOrId: getByNameOrId,
  1120  	}
  1121  	vcd.testFinderGetGenericEntity(def, check)
  1122  }
  1123  
  1124  // Tests VDC retrieval by name, by ID, and by a combination of name and ID
  1125  func (vcd *TestVCD) Test_AdminOrgGetAdminVdc(check *C) {
  1126  
  1127  	if vcd.config.VCD.Org == "" {
  1128  		check.Skip("Test_AdminOrgGetAdminVdc: Org name not given.")
  1129  		return
  1130  	}
  1131  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
  1132  	check.Assert(err, IsNil)
  1133  	check.Assert(adminOrg, NotNil)
  1134  
  1135  	getByName := func(name string, refresh bool) (genericEntity, error) {
  1136  		return adminOrg.GetAdminVDCByName(name, refresh)
  1137  	}
  1138  	getById := func(id string, refresh bool) (genericEntity, error) { return adminOrg.GetAdminVDCById(id, refresh) }
  1139  	getByNameOrId := func(id string, refresh bool) (genericEntity, error) {
  1140  		return adminOrg.GetAdminVDCByNameOrId(id, refresh)
  1141  	}
  1142  
  1143  	var def = getterTestDefinition{
  1144  		parentType:    "AdminOrg",
  1145  		parentName:    vcd.config.VCD.Org,
  1146  		entityType:    "AdminVdc",
  1147  		getterPrefix:  "AdminVDC",
  1148  		entityName:    vcd.config.VCD.Vdc,
  1149  		getByName:     getByName,
  1150  		getById:       getById,
  1151  		getByNameOrId: getByNameOrId,
  1152  	}
  1153  	vcd.testFinderGetGenericEntity(def, check)
  1154  }
  1155  
  1156  // Tests VDC retrieval by name, by ID, and by a combination of name and ID
  1157  func (vcd *TestVCD) Test_OrgGetVdc(check *C) {
  1158  
  1159  	if vcd.config.VCD.Org == "" {
  1160  		check.Skip("Test_OrgGetVdc: Org name not given.")
  1161  		return
  1162  	}
  1163  	org, err := vcd.client.GetOrgByName(vcd.config.VCD.Org)
  1164  	check.Assert(err, IsNil)
  1165  	check.Assert(org, NotNil)
  1166  
  1167  	getByName := func(name string, refresh bool) (genericEntity, error) { return org.GetVDCByName(name, refresh) }
  1168  	getById := func(id string, refresh bool) (genericEntity, error) { return org.GetVDCById(id, refresh) }
  1169  	getByNameOrId := func(id string, refresh bool) (genericEntity, error) { return org.GetVDCByNameOrId(id, refresh) }
  1170  
  1171  	var def = getterTestDefinition{
  1172  		parentType:    "Org",
  1173  		parentName:    vcd.config.VCD.Org,
  1174  		entityType:    "Vdc",
  1175  		getterPrefix:  "VDC",
  1176  		entityName:    vcd.config.VCD.Vdc,
  1177  		getByName:     getByName,
  1178  		getById:       getById,
  1179  		getByNameOrId: getByNameOrId,
  1180  	}
  1181  	vcd.testFinderGetGenericEntity(def, check)
  1182  }
  1183  
  1184  // Tests VDC retrieval by name, by ID, and by a combination of name and ID
  1185  func (vcd *TestVCD) Test_GetTaskList(check *C) {
  1186  
  1187  	if vcd.config.VCD.Org == "" {
  1188  		check.Skip("Test_GetTaskList: Org name not given.")
  1189  		return
  1190  	}
  1191  	// we need to have Tasks
  1192  	if vcd.skipVappTests {
  1193  		check.Skip("Skipping test because vApp wasn't properly created")
  1194  	}
  1195  	org, err := vcd.client.GetOrgByName(vcd.config.VCD.Org)
  1196  	check.Assert(err, IsNil)
  1197  	check.Assert(org, NotNil)
  1198  
  1199  	taskList, err := org.GetTaskList()
  1200  	check.Assert(err, IsNil)
  1201  	check.Assert(len(taskList.Task), Not(Equals), 0)
  1202  	check.Assert(taskList.Task[0], NotNil)
  1203  	check.Assert(taskList.Task[0].ID, Not(Equals), "")
  1204  	check.Assert(taskList.Task[0].Type, Not(Equals), "")
  1205  	check.Assert(taskList.Task[0].Owner, NotNil)
  1206  	check.Assert(taskList.Task[0].Owner.HREF, Not(Equals), "")
  1207  	check.Assert(taskList.Task[0].Status, Not(Equals), "")
  1208  	check.Assert(taskList.Task[0].Progress, FitsTypeOf, 0)
  1209  }
  1210  
  1211  func (vcd *TestVCD) TestQueryOrgVdcList(check *C) {
  1212  	if !vcd.client.Client.IsSysAdmin {
  1213  		check.Skip("TestQueryOrgVdcList: requires admin user")
  1214  		return
  1215  	}
  1216  	if vcd.config.VCD.Org == "" {
  1217  		check.Skip("TestQueryOrgVdcList: Org name not given.")
  1218  		return
  1219  	}
  1220  
  1221  	if testVerbose {
  1222  		fmt.Println("# Setting up 2 additional Orgs and 1 additional VDC")
  1223  	}
  1224  
  1225  	// Pre-create two more Orgs and one VDC to test that filtering behaves correctly
  1226  	newOrgName1 := spawnTestOrg(vcd, check, "org1")
  1227  	newOrgName2 := spawnTestOrg(vcd, check, "org2")
  1228  	vdc := spawnTestVdc(vcd, check, newOrgName1)
  1229  
  1230  	// Dump structure
  1231  	if testVerbose {
  1232  		fmt.Println("# Org and VDC structure layout")
  1233  		queryOrgList := []string{"System", vcd.config.VCD.Org, newOrgName1, newOrgName2}
  1234  		for _, orgName := range queryOrgList {
  1235  			org, err := vcd.client.GetOrgByName(orgName)
  1236  			check.Assert(err, IsNil)
  1237  			check.Assert(org, NotNil)
  1238  
  1239  			vdcs, err := org.QueryOrgVdcList()
  1240  			check.Assert(err, IsNil)
  1241  			if testVerbose {
  1242  				fmt.Printf("VDCs for Org '%s'\n", orgName)
  1243  				for i, vdc := range vdcs {
  1244  					fmt.Printf("%d %s -> %s\n", i+1, vdc.OrgName, vdc.Name)
  1245  				}
  1246  				fmt.Println()
  1247  			}
  1248  		}
  1249  		fmt.Println("")
  1250  	}
  1251  
  1252  	// expectedVdcCountInSystem = 1 NSX-V VDC
  1253  	expectedVdcCountInSystem := 1
  1254  	// If an NSX-T VDC exists - then expected count of VDCs is at least 2
  1255  	if vcd.config.VCD.Nsxt.Vdc != "" {
  1256  		expectedVdcCountInSystem++
  1257  	}
  1258  
  1259  	// System Org does not directly report any child VDCs
  1260  	validateQueryOrgVdcResults(vcd, check, "Org should have no VDCs", "System", addrOf(0), nil)
  1261  	validateQueryOrgVdcResults(vcd, check, fmt.Sprintf("Should have 1 VDC %s", vdc.Vdc.Name), newOrgName1, addrOf(1), nil)
  1262  	validateQueryOrgVdcResults(vcd, check, "Should have 0 VDCs", newOrgName2, addrOf(0), nil)
  1263  	// Main Org 'vcd.config.VCD.Org' is expected to have at least (expectedVdcCountInSystem). Might be more if there are
  1264  	// more VDCs created manually
  1265  	validateQueryOrgVdcResults(vcd, check, fmt.Sprintf("Should have %d VDCs or more", expectedVdcCountInSystem), vcd.config.VCD.Org, nil, &expectedVdcCountInSystem)
  1266  
  1267  	task, err := vdc.Delete(true, true)
  1268  	check.Assert(err, IsNil)
  1269  	err = task.WaitTaskCompletion()
  1270  	check.Assert(err, IsNil)
  1271  	org1, err := vcd.client.GetAdminOrgByName(newOrgName1)
  1272  	check.Assert(err, IsNil)
  1273  	err = org1.Delete(true, true)
  1274  	check.Assert(err, IsNil)
  1275  	org2, err := vcd.client.GetAdminOrgByName(newOrgName2)
  1276  	check.Assert(err, IsNil)
  1277  	err = org2.Delete(true, true)
  1278  	check.Assert(err, IsNil)
  1279  }
  1280  
  1281  func validateQueryOrgVdcResults(vcd *TestVCD, check *C, name, orgName string, expectedVdcCount, expectedVdcCountOrMore *int) {
  1282  	if testVerbose {
  1283  		fmt.Printf("# Checking VDCs in Org '%s' (%s):\n", orgName, name)
  1284  	}
  1285  
  1286  	org, err := vcd.client.GetOrgByName(orgName)
  1287  	check.Assert(err, IsNil)
  1288  	orgList, err := org.QueryOrgVdcList()
  1289  	check.Assert(err, IsNil)
  1290  
  1291  	// Number of components should be equal to the one returned by 'adminOrg.GetAllVDCs' which looks up VDCs in
  1292  	// <AdminOrg> structure
  1293  	adminOrg, err := vcd.client.GetAdminOrgByName(orgName)
  1294  	check.Assert(err, IsNil)
  1295  	allVdcs, err := adminOrg.GetAllVDCs(true)
  1296  	check.Assert(err, IsNil)
  1297  	check.Assert(len(orgList), Equals, len(allVdcs))
  1298  
  1299  	// Ensure the expected count of VDCs is found
  1300  	if expectedVdcCount != nil {
  1301  		check.Assert(len(orgList), Equals, *expectedVdcCount)
  1302  	}
  1303  	// Ensure that no less than 'expectedVdcCountOrMore' VDCs found in object. This validation allows to have more than
  1304  	if expectedVdcCountOrMore != nil {
  1305  		check.Assert(len(orgList) >= *expectedVdcCountOrMore, Equals, true)
  1306  	}
  1307  
  1308  	if testVerbose {
  1309  		if expectedVdcCount != nil {
  1310  			fmt.Printf("Got %d VDCs in Org '%s'. Expected (%d)\n", len(orgList), orgName, *expectedVdcCount)
  1311  		}
  1312  
  1313  		if expectedVdcCountOrMore != nil {
  1314  			fmt.Printf("Got %d VDCs in Org '%s'. Expected (%d) or more\n", len(orgList), orgName, *expectedVdcCountOrMore)
  1315  		}
  1316  	}
  1317  
  1318  	// Ensure that all VDCs have the same parent (or different if a query was performed for 'System')
  1319  	for index := range orgList {
  1320  		if orgName == "System" {
  1321  			check.Assert(orgList[index].OrgName, Not(Equals), orgName)
  1322  		} else {
  1323  			check.Assert(orgList[index].OrgName, Equals, orgName)
  1324  
  1325  		}
  1326  
  1327  	}
  1328  	if testVerbose {
  1329  		fmt.Printf("%d VDC(s) in Org '%s' have correct parent set\n", len(orgList), orgName)
  1330  		fmt.Println()
  1331  	}
  1332  }