github.com/sleungcy/cli@v7.1.0+incompatible/api/cloudcontroller/ccv3/service_plan.go (about)

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