github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+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/ccv2/internal" 8 ) 9 10 // ServiceInstanceType is the type of the Service Instance. 11 type ServiceInstanceType string 12 13 const ( 14 // UserProvidedService is a Service Instance that is created by a user. 15 UserProvidedService ServiceInstanceType = "user_provided_service_instance" 16 17 // ManagedService is a Service Instance that is managed by a service broker. 18 ManagedService ServiceInstanceType = "managed_service_instance" 19 ) 20 21 // ServiceInstance represents a Cloud Controller Service Instance. 22 type ServiceInstance struct { 23 GUID string 24 Name string 25 Type ServiceInstanceType 26 } 27 28 // UnmarshalJSON helps unmarshal a Cloud Controller Service Instance response. 29 func (serviceInstance *ServiceInstance) UnmarshalJSON(data []byte) error { 30 var ccServiceInstance struct { 31 Metadata internal.Metadata 32 Entity struct { 33 Name string 34 Type string 35 } 36 } 37 err := json.Unmarshal(data, &ccServiceInstance) 38 if err != nil { 39 return err 40 } 41 42 serviceInstance.GUID = ccServiceInstance.Metadata.GUID 43 serviceInstance.Name = ccServiceInstance.Entity.Name 44 serviceInstance.Type = ServiceInstanceType(ccServiceInstance.Entity.Type) 45 return nil 46 } 47 48 // UserProvidedService returns true if the Service Instance is a user provided 49 // service. 50 func (serviceInstance ServiceInstance) UserProvided() bool { 51 return serviceInstance.Type == UserProvidedService 52 } 53 54 // Managed returns true if the Service Instance is a managed service. 55 func (serviceInstance ServiceInstance) Managed() bool { 56 return serviceInstance.Type == ManagedService 57 } 58 59 // GetServiceInstances returns back a list of *managed* Service Instances based 60 // off of the provided queries. 61 func (client *Client) GetServiceInstances(queries []Query) ([]ServiceInstance, Warnings, error) { 62 request, err := client.newHTTPRequest(requestOptions{ 63 RequestName: internal.ServiceInstancesRequest, 64 Query: FormatQueryParameters(queries), 65 }) 66 if err != nil { 67 return nil, nil, err 68 } 69 70 var fullInstancesList []ServiceInstance 71 warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error { 72 if instance, ok := item.(ServiceInstance); ok { 73 fullInstancesList = append(fullInstancesList, instance) 74 } else { 75 return cloudcontroller.UnknownObjectInListError{ 76 Expected: ServiceInstance{}, 77 Unexpected: item, 78 } 79 } 80 return nil 81 }) 82 83 return fullInstancesList, warnings, err 84 } 85 86 // GetSpaceServiceInstances returns back a list of Service Instances based off 87 // of the space and queries provided. User provided services will be included 88 // if includeUserProvidedServices is set to true. 89 func (client *Client) GetSpaceServiceInstances(spaceGUID string, includeUserProvidedServices bool, queries []Query) ([]ServiceInstance, Warnings, error) { 90 query := FormatQueryParameters(queries) 91 92 if includeUserProvidedServices { 93 query.Add("return_user_provided_service_instances", "true") 94 } 95 96 request, err := client.newHTTPRequest(requestOptions{ 97 RequestName: internal.SpaceServiceInstancesRequest, 98 URIParams: map[string]string{"guid": spaceGUID}, 99 Query: query, 100 }) 101 if err != nil { 102 return nil, nil, err 103 } 104 105 var fullInstancesList []ServiceInstance 106 warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error { 107 if instance, ok := item.(ServiceInstance); ok { 108 fullInstancesList = append(fullInstancesList, instance) 109 } else { 110 return cloudcontroller.UnknownObjectInListError{ 111 Expected: ServiceInstance{}, 112 Unexpected: item, 113 } 114 } 115 return nil 116 }) 117 118 return fullInstancesList, warnings, err 119 }