github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/service_plans.go (about) 1 package mccpv2 2 3 import ( 4 "fmt" 5 6 "github.com/IBM-Cloud/bluemix-go/bmxerror" 7 "github.com/IBM-Cloud/bluemix-go/client" 8 "github.com/IBM-Cloud/bluemix-go/rest" 9 ) 10 11 //ErrCodeServicePlanDoesNotExist ... 12 const ErrCodeServicePlanDoesNotExist = "ServicePlanDoesNotExist" 13 14 //ServicePlan ... 15 type ServicePlan struct { 16 GUID string 17 Name string `json:"name"` 18 Description string `json:"description"` 19 IsFree bool `json:"free"` 20 IsPublic bool `json:"public"` 21 IsActive bool `json:"active"` 22 ServiceGUID string `json:"service_guid"` 23 UniqueID string `json:"unique_id"` 24 ServiceInstancesURL string `json:"service_instances_url"` 25 } 26 27 //ServicePlanResource ... 28 type ServicePlanResource struct { 29 Resource 30 Entity ServicePlanEntity 31 } 32 33 //ServicePlanEntity ... 34 type ServicePlanEntity struct { 35 Name string `json:"name"` 36 Description string `json:"description"` 37 IsFree bool `json:"free"` 38 IsPublic bool `json:"public"` 39 IsActive bool `json:"active"` 40 ServiceGUID string `json:"service_guid"` 41 UniqueID string `json:"unique_id"` 42 ServiceInstancesURL string `json:"service_instances_url"` 43 } 44 45 //ToFields ... 46 func (resource ServicePlanResource) ToFields() ServicePlan { 47 entity := resource.Entity 48 49 return ServicePlan{ 50 GUID: resource.Metadata.GUID, 51 Name: entity.Name, 52 Description: entity.Description, 53 IsFree: entity.IsFree, 54 IsPublic: entity.IsPublic, 55 IsActive: entity.IsActive, 56 ServiceGUID: entity.ServiceGUID, 57 UniqueID: entity.UniqueID, 58 ServiceInstancesURL: entity.ServiceInstancesURL, 59 } 60 } 61 62 //ServicePlanFields ... 63 type ServicePlanFields struct { 64 Metadata ServicePlanMetadata 65 Entity ServicePlan 66 } 67 68 //ServicePlanMetadata ... 69 type ServicePlanMetadata struct { 70 GUID string `json:"guid"` 71 URL string `json:"url"` 72 } 73 74 //ServicePlans ... 75 type ServicePlans interface { 76 FindPlanInServiceOffering(serviceOfferingGUID string, planType string) (*ServicePlan, error) 77 Get(planGUID string) (*ServicePlanFields, error) 78 } 79 80 type servicePlan struct { 81 client *client.Client 82 } 83 84 func newServicePlanAPI(c *client.Client) ServicePlans { 85 return &servicePlan{ 86 client: c, 87 } 88 } 89 90 func (s *servicePlan) Get(planGUID string) (*ServicePlanFields, error) { 91 rawURL := fmt.Sprintf("/v2/service_plans/%s", planGUID) 92 planFields := ServicePlanFields{} 93 _, err := s.client.Get(rawURL, &planFields) 94 if err != nil { 95 return nil, err 96 } 97 return &planFields, err 98 } 99 100 func (s *servicePlan) FindPlanInServiceOffering(serviceOfferingGUID string, planType string) (*ServicePlan, error) { 101 req := rest.GetRequest("/v2/service_plans") 102 if serviceOfferingGUID != "" { 103 req.Query("q", "service_guid:"+serviceOfferingGUID) 104 } 105 httpReq, err := req.Build() 106 if err != nil { 107 return nil, err 108 } 109 path := httpReq.URL.String() 110 plans, err := s.listServicesPlanWithPath(path) 111 if err != nil { 112 return nil, err 113 } 114 if len(plans) == 0 { 115 return nil, bmxerror.New(ErrCodeServicePlanDoesNotExist, 116 fmt.Sprintf("Given plan %q doesn't exist for the service %q", planType, serviceOfferingGUID)) 117 } 118 for _, p := range plans { 119 if p.Name == planType { 120 return &p, nil 121 } 122 123 } 124 return nil, bmxerror.New(ErrCodeServicePlanDoesNotExist, 125 fmt.Sprintf("Given plan %q doesn't exist for the service %q", planType, serviceOfferingGUID)) 126 127 } 128 129 func (s *servicePlan) listServicesPlanWithPath(path string) ([]ServicePlan, error) { 130 var servicePlans []ServicePlan 131 _, err := s.client.GetPaginated(path, NewCCPaginatedResources(ServicePlanResource{}), func(resource interface{}) bool { 132 if servicePlanResource, ok := resource.(ServicePlanResource); ok { 133 servicePlans = append(servicePlans, servicePlanResource.ToFields()) 134 return true 135 } 136 return false 137 }) 138 return servicePlans, err 139 }