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

     1  /*
     2   * Copyright 2021 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     3   */
     4  package govcd
     5  
     6  import (
     7  	"fmt"
     8  	"net/url"
     9  
    10  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    11  )
    12  
    13  type RightsBundle struct {
    14  	RightsBundle *types.RightsBundle
    15  	client       *Client
    16  }
    17  
    18  // CreateRightsBundle creates a new rights bundle as a system administrator
    19  func (client *Client) CreateRightsBundle(newRightsBundle *types.RightsBundle) (*RightsBundle, error) {
    20  	if !client.IsSysAdmin {
    21  		return nil, fmt.Errorf("only system administrator can handle rights bundles")
    22  	}
    23  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
    24  	minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	urlRef, err := client.OpenApiBuildEndpoint(endpoint)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	if newRightsBundle.BundleKey == "" {
    35  		newRightsBundle.BundleKey = types.VcloudUndefinedKey
    36  	}
    37  	if newRightsBundle.PublishAll == nil {
    38  		newRightsBundle.PublishAll = addrOf(false)
    39  	}
    40  	returnBundle := &RightsBundle{
    41  		RightsBundle: &types.RightsBundle{},
    42  		client:       client,
    43  	}
    44  
    45  	err = client.OpenApiPostItem(minimumApiVersion, urlRef, nil, newRightsBundle, returnBundle.RightsBundle, nil)
    46  	if err != nil {
    47  		return nil, fmt.Errorf("error creating rights bundle: %s", err)
    48  	}
    49  
    50  	return returnBundle, nil
    51  }
    52  
    53  // Update updates existing rights bundle
    54  func (rb *RightsBundle) Update() (*RightsBundle, error) {
    55  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
    56  	minimumApiVersion, err := rb.client.checkOpenApiEndpointCompatibility(endpoint)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	if rb.RightsBundle.Id == "" {
    62  		return nil, fmt.Errorf("cannot update role without id")
    63  	}
    64  
    65  	urlRef, err := rb.client.OpenApiBuildEndpoint(endpoint, rb.RightsBundle.Id)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	returnRightsBundle := &RightsBundle{
    71  		RightsBundle: &types.RightsBundle{},
    72  		client:       rb.client,
    73  	}
    74  
    75  	err = rb.client.OpenApiPutItem(minimumApiVersion, urlRef, nil, rb.RightsBundle, returnRightsBundle.RightsBundle, nil)
    76  	if err != nil {
    77  		return nil, fmt.Errorf("error updating rights bundle: %s", err)
    78  	}
    79  
    80  	return returnRightsBundle, nil
    81  }
    82  
    83  // getAllRightsBundles retrieves all rights bundles. Query parameters can be supplied to perform additional
    84  // filtering
    85  func getAllRightsBundles(client *Client, queryParameters url.Values, additionalHeader map[string]string) ([]*RightsBundle, error) {
    86  	if !client.IsSysAdmin {
    87  		return nil, fmt.Errorf("only system administrator can handle rights bundles")
    88  	}
    89  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
    90  	minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	urlRef, err := client.OpenApiBuildEndpoint(endpoint)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  
   100  	typeResponses := []*types.RightsBundle{{}}
   101  	err = client.OpenApiGetAllItems(minimumApiVersion, urlRef, queryParameters, &typeResponses, additionalHeader)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  	if len(typeResponses) == 0 {
   106  		return []*RightsBundle{}, nil
   107  	}
   108  	var results = make([]*RightsBundle, len(typeResponses))
   109  	for i, r := range typeResponses {
   110  		results[i] = &RightsBundle{
   111  			RightsBundle: r,
   112  			client:       client,
   113  		}
   114  	}
   115  
   116  	return results, nil
   117  }
   118  
   119  // GetAllRightsBundles retrieves all rights bundles. Query parameters can be supplied to perform additional
   120  // filtering
   121  func (client *Client) GetAllRightsBundles(queryParameters url.Values) ([]*RightsBundle, error) {
   122  	return getAllRightsBundles(client, queryParameters, nil)
   123  }
   124  
   125  // GetTenants retrieves all tenants associated to a given Rights Bundle.
   126  // Query parameters can be supplied to perform additional filtering
   127  func (rb *RightsBundle) GetTenants(queryParameters url.Values) ([]types.OpenApiReference, error) {
   128  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
   129  	return getContainerTenants(rb.client, rb.RightsBundle.Id, endpoint, queryParameters)
   130  }
   131  
   132  func (rb *RightsBundle) GetRights(queryParameters url.Values) ([]*types.Right, error) {
   133  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
   134  	return getRights(rb.client, rb.RightsBundle.Id, endpoint, queryParameters, nil)
   135  }
   136  
   137  // AddRights adds a collection of rights to a rights bundle
   138  func (rb *RightsBundle) AddRights(newRights []types.OpenApiReference) error {
   139  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
   140  	return addRightsToRole(rb.client, "RightsBundle", rb.RightsBundle.Name, rb.RightsBundle.Id, endpoint, newRights, nil)
   141  }
   142  
   143  // UpdateRights replaces existing rights with the given collection of rights
   144  func (rb *RightsBundle) UpdateRights(newRights []types.OpenApiReference) error {
   145  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
   146  	return updateRightsInRole(rb.client, "RightsBundle", rb.RightsBundle.Name, rb.RightsBundle.Id, endpoint, newRights, nil)
   147  }
   148  
   149  // RemoveRights removes specific rights from a rights bundle
   150  func (rb *RightsBundle) RemoveRights(removeRights []types.OpenApiReference) error {
   151  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
   152  	return removeRightsFromRole(rb.client, "RightsBundle", rb.RightsBundle.Name, rb.RightsBundle.Id, endpoint, removeRights, nil)
   153  }
   154  
   155  // RemoveAllRights removes all rights from a rights bundle
   156  func (rb *RightsBundle) RemoveAllRights() error {
   157  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
   158  	return removeAllRightsFromRole(rb.client, "RightsBundle", rb.RightsBundle.Name, rb.RightsBundle.Id, endpoint, nil)
   159  }
   160  
   161  // PublishTenants publishes a rights bundle to one or more tenants
   162  func (rb *RightsBundle) PublishTenants(tenants []types.OpenApiReference) error {
   163  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
   164  	return publishContainerToTenants(rb.client, "RightsBundle", rb.RightsBundle.Name, rb.RightsBundle.Id, endpoint, tenants, "add")
   165  }
   166  
   167  // UnpublishTenants removes publication status in rights bundle from one or more tenants
   168  func (rb *RightsBundle) UnpublishTenants(tenants []types.OpenApiReference) error {
   169  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
   170  	return publishContainerToTenants(rb.client, "RightsBundle", rb.RightsBundle.Name, rb.RightsBundle.Id, endpoint, tenants, "remove")
   171  }
   172  
   173  // ReplacePublishedTenants publishes a rights bundle to one or more tenants, removing the tenants already present
   174  func (rb *RightsBundle) ReplacePublishedTenants(tenants []types.OpenApiReference) error {
   175  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
   176  	return publishContainerToTenants(rb.client, "RightsBundle", rb.RightsBundle.Name, rb.RightsBundle.Id, endpoint, tenants, "replace")
   177  }
   178  
   179  // PublishAllTenants removes publication status in rights bundle from one or more tenants
   180  func (rb *RightsBundle) PublishAllTenants() error {
   181  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
   182  	return publishContainerToAllTenants(rb.client, "RightsBundle", rb.RightsBundle.Name, rb.RightsBundle.Id, endpoint, true)
   183  }
   184  
   185  // UnpublishAllTenants removes publication status in rights bundle from one or more tenants
   186  func (rb *RightsBundle) UnpublishAllTenants() error {
   187  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
   188  	return publishContainerToAllTenants(rb.client, "RightsBundle", rb.RightsBundle.Name, rb.RightsBundle.Id, endpoint, false)
   189  }
   190  
   191  // GetRightsBundleByName retrieves rights bundle by given name
   192  func (client *Client) GetRightsBundleByName(name string) (*RightsBundle, error) {
   193  	queryParams := url.Values{}
   194  	queryParams.Add("filter", "name=="+name)
   195  	rightsBundles, err := client.GetAllRightsBundles(queryParams)
   196  	if err != nil {
   197  		return nil, err
   198  	}
   199  	if len(rightsBundles) == 0 {
   200  		return nil, ErrorEntityNotFound
   201  	}
   202  	if len(rightsBundles) > 1 {
   203  		return nil, fmt.Errorf("more than one rights bundle found with name '%s'", name)
   204  	}
   205  	return rightsBundles[0], nil
   206  }
   207  
   208  // GetRightsBundleById retrieves rights bundle by given ID
   209  func (client *Client) GetRightsBundleById(id string) (*RightsBundle, error) {
   210  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
   211  	minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
   212  	if err != nil {
   213  		return nil, err
   214  	}
   215  
   216  	if id == "" {
   217  		return nil, fmt.Errorf("empty rights bundle id")
   218  	}
   219  
   220  	urlRef, err := client.OpenApiBuildEndpoint(endpoint, id)
   221  	if err != nil {
   222  		return nil, err
   223  	}
   224  
   225  	rightsBundle := &RightsBundle{
   226  		RightsBundle: &types.RightsBundle{},
   227  		client:       client,
   228  	}
   229  
   230  	err = client.OpenApiGetItem(minimumApiVersion, urlRef, nil, rightsBundle.RightsBundle, nil)
   231  	if err != nil {
   232  		return nil, err
   233  	}
   234  
   235  	return rightsBundle, nil
   236  }
   237  
   238  // Delete deletes rights bundle
   239  func (rb *RightsBundle) Delete() error {
   240  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRightsBundles
   241  	minimumApiVersion, err := rb.client.checkOpenApiEndpointCompatibility(endpoint)
   242  	if err != nil {
   243  		return err
   244  	}
   245  
   246  	if rb.RightsBundle.Id == "" {
   247  		return fmt.Errorf("cannot delete rights bundle without id")
   248  	}
   249  
   250  	urlRef, err := rb.client.OpenApiBuildEndpoint(endpoint, rb.RightsBundle.Id)
   251  	if err != nil {
   252  		return err
   253  	}
   254  
   255  	err = rb.client.OpenApiDeleteItem(minimumApiVersion, urlRef, nil, nil)
   256  
   257  	if err != nil {
   258  		return fmt.Errorf("error deleting rights bundle: %s", err)
   259  	}
   260  
   261  	return nil
   262  }