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

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api/resources"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/net"
    11  )
    12  
    13  //go:generate counterfeiter . ServicePlanVisibilityRepository
    14  
    15  type ServicePlanVisibilityRepository interface {
    16  	Create(string, string) error
    17  	List() ([]models.ServicePlanVisibilityFields, error)
    18  	Delete(string) error
    19  	Search(map[string]string) ([]models.ServicePlanVisibilityFields, error)
    20  }
    21  
    22  type CloudControllerServicePlanVisibilityRepository struct {
    23  	config  coreconfig.Reader
    24  	gateway net.Gateway
    25  }
    26  
    27  func NewCloudControllerServicePlanVisibilityRepository(config coreconfig.Reader, gateway net.Gateway) CloudControllerServicePlanVisibilityRepository {
    28  	return CloudControllerServicePlanVisibilityRepository{
    29  		config:  config,
    30  		gateway: gateway,
    31  	}
    32  }
    33  
    34  func (repo CloudControllerServicePlanVisibilityRepository) Create(serviceGUID, orgGUID string) error {
    35  	url := "/v2/service_plan_visibilities"
    36  	data := fmt.Sprintf(`{"service_plan_guid":"%s", "organization_guid":"%s"}`, serviceGUID, orgGUID)
    37  	return repo.gateway.CreateResource(repo.config.APIEndpoint(), url, strings.NewReader(data))
    38  }
    39  
    40  func (repo CloudControllerServicePlanVisibilityRepository) List() (visibilities []models.ServicePlanVisibilityFields, err error) {
    41  	err = repo.gateway.ListPaginatedResources(
    42  		repo.config.APIEndpoint(),
    43  		"/v2/service_plan_visibilities",
    44  		resources.ServicePlanVisibilityResource{},
    45  		func(resource interface{}) bool {
    46  			if spv, ok := resource.(resources.ServicePlanVisibilityResource); ok {
    47  				visibilities = append(visibilities, spv.ToFields())
    48  			}
    49  			return true
    50  		})
    51  	return
    52  }
    53  
    54  func (repo CloudControllerServicePlanVisibilityRepository) Delete(servicePlanGUID string) error {
    55  	path := fmt.Sprintf("/v2/service_plan_visibilities/%s", servicePlanGUID)
    56  	return repo.gateway.DeleteResourceSynchronously(repo.config.APIEndpoint(), path)
    57  }
    58  
    59  func (repo CloudControllerServicePlanVisibilityRepository) Search(queryParams map[string]string) ([]models.ServicePlanVisibilityFields, error) {
    60  	var visibilities []models.ServicePlanVisibilityFields
    61  	err := repo.gateway.ListPaginatedResources(
    62  		repo.config.APIEndpoint(),
    63  		combineQueryParametersWithURI("/v2/service_plan_visibilities", queryParams),
    64  		resources.ServicePlanVisibilityResource{},
    65  		func(resource interface{}) bool {
    66  			if sp, ok := resource.(resources.ServicePlanVisibilityResource); ok {
    67  				visibilities = append(visibilities, sp.ToFields())
    68  			}
    69  			return true
    70  		})
    71  	return visibilities, err
    72  }