github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/service_plan.go (about) 1 package ccv3 2 3 import ( 4 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 5 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" 7 "code.cloudfoundry.org/cli/resources" 8 "code.cloudfoundry.org/cli/util/lookuptable" 9 ) 10 11 func (client *Client) GetServicePlanByGUID(guid string) (resources.ServicePlan, Warnings, error) { 12 if guid == "" { 13 return resources.ServicePlan{}, nil, ccerror.ServicePlanNotFound{} 14 } 15 16 var result resources.ServicePlan 17 18 _, warnings, err := client.MakeRequest(RequestParams{ 19 RequestName: internal.GetServicePlanRequest, 20 URIParams: internal.Params{"service_plan_guid": guid}, 21 ResponseBody: &result, 22 }) 23 24 return result, warnings, err 25 } 26 27 // GetServicePlans lists service plan with optional filters. 28 func (client *Client) GetServicePlans(query ...Query) ([]resources.ServicePlan, Warnings, error) { 29 plans, _, warnings, err := client.getServicePlans(query...) 30 return plans, warnings, err 31 } 32 33 func (client *Client) getServicePlans(query ...Query) ([]resources.ServicePlan, IncludedResources, Warnings, error) { 34 var plans []resources.ServicePlan 35 36 included, warnings, err := client.MakeListRequest(RequestParams{ 37 RequestName: internal.GetServicePlansRequest, 38 Query: query, 39 ResponseBody: resources.ServicePlan{}, 40 AppendToList: func(item interface{}) error { 41 plans = append(plans, item.(resources.ServicePlan)) 42 return nil 43 }, 44 }) 45 46 return plans, included, warnings, err 47 } 48 49 type ServicePlanWithSpaceAndOrganization struct { 50 // GUID is a unique service plan identifier. 51 GUID string 52 // Name is the name of the service plan. 53 Name string 54 // VisibilityType can be "public", "admin", "organization" or "space" 55 VisibilityType resources.ServicePlanVisibilityType 56 // ServicePlanGUID is the GUID of the service offering 57 ServiceOfferingGUID string 58 59 SpaceGUID string 60 61 SpaceName string 62 63 OrganizationName string 64 } 65 66 type planSpaceDetails struct{ spaceName, orgName string } 67 68 func (client *Client) GetServicePlansWithSpaceAndOrganization(query ...Query) ([]ServicePlanWithSpaceAndOrganization, Warnings, error) { 69 query = append(query, Query{ 70 Key: Include, 71 Values: []string{"space.organization"}, 72 }) 73 74 plans, included, warnings, err := client.getServicePlans(query...) 75 76 spaceDetailsFromGUID := computeSpaceDetailsTable(included) 77 78 var enrichedPlans []ServicePlanWithSpaceAndOrganization 79 for _, plan := range plans { 80 sd := spaceDetailsFromGUID[plan.SpaceGUID] 81 82 enrichedPlans = append(enrichedPlans, ServicePlanWithSpaceAndOrganization{ 83 GUID: plan.GUID, 84 Name: plan.Name, 85 VisibilityType: plan.VisibilityType, 86 ServiceOfferingGUID: plan.ServiceOfferingGUID, 87 SpaceGUID: plan.SpaceGUID, 88 SpaceName: sd.spaceName, 89 OrganizationName: sd.orgName, 90 }) 91 } 92 93 return enrichedPlans, warnings, err 94 } 95 96 type ServiceOfferingWithPlans struct { 97 // GUID is a unique service offering identifier. 98 GUID string 99 // Name is the name of the service offering. 100 Name string 101 // Description of the service offering 102 Description string 103 // ServiceBrokerName is the name of the service broker 104 ServiceBrokerName string 105 106 // List of service plans that this service offering provides 107 Plans []resources.ServicePlan 108 } 109 110 func (client *Client) GetServicePlansWithOfferings(query ...Query) ([]ServiceOfferingWithPlans, Warnings, error) { 111 query = append(query, 112 Query{ 113 Key: Include, 114 Values: []string{"service_offering"}, 115 }, 116 Query{ 117 Key: FieldsServiceOfferingServiceBroker, 118 Values: []string{"name,guid"}, 119 }, 120 ) 121 122 plans, included, warnings, err := client.getServicePlans(query...) 123 if err != nil { 124 return nil, warnings, err 125 } 126 127 var offeringsWithPlans []ServiceOfferingWithPlans 128 offeringGUIDLookup := make(map[string]int) 129 130 indexOfOffering := func(serviceOfferingGUID string) int { 131 if i, ok := offeringGUIDLookup[serviceOfferingGUID]; ok { 132 return i 133 } 134 135 i := len(offeringsWithPlans) 136 offeringGUIDLookup[serviceOfferingGUID] = i 137 offeringsWithPlans = append(offeringsWithPlans, ServiceOfferingWithPlans{GUID: serviceOfferingGUID}) 138 139 return i 140 } 141 142 brokerNameLookup := lookuptable.NameFromGUID(included.ServiceBrokers) 143 144 for _, p := range plans { 145 i := indexOfOffering(p.ServiceOfferingGUID) 146 offeringsWithPlans[i].Plans = append(offeringsWithPlans[i].Plans, p) 147 } 148 149 for _, o := range included.ServiceOfferings { 150 i := indexOfOffering(o.GUID) 151 offeringsWithPlans[i].Name = o.Name 152 offeringsWithPlans[i].Description = o.Description 153 offeringsWithPlans[i].ServiceBrokerName = brokerNameLookup[o.ServiceBrokerGUID] 154 } 155 156 return offeringsWithPlans, warnings, nil 157 } 158 159 func computeSpaceDetailsTable(included IncludedResources) map[string]planSpaceDetails { 160 orgNameFromGUID := lookuptable.NameFromGUID(included.Organizations) 161 162 spaceDetailsFromGUID := make(map[string]planSpaceDetails) 163 for _, space := range included.Spaces { 164 details := planSpaceDetails{spaceName: space.Name} 165 166 if orgRelationship, ok := space.Relationships[constant.RelationshipTypeOrganization]; ok { 167 details.orgName = orgNameFromGUID[orgRelationship.GUID] 168 } 169 170 spaceDetailsFromGUID[space.GUID] = details 171 } 172 173 return spaceDetailsFromGUID 174 }