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

     1  package actors
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/actors/brokerbuilder"
     5  	"code.cloudfoundry.org/cli/cf/actors/servicebuilder"
     6  	"code.cloudfoundry.org/cli/cf/api/organizations"
     7  	"code.cloudfoundry.org/cli/cf/models"
     8  )
     9  
    10  //go:generate counterfeiter . ServiceActor
    11  
    12  type ServiceActor interface {
    13  	FilterBrokers(brokerFlag string, serviceFlag string, orgFlag string) ([]models.ServiceBroker, error)
    14  }
    15  
    16  type ServiceHandler struct {
    17  	orgRepo        organizations.OrganizationRepository
    18  	brokerBuilder  brokerbuilder.BrokerBuilder
    19  	serviceBuilder servicebuilder.ServiceBuilder
    20  }
    21  
    22  func NewServiceHandler(org organizations.OrganizationRepository, brokerBuilder brokerbuilder.BrokerBuilder, serviceBuilder servicebuilder.ServiceBuilder) ServiceHandler {
    23  	return ServiceHandler{
    24  		orgRepo:        org,
    25  		brokerBuilder:  brokerBuilder,
    26  		serviceBuilder: serviceBuilder,
    27  	}
    28  }
    29  
    30  func (actor ServiceHandler) FilterBrokers(brokerFlag string, serviceFlag string, orgFlag string) ([]models.ServiceBroker, error) {
    31  	if orgFlag == "" {
    32  		return actor.getServiceBrokers(brokerFlag, serviceFlag)
    33  	}
    34  	_, err := actor.orgRepo.FindByName(orgFlag)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	return actor.buildBrokersVisibleFromOrg(brokerFlag, serviceFlag, orgFlag)
    39  }
    40  
    41  func (actor ServiceHandler) getServiceBrokers(brokerName string, serviceName string) ([]models.ServiceBroker, error) {
    42  	if serviceName != "" {
    43  		broker, err := actor.brokerBuilder.GetBrokerWithSpecifiedService(serviceName)
    44  		if err != nil {
    45  			return nil, err
    46  		}
    47  
    48  		if brokerName != "" {
    49  			if broker.Name != brokerName {
    50  				return nil, nil
    51  			}
    52  		}
    53  		return []models.ServiceBroker{broker}, nil
    54  	}
    55  
    56  	if brokerName != "" && serviceName == "" {
    57  		broker, err := actor.brokerBuilder.GetBrokerWithAllServices(brokerName)
    58  		if err != nil {
    59  			return nil, err
    60  		}
    61  		return []models.ServiceBroker{broker}, nil
    62  	}
    63  
    64  	return actor.brokerBuilder.GetAllServiceBrokers()
    65  }
    66  
    67  func (actor ServiceHandler) buildBrokersVisibleFromOrg(brokerFlag string, serviceFlag string, orgFlag string) ([]models.ServiceBroker, error) {
    68  	if serviceFlag != "" && brokerFlag != "" {
    69  		service, err := actor.serviceBuilder.GetServiceVisibleToOrg(serviceFlag, orgFlag)
    70  		if err != nil {
    71  			return nil, err
    72  		}
    73  		broker, err := actor.brokerBuilder.AttachSpecificBrokerToServices(brokerFlag, []models.ServiceOffering{service})
    74  		if err != nil {
    75  			return nil, err
    76  		}
    77  		return []models.ServiceBroker{broker}, nil
    78  	}
    79  
    80  	if serviceFlag != "" && brokerFlag == "" {
    81  		service, err := actor.serviceBuilder.GetServiceVisibleToOrg(serviceFlag, orgFlag)
    82  		if err != nil {
    83  			return nil, err
    84  		}
    85  		return actor.brokerBuilder.AttachBrokersToServices([]models.ServiceOffering{service})
    86  	}
    87  
    88  	if serviceFlag == "" && brokerFlag != "" {
    89  		services, err := actor.serviceBuilder.GetServicesVisibleToOrg(orgFlag)
    90  		if err != nil {
    91  			return nil, err
    92  		}
    93  		broker, err := actor.brokerBuilder.AttachSpecificBrokerToServices(brokerFlag, services)
    94  		if err != nil {
    95  			return nil, err
    96  		}
    97  		return []models.ServiceBroker{broker}, nil
    98  	}
    99  
   100  	if serviceFlag == "" && brokerFlag == "" {
   101  		services, err := actor.serviceBuilder.GetServicesVisibleToOrg(orgFlag)
   102  		if err != nil {
   103  			return nil, err
   104  		}
   105  		return actor.brokerBuilder.AttachBrokersToServices(services)
   106  	}
   107  
   108  	return nil, nil
   109  }