github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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><Return Value>'
    15  // approach to method names.  If the <Top Level Endpoint> and <Return Value>
    16  // are similar, they do not need to be repeated. If a GUID is required for the
    17  // <Top Level Endpoint>, the pluralization is removed from said endpoint in the
    18  // 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  // Use the following table to determine which HTTP Command equates to which
    40  // Action Name:
    41  //   HTTP Command -> Action Name
    42  //   POST -> Create
    43  //   GET -> Get
    44  //   PUT -> Update
    45  //   DELETE -> Delete
    46  //   PATCH -> Patch
    47  //
    48  // Method Locations
    49  //
    50  // Methods exist in the same file as their return type, regardless of which
    51  // endpoint they use.
    52  //
    53  // Error Handling
    54  //
    55  // All error handling that requires parsing the error_code/code returned back
    56  // from the Cloud Controller should be placed in the errorWrapper. Everything
    57  // else can be handled in the individual operations. All parsed cloud
    58  // controller errors should exist in errors.go, all generic HTTP errors should
    59  // exist in the cloudcontroller's errors.go. Errors related to the individaul
    60  // operation should exist at the top of that operation's file.
    61  //
    62  // No inline-relations-depth And summary Endpoints
    63  //
    64  // This package will not use ever use 'inline-relations-depth' or the
    65  // '/summary' endpoints for any operations. These requests can be extremely
    66  // taxing on the Cloud Controller and are avoided at all costs. Additionally,
    67  // the objects returned back from these requests can become extremely
    68  // inconsistant across versions and are problematic to deal with in general.
    69  package ccv2
    70  
    71  import (
    72  	"fmt"
    73  	"runtime"
    74  	"time"
    75  
    76  	"code.cloudfoundry.org/cli/api/cloudcontroller"
    77  	"github.com/tedsuo/rata"
    78  )
    79  
    80  // Warnings are a collection of warnings that the Cloud Controller can return
    81  // back from an API request.
    82  type Warnings []string
    83  
    84  // Client is a client that can be used to talk to a Cloud Controller's V2
    85  // Endpoints.
    86  type Client struct {
    87  	authorizationEndpoint     string
    88  	cloudControllerAPIVersion string
    89  	cloudControllerURL        string
    90  	dopplerEndpoint           string
    91  	minCLIVersion             string
    92  	routingEndpoint           string
    93  	tokenEndpoint             string
    94  
    95  	jobPollingInterval time.Duration
    96  	jobPollingTimeout  time.Duration
    97  
    98  	connection cloudcontroller.Connection
    99  	router     *rata.RequestGenerator
   100  	userAgent  string
   101  	wrappers   []ConnectionWrapper
   102  }
   103  
   104  // Config allows the Client to be configured
   105  type Config struct {
   106  	// AppName is the name of the application/process using the client.
   107  	AppName string
   108  
   109  	// AppVersion is the version of the application/process using the client.
   110  	AppVersion string
   111  
   112  	// JobPollingTimeout is the maximum amount of time a job polls for.
   113  	JobPollingTimeout time.Duration
   114  
   115  	// JobPollingInterval is the wait time between job polls.
   116  	JobPollingInterval time.Duration
   117  
   118  	// Wrappers that apply to the client connection.
   119  	Wrappers []ConnectionWrapper
   120  }
   121  
   122  // NewClient returns a new Cloud Controller Client.
   123  func NewClient(config Config) *Client {
   124  	userAgent := fmt.Sprintf("%s/%s (%s; %s %s)", config.AppName, config.AppVersion, runtime.Version(), runtime.GOARCH, runtime.GOOS)
   125  	return &Client{
   126  		userAgent:          userAgent,
   127  		jobPollingInterval: config.JobPollingInterval,
   128  		jobPollingTimeout:  config.JobPollingTimeout,
   129  		wrappers:           append([]ConnectionWrapper{newErrorWrapper()}, config.Wrappers...),
   130  	}
   131  }