github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+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   struct {
    63  				Type        string `json:"type"`
    64  				State       string `json:"state"`
    65  				Description string `json:"description"`
    66  				UpdatedAt   string `json:"updated_at"`
    67  				CreatedAt   string `json:"created_at"`
    68  			} `json:"last_operation"`
    69  		}
    70  	}
    71  	err := cloudcontroller.DecodeJSON(data, &ccServiceInstance)
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	serviceInstance.GUID = ccServiceInstance.Metadata.GUID
    77  	serviceInstance.Name = ccServiceInstance.Entity.Name
    78  	serviceInstance.SpaceGUID = ccServiceInstance.Entity.SpaceGUID
    79  	serviceInstance.ServiceGUID = ccServiceInstance.Entity.ServiceGUID
    80  	serviceInstance.ServicePlanGUID = ccServiceInstance.Entity.ServicePlanGUID
    81  	serviceInstance.Type = constant.ServiceInstanceType(ccServiceInstance.Entity.Type)
    82  	serviceInstance.Tags = ccServiceInstance.Entity.Tags
    83  	serviceInstance.DashboardURL = ccServiceInstance.Entity.DashboardURL
    84  	serviceInstance.LastOperation = 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  		Result: &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  }