github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/actors/servicebuilder/service_builder.go (about)

     1  package servicebuilder
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/actors/planbuilder"
     7  	"code.cloudfoundry.org/cli/cf/api"
     8  	"code.cloudfoundry.org/cli/cf/models"
     9  )
    10  
    11  //go:generate counterfeiter . ServiceBuilder
    12  
    13  type ServiceBuilder interface {
    14  	GetAllServices() ([]models.ServiceOffering, error)
    15  	GetAllServicesWithPlans() ([]models.ServiceOffering, error)
    16  
    17  	GetServiceByNameWithPlans(string) (models.ServiceOffering, error)
    18  	GetServiceByNameWithPlansWithOrgNames(string) (models.ServiceOffering, error)
    19  	GetServiceByNameForSpace(string, string) (models.ServiceOffering, error)
    20  	GetServiceByNameForSpaceWithPlans(string, string) (models.ServiceOffering, error)
    21  	GetServicesByNameForSpaceWithPlans(string, string) (models.ServiceOfferings, error)
    22  	GetServiceByNameForOrg(string, string) (models.ServiceOffering, error)
    23  
    24  	GetServicesForManyBrokers([]string) ([]models.ServiceOffering, error)
    25  	GetServicesForBroker(string) ([]models.ServiceOffering, error)
    26  
    27  	GetServicesForSpace(string) ([]models.ServiceOffering, error)
    28  	GetServicesForSpaceWithPlans(string) ([]models.ServiceOffering, error)
    29  
    30  	GetServiceVisibleToOrg(string, string) (models.ServiceOffering, error)
    31  	GetServicesVisibleToOrg(string) ([]models.ServiceOffering, error)
    32  }
    33  
    34  type Builder struct {
    35  	serviceRepo api.ServiceRepository
    36  	planBuilder planbuilder.PlanBuilder
    37  }
    38  
    39  func NewBuilder(service api.ServiceRepository, planBuilder planbuilder.PlanBuilder) Builder {
    40  	return Builder{
    41  		serviceRepo: service,
    42  		planBuilder: planBuilder,
    43  	}
    44  }
    45  
    46  func (builder Builder) GetAllServices() ([]models.ServiceOffering, error) {
    47  	return builder.serviceRepo.GetAllServiceOfferings()
    48  }
    49  
    50  func (builder Builder) GetAllServicesWithPlans() ([]models.ServiceOffering, error) {
    51  	services, err := builder.GetAllServices()
    52  	if err != nil {
    53  		return []models.ServiceOffering{}, err
    54  	}
    55  
    56  	var plans []models.ServicePlanFields
    57  	for index, service := range services {
    58  		plans, err = builder.planBuilder.GetPlansForService(service.GUID)
    59  		if err != nil {
    60  			return []models.ServiceOffering{}, err
    61  		}
    62  		services[index].Plans = plans
    63  	}
    64  
    65  	return services, err
    66  }
    67  
    68  func (builder Builder) GetServicesForSpace(spaceGUID string) ([]models.ServiceOffering, error) {
    69  	return builder.serviceRepo.GetServiceOfferingsForSpace(spaceGUID)
    70  }
    71  
    72  func (builder Builder) GetServicesForSpaceWithPlans(spaceGUID string) ([]models.ServiceOffering, error) {
    73  	services, err := builder.GetServicesForSpace(spaceGUID)
    74  	if err != nil {
    75  		return []models.ServiceOffering{}, err
    76  	}
    77  
    78  	for index, service := range services {
    79  		services[index].Plans, err = builder.planBuilder.GetPlansForService(service.GUID)
    80  		if err != nil {
    81  			return []models.ServiceOffering{}, err
    82  		}
    83  	}
    84  
    85  	return services, nil
    86  }
    87  
    88  func (builder Builder) GetServiceByNameWithPlans(serviceLabel string) (models.ServiceOffering, error) {
    89  	services, err := builder.serviceRepo.FindServiceOfferingsByLabel(serviceLabel)
    90  	if err != nil {
    91  		return models.ServiceOffering{}, err
    92  	}
    93  	service := returnV2Service(services)
    94  
    95  	service.Plans, err = builder.planBuilder.GetPlansForService(service.GUID)
    96  	if err != nil {
    97  		return models.ServiceOffering{}, err
    98  	}
    99  
   100  	return service, nil
   101  }
   102  
   103  func (builder Builder) GetServiceByNameForOrg(serviceLabel, orgName string) (models.ServiceOffering, error) {
   104  	services, err := builder.serviceRepo.FindServiceOfferingsByLabel(serviceLabel)
   105  	if err != nil {
   106  		return models.ServiceOffering{}, err
   107  	}
   108  
   109  	service, err := builder.attachPlansToServiceForOrg(services[0], orgName)
   110  	if err != nil {
   111  		return models.ServiceOffering{}, err
   112  	}
   113  	return service, nil
   114  }
   115  
   116  func (builder Builder) GetServiceByNameForSpace(serviceLabel, spaceGUID string) (models.ServiceOffering, error) {
   117  	offerings, err := builder.serviceRepo.FindServiceOfferingsForSpaceByLabel(spaceGUID, serviceLabel)
   118  	if err != nil {
   119  		return models.ServiceOffering{}, err
   120  	}
   121  
   122  	for _, offering := range offerings {
   123  		if offering.Provider == "" {
   124  			return offering, nil
   125  		}
   126  	}
   127  
   128  	return models.ServiceOffering{}, errors.New("Could not find service")
   129  }
   130  
   131  func (builder Builder) GetServiceByNameForSpaceWithPlans(serviceLabel, spaceGUID string) (models.ServiceOffering, error) {
   132  	offering, err := builder.GetServiceByNameForSpace(serviceLabel, spaceGUID)
   133  	if err != nil {
   134  		return models.ServiceOffering{}, err
   135  	}
   136  
   137  	offering.Plans, err = builder.planBuilder.GetPlansForService(offering.GUID)
   138  	if err != nil {
   139  		return models.ServiceOffering{}, err
   140  	}
   141  
   142  	return offering, nil
   143  }
   144  
   145  func (builder Builder) GetServicesByNameForSpaceWithPlans(serviceLabel, spaceGUID string) (models.ServiceOfferings, error) {
   146  	offerings, err := builder.serviceRepo.FindServiceOfferingsForSpaceByLabel(serviceLabel, spaceGUID)
   147  	if err != nil {
   148  		return models.ServiceOfferings{}, err
   149  	}
   150  
   151  	for index, offering := range offerings {
   152  		offerings[index].Plans, err = builder.planBuilder.GetPlansForService(offering.GUID)
   153  		if err != nil {
   154  			return models.ServiceOfferings{}, err
   155  		}
   156  	}
   157  
   158  	return offerings, nil
   159  }
   160  
   161  func (builder Builder) GetServiceByNameWithPlansWithOrgNames(serviceLabel string) (models.ServiceOffering, error) {
   162  	services, err := builder.serviceRepo.FindServiceOfferingsByLabel(serviceLabel)
   163  	if err != nil {
   164  		return models.ServiceOffering{}, err
   165  	}
   166  
   167  	service, err := builder.attachPlansToService(services[0])
   168  	if err != nil {
   169  		return models.ServiceOffering{}, err
   170  	}
   171  	return service, nil
   172  }
   173  
   174  func (builder Builder) GetServicesForManyBrokers(brokerGUIDs []string) ([]models.ServiceOffering, error) {
   175  	services, err := builder.serviceRepo.ListServicesFromManyBrokers(brokerGUIDs)
   176  	if err != nil {
   177  		return nil, err
   178  	}
   179  	return builder.populateServicesWithPlansAndOrgs(services)
   180  }
   181  
   182  func (builder Builder) GetServicesForBroker(brokerGUID string) ([]models.ServiceOffering, error) {
   183  	services, err := builder.serviceRepo.ListServicesFromBroker(brokerGUID)
   184  	if err != nil {
   185  		return nil, err
   186  	}
   187  	return builder.populateServicesWithPlansAndOrgs(services)
   188  }
   189  
   190  func (builder Builder) populateServicesWithPlansAndOrgs(services []models.ServiceOffering) ([]models.ServiceOffering, error) {
   191  	serviceGUIDs := []string{}
   192  	for _, service := range services {
   193  		serviceGUIDs = append(serviceGUIDs, service.GUID)
   194  	}
   195  
   196  	plans, err := builder.planBuilder.GetPlansForManyServicesWithOrgs(serviceGUIDs)
   197  	if err != nil {
   198  		return nil, err
   199  	}
   200  	return builder.attachPlansToManyServices(services, plans)
   201  }
   202  
   203  func (builder Builder) GetServiceVisibleToOrg(serviceName string, orgName string) (models.ServiceOffering, error) {
   204  	visiblePlans, err := builder.planBuilder.GetPlansVisibleToOrg(orgName)
   205  	if err != nil {
   206  		return models.ServiceOffering{}, err
   207  	}
   208  
   209  	if len(visiblePlans) == 0 {
   210  		return models.ServiceOffering{}, nil
   211  	}
   212  
   213  	return builder.attachSpecificServiceToPlans(serviceName, visiblePlans)
   214  }
   215  
   216  func (builder Builder) GetServicesVisibleToOrg(orgName string) ([]models.ServiceOffering, error) {
   217  	visiblePlans, err := builder.planBuilder.GetPlansVisibleToOrg(orgName)
   218  	if err != nil {
   219  		return nil, err
   220  	}
   221  
   222  	if len(visiblePlans) == 0 {
   223  		return nil, nil
   224  	}
   225  
   226  	return builder.attachServicesToPlans(visiblePlans)
   227  }
   228  
   229  func (builder Builder) attachPlansToServiceForOrg(service models.ServiceOffering, orgName string) (models.ServiceOffering, error) {
   230  	plans, err := builder.planBuilder.GetPlansForServiceForOrg(service.GUID, orgName)
   231  	if err != nil {
   232  		return models.ServiceOffering{}, err
   233  	}
   234  
   235  	service.Plans = plans
   236  	return service, nil
   237  }
   238  
   239  func (builder Builder) attachPlansToManyServices(services []models.ServiceOffering, plans []models.ServicePlanFields) ([]models.ServiceOffering, error) {
   240  	for _, plan := range plans {
   241  		for index, service := range services {
   242  			if service.GUID == plan.ServiceOfferingGUID {
   243  				services[index].Plans = append(service.Plans, plan)
   244  				break
   245  			}
   246  		}
   247  	}
   248  	return services, nil
   249  }
   250  
   251  func (builder Builder) attachPlansToService(service models.ServiceOffering) (models.ServiceOffering, error) {
   252  	plans, err := builder.planBuilder.GetPlansForServiceWithOrgs(service.GUID)
   253  	if err != nil {
   254  		return models.ServiceOffering{}, err
   255  	}
   256  
   257  	service.Plans = plans
   258  	return service, nil
   259  }
   260  
   261  func (builder Builder) attachServicesToPlans(plans []models.ServicePlanFields) ([]models.ServiceOffering, error) {
   262  	var services []models.ServiceOffering
   263  	servicesMap := make(map[string]models.ServiceOffering)
   264  
   265  	for _, plan := range plans {
   266  		if plan.ServiceOfferingGUID == "" {
   267  			continue
   268  		}
   269  
   270  		if service, ok := servicesMap[plan.ServiceOfferingGUID]; ok {
   271  			service.Plans = append(service.Plans, plan)
   272  			servicesMap[service.GUID] = service
   273  		} else {
   274  			service, err := builder.serviceRepo.GetServiceOfferingByGUID(plan.ServiceOfferingGUID)
   275  			if err != nil {
   276  				return nil, err
   277  			}
   278  			service.Plans = append(service.Plans, plan)
   279  			servicesMap[service.GUID] = service
   280  		}
   281  	}
   282  
   283  	for _, service := range servicesMap {
   284  		services = append(services, service)
   285  	}
   286  
   287  	return services, nil
   288  }
   289  
   290  func (builder Builder) attachSpecificServiceToPlans(serviceName string, plans []models.ServicePlanFields) (models.ServiceOffering, error) {
   291  	services, err := builder.serviceRepo.FindServiceOfferingsByLabel(serviceName)
   292  	if err != nil {
   293  		return models.ServiceOffering{}, err
   294  	}
   295  
   296  	service := services[0]
   297  	for _, plan := range plans {
   298  		if plan.ServiceOfferingGUID == service.GUID {
   299  			service.Plans = append(service.Plans, plan)
   300  		}
   301  	}
   302  
   303  	return service, nil
   304  }
   305  
   306  func returnV2Service(services models.ServiceOfferings) models.ServiceOffering {
   307  	for _, service := range services {
   308  		if service.Provider == "" {
   309  			return service
   310  		}
   311  	}
   312  
   313  	return models.ServiceOffering{}
   314  }