github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/api/cloudcontroller/ccv2/service_instance.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
     9  )
    10  
    11  // ServiceInstanceType is the type of the Service Instance.
    12  type ServiceInstanceType string
    13  
    14  const (
    15  	// UserProvidedService is a Service Instance that is created by a user.
    16  	UserProvidedService ServiceInstanceType = "user_provided_service_instance"
    17  
    18  	// ManagedService is a Service Instance that is managed by a service broker.
    19  	ManagedService ServiceInstanceType = "managed_service_instance"
    20  )
    21  
    22  // ServiceInstance represents a Cloud Controller Service Instance.
    23  type ServiceInstance struct {
    24  	GUID      string
    25  	Name      string
    26  	SpaceGUID string
    27  	Type      ServiceInstanceType
    28  }
    29  
    30  // UnmarshalJSON helps unmarshal a Cloud Controller Service Instance response.
    31  func (serviceInstance *ServiceInstance) UnmarshalJSON(data []byte) error {
    32  	var ccServiceInstance struct {
    33  		Metadata internal.Metadata
    34  		Entity   struct {
    35  			Name      string `json:"name"`
    36  			SpaceGUID string `json:"space_guid"`
    37  			Type      string `json:"type"`
    38  		}
    39  	}
    40  	err := json.Unmarshal(data, &ccServiceInstance)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	serviceInstance.GUID = ccServiceInstance.Metadata.GUID
    46  	serviceInstance.Name = ccServiceInstance.Entity.Name
    47  	serviceInstance.SpaceGUID = ccServiceInstance.Entity.SpaceGUID
    48  	serviceInstance.Type = ServiceInstanceType(ccServiceInstance.Entity.Type)
    49  	return nil
    50  }
    51  
    52  // UserProvided returns true if the Service Instance is a user provided
    53  // service.
    54  func (serviceInstance ServiceInstance) UserProvided() bool {
    55  	return serviceInstance.Type == UserProvidedService
    56  }
    57  
    58  // Managed returns true if the Service Instance is a managed service.
    59  func (serviceInstance ServiceInstance) Managed() bool {
    60  	return serviceInstance.Type == ManagedService
    61  }
    62  
    63  // GetServiceInstance returns the service instance with the given GUID. This
    64  // service can be either a managed or user provided.
    65  func (client *Client) GetServiceInstance(serviceInstanceGUID string) (ServiceInstance, Warnings, error) {
    66  	request, err := client.newHTTPRequest(requestOptions{
    67  		RequestName: internal.GetServiceInstanceRequest,
    68  		URIParams:   Params{"service_instance_guid": serviceInstanceGUID},
    69  	})
    70  	if err != nil {
    71  		return ServiceInstance{}, nil, err
    72  	}
    73  
    74  	var serviceInstance ServiceInstance
    75  	response := cloudcontroller.Response{
    76  		Result: &serviceInstance,
    77  	}
    78  
    79  	err = client.connection.Make(request, &response)
    80  	return serviceInstance, response.Warnings, err
    81  }
    82  
    83  // GetServiceInstances returns back a list of *managed* Service Instances based
    84  // off of the provided queries.
    85  func (client *Client) GetServiceInstances(queries ...Query) ([]ServiceInstance, Warnings, error) {
    86  	request, err := client.newHTTPRequest(requestOptions{
    87  		RequestName: internal.GetServiceInstancesRequest,
    88  		Query:       FormatQueryParameters(queries),
    89  	})
    90  	if err != nil {
    91  		return nil, nil, err
    92  	}
    93  
    94  	var fullInstancesList []ServiceInstance
    95  	warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error {
    96  		if instance, ok := item.(ServiceInstance); ok {
    97  			fullInstancesList = append(fullInstancesList, instance)
    98  		} else {
    99  			return ccerror.UnknownObjectInListError{
   100  				Expected:   ServiceInstance{},
   101  				Unexpected: item,
   102  			}
   103  		}
   104  		return nil
   105  	})
   106  
   107  	return fullInstancesList, warnings, err
   108  }
   109  
   110  // GetSpaceServiceInstances returns back a list of Service Instances based off
   111  // of the space and queries provided. User provided services will be included
   112  // if includeUserProvidedServices is set to true.
   113  func (client *Client) GetSpaceServiceInstances(spaceGUID string, includeUserProvidedServices bool, queries ...Query) ([]ServiceInstance, Warnings, error) {
   114  	query := FormatQueryParameters(queries)
   115  
   116  	if includeUserProvidedServices {
   117  		query.Add("return_user_provided_service_instances", "true")
   118  	}
   119  
   120  	request, err := client.newHTTPRequest(requestOptions{
   121  		RequestName: internal.GetSpaceServiceInstancesRequest,
   122  		URIParams:   map[string]string{"guid": spaceGUID},
   123  		Query:       query,
   124  	})
   125  	if err != nil {
   126  		return nil, nil, err
   127  	}
   128  
   129  	var fullInstancesList []ServiceInstance
   130  	warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error {
   131  		if instance, ok := item.(ServiceInstance); ok {
   132  			fullInstancesList = append(fullInstancesList, instance)
   133  		} else {
   134  			return ccerror.UnknownObjectInListError{
   135  				Expected:   ServiceInstance{},
   136  				Unexpected: item,
   137  			}
   138  		}
   139  		return nil
   140  	})
   141  
   142  	return fullInstancesList, warnings, err
   143  }