github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/api/cloudcontroller/ccv2/service_instance.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
     8  )
     9  
    10  // ServiceInstance represents a Cloud Controller Service Instance.
    11  type ServiceInstance struct {
    12  	// GUID is the unique service instance identifier.
    13  	GUID string
    14  
    15  	// Name is the name given to the service instance.
    16  	Name string
    17  
    18  	// SpaceGUID is the unique identifier of the space that this service instance
    19  	// belongs to.
    20  	SpaceGUID string
    21  
    22  	// ServiceGUID is the unique identifier of the service that this service
    23  	// instance belongs to.
    24  	ServiceGUID string
    25  
    26  	// ServicePlanGUID is the unique identifier of the service plan that this
    27  	// service instance belongs to.
    28  	ServicePlanGUID string
    29  
    30  	// Type is the type of service instance.
    31  	Type constant.ServiceInstanceType
    32  
    33  	// Tags is a list of all tags for the service instance.
    34  	Tags []string
    35  
    36  	// DashboardURL is the service-broker provided URL to access administrative
    37  	// features of the service instance.
    38  	DashboardURL string
    39  
    40  	// LastOperation is the status of the last operation requested on the service
    41  	// instance.
    42  	LastOperation LastOperation
    43  }
    44  
    45  // Managed returns true if the Service Instance is a managed service.
    46  func (serviceInstance ServiceInstance) Managed() bool {
    47  	return serviceInstance.Type == constant.ServiceInstanceTypeManagedService
    48  }
    49  
    50  // UnmarshalJSON helps unmarshal a Cloud Controller Service Instance response.
    51  func (serviceInstance *ServiceInstance) UnmarshalJSON(data []byte) error {
    52  	var ccServiceInstance struct {
    53  		Metadata internal.Metadata
    54  		Entity   struct {
    55  			Name            string        `json:"name"`
    56  			SpaceGUID       string        `json:"space_guid"`
    57  			ServiceGUID     string        `json:"service_guid"`
    58  			ServicePlanGUID string        `json:"service_plan_guid"`
    59  			Type            string        `json:"type"`
    60  			Tags            []string      `json:"tags"`
    61  			DashboardURL    string        `json:"dashboard_url"`
    62  			LastOperation   LastOperation `json:"last_operation"`
    63  		}
    64  	}
    65  	err := cloudcontroller.DecodeJSON(data, &ccServiceInstance)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	serviceInstance.GUID = ccServiceInstance.Metadata.GUID
    71  	serviceInstance.Name = ccServiceInstance.Entity.Name
    72  	serviceInstance.SpaceGUID = ccServiceInstance.Entity.SpaceGUID
    73  	serviceInstance.ServiceGUID = ccServiceInstance.Entity.ServiceGUID
    74  	serviceInstance.ServicePlanGUID = ccServiceInstance.Entity.ServicePlanGUID
    75  	serviceInstance.Type = constant.ServiceInstanceType(ccServiceInstance.Entity.Type)
    76  	serviceInstance.Tags = ccServiceInstance.Entity.Tags
    77  	serviceInstance.DashboardURL = ccServiceInstance.Entity.DashboardURL
    78  	serviceInstance.LastOperation = ccServiceInstance.Entity.LastOperation
    79  	return nil
    80  }
    81  
    82  // UserProvided returns true if the Service Instance is a user provided
    83  // service.
    84  func (serviceInstance ServiceInstance) UserProvided() bool {
    85  	return serviceInstance.Type == constant.ServiceInstanceTypeUserProvidedService
    86  }
    87  
    88  // GetServiceInstance returns the service instance with the given GUID. This
    89  // service can be either a managed or user provided.
    90  func (client *Client) GetServiceInstance(serviceInstanceGUID string) (ServiceInstance, Warnings, error) {
    91  	request, err := client.newHTTPRequest(requestOptions{
    92  		RequestName: internal.GetServiceInstanceRequest,
    93  		URIParams:   Params{"service_instance_guid": serviceInstanceGUID},
    94  	})
    95  	if err != nil {
    96  		return ServiceInstance{}, nil, err
    97  	}
    98  
    99  	var serviceInstance ServiceInstance
   100  	response := cloudcontroller.Response{
   101  		Result: &serviceInstance,
   102  	}
   103  
   104  	err = client.connection.Make(request, &response)
   105  	return serviceInstance, response.Warnings, err
   106  }
   107  
   108  // GetServiceInstances returns back a list of *managed* Service Instances based
   109  // off of the provided filters.
   110  func (client *Client) GetServiceInstances(filters ...Filter) ([]ServiceInstance, Warnings, error) {
   111  	request, err := client.newHTTPRequest(requestOptions{
   112  		RequestName: internal.GetServiceInstancesRequest,
   113  		Query:       ConvertFilterParameters(filters),
   114  	})
   115  	if err != nil {
   116  		return nil, nil, err
   117  	}
   118  
   119  	var fullInstancesList []ServiceInstance
   120  	warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error {
   121  		if instance, ok := item.(ServiceInstance); ok {
   122  			fullInstancesList = append(fullInstancesList, instance)
   123  		} else {
   124  			return ccerror.UnknownObjectInListError{
   125  				Expected:   ServiceInstance{},
   126  				Unexpected: item,
   127  			}
   128  		}
   129  		return nil
   130  	})
   131  
   132  	return fullInstancesList, warnings, err
   133  }
   134  
   135  // GetSpaceServiceInstances returns back a list of Service Instances based off
   136  // of the space and filters provided. User provided services will be included
   137  // if includeUserProvidedServices is set to true.
   138  func (client *Client) GetSpaceServiceInstances(spaceGUID string, includeUserProvidedServices bool, filters ...Filter) ([]ServiceInstance, Warnings, error) {
   139  	query := ConvertFilterParameters(filters)
   140  
   141  	if includeUserProvidedServices {
   142  		query.Add("return_user_provided_service_instances", "true")
   143  	}
   144  
   145  	request, err := client.newHTTPRequest(requestOptions{
   146  		RequestName: internal.GetSpaceServiceInstancesRequest,
   147  		URIParams:   map[string]string{"guid": spaceGUID},
   148  		Query:       query,
   149  	})
   150  	if err != nil {
   151  		return nil, nil, err
   152  	}
   153  
   154  	var fullInstancesList []ServiceInstance
   155  	warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error {
   156  		if instance, ok := item.(ServiceInstance); ok {
   157  			fullInstancesList = append(fullInstancesList, instance)
   158  		} else {
   159  			return ccerror.UnknownObjectInListError{
   160  				Expected:   ServiceInstance{},
   161  				Unexpected: item,
   162  			}
   163  		}
   164  		return nil
   165  	})
   166  
   167  	return fullInstancesList, warnings, err
   168  }
   169  
   170  // GetUserProvidedServiceInstances returns back a list of *user provided* Service Instances based
   171  // off the provided queries.
   172  func (client *Client) GetUserProvidedServiceInstances(filters ...Filter) ([]ServiceInstance, Warnings, error) {
   173  	request, err := client.newHTTPRequest(requestOptions{
   174  		RequestName: internal.GetUserProvidedServiceInstancesRequest,
   175  		Query:       ConvertFilterParameters(filters),
   176  	})
   177  	if err != nil {
   178  		return nil, nil, err
   179  	}
   180  
   181  	var fullInstancesList []ServiceInstance
   182  	warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error {
   183  		if instance, ok := item.(ServiceInstance); ok {
   184  			fullInstancesList = append(fullInstancesList, instance)
   185  		} else {
   186  			return ccerror.UnknownObjectInListError{
   187  				Expected:   ServiceInstance{},
   188  				Unexpected: item,
   189  			}
   190  		}
   191  		return nil
   192  	})
   193  
   194  	return fullInstancesList, warnings, err
   195  }