github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/query.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"net/url"
     5  	"strings"
     6  )
     7  
     8  // QueryKey is the type of query that is being selected on.
     9  type QueryKey string
    10  
    11  const (
    12  	// AppGUIDFilter is a query parameter for listing objects by app GUID.
    13  	AppGUIDFilter QueryKey = "app_guids"
    14  	// AvailableFilter is a query parameter for listing available resources
    15  	AvailableFilter QueryKey = "available"
    16  	// GUIDFilter is a query parameter for listing objects by GUID.
    17  	GUIDFilter QueryKey = "guids"
    18  	// LabelSelectorFilter is a query parameter for listing objects by label
    19  	LabelSelectorFilter QueryKey = "label_selector"
    20  	// NameFilter is a query parameter for listing objects by name.
    21  	NameFilter QueryKey = "names"
    22  	// NoRouteFilter is a query parameter for skipping route creation and unmapping existing routes.
    23  	NoRouteFilter QueryKey = "no_route"
    24  	// OrganizationGUIDFilter is a query parameter for listing objects by Organization GUID.
    25  	OrganizationGUIDFilter QueryKey = "organization_guids"
    26  	// SequenceIDFilter is a query parameter for listing objects by sequence ID.
    27  	SequenceIDFilter QueryKey = "sequence_ids"
    28  	// RouteGUIDFilter is a query parameter for listing objects by Route GUID.
    29  	RouteGUIDFilter QueryKey = "route_guids"
    30  	// ServiceInstanceGUIDFilter is a query parameter for listing objects by Service Instance GUID.
    31  	ServiceInstanceGUIDFilter QueryKey = "service_instance_guids"
    32  	// SpaceGUIDFilter is a query parameter for listing objects by Space GUID.
    33  	SpaceGUIDFilter QueryKey = "space_guids"
    34  	// StatusValueFilter is a query parameter for listing deployments by status.value
    35  	StatusValueFilter QueryKey = "status_values"
    36  	// DomainGUIDFilter is a query param for listing events by target_guid
    37  	TargetGUIDFilter QueryKey = "target_guids"
    38  	// DomainGUIDFilter is a query param for listing objects by domain_guid
    39  	DomainGUIDFilter QueryKey = "domain_guids"
    40  	// HostsFilter is a query param for listing objects by hostname
    41  	HostsFilter QueryKey = "hosts"
    42  	// HostFilter is a query param for getting an object with the given host
    43  	HostFilter QueryKey = "host"
    44  	// Origins filter is a query parameter when getting a user by origin (Note: CAPI will return an error if usernames filter is not also provided)
    45  	OriginsFilter QueryKey = "origins"
    46  	// PathsFilter is a query param for listing objects by path
    47  	PathsFilter QueryKey = "paths"
    48  	// PathFilter is a query param for getting an object with the given host
    49  	PathFilter QueryKey = "path"
    50  	// PortFilter is a query param for getting an object with the given port (TCP routes)
    51  	PortFilter QueryKey = "port"
    52  	// PortsFilter is a query param for getting an object with the given ports (TCP routes)
    53  	PortsFilter QueryKey = "ports"
    54  	// RoleTypesFilter is a query param for getting a role by type
    55  	RoleTypesFilter QueryKey = "types"
    56  	// StackFilter is a query parameter for listing objects by stack name
    57  	StackFilter QueryKey = "stacks"
    58  	// TypeFiler is a query parameter for selecting binding type
    59  	TypeFilter QueryKey = "type"
    60  	// UnmappedFilter is a query parameter specifying unmapped routes
    61  	UnmappedFilter QueryKey = "unmapped"
    62  	// UserGUIDFilter is a query parameter when getting a user by GUID
    63  	UserGUIDFilter QueryKey = "user_guids"
    64  	// UsernamesFilter is a query parameter when getting a user by username
    65  	UsernamesFilter QueryKey = "usernames"
    66  	// StatesFilter is a query parameter when getting a package's droplets by state
    67  	VersionsFilter QueryKey = "versions"
    68  	// VersionsFilter is a query parameter when getting an apps revisions by version
    69  	StatesFilter QueryKey = "states"
    70  	// ServiceBrokerNamesFilter is a query parameter when getting plans or offerings according to the Service Brokers that it relates to
    71  	ServiceBrokerNamesFilter QueryKey = "service_broker_names"
    72  	// ServiceBrokerGUIDsFilter is a query parameter for getting resources according to the service broker GUID
    73  	ServiceBrokerGUIDsFilter QueryKey = "service_broker_guids"
    74  	// ServiceOfferingNamesFilter is a query parameter when getting a plan according to the Service Offerings that it relates to
    75  	ServiceOfferingNamesFilter QueryKey = "service_offering_names"
    76  	// ServiceOfferingGUIDsFilter is a query parameter when getting resources according to service offering GUIDs
    77  	ServiceOfferingGUIDsFilter QueryKey = "service_offering_guids"
    78  	// FieldsServiceOfferingServiceBroker is a query parameter to include specific fields from a service broker in a plan response
    79  	FieldsServiceOfferingServiceBroker QueryKey = "fields[service_offering.service_broker]"
    80  	// FieldsServiceBroker is a query parameter to include specific fields from a service broker in an offering response
    81  	FieldsServiceBroker QueryKey = "fields[service_broker]"
    82  	// FieldsServicePlan is a query parameter to include specific fields from a service plan
    83  	FieldsServicePlan QueryKey = "fields[service_plan]"
    84  	// FieldsServicePlanServiceOffering is a query parameter to include specific fields from a service offering
    85  	FieldsServicePlanServiceOffering QueryKey = "fields[service_plan.service_offering]"
    86  	// FieldsServicePlanServiceOfferingServiceBroker is a query parameter to include specific fields from a service broker
    87  	FieldsServicePlanServiceOfferingServiceBroker QueryKey = "fields[service_plan.service_offering.service_broker]"
    88  	// FieldsSpace is a query parameter to include specific fields from a space
    89  	FieldsSpace QueryKey = "fields[space]"
    90  	// FieldsSpaceOrganization is a query parameter to include specific fields from a organization
    91  	FieldsSpaceOrganization QueryKey = "fields[space.organization]"
    92  
    93  	// OrderBy is a query parameter to specify how to order objects.
    94  	OrderBy QueryKey = "order_by"
    95  	// PerPage is a query parameter for specifying the number of results per page.
    96  	PerPage QueryKey = "per_page"
    97  	// Page is a query parameter for specifying the number of the requested page.
    98  	Page QueryKey = "page"
    99  	// Include is a query parameter for specifying other resources associated with the
   100  	// resource returned by the endpoint
   101  	Include QueryKey = "include"
   102  
   103  	// GloballyEnabledStaging is the query parameter for getting only security groups that are globally enabled for staging
   104  	GloballyEnabledStaging QueryKey = "globally_enabled_staging"
   105  
   106  	// GloballyEnabledRunning is the query parameter for getting only security groups that are globally enabled for running
   107  	GloballyEnabledRunning QueryKey = "globally_enabled_running"
   108  
   109  	// NameOrder is a query value for ordering by name. This value is used in
   110  	// conjunction with the OrderBy QueryKey.
   111  	NameOrder = "name"
   112  
   113  	// PositionOrder is a query value for ordering by position. This value is
   114  	// used in conjunction with the OrderBy QueryKey.
   115  	PositionOrder = "position"
   116  
   117  	// CreatedAtDescendingOrder is a query value for ordering by created_at timestamp,
   118  	// in descending order.
   119  	CreatedAtDescendingOrder = "-created_at"
   120  
   121  	// SourceGUID is the query parameter for getting an object. Currently it's used as a package GUID
   122  	// to retrieve a package to later copy it to an app (CopyPackage())
   123  	SourceGUID = "source_guid"
   124  
   125  	// Purge is a query parameter used on a Delete request to indicate that dependent resources should also be deleted
   126  	Purge = "purge"
   127  
   128  	// MaxPerPage is the largest value of "per_page" that should be used
   129  	MaxPerPage = "5000"
   130  )
   131  
   132  // Query is additional settings that can be passed to some requests that can
   133  // filter, sort, etc. the results.
   134  type Query struct {
   135  	Key    QueryKey
   136  	Values []string
   137  }
   138  
   139  // FormatQueryParameters converts a Query object into a collection that
   140  // cloudcontroller.Request can accept.
   141  func FormatQueryParameters(queries []Query) url.Values {
   142  	params := url.Values{}
   143  	for _, query := range queries {
   144  		if query.Key == NameFilter {
   145  			encodedParamValues := []string{}
   146  			for _, valString := range query.Values {
   147  				commaEncoded := strings.ReplaceAll(valString, ",", "%2C")
   148  				encodedParamValues = append(encodedParamValues, commaEncoded)
   149  			}
   150  			query.Values = encodedParamValues
   151  		}
   152  
   153  		params.Add(string(query.Key), strings.Join(query.Values, ","))
   154  	}
   155  
   156  	return params
   157  }