github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/service_offerings.go (about) 1 package mccpv2 2 3 import ( 4 "fmt" 5 6 "github.com/IBM-Cloud/bluemix-go/client" 7 8 "github.com/IBM-Cloud/bluemix-go/bmxerror" 9 "github.com/IBM-Cloud/bluemix-go/rest" 10 ) 11 12 //ErrCodeServiceDoesnotExist ... 13 const ErrCodeServiceDoesnotExist = "ServiceDoesnotExist" 14 15 //ServiceOffering model 16 type ServiceOffering struct { 17 GUID string 18 Label string `json:"label"` 19 Provider string `json:"provider"` 20 Description string `json:"description"` 21 LongDescription string `json:"long_description"` 22 Version string `json:"version"` 23 URL string `json:"url"` 24 InfoURL string `json:"info_url"` 25 DocumentURL string `json:"documentation_url"` 26 Timeout string `json:"timeout"` 27 UniqueID string `json:"unique_id"` 28 ServiceBrokerGUID string `json:"service_broker_guid"` 29 ServicePlansURL string `json:"service_plans_url"` 30 Tags []string `json:"tags"` 31 Requires []string `json:"requires"` 32 IsActive bool `json:"active"` 33 IsBindable bool `json:"bindable"` 34 IsPlanUpdateable bool `json:"plan_updateable"` 35 } 36 37 //ServiceOfferingResource ... 38 type ServiceOfferingResource struct { 39 Resource 40 Entity ServiceOfferingEntity 41 } 42 43 //ServiceOfferingEntity ... 44 type ServiceOfferingEntity struct { 45 Label string `json:"label"` 46 Provider string `json:"provider"` 47 Description string `json:"description"` 48 LongDescription string `json:"long_description"` 49 Version string `json:"version"` 50 URL string `json:"url"` 51 InfoURL string `json:"info_url"` 52 DocumentURL string `json:"documentation_url"` 53 Timeout string `json:"timeout"` 54 UniqueID string `json:"unique_id"` 55 ServiceBrokerGUID string `json:"service_broker_guid"` 56 ServicePlansURL string `json:"service_plans_url"` 57 Tags []string `json:"tags"` 58 Requires []string `json:"requires"` 59 IsActive bool `json:"active"` 60 IsBindable bool `json:"bindable"` 61 IsPlanUpdateable bool `json:"plan_updateable"` 62 } 63 64 //ToFields ... 65 func (resource ServiceOfferingResource) ToFields() ServiceOffering { 66 entity := resource.Entity 67 68 return ServiceOffering{ 69 GUID: resource.Metadata.GUID, 70 Label: entity.Label, 71 Provider: entity.Provider, 72 Description: entity.Description, 73 LongDescription: entity.LongDescription, 74 Version: entity.Version, 75 URL: entity.URL, 76 InfoURL: entity.InfoURL, 77 DocumentURL: entity.DocumentURL, 78 Timeout: entity.Timeout, 79 UniqueID: entity.UniqueID, 80 ServiceBrokerGUID: entity.ServiceBrokerGUID, 81 ServicePlansURL: entity.ServicePlansURL, 82 Tags: entity.Tags, 83 Requires: entity.Requires, 84 IsActive: entity.IsActive, 85 IsBindable: entity.IsBindable, 86 IsPlanUpdateable: entity.IsPlanUpdateable, 87 } 88 } 89 90 //ServiceOfferingFields ... 91 type ServiceOfferingFields struct { 92 Metadata ServiceOfferingMetadata 93 Entity ServiceOffering 94 } 95 96 //ServiceOfferingMetadata ... 97 type ServiceOfferingMetadata struct { 98 GUID string `json:"guid"` 99 URL string `json:"url"` 100 } 101 102 //ServiceOfferings ... 103 type ServiceOfferings interface { 104 FindByLabel(serviceName string) (*ServiceOffering, error) 105 Get(svcOfferingGUID string) (*ServiceOfferingFields, error) 106 } 107 108 type serviceOfferrings struct { 109 client *client.Client 110 } 111 112 func newServiceOfferingAPI(c *client.Client) ServiceOfferings { 113 return &serviceOfferrings{ 114 client: c, 115 } 116 } 117 118 func (s *serviceOfferrings) Get(svcGUID string) (*ServiceOfferingFields, error) { 119 rawURL := fmt.Sprintf("/v2/services/%s", svcGUID) 120 svcFields := ServiceOfferingFields{} 121 _, err := s.client.Get(rawURL, &svcFields) 122 if err != nil { 123 return nil, err 124 } 125 return &svcFields, err 126 } 127 128 func (s *serviceOfferrings) FindByLabel(serviceName string) (*ServiceOffering, error) { 129 req := rest.GetRequest("v2/services") 130 if serviceName != "" { 131 req.Query("q", "label:"+serviceName) 132 } 133 httpReq, err := req.Build() 134 if err != nil { 135 return nil, err 136 } 137 path := httpReq.URL.String() 138 var services ServiceOffering 139 var found bool 140 err = s.listServicesOfferingWithPath(path, func(serviceOfferingResource ServiceOfferingResource) bool { 141 services = serviceOfferingResource.ToFields() 142 found = true 143 return false 144 }) 145 146 if err != nil { 147 return nil, err 148 } 149 150 if found { 151 return &services, err 152 } 153 //May not be found and no error 154 155 return nil, bmxerror.New(ErrCodeServiceDoesnotExist, 156 fmt.Sprintf("Given service %q doesn't exist", serviceName)) 157 158 } 159 160 func (s *serviceOfferrings) listServicesOfferingWithPath(path string, cb func(ServiceOfferingResource) bool) error { 161 _, err := s.client.GetPaginated(path, NewCCPaginatedResources(ServiceOfferingResource{}), func(resource interface{}) bool { 162 if serviceOfferingResource, ok := resource.(ServiceOfferingResource); ok { 163 return cb(serviceOfferingResource) 164 } 165 return false 166 }) 167 return err 168 }