github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/api/service_plan.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  
     8  	"code.cloudfoundry.org/cli/cf/api/resources"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/net"
    12  )
    13  
    14  //go:generate counterfeiter . ServicePlanRepository
    15  
    16  type ServicePlanRepository interface {
    17  	Search(searchParameters map[string]string) ([]models.ServicePlanFields, error)
    18  	Update(models.ServicePlanFields, string, bool) error
    19  	ListPlansFromManyServices(serviceGUIDs []string) ([]models.ServicePlanFields, error)
    20  }
    21  
    22  type CloudControllerServicePlanRepository struct {
    23  	config  coreconfig.Reader
    24  	gateway net.Gateway
    25  }
    26  
    27  func NewCloudControllerServicePlanRepository(config coreconfig.Reader, gateway net.Gateway) CloudControllerServicePlanRepository {
    28  	return CloudControllerServicePlanRepository{
    29  		config:  config,
    30  		gateway: gateway,
    31  	}
    32  }
    33  
    34  func (repo CloudControllerServicePlanRepository) Update(servicePlan models.ServicePlanFields, serviceGUID string, public bool) error {
    35  	return repo.gateway.UpdateResource(
    36  		repo.config.APIEndpoint(),
    37  		fmt.Sprintf("/v2/service_plans/%s", servicePlan.GUID),
    38  		strings.NewReader(fmt.Sprintf(`{"public":%t}`, public)),
    39  	)
    40  }
    41  
    42  func (repo CloudControllerServicePlanRepository) ListPlansFromManyServices(serviceGUIDs []string) ([]models.ServicePlanFields, error) {
    43  	serviceGUIDsString := strings.Join(serviceGUIDs, ",")
    44  	plans := []models.ServicePlanFields{}
    45  
    46  	err := repo.gateway.ListPaginatedResources(
    47  		repo.config.APIEndpoint(),
    48  		fmt.Sprintf("/v2/service_plans?q=%s", url.QueryEscape("service_guid IN "+serviceGUIDsString)),
    49  		resources.ServicePlanResource{},
    50  		func(resource interface{}) bool {
    51  			if plan, ok := resource.(resources.ServicePlanResource); ok {
    52  				plans = append(plans, plan.ToFields())
    53  			}
    54  			return true
    55  		})
    56  	return plans, err
    57  }
    58  
    59  func (repo CloudControllerServicePlanRepository) Search(queryParams map[string]string) (plans []models.ServicePlanFields, err error) {
    60  	err = repo.gateway.ListPaginatedResources(
    61  		repo.config.APIEndpoint(),
    62  		combineQueryParametersWithURI("/v2/service_plans", queryParams),
    63  		resources.ServicePlanResource{},
    64  		func(resource interface{}) bool {
    65  			if sp, ok := resource.(resources.ServicePlanResource); ok {
    66  				plans = append(plans, sp.ToFields())
    67  			}
    68  			return true
    69  		})
    70  	return
    71  }
    72  
    73  func combineQueryParametersWithURI(uri string, queryParams map[string]string) string {
    74  	if len(queryParams) == 0 {
    75  		return uri
    76  	}
    77  
    78  	params := []string{}
    79  	for key, value := range queryParams {
    80  		params = append(params, url.QueryEscape(key+":"+value))
    81  	}
    82  
    83  	return uri + "?q=" + strings.Join(params, "%3B")
    84  }