github.com/Thanhphan1147/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/service_summary.go (about)

     1  package v2action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     7  )
     8  
     9  type ServiceSummary struct {
    10  	Service
    11  	Plans []ServicePlanSummary
    12  }
    13  
    14  func (actor Actor) GetServicesSummaries() ([]ServiceSummary, Warnings, error) {
    15  	services, warnings, err := actor.CloudControllerClient.GetServices()
    16  	if err != nil {
    17  		return []ServiceSummary{}, Warnings(warnings), err
    18  	}
    19  
    20  	summaries, serviceWarnings, err := actor.createServiceSummaries(services)
    21  	warnings = append(warnings, serviceWarnings...)
    22  	return summaries, Warnings(warnings), err
    23  }
    24  
    25  func (actor Actor) GetServicesSummariesForSpace(spaceGUID string) ([]ServiceSummary, Warnings, error) {
    26  	services, warnings, err := actor.CloudControllerClient.GetSpaceServices(spaceGUID)
    27  	if err != nil {
    28  		return []ServiceSummary{}, Warnings(warnings), err
    29  	}
    30  
    31  	summaries, summaryWarnings, err := actor.createServiceSummaries(services)
    32  	warnings = append(warnings, summaryWarnings...)
    33  	return summaries, Warnings(warnings), err
    34  }
    35  
    36  func (actor Actor) GetServiceSummaryByName(serviceName string) (ServiceSummary, Warnings, error) {
    37  	services, warnings, err := actor.CloudControllerClient.GetServices(ccv2.Filter{
    38  		Type:     constant.LabelFilter,
    39  		Operator: constant.EqualOperator,
    40  		Values:   []string{serviceName},
    41  	})
    42  	if err != nil {
    43  		return ServiceSummary{}, Warnings(warnings), err
    44  	}
    45  
    46  	if len(services) == 0 {
    47  		return ServiceSummary{}, Warnings(warnings), actionerror.ServiceNotFoundError{Name: serviceName}
    48  	}
    49  
    50  	summary, summaryWarnings, err := actor.createServiceSummary(services[0])
    51  	warnings = append(warnings, summaryWarnings...)
    52  	if err != nil {
    53  		return ServiceSummary{}, Warnings(warnings), err
    54  	}
    55  
    56  	return summary, Warnings(warnings), err
    57  }
    58  
    59  func (actor Actor) GetServiceSummaryForSpaceByName(spaceGUID, serviceName string) (ServiceSummary, Warnings, error) {
    60  	services, warnings, err := actor.CloudControllerClient.GetSpaceServices(spaceGUID, ccv2.Filter{
    61  		Type:     constant.LabelFilter,
    62  		Operator: constant.EqualOperator,
    63  		Values:   []string{serviceName},
    64  	})
    65  	if err != nil {
    66  		return ServiceSummary{}, Warnings(warnings), err
    67  	}
    68  
    69  	if len(services) == 0 {
    70  		return ServiceSummary{}, Warnings(warnings), actionerror.ServiceNotFoundError{Name: serviceName}
    71  	}
    72  
    73  	summary, summaryWarnings, err := actor.createServiceSummary(services[0])
    74  	warnings = append(warnings, summaryWarnings...)
    75  	if err != nil {
    76  		return ServiceSummary{}, Warnings(warnings), err
    77  	}
    78  
    79  	return summary, Warnings(warnings), err
    80  }
    81  
    82  func (actor Actor) createServiceSummaries(services []ccv2.Service) ([]ServiceSummary, ccv2.Warnings, error) {
    83  	var serviceSummaries []ServiceSummary
    84  	var warnings ccv2.Warnings
    85  
    86  	for _, service := range services {
    87  		summary, summaryWarnings, err := actor.createServiceSummary(service)
    88  		warnings = append(warnings, summaryWarnings...)
    89  		if err != nil {
    90  			return []ServiceSummary{}, warnings, err
    91  		}
    92  
    93  		serviceSummaries = append(serviceSummaries, summary)
    94  	}
    95  
    96  	return serviceSummaries, warnings, nil
    97  }
    98  
    99  func (actor Actor) createServiceSummary(service ccv2.Service) (ServiceSummary, ccv2.Warnings, error) {
   100  	planSummaries, warnings, err := actor.getPlanSummariesForService(service)
   101  	if err != nil {
   102  		return ServiceSummary{}, warnings, err
   103  	}
   104  
   105  	return ServiceSummary{Service: Service(service), Plans: planSummaries}, warnings, nil
   106  }
   107  
   108  func (actor Actor) getPlanSummariesForService(service ccv2.Service) ([]ServicePlanSummary, ccv2.Warnings, error) {
   109  	plans, warnings, err := actor.CloudControllerClient.GetServicePlans(ccv2.Filter{
   110  		Type:     constant.ServiceGUIDFilter,
   111  		Operator: constant.EqualOperator,
   112  		Values:   []string{service.GUID},
   113  	})
   114  	if err != nil {
   115  		return []ServicePlanSummary{}, warnings, err
   116  	}
   117  
   118  	var planSummaries []ServicePlanSummary
   119  	for _, plan := range plans {
   120  		planSummaries = append(planSummaries, ServicePlanSummary{ServicePlan: ServicePlan(plan)})
   121  	}
   122  
   123  	return planSummaries, warnings, nil
   124  }