github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/actor/v2action/service_instance.go (about)

     1  package v2action
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccerror"
     7  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccv2"
     8  )
     9  
    10  // ServiceInstance represents an instance of a service.
    11  type ServiceInstance ccv2.ServiceInstance
    12  
    13  type ServiceInstanceNotFoundError struct {
    14  	GUID string
    15  	Name string
    16  }
    17  
    18  func (e ServiceInstanceNotFoundError) Error() string {
    19  	if e.Name == "" {
    20  		return fmt.Sprintf("Service instance (GUID: %s) not found.", e.GUID)
    21  	}
    22  	return fmt.Sprintf("Service instance '%s' not found.", e.Name)
    23  }
    24  
    25  func (actor Actor) GetServiceInstance(guid string) (ServiceInstance, Warnings, error) {
    26  	instance, warnings, err := actor.CloudControllerClient.GetServiceInstance(guid)
    27  	if _, ok := err.(ccerror.ResourceNotFoundError); ok {
    28  		return ServiceInstance{}, Warnings(warnings), ServiceInstanceNotFoundError{GUID: guid}
    29  	}
    30  	return ServiceInstance(instance), Warnings(warnings), err
    31  }
    32  
    33  func (actor Actor) GetServiceInstanceByNameAndSpace(name string, spaceGUID string) (ServiceInstance, Warnings, error) {
    34  	serviceInstances, warnings, err := actor.CloudControllerClient.GetSpaceServiceInstances(
    35  		spaceGUID,
    36  		true,
    37  		ccv2.Query{
    38  			Filter:   ccv2.NameFilter,
    39  			Operator: ccv2.EqualOperator,
    40  			Values:   []string{name},
    41  		})
    42  
    43  	if err != nil {
    44  		return ServiceInstance{}, Warnings(warnings), err
    45  	}
    46  
    47  	if len(serviceInstances) == 0 {
    48  		return ServiceInstance{}, Warnings(warnings), ServiceInstanceNotFoundError{
    49  			Name: name,
    50  		}
    51  	}
    52  
    53  	return ServiceInstance(serviceInstances[0]), Warnings(warnings), nil
    54  }
    55  
    56  func (actor Actor) GetServiceInstancesByApplication(appGUID string) ([]ServiceInstance, Warnings, error) {
    57  	var allWarnings Warnings
    58  	bindings, apiWarnings, err := actor.CloudControllerClient.GetServiceBindings(ccv2.Query{
    59  		Filter:   ccv2.AppGUIDFilter,
    60  		Operator: ccv2.EqualOperator,
    61  		Values:   []string{appGUID},
    62  	})
    63  	allWarnings = append(allWarnings, apiWarnings...)
    64  
    65  	if err != nil {
    66  		return nil, allWarnings, err
    67  	}
    68  
    69  	var serviceInstances []ServiceInstance
    70  	for _, binding := range bindings {
    71  		instance, warnings, instanceErr := actor.GetServiceInstance(binding.ServiceInstanceGUID)
    72  		allWarnings = append(allWarnings, warnings...)
    73  		if instanceErr != nil {
    74  			return nil, allWarnings, instanceErr
    75  		}
    76  		serviceInstances = append(serviceInstances, ServiceInstance(instance))
    77  	}
    78  
    79  	return serviceInstances, allWarnings, err
    80  }
    81  
    82  func (actor Actor) GetServiceInstancesBySpace(spaceGUID string) ([]ServiceInstance, Warnings, error) {
    83  	ccv2ServiceInstances, warnings, err := actor.CloudControllerClient.GetSpaceServiceInstances(spaceGUID, true)
    84  
    85  	if err != nil {
    86  		return []ServiceInstance{}, Warnings(warnings), err
    87  	}
    88  
    89  	serviceInstances := make([]ServiceInstance, len(ccv2ServiceInstances))
    90  	for i, ccv2ServiceInstance := range ccv2ServiceInstances {
    91  		serviceInstances[i] = ServiceInstance(ccv2ServiceInstance)
    92  	}
    93  
    94  	return serviceInstances, Warnings(warnings), nil
    95  }