github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/actor/v2action/service_instance.go (about)

     1  package v2action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     8  )
     9  
    10  // ServiceInstance represents an instance of a service.
    11  type ServiceInstance ccv2.ServiceInstance
    12  
    13  // CreateServiceInstance creates a new service instance with the provided attributes.
    14  func (actor Actor) CreateServiceInstance(spaceGUID, serviceName, servicePlanName, serviceInstanceName, brokerName string, params map[string]interface{}, tags []string) (ServiceInstance, Warnings, error) {
    15  	var allWarnings Warnings
    16  	plan, allWarnings, err := actor.getServicePlanForServiceInSpace(servicePlanName, serviceName, spaceGUID, brokerName)
    17  
    18  	if err != nil {
    19  		return ServiceInstance{}, allWarnings, err
    20  	}
    21  
    22  	instance, warnings, err := actor.CloudControllerClient.CreateServiceInstance(spaceGUID, plan.GUID, serviceInstanceName, params, tags)
    23  	allWarnings = append(allWarnings, warnings...)
    24  	if err != nil {
    25  		return ServiceInstance{}, allWarnings, err
    26  	}
    27  
    28  	return ServiceInstance(instance), allWarnings, nil
    29  }
    30  
    31  func (actor Actor) GetServiceInstance(guid string) (ServiceInstance, Warnings, error) {
    32  	instance, warnings, err := actor.CloudControllerClient.GetServiceInstance(guid)
    33  	if _, ok := err.(ccerror.ResourceNotFoundError); ok {
    34  		return ServiceInstance{}, Warnings(warnings), actionerror.ServiceInstanceNotFoundError{GUID: guid}
    35  	}
    36  
    37  	return ServiceInstance(instance), Warnings(warnings), err
    38  }
    39  
    40  func (actor Actor) GetServiceInstanceByNameAndSpace(name string, spaceGUID string) (ServiceInstance, Warnings, error) {
    41  	serviceInstances, warnings, err := actor.CloudControllerClient.GetSpaceServiceInstances(
    42  		spaceGUID,
    43  		true,
    44  		ccv2.Filter{
    45  			Type:     constant.NameFilter,
    46  			Operator: constant.EqualOperator,
    47  			Values:   []string{name},
    48  		})
    49  
    50  	if err != nil {
    51  		return ServiceInstance{}, Warnings(warnings), err
    52  	}
    53  
    54  	if len(serviceInstances) == 0 {
    55  		return ServiceInstance{}, Warnings(warnings), actionerror.ServiceInstanceNotFoundError{
    56  			Name: name,
    57  		}
    58  	}
    59  
    60  	return ServiceInstance(serviceInstances[0]), Warnings(warnings), nil
    61  }
    62  
    63  func (actor Actor) GetServiceInstancesByApplication(appGUID string) ([]ServiceInstance, Warnings, error) {
    64  	var allWarnings Warnings
    65  	bindings, apiWarnings, err := actor.CloudControllerClient.GetServiceBindings(ccv2.Filter{
    66  		Type:     constant.AppGUIDFilter,
    67  		Operator: constant.EqualOperator,
    68  		Values:   []string{appGUID},
    69  	})
    70  	allWarnings = append(allWarnings, apiWarnings...)
    71  
    72  	if err != nil {
    73  		return nil, allWarnings, err
    74  	}
    75  
    76  	var serviceInstances []ServiceInstance
    77  	for _, binding := range bindings {
    78  		instance, warnings, instanceErr := actor.GetServiceInstance(binding.ServiceInstanceGUID)
    79  		allWarnings = append(allWarnings, warnings...)
    80  		if instanceErr != nil {
    81  			return nil, allWarnings, instanceErr
    82  		}
    83  		serviceInstances = append(serviceInstances, ServiceInstance(instance))
    84  	}
    85  
    86  	return serviceInstances, allWarnings, err
    87  }
    88  
    89  func (actor Actor) GetServiceInstancesBySpace(spaceGUID string) ([]ServiceInstance, Warnings, error) {
    90  	ccv2ServiceInstances, warnings, err := actor.CloudControllerClient.GetSpaceServiceInstances(spaceGUID, true)
    91  
    92  	if err != nil {
    93  		return []ServiceInstance{}, Warnings(warnings), err
    94  	}
    95  
    96  	serviceInstances := make([]ServiceInstance, len(ccv2ServiceInstances))
    97  	for i, ccv2ServiceInstance := range ccv2ServiceInstances {
    98  		serviceInstances[i] = ServiceInstance(ccv2ServiceInstance)
    99  	}
   100  
   101  	return serviceInstances, Warnings(warnings), nil
   102  }
   103  
   104  // IsManaged returns true if the service instance is managed, othersise false.
   105  func (instance ServiceInstance) IsManaged() bool {
   106  	return ccv2.ServiceInstance(instance).Managed()
   107  }
   108  
   109  // IsUserProvided returns true if the service instance is user provided, othersise false.
   110  func (instance ServiceInstance) IsUserProvided() bool {
   111  	return ccv2.ServiceInstance(instance).UserProvided()
   112  }