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

     1  // Package ccv2 represents a Cloud Controller V2 client.
     2  //
     3  // These sets of packages are still under development/pre-pre-pre...alpha. Use
     4  // at your own risk! Functionality and design may change without warning.
     5  //
     6  // It is currently designed to support Cloud Controller API 2.23.0. However, it
     7  // may include features and endpoints of later API versions.
     8  //
     9  // For more information on the Cloud Controller API see
    10  // https://apidocs.cloudfoundry.org/
    11  //
    12  // Method Naming Conventions
    13  //
    14  // The client takes a '<Action Name><Top Level Endpoint><nth... level
    15  // endpoint><Return Value>' approach to method names.  If the <Top Level
    16  // Endpoint> and <Return Value> are similar, they do not need to be repeated.
    17  // If a GUID is required for the <Top Level Endpoint>, the pluralization is
    18  // removed from said endpoint in the method name.
    19  //
    20  // For Example:
    21  //   Method Name: GetApplication
    22  //   Endpoint: /v2/applications/:guid
    23  //   Action Name: GET
    24  //   Top Level Endpoint: applications
    25  //   Return Value: Application
    26  //
    27  //   Method Name: GetServiceInstances
    28  //   Endpoint: /v2/service_instances
    29  //   Action Name: GET
    30  //   Top Level Endpoint: service_instances
    31  //   Return Value: []ServiceInstance
    32  //
    33  //   Method Name: GetSpaceServiceInstances
    34  //   Endpoint: /v2/spaces/:guid/service_instances
    35  //   Action Name: GET
    36  //   Top Level Endpoint: spaces
    37  //   Return Value: []ServiceInstance
    38  //
    39  //   Method Name: UpdateRouteApplication
    40  //   Endpoint: /v2/routes/:route_guid/apps/:app_guid
    41  //   Action Name: PUT
    42  //   Top Level Endpoint: routes
    43  //   Second Level Endpoint: Application
    44  //   Return Value: Route
    45  //
    46  //   Method Name: DeleteSpaceJob
    47  //   Endpoint: /v2/spaces/:space_guid
    48  //   Action Name: DELETE
    49  //   Top Level Endpoint: spaces
    50  //   Return Value: Job
    51  //
    52  // Use the following table to determine which HTTP Command equates to which
    53  // Action Name:
    54  //   HTTP Command -> Action Name
    55  //   POST -> Create
    56  //   GET -> Get
    57  //   PUT -> Update
    58  //   DELETE -> Delete
    59  //   PATCH -> Patch
    60  //
    61  // Method Locations
    62  //
    63  // Methods exist in the same file as their return type, regardless of which
    64  // endpoint they use.
    65  //
    66  // Error Handling
    67  //
    68  // All error handling that requires parsing the error_code/code returned back
    69  // from the Cloud Controller should be placed in the errorWrapper. Everything
    70  // else can be handled in the individual operations. All errors structs should
    71  // be placed in individual files in the ccerror package.
    72  //
    73  // No inline-relations-depth And summary Endpoints
    74  //
    75  // This package will not ever use 'inline-relations-depth' or the
    76  // '/summary' endpoints for any operations. These requests can be extremely
    77  // taxing on the Cloud Controller and are avoided at all costs. Additionally,
    78  // the objects returned back from these requests can become extremely
    79  // inconsistent across versions and are problematic to deal with in general.
    80  //
    81  // An additional problem that occurs with these endpoints is that lists of 51+
    82  // objects do not get returned by the CC. Example: Summary of Organizations
    83  // includes a list of spaces. If there are 50 spaces, the endpoint returns
    84  // these spaces under the 'spaces' field; if there are 51+, spaces, the
    85  // 'spaces' field is missing from the CC return.
    86  package ccv2
    87  
    88  import (
    89  	"fmt"
    90  	"runtime"
    91  	"time"
    92  
    93  	"code.cloudfoundry.org/cli/api/cloudcontroller"
    94  	"github.com/tedsuo/rata"
    95  )
    96  
    97  // Warnings are a collection of warnings that the Cloud Controller can return
    98  // back from an API request.
    99  type Warnings []string
   100  
   101  // Client is a client that can be used to talk to a Cloud Controller's V2
   102  // Endpoints.
   103  type Client struct {
   104  	authorizationEndpoint     string
   105  	cloudControllerAPIVersion string
   106  	cloudControllerURL        string
   107  	dopplerEndpoint           string
   108  	minCLIVersion             string
   109  	routingEndpoint           string
   110  	tokenEndpoint             string
   111  
   112  	jobPollingInterval time.Duration
   113  	jobPollingTimeout  time.Duration
   114  
   115  	connection cloudcontroller.Connection
   116  	router     *rata.RequestGenerator
   117  	userAgent  string
   118  	wrappers   []ConnectionWrapper
   119  }
   120  
   121  // Config allows the Client to be configured
   122  type Config struct {
   123  	// AppName is the name of the application/process using the client.
   124  	AppName string
   125  
   126  	// AppVersion is the version of the application/process using the client.
   127  	AppVersion string
   128  
   129  	// JobPollingTimeout is the maximum amount of time a job polls for.
   130  	JobPollingTimeout time.Duration
   131  
   132  	// JobPollingInterval is the wait time between job polls.
   133  	JobPollingInterval time.Duration
   134  
   135  	// Wrappers that apply to the client connection.
   136  	Wrappers []ConnectionWrapper
   137  }
   138  
   139  // NewClient returns a new Cloud Controller Client.
   140  func NewClient(config Config) *Client {
   141  	userAgent := fmt.Sprintf("%s/%s (%s; %s %s)", config.AppName, config.AppVersion, runtime.Version(), runtime.GOARCH, runtime.GOOS)
   142  	return &Client{
   143  		userAgent:          userAgent,
   144  		jobPollingInterval: config.JobPollingInterval,
   145  		jobPollingTimeout:  config.JobPollingTimeout,
   146  		wrappers:           append([]ConnectionWrapper{newErrorWrapper()}, config.Wrappers...),
   147  	}
   148  }