github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/adminorg_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  package govcd
     7  
     8  import (
     9  	"fmt"
    10  
    11  	. "gopkg.in/check.v1"
    12  )
    13  
    14  // Creates a Catalog and then verify that finds it
    15  func (vcd *TestVCD) Test_FindAdminCatalogRecords(check *C) {
    16  	if vcd.skipAdminTests {
    17  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
    18  	}
    19  
    20  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
    21  	check.Assert(err, IsNil)
    22  	check.Assert(adminOrg, NotNil)
    23  	catalogName := "catalogForQuery"
    24  	adminCatalog, err := adminOrg.CreateCatalog(catalogName, "catalogForQueryDescription")
    25  	check.Assert(err, IsNil)
    26  	AddToCleanupList(catalogName, "catalog", vcd.config.VCD.Org, check.TestName())
    27  	check.Assert(adminCatalog.AdminCatalog.Name, Equals, catalogName)
    28  
    29  	// just imitate wait
    30  	err = adminOrg.Refresh()
    31  	check.Assert(err, IsNil)
    32  
    33  	findRecords, err := adminOrg.FindAdminCatalogRecords(catalogName)
    34  	check.Assert(err, IsNil)
    35  	check.Assert(findRecords, NotNil)
    36  	check.Assert(len(findRecords), Equals, 1)
    37  	check.Assert(findRecords[0].Name, Equals, catalogName)
    38  	check.Assert(findRecords[0].OrgName, Equals, adminOrg.AdminOrg.Name)
    39  }
    40  
    41  // Tests AdminOrg lease settings for vApp and vApp template
    42  func (vcd *TestVCD) TestAdminOrg_SetLease(check *C) {
    43  	type leaseParams struct {
    44  		deploymentLeaseSeconds                     int
    45  		vappStorageLease                           int
    46  		vappTemplateStorageLease                   int
    47  		powerOffOnRuntimeLeaseExpiration           bool
    48  		vappDeleteOnStorageLeaseExpiration         bool
    49  		vappTemplateDeleteOnStorageLeaseExpiration bool
    50  	}
    51  
    52  	if vcd.skipAdminTests {
    53  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
    54  	}
    55  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
    56  	check.Assert(err, IsNil)
    57  	check.Assert(adminOrg, NotNil)
    58  
    59  	// Save vApp and vApp template lease parameters
    60  	var saveParams = leaseParams{
    61  		deploymentLeaseSeconds:                     *adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.DeploymentLeaseSeconds,
    62  		vappStorageLease:                           *adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.StorageLeaseSeconds,
    63  		vappTemplateStorageLease:                   *adminOrg.AdminOrg.OrgSettings.OrgVAppTemplateSettings.StorageLeaseSeconds,
    64  		powerOffOnRuntimeLeaseExpiration:           *adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.PowerOffOnRuntimeLeaseExpiration,
    65  		vappDeleteOnStorageLeaseExpiration:         *adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.DeleteOnStorageLeaseExpiration,
    66  		vappTemplateDeleteOnStorageLeaseExpiration: *adminOrg.AdminOrg.OrgSettings.OrgVAppTemplateSettings.DeleteOnStorageLeaseExpiration,
    67  	}
    68  
    69  	var leaseData = []leaseParams{
    70  		{
    71  			deploymentLeaseSeconds:                     0, // never expires
    72  			vappStorageLease:                           0, // never expires
    73  			vappTemplateStorageLease:                   0, // never expires
    74  			powerOffOnRuntimeLeaseExpiration:           true,
    75  			vappDeleteOnStorageLeaseExpiration:         true,
    76  			vappTemplateDeleteOnStorageLeaseExpiration: true,
    77  		},
    78  		{
    79  			deploymentLeaseSeconds:                     0, // never expires
    80  			vappStorageLease:                           0, // never expires
    81  			vappTemplateStorageLease:                   0, // never expires
    82  			powerOffOnRuntimeLeaseExpiration:           false,
    83  			vappDeleteOnStorageLeaseExpiration:         false,
    84  			vappTemplateDeleteOnStorageLeaseExpiration: false,
    85  		},
    86  		{
    87  			deploymentLeaseSeconds:                     3600,          // 1 hour
    88  			vappStorageLease:                           3600 * 24,     // 1 day
    89  			vappTemplateStorageLease:                   3600 * 24 * 7, // 1 week
    90  			powerOffOnRuntimeLeaseExpiration:           true,
    91  			vappDeleteOnStorageLeaseExpiration:         true,
    92  			vappTemplateDeleteOnStorageLeaseExpiration: true,
    93  		},
    94  		{
    95  			deploymentLeaseSeconds:                     3600,          // 1 hour
    96  			vappStorageLease:                           3600 * 24,     // 1 day
    97  			vappTemplateStorageLease:                   3600 * 24 * 7, // 1 week
    98  			powerOffOnRuntimeLeaseExpiration:           false,
    99  			vappDeleteOnStorageLeaseExpiration:         false,
   100  			vappTemplateDeleteOnStorageLeaseExpiration: false,
   101  		},
   102  		{
   103  			deploymentLeaseSeconds:                     3600 * 24 * 30,  // 1 month
   104  			vappStorageLease:                           3600 * 24 * 90,  // 1 quarter
   105  			vappTemplateStorageLease:                   3600 * 24 * 365, // 1 year
   106  			powerOffOnRuntimeLeaseExpiration:           true,
   107  			vappDeleteOnStorageLeaseExpiration:         true,
   108  			vappTemplateDeleteOnStorageLeaseExpiration: true,
   109  		},
   110  		{
   111  			deploymentLeaseSeconds:                     3600 * 24 * 30,  // 1 month
   112  			vappStorageLease:                           3600 * 24 * 90,  // 1 quarter
   113  			vappTemplateStorageLease:                   3600 * 24 * 365, // 1 year
   114  			powerOffOnRuntimeLeaseExpiration:           false,
   115  			vappDeleteOnStorageLeaseExpiration:         false,
   116  			vappTemplateDeleteOnStorageLeaseExpiration: false,
   117  		},
   118  	}
   119  
   120  	for infoIndex, info := range leaseData {
   121  		fmt.Printf("update lease params %v\n", info)
   122  		// Change the lease parameters for both vapp and vApp template
   123  		adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.StorageLeaseSeconds = &leaseData[infoIndex].vappStorageLease
   124  		adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.DeploymentLeaseSeconds = &leaseData[infoIndex].deploymentLeaseSeconds
   125  		adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.PowerOffOnRuntimeLeaseExpiration = &leaseData[infoIndex].powerOffOnRuntimeLeaseExpiration
   126  		adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.DeleteOnStorageLeaseExpiration = &leaseData[infoIndex].vappDeleteOnStorageLeaseExpiration
   127  
   128  		adminOrg.AdminOrg.OrgSettings.OrgVAppTemplateSettings.StorageLeaseSeconds = &leaseData[infoIndex].vappTemplateStorageLease
   129  		adminOrg.AdminOrg.OrgSettings.OrgVAppTemplateSettings.DeleteOnStorageLeaseExpiration = &leaseData[infoIndex].vappTemplateDeleteOnStorageLeaseExpiration
   130  
   131  		task, err := adminOrg.Update()
   132  		check.Assert(err, IsNil)
   133  		check.Assert(task, NotNil)
   134  		err = task.WaitTaskCompletion()
   135  		check.Assert(err, IsNil)
   136  
   137  		// Check the results
   138  		check.Assert(*adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.DeploymentLeaseSeconds, Equals, info.deploymentLeaseSeconds)
   139  		check.Assert(*adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.StorageLeaseSeconds, Equals, info.vappStorageLease)
   140  		check.Assert(*adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.PowerOffOnRuntimeLeaseExpiration, Equals, info.powerOffOnRuntimeLeaseExpiration)
   141  		check.Assert(*adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.DeleteOnStorageLeaseExpiration, Equals, info.vappDeleteOnStorageLeaseExpiration)
   142  		check.Assert(*adminOrg.AdminOrg.OrgSettings.OrgVAppTemplateSettings.DeleteOnStorageLeaseExpiration, Equals, info.vappTemplateDeleteOnStorageLeaseExpiration)
   143  		check.Assert(*adminOrg.AdminOrg.OrgSettings.OrgVAppTemplateSettings.StorageLeaseSeconds, Equals, info.vappTemplateStorageLease)
   144  
   145  	}
   146  	// Restore the initial parameters
   147  	adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.StorageLeaseSeconds = &saveParams.vappStorageLease
   148  	adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.DeploymentLeaseSeconds = &saveParams.deploymentLeaseSeconds
   149  	adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.PowerOffOnRuntimeLeaseExpiration = &saveParams.powerOffOnRuntimeLeaseExpiration
   150  	adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.DeleteOnStorageLeaseExpiration = &saveParams.vappDeleteOnStorageLeaseExpiration
   151  
   152  	adminOrg.AdminOrg.OrgSettings.OrgVAppTemplateSettings.StorageLeaseSeconds = &saveParams.vappTemplateStorageLease
   153  	adminOrg.AdminOrg.OrgSettings.OrgVAppTemplateSettings.DeleteOnStorageLeaseExpiration = &saveParams.vappTemplateDeleteOnStorageLeaseExpiration
   154  
   155  	fmt.Printf("restore lease params %v\n", saveParams)
   156  	task, err := adminOrg.Update()
   157  	check.Assert(err, IsNil)
   158  	check.Assert(task, NotNil)
   159  	err = task.WaitTaskCompletion()
   160  	check.Assert(err, IsNil)
   161  
   162  	// Check that the initial parameters were restored
   163  	check.Assert(*adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.DeploymentLeaseSeconds, Equals, saveParams.deploymentLeaseSeconds)
   164  	check.Assert(*adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.StorageLeaseSeconds, Equals, saveParams.vappStorageLease)
   165  	check.Assert(*adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.PowerOffOnRuntimeLeaseExpiration, Equals, saveParams.powerOffOnRuntimeLeaseExpiration)
   166  	check.Assert(*adminOrg.AdminOrg.OrgSettings.OrgVAppLeaseSettings.DeleteOnStorageLeaseExpiration, Equals, saveParams.vappDeleteOnStorageLeaseExpiration)
   167  	check.Assert(*adminOrg.AdminOrg.OrgSettings.OrgVAppTemplateSettings.DeleteOnStorageLeaseExpiration, Equals, saveParams.vappTemplateDeleteOnStorageLeaseExpiration)
   168  	check.Assert(*adminOrg.AdminOrg.OrgSettings.OrgVAppTemplateSettings.StorageLeaseSeconds, Equals, saveParams.vappTemplateStorageLease)
   169  
   170  }
   171  
   172  func (vcd *TestVCD) TestOrg_AdminOrg_QueryCatalogList(check *C) {
   173  	if vcd.config.VCD.Org == "" {
   174  		check.Skip("no org name provided. test skipped")
   175  	}
   176  	if vcd.config.VCD.Catalog.Name == "" {
   177  		check.Skip("no catalog name provided. test skipped")
   178  	}
   179  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
   180  	check.Assert(err, IsNil)
   181  	check.Assert(adminOrg, NotNil)
   182  
   183  	org, err := vcd.client.GetOrgByName(vcd.config.VCD.Org)
   184  	check.Assert(err, IsNil)
   185  	check.Assert(org, NotNil)
   186  
   187  	// gets the catalog list as an adminOrg
   188  	catalogsInAdminOrg, err := adminOrg.QueryCatalogList()
   189  	check.Assert(err, IsNil)
   190  
   191  	// gets a specific catalog as an adminOrg
   192  	singleCatalogInAdminOrg, err := adminOrg.FindCatalogRecords(vcd.config.VCD.Catalog.Name)
   193  	check.Assert(err, IsNil)
   194  	check.Assert(singleCatalogInAdminOrg, NotNil)
   195  	check.Assert(len(singleCatalogInAdminOrg), Equals, 1)
   196  
   197  	// try to get a non-existent catalog
   198  	nonExistentCatalog, err := adminOrg.FindCatalogRecords("iCompletelyMadeThisUp")
   199  	check.Assert(nonExistentCatalog, IsNil)
   200  	check.Assert(err, Equals, ErrorEntityNotFound)
   201  
   202  	// try to get a non-existent catalog with space
   203  	spaceTestCatalog, err := adminOrg.FindCatalogRecords("space test catalog name")
   204  	check.Assert(spaceTestCatalog, IsNil)
   205  	check.Assert(err, Equals, ErrorEntityNotFound)
   206  
   207  	// gets the catalog list as an Org
   208  	catalogsInOrg, err := org.QueryCatalogList()
   209  	check.Assert(err, IsNil)
   210  
   211  	foundInOrg := false
   212  	// Searches the org catalogs list for a known catalog
   213  	for _, catOrg := range catalogsInOrg {
   214  		if catOrg.Name == vcd.config.VCD.Catalog.Name {
   215  			foundInOrg = true
   216  		}
   217  	}
   218  	check.Assert(foundInOrg, Equals, true)
   219  
   220  	foundInAdminOrg := false
   221  	// Searches the admin org catalogs list for a known catalog
   222  	for _, catOrg := range catalogsInAdminOrg {
   223  		if catOrg.Name == vcd.config.VCD.Catalog.Name {
   224  			foundInAdminOrg = true
   225  		}
   226  	}
   227  	check.Assert(foundInAdminOrg, Equals, true)
   228  
   229  	// both lists should have the same number of items
   230  	check.Assert(len(catalogsInAdminOrg), Equals, len(catalogsInOrg))
   231  
   232  	// Check that every item in one list is also in the other list
   233  	for _, catA := range catalogsInAdminOrg {
   234  		foundInBoth := false
   235  		for _, catO := range catalogsInOrg {
   236  			if catA.Name == catO.Name {
   237  				foundInBoth = true
   238  			}
   239  		}
   240  		check.Assert(foundInBoth, Equals, true)
   241  	}
   242  }
   243  
   244  // Test_GetAllVDCs checks that adminOrg.GetAllVDCs returns at least one VDC
   245  func (vcd *TestVCD) Test_AdminOrgGetAllVDCs(check *C) {
   246  	if vcd.skipAdminTests {
   247  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
   248  	}
   249  
   250  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
   251  	check.Assert(err, IsNil)
   252  	check.Assert(adminOrg, NotNil)
   253  
   254  	vdcs, err := adminOrg.GetAllVDCs(true)
   255  	check.Assert(err, IsNil)
   256  	check.Assert(len(vdcs) > 0, Equals, true)
   257  
   258  	// If NSX-T VDC is configured we expect to see at least 2 VDCs (NSX-V and NSX-T)
   259  	if vcd.config.VCD.Nsxt.Vdc != "" {
   260  		check.Assert(len(vdcs) >= 2, Equals, true)
   261  	}
   262  }
   263  
   264  // Test_GetAllStorageProfileReferences checks that adminOrg.GetAllStorageProfileReferences returns at least one storage
   265  // profile reference
   266  func (vcd *TestVCD) Test_GetAllStorageProfileReferences(check *C) {
   267  	if vcd.skipAdminTests {
   268  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
   269  	}
   270  
   271  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.config.VCD.Org)
   272  	check.Assert(err, IsNil)
   273  	check.Assert(adminOrg, NotNil)
   274  
   275  	storageProfileReferences, err := adminOrg.GetAllStorageProfileReferences(true)
   276  	check.Assert(err, IsNil)
   277  	check.Assert(len(storageProfileReferences) > 0, Equals, true)
   278  }