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