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

     1  //go:build functional || openapi || role || ALL
     2  
     3  /*
     4   * Copyright 2021 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	"net/url"
    11  
    12  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    13  	. "gopkg.in/check.v1"
    14  )
    15  
    16  func (vcd *TestVCD) Test_RightsBundle(check *C) {
    17  	client := vcd.client.Client
    18  	if !client.IsSysAdmin {
    19  		check.Skip("test Test_RightsBundle requires system administrator privileges")
    20  	}
    21  	vcd.checkSkipWhenApiToken(check)
    22  
    23  	// Step 1 - Get all rights bundles
    24  	allExistingRightsBundle, err := client.GetAllRightsBundles(nil)
    25  	check.Assert(err, IsNil)
    26  	check.Assert(allExistingRightsBundle, NotNil)
    27  
    28  	// Step 2 - Get all roles using query filters
    29  	for _, oneRightsBundle := range allExistingRightsBundle {
    30  
    31  		// Step 2.1 - retrieve specific rights bundle by using FIQL filter
    32  		queryParams := url.Values{}
    33  		queryParams.Add("filter", "id=="+oneRightsBundle.RightsBundle.Id)
    34  
    35  		expectOneRightsBundleResultById, err := client.GetAllRightsBundles(queryParams)
    36  		check.Assert(err, IsNil)
    37  		check.Assert(len(expectOneRightsBundleResultById) == 1, Equals, true)
    38  
    39  		// Step 2.2 - retrieve specific rights bundle by using endpoint
    40  		exactItem, err := client.GetRightsBundleById(oneRightsBundle.RightsBundle.Id)
    41  		check.Assert(err, IsNil)
    42  
    43  		check.Assert(err, IsNil)
    44  		check.Assert(exactItem, NotNil)
    45  
    46  		// Step 2.3 - compare struct retrieved by using filter and the one retrieved by exact endpoint ID
    47  		check.Assert(oneRightsBundle, DeepEquals, expectOneRightsBundleResultById[0])
    48  
    49  	}
    50  
    51  	// Step 3 - Create a new rights bundle and ensure it is created as specified by doing deep comparison
    52  
    53  	newGR := &types.RightsBundle{
    54  		Name:        check.TestName(),
    55  		Description: "Global Role created by test",
    56  		// This BundleKey is being set by VCD even if it is not sent
    57  		BundleKey: types.VcloudUndefinedKey,
    58  		ReadOnly:  false,
    59  	}
    60  
    61  	createdRightsBundle, err := client.CreateRightsBundle(newGR)
    62  	check.Assert(err, IsNil)
    63  	AddToCleanupListOpenApi(createdRightsBundle.RightsBundle.Name, check.TestName(),
    64  		types.OpenApiPathVersion1_0_0+types.OpenApiEndpointRightsBundles+createdRightsBundle.RightsBundle.Id)
    65  
    66  	// Ensure supplied and created structs differ only by ID
    67  	newGR.Id = createdRightsBundle.RightsBundle.Id
    68  	check.Assert(createdRightsBundle.RightsBundle, DeepEquals, newGR)
    69  
    70  	// Step 4 - updated created rights bundle
    71  	createdRightsBundle.RightsBundle.Description = "Updated description"
    72  	updatedRightsBundle, err := createdRightsBundle.Update()
    73  	check.Assert(err, IsNil)
    74  	check.Assert(updatedRightsBundle.RightsBundle, DeepEquals, createdRightsBundle.RightsBundle)
    75  
    76  	// Step 5 - add rights to rights bundle
    77  
    78  	// These rights include 5 implied rights, which will be added by globalRole.AddRights
    79  	rightNames := []string{"Catalog: Add vApp from My Cloud", "Catalog: Edit Properties"}
    80  
    81  	rightSet, err := getRightsSet(&client, rightNames)
    82  	check.Assert(err, IsNil)
    83  
    84  	err = updatedRightsBundle.AddRights(rightSet)
    85  	check.Assert(err, IsNil)
    86  
    87  	rights, err := updatedRightsBundle.GetRights(nil)
    88  	check.Assert(err, IsNil)
    89  	check.Assert(len(rights), Equals, len(rightSet))
    90  
    91  	// Step 6 - remove 1 right from rights bundle
    92  
    93  	err = updatedRightsBundle.RemoveRights([]types.OpenApiReference{rightSet[0]})
    94  	check.Assert(err, IsNil)
    95  	rights, err = updatedRightsBundle.GetRights(nil)
    96  	check.Assert(err, IsNil)
    97  	check.Assert(len(rights), Equals, len(rightSet)-1)
    98  
    99  	testRightsContainerTenants(vcd, check, updatedRightsBundle)
   100  
   101  	// Step 7 - remove all rights from rights bundle
   102  	err = updatedRightsBundle.RemoveAllRights()
   103  	check.Assert(err, IsNil)
   104  
   105  	rights, err = updatedRightsBundle.GetRights(nil)
   106  	check.Assert(err, IsNil)
   107  	check.Assert(len(rights), Equals, 0)
   108  
   109  	// Step 8 - delete created rights bundle
   110  	err = updatedRightsBundle.Delete()
   111  	check.Assert(err, IsNil)
   112  
   113  	// Step 9 - try to read deleted rights bundle and expect error to contain 'ErrorEntityNotFound'
   114  	// Read is tricky - it throws an error ACCESS_TO_RESOURCE_IS_FORBIDDEN when the resource with ID does not
   115  	// exist therefore one cannot know what kind of error occurred.
   116  	deletedRightsBundle, err := client.GetRightsBundleById(createdRightsBundle.RightsBundle.Id)
   117  	check.Assert(ContainsNotFound(err), Equals, true)
   118  	check.Assert(deletedRightsBundle, IsNil)
   119  }