github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/v7action/service_instance_sharing.go (about)

     1  package v7action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     5  	"code.cloudfoundry.org/cli/resources"
     6  	"code.cloudfoundry.org/cli/types"
     7  	"code.cloudfoundry.org/cli/util/railway"
     8  )
     9  
    10  type ServiceInstanceSharingParams struct {
    11  	SpaceName string
    12  	OrgName   types.OptionalString
    13  }
    14  
    15  func (actor Actor) ShareServiceInstanceToSpaceAndOrg(
    16  	serviceInstanceName, targetedSpaceGUID, targetedOrgGUID string,
    17  	sharedToDetails ServiceInstanceSharingParams,
    18  ) (Warnings, error) {
    19  	var serviceInstance resources.ServiceInstance
    20  	var shareSpace resources.Space
    21  
    22  	return handleServiceInstanceErrors(railway.Sequentially(
    23  		func() (warnings ccv3.Warnings, err error) {
    24  			serviceInstance, shareSpace, warnings, err = actor.validateSharingDetails(serviceInstanceName, targetedSpaceGUID, targetedOrgGUID, sharedToDetails)
    25  			return
    26  		},
    27  		func() (warnings ccv3.Warnings, err error) {
    28  			_, warnings, err = actor.CloudControllerClient.ShareServiceInstanceToSpaces(serviceInstance.GUID, []string{shareSpace.GUID})
    29  			return
    30  		},
    31  	))
    32  }
    33  
    34  func (actor Actor) UnshareServiceInstanceFromSpaceAndOrg(
    35  	serviceInstanceName, targetedSpaceGUID, targetedOrgGUID string,
    36  	sharedToDetails ServiceInstanceSharingParams,
    37  ) (Warnings, error) {
    38  	var serviceInstance resources.ServiceInstance
    39  	var unshareSpace resources.Space
    40  
    41  	return handleServiceInstanceErrors(railway.Sequentially(
    42  		func() (warnings ccv3.Warnings, err error) {
    43  			serviceInstance, unshareSpace, warnings, err = actor.validateSharingDetails(
    44  				serviceInstanceName,
    45  				targetedSpaceGUID,
    46  				targetedOrgGUID,
    47  				sharedToDetails,
    48  			)
    49  			return
    50  		},
    51  		func() (warnings ccv3.Warnings, err error) {
    52  			warnings, err = actor.CloudControllerClient.UnshareServiceInstanceFromSpace(serviceInstance.GUID, unshareSpace.GUID)
    53  			return
    54  		},
    55  	))
    56  }
    57  
    58  func (actor Actor) validateSharingDetails(
    59  	serviceInstanceName, targetedSpaceGUID, targetedOrgGUID string,
    60  	sharedToDetails ServiceInstanceSharingParams,
    61  ) (resources.ServiceInstance, resources.Space, ccv3.Warnings, error) {
    62  	var serviceInstance resources.ServiceInstance
    63  	var shareSpace resources.Space
    64  	var shareToOrgGUID = targetedOrgGUID
    65  
    66  	warnings, err := railway.Sequentially(
    67  		func() (warnings ccv3.Warnings, err error) {
    68  			serviceInstance, _, warnings, err = actor.CloudControllerClient.GetServiceInstanceByNameAndSpace(serviceInstanceName, targetedSpaceGUID)
    69  			return
    70  		},
    71  		func() (warnings ccv3.Warnings, err error) {
    72  			if sharedToDetails.OrgName.IsSet {
    73  				var (
    74  					orgWarnings  Warnings
    75  					organization resources.Organization
    76  				)
    77  
    78  				organization, orgWarnings, err = actor.GetOrganizationByName(sharedToDetails.OrgName.Value)
    79  				warnings = ccv3.Warnings(orgWarnings)
    80  				shareToOrgGUID = organization.GUID
    81  			}
    82  			return
    83  		},
    84  		func() (warnings ccv3.Warnings, err error) {
    85  			var spaceWarnings Warnings
    86  			shareSpace, spaceWarnings, err = actor.GetSpaceByNameAndOrganization(sharedToDetails.SpaceName, shareToOrgGUID)
    87  			warnings = ccv3.Warnings(spaceWarnings)
    88  			return
    89  		},
    90  	)
    91  
    92  	if err != nil {
    93  		return resources.ServiceInstance{}, resources.Space{}, warnings, err
    94  	}
    95  
    96  	return serviceInstance, shareSpace, warnings, nil
    97  }