github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/resource/resourcev2/controllerv2/resource_service_instance.go (about) 1 package controllerv2 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/url" 7 8 "github.com/IBM-Cloud/bluemix-go/bmxerror" 9 "github.com/IBM-Cloud/bluemix-go/client" 10 "github.com/IBM-Cloud/bluemix-go/models" 11 "github.com/IBM-Cloud/bluemix-go/rest" 12 ) 13 14 type CreateServiceInstanceRequest struct { 15 Name string `json:"name"` 16 ServicePlanID string `json:"resource_plan_id"` 17 ResourceGroupID string `json:"resource_group_id"` 18 Crn string `json:"crn,omitempty"` 19 Tags []string `json:"tags,omitempty"` 20 Parameters map[string]interface{} `json:"parameters,omitempty"` 21 TargetCrn string `json:"target_crn"` 22 } 23 24 type UpdateServiceInstanceRequest struct { 25 Name string `json:"name,omitempty"` 26 ServicePlanID string `json:"resource_plan_id,omitempty"` 27 Tags []string `json:"tags,omitempty"` 28 Parameters map[string]interface{} `json:"parameters,omitempty"` 29 UpdateTime int64 `json:"update_time,omitempty"` 30 } 31 32 type ServiceInstanceQuery struct { 33 ResourceGroupID string 34 ServiceID string 35 ServicePlanID string 36 Name string 37 Type string 38 SubType string 39 Limit string 40 UpdatedFrom string 41 UpdatedTo string 42 Guid string 43 } 44 45 //ErrCodeResourceServiceInstanceDoesnotExist ... 46 const ErrCodeResourceServiceInstanceDoesnotExist = "ResourceServiceInstanceDoesnotExist" 47 48 //ResourceServiceInstanceQuery ... 49 type ResourceServiceInstanceRepository interface { 50 ListInstances(query ServiceInstanceQuery) ([]models.ServiceInstanceV2, error) 51 GetInstance(serviceInstanceID string) (models.ServiceInstanceV2, error) 52 } 53 54 type resourceServiceInstance struct { 55 client *client.Client 56 } 57 58 func newResourceServiceInstanceAPI(c *client.Client) ResourceServiceInstanceRepository { 59 return &resourceServiceInstance{ 60 client: c, 61 } 62 } 63 64 func (r *resourceServiceInstance) ListInstances(query ServiceInstanceQuery) ([]models.ServiceInstanceV2, error) { 65 listRequest := rest.GetRequest("/v2/resource_instances"). 66 Query("resource_group_id", query.ResourceGroupID). 67 Query("resource_id", query.ServiceID). 68 Query("resource_plan_id", query.ServicePlanID). 69 Query("type", query.Type). 70 Query("sub_type", query.SubType). 71 Query("limit", query.Limit). 72 Query("updated_from", query.UpdatedFrom). 73 Query("updated_to", query.UpdatedTo). 74 Query("guid", query.Guid) 75 76 req, err := listRequest.Build() 77 if err != nil { 78 return nil, err 79 } 80 81 var instances []models.ServiceInstanceV2 82 _, err = r.client.GetPaginated( 83 req.URL.String(), 84 NewRCPaginatedResources(models.ServiceInstanceV2{}), 85 func(resource interface{}) bool { 86 if instance, ok := resource.(models.ServiceInstanceV2); ok { 87 instances = append(instances, instance) 88 return true 89 } 90 return false 91 }, 92 ) 93 if err != nil { 94 return []models.ServiceInstanceV2{}, err 95 } 96 97 if query.Name != "" { 98 instances = filterInstancesByName(instances, query.Name) 99 } 100 return instances, nil 101 } 102 103 func (r *resourceServiceInstance) GetInstance(serviceInstanceID string) (models.ServiceInstanceV2, error) { 104 var instance models.ServiceInstanceV2 105 resp, err := r.client.Get("/v2/resource_instances/"+url.PathEscape(serviceInstanceID), &instance) 106 if resp.StatusCode == http.StatusNotFound { 107 return models.ServiceInstanceV2{}, bmxerror.New(ErrCodeResourceServiceInstanceDoesnotExist, 108 fmt.Sprintf("Given service instance : %q doesn't exist", serviceInstanceID)) 109 } 110 return instance, err 111 } 112 113 func filterInstancesByName(instances []models.ServiceInstanceV2, name string) []models.ServiceInstanceV2 { 114 ret := []models.ServiceInstanceV2{} 115 for _, instance := range instances { 116 if instance.Name == name { 117 ret = append(ret, instance) 118 } 119 } 120 return ret 121 }