github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/service_instances.go (about) 1 package mccpv2 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/IBM-Cloud/bluemix-go/client" 8 "github.com/IBM-Cloud/bluemix-go/rest" 9 ) 10 11 //ServiceInstanceCreateRequest ... 12 type ServiceInstanceCreateRequest struct { 13 Name string `json:"name"` 14 SpaceGUID string `json:"space_guid"` 15 PlanGUID string `json:"service_plan_guid"` 16 Params map[string]interface{} `json:"parameters,omitempty"` 17 Tags []string `json:"tags,omitempty"` 18 } 19 20 //ServiceInstanceUpdateRequest ... 21 type ServiceInstanceUpdateRequest struct { 22 Name *string `json:"name,omitempty"` 23 PlanGUID *string `json:"service_plan_guid,omitempty"` 24 Params map[string]interface{} `json:"parameters,omitempty"` 25 Tags []string `json:"tags,omitempty"` 26 } 27 28 //ServiceInstance ... 29 type ServiceInstance struct { 30 GUID string 31 Name string `json:"name"` 32 Credentials map[string]interface{} `json:"credentials"` 33 ServicePlanGUID string `json:"service_plan_guid"` 34 SpaceGUID string `json:"space_guid"` 35 GatewayData string `json:"gateway_data"` 36 Type string `json:"type"` 37 DashboardURL string `json:"dashboard_url"` 38 LastOperation LastOperationFields `json:"last_operation"` 39 RouteServiceURL string `json:"routes_url"` 40 Tags []string `json:"tags"` 41 SpaceURL string `json:"space_url"` 42 ServicePlanURL string `json:"service_plan_url"` 43 ServiceBindingURL string `json:"service_bindings_url"` 44 ServiceKeysURL string `json:"service_keys_url"` 45 ServiceKeys []ServiceKeyFields `json:"service_keys"` 46 ServicePlan ServicePlanFields `json:"service_plan"` 47 } 48 49 //ServiceInstanceFields ... 50 type ServiceInstanceFields struct { 51 Metadata ServiceInstanceMetadata 52 Entity ServiceInstance 53 } 54 55 //ServiceInstanceMetadata ... 56 type ServiceInstanceMetadata struct { 57 GUID string `json:"guid"` 58 URL string `json:"url"` 59 } 60 61 //LastOperationFields ... 62 type LastOperationFields struct { 63 Type string `json:"type"` 64 State string `json:"state"` 65 Description string `json:"description"` 66 CreatedAt string `json:"created_at"` 67 UpdatedAt string `json:"updated_at"` 68 } 69 70 //ServiceInstanceResource ... 71 type ServiceInstanceResource struct { 72 Resource 73 Entity ServiceInstanceEntity 74 } 75 76 //ServiceInstanceEntity ... 77 type ServiceInstanceEntity struct { 78 Name string `json:"name"` 79 Credentials map[string]interface{} `json:"credentials"` 80 ServicePlanGUID string `json:"service_plan_guid"` 81 SpaceGUID string `json:"space_guid"` 82 GatewayData string `json:"gateway_data"` 83 Type string `json:"type"` 84 DashboardURL string `json:"dashboard_url"` 85 LastOperation LastOperationFields `json:"last_operation"` 86 RouteServiceURL string `json:"routes_url"` 87 Tags []string `json:"tags"` 88 SpaceURL string `json:"space_url"` 89 ServicePlanURL string `json:"service_plan_url"` 90 ServiceBindingURL string `json:"service_bindings_url"` 91 ServiceKeysURL string `json:"service_keys_url"` 92 ServiceKeys []ServiceKeyFields `json:"service_keys"` 93 ServicePlan ServicePlanFields `json:"service_plan"` 94 } 95 96 //ToModel ... 97 func (resource ServiceInstanceResource) ToModel() ServiceInstance { 98 99 entity := resource.Entity 100 101 return ServiceInstance{ 102 GUID: resource.Metadata.GUID, 103 Name: entity.Name, 104 Credentials: entity.Credentials, 105 ServicePlanGUID: entity.ServicePlanGUID, 106 SpaceGUID: entity.SpaceGUID, 107 GatewayData: entity.GatewayData, 108 Type: entity.Type, 109 LastOperation: entity.LastOperation, 110 RouteServiceURL: entity.RouteServiceURL, 111 DashboardURL: entity.DashboardURL, 112 Tags: entity.Tags, 113 SpaceURL: entity.SpaceURL, 114 ServicePlanURL: entity.ServicePlanURL, 115 ServiceBindingURL: entity.ServiceBindingURL, 116 ServiceKeysURL: entity.ServiceKeysURL, 117 } 118 } 119 120 //ServiceInstances ... 121 type ServiceInstances interface { 122 Create(req ServiceInstanceCreateRequest) (*ServiceInstanceFields, error) 123 Update(instanceGUID string, req ServiceInstanceUpdateRequest) (*ServiceInstanceFields, error) 124 Delete(instanceGUID string, opts ...bool) error 125 FindByName(instanceName string) (*ServiceInstance, error) 126 FindByNameInSpace(spaceGUID string, instanceName string) (*ServiceInstance, error) 127 Get(instanceGUID string, depth ...int) (*ServiceInstanceFields, error) 128 ListServiceBindings(instanceGUID string) ([]ServiceBinding, error) 129 } 130 131 type serviceInstance struct { 132 client *client.Client 133 } 134 135 func newServiceInstanceAPI(c *client.Client) ServiceInstances { 136 return &serviceInstance{ 137 client: c, 138 } 139 } 140 141 func (s *serviceInstance) Create(req ServiceInstanceCreateRequest) (*ServiceInstanceFields, error) { 142 rawURL := "/v2/service_instances?accepts_incomplete=true" 143 serviceFields := ServiceInstanceFields{} 144 _, err := s.client.Post(rawURL, req, &serviceFields) 145 if err != nil { 146 return nil, err 147 } 148 return &serviceFields, nil 149 } 150 151 func (s *serviceInstance) Get(instanceGUID string, depth ...int) (*ServiceInstanceFields, error) { 152 rawURL := fmt.Sprintf("/v2/service_instances/%s", instanceGUID) 153 req := rest.GetRequest(rawURL) 154 if len(depth) > 0 { 155 req.Query("inline-relations-depth", strconv.Itoa(depth[0])) 156 } 157 httpReq, err := req.Build() 158 if err != nil { 159 return nil, err 160 } 161 path := httpReq.URL.String() 162 163 serviceFields := ServiceInstanceFields{} 164 _, err = s.client.Get(path, &serviceFields) 165 if err != nil { 166 return nil, err 167 } 168 return &serviceFields, err 169 } 170 171 func (s *serviceInstance) FindByName(instanceName string) (*ServiceInstance, error) { 172 req := rest.GetRequest("/v2/service_instances") 173 req.Query("return_user_provided_service_instances", "true") 174 if instanceName != "" { 175 req.Query("q", "name:"+instanceName) 176 } 177 httpReq, err := req.Build() 178 if err != nil { 179 return nil, err 180 } 181 path := httpReq.URL.String() 182 services, err := listServicesWithPath(s.client, path) 183 if err != nil { 184 return nil, err 185 } 186 if len(services) == 0 { 187 return nil, fmt.Errorf("Service instance: %q doesn't exist", instanceName) 188 } 189 return &services[0], nil 190 } 191 192 func (s *serviceInstance) FindByNameInSpace(spaceGUID string, instanceName string) (*ServiceInstance, error) { 193 req := rest.GetRequest(fmt.Sprintf("/v2/spaces/%s/service_instances", spaceGUID)) 194 req.Query("return_user_provided_service_instances", "true") 195 if instanceName != "" { 196 req.Query("q", "name:"+instanceName) 197 } 198 httpReq, err := req.Build() 199 if err != nil { 200 return nil, err 201 } 202 path := httpReq.URL.String() 203 services, err := listServicesWithPath(s.client, path) 204 if err != nil { 205 return nil, err 206 } 207 if len(services) == 0 { 208 return nil, fmt.Errorf("Service instance: %q doesn't exist in the space %s", instanceName, spaceGUID) 209 } 210 return &services[0], nil 211 } 212 213 // opts is list of boolean parametes 214 // opts[0] - async - Will run the delete request in a background job. Recommended: 'true'. Default to 'true'. 215 // opts[1] - recursive - Will delete service bindings, service keys, and routes associated with the service instance. Default to 'false'. 216 217 func (s *serviceInstance) Delete(instanceGUID string, opts ...bool) error { 218 async := true 219 recursive := false 220 if len(opts) > 0 { 221 async = opts[0] 222 } 223 if len(opts) > 1 { 224 recursive = opts[1] 225 } 226 rawURL := fmt.Sprintf("/v2/service_instances/%s?accepts_incomplete=true&async=%t&recursive=%t", instanceGUID, async, recursive) 227 _, err := s.client.Delete(rawURL) 228 return err 229 } 230 231 func (s *serviceInstance) Update(instanceGUID string, req ServiceInstanceUpdateRequest) (*ServiceInstanceFields, error) { 232 rawURL := fmt.Sprintf("/v2/service_instances/%s?accepts_incomplete=true", instanceGUID) 233 serviceFields := ServiceInstanceFields{} 234 _, err := s.client.Put(rawURL, req, &serviceFields) 235 if err != nil { 236 return nil, err 237 } 238 return &serviceFields, nil 239 } 240 241 func (s *serviceInstance) ListServiceBindings(instanceGUID string) ([]ServiceBinding, error) { 242 rawURL := fmt.Sprintf("/v2/service_instances/%s/service_bindings", instanceGUID) 243 req := rest.GetRequest(rawURL) 244 httpReq, err := req.Build() 245 if err != nil { 246 return nil, err 247 } 248 path := httpReq.URL.String() 249 sb, err := listServiceBindingWithPath(s.client, path) 250 if err != nil { 251 return nil, err 252 } 253 return sb, nil 254 } 255 256 func listServicesWithPath(client *client.Client, path string) ([]ServiceInstance, error) { 257 var services []ServiceInstance 258 _, err := client.GetPaginated(path, NewCCPaginatedResources(ServiceInstanceResource{}), func(resource interface{}) bool { 259 if serviceInstanceResource, ok := resource.(ServiceInstanceResource); ok { 260 services = append(services, serviceInstanceResource.ToModel()) 261 return true 262 } 263 return false 264 }) 265 return services, err 266 }