github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+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.LastOperation.Type = instanceSummary.LastOperation.Type
    35  		instance.LastOperation.State = instanceSummary.LastOperation.State
    36  		instance.LastOperation.Description = instanceSummary.LastOperation.Description
    37  		instance.ApplicationNames = applicationNames
    38  		instance.ServicePlan = servicePlan
    39  		instance.ServiceOffering = serviceOffering
    40  
    41  		instances = append(instances, instance)
    42  	}
    43  
    44  	return instances
    45  }
    46  
    47  func (resource ServiceInstancesSummaries) findApplicationNamesForInstance(instanceName string) []string {
    48  	var applicationNames []string
    49  	for _, app := range resource.Apps {
    50  		for _, name := range app.ServiceNames {
    51  			if name == instanceName {
    52  				applicationNames = append(applicationNames, app.Name)
    53  			}
    54  		}
    55  	}
    56  
    57  	return applicationNames
    58  }
    59  
    60  type ServiceInstanceSummaryApp struct {
    61  	Name         string
    62  	ServiceNames []string `json:"service_names"`
    63  }
    64  
    65  type LastOperationSummary struct {
    66  	Type        string `json:"type"`
    67  	State       string `json:"state"`
    68  	Description string `json:"description"`
    69  }
    70  
    71  type ServiceInstanceSummary struct {
    72  	Name          string
    73  	LastOperation LastOperationSummary `json:"last_operation"`
    74  	ServicePlan   ServicePlanSummary   `json:"service_plan"`
    75  }
    76  
    77  type ServicePlanSummary struct {
    78  	Name            string
    79  	GUID            string
    80  	ServiceOffering ServiceOfferingSummary `json:"service"`
    81  }
    82  
    83  type ServiceOfferingSummary struct {
    84  	Label    string
    85  	Provider string
    86  	Version  string
    87  }
    88  
    89  //go:generate counterfeiter . ServiceSummaryRepository
    90  
    91  type ServiceSummaryRepository interface {
    92  	GetSummariesInCurrentSpace() ([]models.ServiceInstance, error)
    93  }
    94  
    95  type CloudControllerServiceSummaryRepository struct {
    96  	config  coreconfig.Reader
    97  	gateway net.Gateway
    98  }
    99  
   100  func NewCloudControllerServiceSummaryRepository(config coreconfig.Reader, gateway net.Gateway) CloudControllerServiceSummaryRepository {
   101  	return CloudControllerServiceSummaryRepository{
   102  		config:  config,
   103  		gateway: gateway,
   104  	}
   105  }
   106  
   107  func (repo CloudControllerServiceSummaryRepository) GetSummariesInCurrentSpace() ([]models.ServiceInstance, error) {
   108  	var instances []models.ServiceInstance
   109  	path := fmt.Sprintf("%s/v2/spaces/%s/summary", repo.config.APIEndpoint(), repo.config.SpaceFields().GUID)
   110  	resource := new(ServiceInstancesSummaries)
   111  
   112  	err := repo.gateway.GetResource(path, resource)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	instances = resource.ToModels()
   118  
   119  	return instances, nil
   120  }