github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/api/cloudcontroller/ccv2/service_instance.go (about) 1 package ccv2 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "net/url" 7 8 "code.cloudfoundry.org/cli/api/cloudcontroller" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 11 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 12 ) 13 14 // ServiceInstance represents a Cloud Controller Service Instance. 15 type ServiceInstance struct { 16 // GUID is the unique service instance identifier. 17 GUID string 18 19 // Name is the name given to the service instance. 20 Name string 21 22 // SpaceGUID is the unique identifier of the space that this service instance 23 // belongs to. 24 SpaceGUID string 25 26 // ServiceGUID is the unique identifier of the service that this service 27 // instance belongs to. 28 ServiceGUID string 29 30 // ServicePlanGUID is the unique identifier of the service plan that this 31 // service instance belongs to. 32 ServicePlanGUID string 33 34 // Type is the type of service instance. 35 Type constant.ServiceInstanceType 36 37 // Tags is a list of all tags for the service instance. 38 Tags []string 39 40 // DashboardURL is the service-broker provided URL to access administrative 41 // features of the service instance. 42 DashboardURL string 43 44 // RouteServiceURL is the URL of the user-provided service to which requests 45 // for bound routes will be forwarded. 46 RouteServiceURL string 47 48 // LastOperation is the status of the last operation requested on the service 49 // instance. 50 LastOperation LastOperation 51 } 52 53 // Managed returns true if the Service Instance is a managed service. 54 func (serviceInstance ServiceInstance) Managed() bool { 55 return serviceInstance.Type == constant.ServiceInstanceTypeManagedService 56 } 57 58 // UnmarshalJSON helps unmarshal a Cloud Controller Service Instance response. 59 func (serviceInstance *ServiceInstance) UnmarshalJSON(data []byte) error { 60 var ccServiceInstance struct { 61 Metadata internal.Metadata 62 Entity struct { 63 Name string `json:"name"` 64 SpaceGUID string `json:"space_guid"` 65 ServiceGUID string `json:"service_guid"` 66 ServicePlanGUID string `json:"service_plan_guid"` 67 Type string `json:"type"` 68 Tags []string `json:"tags"` 69 DashboardURL string `json:"dashboard_url"` 70 RouteServiceURL string `json:"route_service_url"` 71 LastOperation LastOperation `json:"last_operation"` 72 } 73 } 74 err := cloudcontroller.DecodeJSON(data, &ccServiceInstance) 75 if err != nil { 76 return err 77 } 78 79 serviceInstance.GUID = ccServiceInstance.Metadata.GUID 80 serviceInstance.Name = ccServiceInstance.Entity.Name 81 serviceInstance.SpaceGUID = ccServiceInstance.Entity.SpaceGUID 82 serviceInstance.ServiceGUID = ccServiceInstance.Entity.ServiceGUID 83 serviceInstance.ServicePlanGUID = ccServiceInstance.Entity.ServicePlanGUID 84 serviceInstance.Type = constant.ServiceInstanceType(ccServiceInstance.Entity.Type) 85 serviceInstance.Tags = ccServiceInstance.Entity.Tags 86 serviceInstance.DashboardURL = ccServiceInstance.Entity.DashboardURL 87 serviceInstance.RouteServiceURL = ccServiceInstance.Entity.RouteServiceURL 88 serviceInstance.LastOperation = ccServiceInstance.Entity.LastOperation 89 return nil 90 } 91 92 // UserProvided returns true if the Service Instance is a user provided 93 // service. 94 func (serviceInstance ServiceInstance) UserProvided() bool { 95 return serviceInstance.Type == constant.ServiceInstanceTypeUserProvidedService 96 } 97 98 type createServiceInstanceRequestBody struct { 99 Name string `json:"name"` 100 ServicePlanGUID string `json:"service_plan_guid"` 101 SpaceGUID string `json:"space_guid"` 102 Parameters map[string]interface{} `json:"parameters,omitempty"` 103 Tags []string `json:"tags,omitempty"` 104 } 105 106 // CreateServiceInstance posts a service instance resource with the provided 107 // attributes to the api and returns the result. 108 func (client *Client) CreateServiceInstance(spaceGUID, servicePlanGUID, serviceInstance string, parameters map[string]interface{}, tags []string) (ServiceInstance, Warnings, error) { 109 requestBody := createServiceInstanceRequestBody{ 110 Name: serviceInstance, 111 ServicePlanGUID: servicePlanGUID, 112 SpaceGUID: spaceGUID, 113 Parameters: parameters, 114 Tags: tags, 115 } 116 117 bodyBytes, err := json.Marshal(requestBody) 118 if err != nil { 119 return ServiceInstance{}, nil, err 120 } 121 122 request, err := client.newHTTPRequest(requestOptions{ 123 RequestName: internal.PostServiceInstancesRequest, 124 Body: bytes.NewReader(bodyBytes), 125 Query: url.Values{"accepts_incomplete": {"true"}}, 126 }) 127 if err != nil { 128 return ServiceInstance{}, nil, err 129 } 130 131 var instance ServiceInstance 132 response := cloudcontroller.Response{ 133 DecodeJSONResponseInto: &instance, 134 } 135 136 err = client.connection.Make(request, &response) 137 return instance, response.Warnings, err 138 } 139 140 // GetServiceInstance returns the service instance with the given GUID. This 141 // service can be either a managed or user provided. 142 func (client *Client) GetServiceInstance(serviceInstanceGUID string) (ServiceInstance, Warnings, error) { 143 request, err := client.newHTTPRequest(requestOptions{ 144 RequestName: internal.GetServiceInstanceRequest, 145 URIParams: Params{"service_instance_guid": serviceInstanceGUID}, 146 }) 147 if err != nil { 148 return ServiceInstance{}, nil, err 149 } 150 151 var serviceInstance ServiceInstance 152 response := cloudcontroller.Response{ 153 DecodeJSONResponseInto: &serviceInstance, 154 } 155 156 err = client.connection.Make(request, &response) 157 return serviceInstance, response.Warnings, err 158 } 159 160 // GetServiceInstances returns back a list of *managed* Service Instances based 161 // off of the provided filters. 162 func (client *Client) GetServiceInstances(filters ...Filter) ([]ServiceInstance, Warnings, error) { 163 request, err := client.newHTTPRequest(requestOptions{ 164 RequestName: internal.GetServiceInstancesRequest, 165 Query: ConvertFilterParameters(filters), 166 }) 167 if err != nil { 168 return nil, nil, err 169 } 170 171 var fullInstancesList []ServiceInstance 172 warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error { 173 if instance, ok := item.(ServiceInstance); ok { 174 fullInstancesList = append(fullInstancesList, instance) 175 } else { 176 return ccerror.UnknownObjectInListError{ 177 Expected: ServiceInstance{}, 178 Unexpected: item, 179 } 180 } 181 return nil 182 }) 183 184 return fullInstancesList, warnings, err 185 } 186 187 // GetSpaceServiceInstances returns back a list of Service Instances based off 188 // of the space and filters provided. User provided services will be included 189 // if includeUserProvidedServices is set to true. 190 func (client *Client) GetSpaceServiceInstances(spaceGUID string, includeUserProvidedServices bool, filters ...Filter) ([]ServiceInstance, Warnings, error) { 191 query := ConvertFilterParameters(filters) 192 193 if includeUserProvidedServices { 194 query.Add("return_user_provided_service_instances", "true") 195 } 196 197 request, err := client.newHTTPRequest(requestOptions{ 198 RequestName: internal.GetSpaceServiceInstancesRequest, 199 URIParams: map[string]string{"guid": spaceGUID}, 200 Query: query, 201 }) 202 if err != nil { 203 return nil, nil, err 204 } 205 206 var fullInstancesList []ServiceInstance 207 warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error { 208 if instance, ok := item.(ServiceInstance); ok { 209 fullInstancesList = append(fullInstancesList, instance) 210 } else { 211 return ccerror.UnknownObjectInListError{ 212 Expected: ServiceInstance{}, 213 Unexpected: item, 214 } 215 } 216 return nil 217 }) 218 219 return fullInstancesList, warnings, err 220 } 221 222 // GetUserProvidedServiceInstances returns back a list of *user provided* Service Instances based 223 // off the provided queries. 224 func (client *Client) GetUserProvidedServiceInstances(filters ...Filter) ([]ServiceInstance, Warnings, error) { 225 request, err := client.newHTTPRequest(requestOptions{ 226 RequestName: internal.GetUserProvidedServiceInstancesRequest, 227 Query: ConvertFilterParameters(filters), 228 }) 229 if err != nil { 230 return nil, nil, err 231 } 232 233 var fullInstancesList []ServiceInstance 234 warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error { 235 if instance, ok := item.(ServiceInstance); ok { 236 fullInstancesList = append(fullInstancesList, instance) 237 } else { 238 return ccerror.UnknownObjectInListError{ 239 Expected: ServiceInstance{}, 240 Unexpected: item, 241 } 242 } 243 return nil 244 }) 245 246 return fullInstancesList, warnings, err 247 }