github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/service_summary.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"code.cloudfoundry.org/cli/cf/models"
     8  	"code.cloudfoundry.org/cli/cf/net"
     9  )
    10  
    11  type ServiceInstancesSummaries struct {
    12  	Apps             []ServiceInstanceSummaryApp
    13  	ServiceInstances []ServiceInstanceSummary `json:"services"`
    14  }
    15  
    16  func (resource ServiceInstancesSummaries) ToModels() []models.ServiceInstance {
    17  	var instances []models.ServiceInstance
    18  	for _, instanceSummary := range resource.ServiceInstances {
    19  		applicationNames := resource.findApplicationNamesForInstance(instanceSummary.Name)
    20  
    21  		planSummary := instanceSummary.ServicePlan
    22  		servicePlan := models.ServicePlanFields{}
    23  		servicePlan.Name = planSummary.Name
    24  		servicePlan.GUID = planSummary.GUID
    25  
    26  		offeringSummary := planSummary.ServiceOffering
    27  		serviceOffering := models.ServiceOfferingFields{}
    28  		serviceOffering.Label = offeringSummary.Label
    29  		serviceOffering.Provider = offeringSummary.Provider
    30  		serviceOffering.Version = offeringSummary.Version
    31  
    32  		instance := models.ServiceInstance{}
    33  		instance.Name = instanceSummary.Name
    34  		instance.GUID = instanceSummary.GUID
    35  		instance.LastOperation.Type = instanceSummary.LastOperation.Type
    36  		instance.LastOperation.State = instanceSummary.LastOperation.State
    37  		instance.LastOperation.Description = instanceSummary.LastOperation.Description
    38  		instance.ApplicationNames = applicationNames
    39  		instance.ServicePlan = servicePlan
    40  		instance.ServiceOffering = serviceOffering
    41  
    42  		instances = append(instances, instance)
    43  	}
    44  
    45  	return instances
    46  }
    47  
    48  func (resource ServiceInstancesSummaries) findApplicationNamesForInstance(instanceName string) []string {
    49  	var applicationNames []string
    50  	for _, app := range resource.Apps {
    51  		for _, name := range app.ServiceNames {
    52  			if name == instanceName {
    53  				applicationNames = append(applicationNames, app.Name)
    54  			}
    55  		}
    56  	}
    57  
    58  	return applicationNames
    59  }
    60  
    61  type ServiceInstanceSummaryApp struct {
    62  	Name         string
    63  	ServiceNames []string `json:"service_names"`
    64  }
    65  
    66  type LastOperationSummary struct {
    67  	Type        string `json:"type"`
    68  	State       string `json:"state"`
    69  	Description string `json:"description"`
    70  }
    71  
    72  type ServiceInstanceSummary struct {
    73  	Name          string
    74  	GUID          string               `json:"guid"`
    75  	LastOperation LastOperationSummary `json:"last_operation"`
    76  	ServicePlan   ServicePlanSummary   `json:"service_plan"`
    77  }
    78  
    79  type ServicePlanSummary struct {
    80  	Name            string
    81  	GUID            string
    82  	ServiceOffering ServiceOfferingSummary `json:"service"`
    83  }
    84  
    85  type ServiceOfferingSummary struct {
    86  	Label    string
    87  	Provider string
    88  	Version  string
    89  }
    90  
    91  //go:generate counterfeiter . ServiceSummaryRepository
    92  
    93  type ServiceSummaryRepository interface {
    94  	GetSummariesInCurrentSpace() ([]models.ServiceInstance, error)
    95  }
    96  
    97  type CloudControllerServiceSummaryRepository struct {
    98  	config  coreconfig.Reader
    99  	gateway net.Gateway
   100  }
   101  
   102  func NewCloudControllerServiceSummaryRepository(config coreconfig.Reader, gateway net.Gateway) CloudControllerServiceSummaryRepository {
   103  	return CloudControllerServiceSummaryRepository{
   104  		config:  config,
   105  		gateway: gateway,
   106  	}
   107  }
   108  
   109  func (repo CloudControllerServiceSummaryRepository) GetSummariesInCurrentSpace() ([]models.ServiceInstance, error) {
   110  	var instances []models.ServiceInstance
   111  	path := fmt.Sprintf("%s/v2/spaces/%s/summary", repo.config.APIEndpoint(), repo.config.SpaceFields().GUID)
   112  	resource := new(ServiceInstancesSummaries)
   113  
   114  	err := repo.gateway.GetResource(path, resource)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	instances = resource.ToModels()
   120  
   121  	return instances, nil
   122  }