github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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/ccerror" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 9 ) 10 11 // ServiceInstanceType is the type of the Service Instance. 12 type ServiceInstanceType string 13 14 const ( 15 // UserProvidedService is a Service Instance that is created by a user. 16 UserProvidedService ServiceInstanceType = "user_provided_service_instance" 17 18 // ManagedService is a Service Instance that is managed by a service broker. 19 ManagedService ServiceInstanceType = "managed_service_instance" 20 ) 21 22 // ServiceInstance represents a Cloud Controller Service Instance. 23 type ServiceInstance struct { 24 GUID string 25 Name string 26 SpaceGUID string 27 ServiceGUID string 28 ServicePlanGUID string 29 Type ServiceInstanceType 30 Tags []string 31 DashboardURL string 32 LastOperation LastOperation 33 } 34 35 type LastOperation struct { 36 Type string 37 State string 38 Description string 39 UpdatedAt string 40 CreatedAt string 41 } 42 43 // UnmarshalJSON helps unmarshal a Cloud Controller Service Instance response. 44 func (serviceInstance *ServiceInstance) UnmarshalJSON(data []byte) error { 45 var ccServiceInstance struct { 46 Metadata internal.Metadata 47 Entity struct { 48 Name string `json:"name"` 49 SpaceGUID string `json:"space_guid"` 50 ServiceGUID string `json:"service_guid"` 51 ServicePlanGUID string `json:"service_plan_guid"` 52 Type string `json:"type"` 53 Tags []string `json:"tags"` 54 DashboardURL string `json:"dashboard_url"` 55 LastOperation struct { 56 Type string `json:"type"` 57 State string `json:"state"` 58 Description string `json:"description"` 59 UpdatedAt string `json:"updated_at"` 60 CreatedAt string `json:"created_at"` 61 } `json:"last_operation"` 62 } 63 } 64 err := json.Unmarshal(data, &ccServiceInstance) 65 if err != nil { 66 return err 67 } 68 69 serviceInstance.GUID = ccServiceInstance.Metadata.GUID 70 serviceInstance.Name = ccServiceInstance.Entity.Name 71 serviceInstance.SpaceGUID = ccServiceInstance.Entity.SpaceGUID 72 serviceInstance.ServiceGUID = ccServiceInstance.Entity.ServiceGUID 73 serviceInstance.ServicePlanGUID = ccServiceInstance.Entity.ServicePlanGUID 74 serviceInstance.Type = ServiceInstanceType(ccServiceInstance.Entity.Type) 75 serviceInstance.Tags = ccServiceInstance.Entity.Tags 76 serviceInstance.DashboardURL = ccServiceInstance.Entity.DashboardURL 77 serviceInstance.LastOperation = LastOperation(ccServiceInstance.Entity.LastOperation) 78 return nil 79 } 80 81 // UserProvided returns true if the Service Instance is a user provided 82 // service. 83 func (serviceInstance ServiceInstance) UserProvided() bool { 84 return serviceInstance.Type == UserProvidedService 85 } 86 87 // Managed returns true if the Service Instance is a managed service. 88 func (serviceInstance ServiceInstance) Managed() bool { 89 return serviceInstance.Type == ManagedService 90 } 91 92 // GetServiceInstance returns the service instance with the given GUID. This 93 // service can be either a managed or user provided. 94 func (client *Client) GetServiceInstance(serviceInstanceGUID string) (ServiceInstance, Warnings, error) { 95 request, err := client.newHTTPRequest(requestOptions{ 96 RequestName: internal.GetServiceInstanceRequest, 97 URIParams: Params{"service_instance_guid": serviceInstanceGUID}, 98 }) 99 if err != nil { 100 return ServiceInstance{}, nil, err 101 } 102 103 var serviceInstance ServiceInstance 104 response := cloudcontroller.Response{ 105 Result: &serviceInstance, 106 } 107 108 err = client.connection.Make(request, &response) 109 return serviceInstance, response.Warnings, err 110 } 111 112 // GetServiceInstances returns back a list of *managed* Service Instances based 113 // off of the provided filters. 114 func (client *Client) GetServiceInstances(filters ...Filter) ([]ServiceInstance, Warnings, error) { 115 request, err := client.newHTTPRequest(requestOptions{ 116 RequestName: internal.GetServiceInstancesRequest, 117 Query: ConvertFilterParameters(filters), 118 }) 119 if err != nil { 120 return nil, nil, err 121 } 122 123 var fullInstancesList []ServiceInstance 124 warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error { 125 if instance, ok := item.(ServiceInstance); ok { 126 fullInstancesList = append(fullInstancesList, instance) 127 } else { 128 return ccerror.UnknownObjectInListError{ 129 Expected: ServiceInstance{}, 130 Unexpected: item, 131 } 132 } 133 return nil 134 }) 135 136 return fullInstancesList, warnings, err 137 } 138 139 // GetSpaceServiceInstances returns back a list of Service Instances based off 140 // of the space and filters provided. User provided services will be included 141 // if includeUserProvidedServices is set to true. 142 func (client *Client) GetSpaceServiceInstances(spaceGUID string, includeUserProvidedServices bool, filters ...Filter) ([]ServiceInstance, Warnings, error) { 143 query := ConvertFilterParameters(filters) 144 145 if includeUserProvidedServices { 146 query.Add("return_user_provided_service_instances", "true") 147 } 148 149 request, err := client.newHTTPRequest(requestOptions{ 150 RequestName: internal.GetSpaceServiceInstancesRequest, 151 URIParams: map[string]string{"guid": spaceGUID}, 152 Query: query, 153 }) 154 if err != nil { 155 return nil, nil, err 156 } 157 158 var fullInstancesList []ServiceInstance 159 warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error { 160 if instance, ok := item.(ServiceInstance); ok { 161 fullInstancesList = append(fullInstancesList, instance) 162 } else { 163 return ccerror.UnknownObjectInListError{ 164 Expected: ServiceInstance{}, 165 Unexpected: item, 166 } 167 } 168 return nil 169 }) 170 171 return fullInstancesList, warnings, err 172 }