github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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  	// RouteServiceURL is the URL of the user-provided service to which requests
    41  	// for bound routes will be forwarded.
    42  	RouteServiceURL string
    43  
    44  	// LastOperation is the status of the last operation requested on the service
    45  	// instance.
    46  	LastOperation LastOperation
    47  }
    48  
    49  // Managed returns true if the Service Instance is a managed service.
    50  func (serviceInstance ServiceInstance) Managed() bool {
    51  	return serviceInstance.Type == constant.ServiceInstanceTypeManagedService
    52  }
    53  
    54  // UnmarshalJSON helps unmarshal a Cloud Controller Service Instance response.
    55  func (serviceInstance *ServiceInstance) UnmarshalJSON(data []byte) error {
    56  	var ccServiceInstance struct {
    57  		Metadata internal.Metadata
    58  		Entity   struct {
    59  			Name            string        `json:"name"`
    60  			SpaceGUID       string        `json:"space_guid"`
    61  			ServiceGUID     string        `json:"service_guid"`
    62  			ServicePlanGUID string        `json:"service_plan_guid"`
    63  			Type            string        `json:"type"`
    64  			Tags            []string      `json:"tags"`
    65  			DashboardURL    string        `json:"dashboard_url"`
    66  			RouteServiceURL string        `json:"route_service_url"`
    67  			LastOperation   LastOperation `json:"last_operation"`
    68  		}
    69  	}
    70  	err := cloudcontroller.DecodeJSON(data, &ccServiceInstance)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	serviceInstance.GUID = ccServiceInstance.Metadata.GUID
    76  	serviceInstance.Name = ccServiceInstance.Entity.Name
    77  	serviceInstance.SpaceGUID = ccServiceInstance.Entity.SpaceGUID
    78  	serviceInstance.ServiceGUID = ccServiceInstance.Entity.ServiceGUID
    79  	serviceInstance.ServicePlanGUID = ccServiceInstance.Entity.ServicePlanGUID
    80  	serviceInstance.Type = constant.ServiceInstanceType(ccServiceInstance.Entity.Type)
    81  	serviceInstance.Tags = ccServiceInstance.Entity.Tags
    82  	serviceInstance.DashboardURL = ccServiceInstance.Entity.DashboardURL
    83  	serviceInstance.RouteServiceURL = ccServiceInstance.Entity.RouteServiceURL
    84  	serviceInstance.LastOperation = ccServiceInstance.Entity.LastOperation
    85  	return nil
    86  }
    87  
    88  // UserProvided returns true if the Service Instance is a user provided
    89  // service.
    90  func (serviceInstance ServiceInstance) UserProvided() bool {
    91  	return serviceInstance.Type == constant.ServiceInstanceTypeUserProvidedService
    92  }
    93  
    94  // GetServiceInstance returns the service instance with the given GUID. This
    95  // service can be either a managed or user provided.
    96  func (client *Client) GetServiceInstance(serviceInstanceGUID string) (ServiceInstance, Warnings, error) {
    97  	request, err := client.newHTTPRequest(requestOptions{
    98  		RequestName: internal.GetServiceInstanceRequest,
    99  		URIParams:   Params{"service_instance_guid": serviceInstanceGUID},
   100  	})
   101  	if err != nil {
   102  		return ServiceInstance{}, nil, err
   103  	}
   104  
   105  	var serviceInstance ServiceInstance
   106  	response := cloudcontroller.Response{
   107  		DecodeJSONResponseInto: &serviceInstance,
   108  	}
   109  
   110  	err = client.connection.Make(request, &response)
   111  	return serviceInstance, response.Warnings, err
   112  }
   113  
   114  // GetServiceInstances returns back a list of *managed* Service Instances based
   115  // off of the provided filters.
   116  func (client *Client) GetServiceInstances(filters ...Filter) ([]ServiceInstance, Warnings, error) {
   117  	request, err := client.newHTTPRequest(requestOptions{
   118  		RequestName: internal.GetServiceInstancesRequest,
   119  		Query:       ConvertFilterParameters(filters),
   120  	})
   121  	if err != nil {
   122  		return nil, nil, err
   123  	}
   124  
   125  	var fullInstancesList []ServiceInstance
   126  	warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error {
   127  		if instance, ok := item.(ServiceInstance); ok {
   128  			fullInstancesList = append(fullInstancesList, instance)
   129  		} else {
   130  			return ccerror.UnknownObjectInListError{
   131  				Expected:   ServiceInstance{},
   132  				Unexpected: item,
   133  			}
   134  		}
   135  		return nil
   136  	})
   137  
   138  	return fullInstancesList, warnings, err
   139  }
   140  
   141  // GetSpaceServiceInstances returns back a list of Service Instances based off
   142  // of the space and filters provided. User provided services will be included
   143  // if includeUserProvidedServices is set to true.
   144  func (client *Client) GetSpaceServiceInstances(spaceGUID string, includeUserProvidedServices bool, filters ...Filter) ([]ServiceInstance, Warnings, error) {
   145  	query := ConvertFilterParameters(filters)
   146  
   147  	if includeUserProvidedServices {
   148  		query.Add("return_user_provided_service_instances", "true")
   149  	}
   150  
   151  	request, err := client.newHTTPRequest(requestOptions{
   152  		RequestName: internal.GetSpaceServiceInstancesRequest,
   153  		URIParams:   map[string]string{"guid": spaceGUID},
   154  		Query:       query,
   155  	})
   156  	if err != nil {
   157  		return nil, nil, err
   158  	}
   159  
   160  	var fullInstancesList []ServiceInstance
   161  	warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error {
   162  		if instance, ok := item.(ServiceInstance); ok {
   163  			fullInstancesList = append(fullInstancesList, instance)
   164  		} else {
   165  			return ccerror.UnknownObjectInListError{
   166  				Expected:   ServiceInstance{},
   167  				Unexpected: item,
   168  			}
   169  		}
   170  		return nil
   171  	})
   172  
   173  	return fullInstancesList, warnings, err
   174  }
   175  
   176  // GetUserProvidedServiceInstances returns back a list of *user provided* Service Instances based
   177  // off the provided queries.
   178  func (client *Client) GetUserProvidedServiceInstances(filters ...Filter) ([]ServiceInstance, Warnings, error) {
   179  	request, err := client.newHTTPRequest(requestOptions{
   180  		RequestName: internal.GetUserProvidedServiceInstancesRequest,
   181  		Query:       ConvertFilterParameters(filters),
   182  	})
   183  	if err != nil {
   184  		return nil, nil, err
   185  	}
   186  
   187  	var fullInstancesList []ServiceInstance
   188  	warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error {
   189  		if instance, ok := item.(ServiceInstance); ok {
   190  			fullInstancesList = append(fullInstancesList, instance)
   191  		} else {
   192  			return ccerror.UnknownObjectInListError{
   193  				Expected:   ServiceInstance{},
   194  				Unexpected: item,
   195  			}
   196  		}
   197  		return nil
   198  	})
   199  
   200  	return fullInstancesList, warnings, err
   201  }