github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/actor/v2action/service_broker_summary.go (about)

     1  package v2action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     6  )
     7  
     8  type ServiceBrokerSummary struct {
     9  	ServiceBroker
    10  	Services []ServiceSummary
    11  }
    12  
    13  func (actor Actor) GetServiceBrokerSummaries(broker string, service string, organization string) ([]ServiceBrokerSummary, Warnings, error) {
    14  	var filters []ccv2.Filter
    15  	if broker != "" {
    16  		filters = append(filters, ccv2.Filter{
    17  			Type:     constant.NameFilter,
    18  			Operator: constant.EqualOperator,
    19  			Values:   []string{broker},
    20  		})
    21  	}
    22  	brokers, ccWarnings, brokersErr := actor.CloudControllerClient.GetServiceBrokers(filters...)
    23  
    24  	warnings := Warnings(ccWarnings)
    25  	var brokerSummaries []ServiceBrokerSummary
    26  	for _, broker := range brokers {
    27  		brokerSummary, brokerWarnings, err := actor.fetchBrokerSummary(broker)
    28  		warnings = append(warnings, brokerWarnings...)
    29  		if err != nil {
    30  			return nil, warnings, err
    31  		}
    32  		brokerSummaries = append(brokerSummaries, brokerSummary)
    33  	}
    34  
    35  	return brokerSummaries, warnings, brokersErr
    36  }
    37  
    38  func (actor Actor) fetchBrokerSummary(broker ccv2.ServiceBroker) (ServiceBrokerSummary, Warnings, error) {
    39  	ccServices, ccWarnings, servicesErr := actor.CloudControllerClient.GetServices(ccv2.Filter{
    40  		Type:     constant.ServiceBrokerGUIDFilter,
    41  		Operator: constant.EqualOperator,
    42  		Values:   []string{broker.GUID},
    43  	})
    44  
    45  	warnings := Warnings(ccWarnings)
    46  
    47  	if servicesErr != nil {
    48  		return ServiceBrokerSummary{}, warnings, servicesErr
    49  	}
    50  
    51  	var services []ServiceSummary
    52  	for _, service := range ccServices {
    53  		serviceSummary, serviceWarnings, err := actor.fetchServiceSummary(service)
    54  		warnings = append(warnings, serviceWarnings...)
    55  		if err != nil {
    56  			return ServiceBrokerSummary{}, warnings, err
    57  		}
    58  
    59  		services = append(services, serviceSummary)
    60  	}
    61  
    62  	return ServiceBrokerSummary{
    63  		ServiceBroker: ServiceBroker(broker),
    64  		Services:      services,
    65  	}, warnings, servicesErr
    66  }
    67  
    68  func (actor Actor) fetchServiceSummary(service ccv2.Service) (ServiceSummary, Warnings, error) {
    69  	ccServicePlans, warnings, plansErr := actor.CloudControllerClient.GetServicePlans(ccv2.Filter{
    70  		Type:     constant.ServiceGUIDFilter,
    71  		Operator: constant.EqualOperator,
    72  		Values:   []string{service.GUID},
    73  	})
    74  
    75  	if plansErr != nil {
    76  		return ServiceSummary{}, Warnings(warnings), plansErr
    77  	}
    78  
    79  	var servicePlanSummaries []ServicePlanSummary
    80  	for _, plan := range ccServicePlans {
    81  		var visibleTo []string
    82  		if !plan.Public {
    83  			serviceVisibilities, visibilityWarnings, visErr := actor.CloudControllerClient.GetServicePlanVisibilities(ccv2.Filter{
    84  				Type:     constant.ServicePlanGUIDFilter,
    85  				Operator: constant.EqualOperator,
    86  				Values:   []string{plan.GUID},
    87  			})
    88  			warnings = append(warnings, visibilityWarnings...)
    89  
    90  			if visErr != nil {
    91  				return ServiceSummary{}, Warnings(warnings), visErr
    92  			}
    93  
    94  			for _, serviceVisibility := range serviceVisibilities {
    95  				org, orgWarnings, orgsErr := actor.GetOrganization(serviceVisibility.OrganizationGUID)
    96  				warnings = append(warnings, orgWarnings...)
    97  
    98  				if orgsErr != nil {
    99  					return ServiceSummary{}, Warnings(warnings), orgsErr
   100  				}
   101  
   102  				visibleTo = append(visibleTo, org.Name)
   103  			}
   104  		}
   105  
   106  		servicePlanSummaries = append(servicePlanSummaries,
   107  			ServicePlanSummary{
   108  				ServicePlan: ServicePlan(plan),
   109  				VisibleTo:   visibleTo,
   110  			})
   111  	}
   112  
   113  	return ServiceSummary{
   114  		Service: Service(service),
   115  		Plans:   servicePlanSummaries,
   116  	}, Warnings(warnings), plansErr
   117  }