github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv3/client.go (about)

     1  // Package ccv3 represents a Cloud Controller V3 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 3.0.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  // Additionally, if the endpoint is an "action" endpoint, do not include the
    21  // word "Action" in the method name.
    22  //
    23  // For Example:
    24  //   Method Name: GetApplication
    25  //   Endpoint: /v3/applications/:guid
    26  //   Action Name: Get
    27  //   Top Level Endpoint: applications
    28  //   Return Value: Application
    29  //
    30  //   Method Name: GetServiceInstances
    31  //   Endpoint: /v3/service_instances
    32  //   Action Name: Get
    33  //   Top Level Endpoint: service_instances
    34  //   Return Value: []ServiceInstance
    35  //
    36  //   Method Name: GetSpaceServiceInstances
    37  //   Endpoint: /v3/spaces/:guid/service_instances
    38  //   Action Name: Get
    39  //   Top Level Endpoint: spaces
    40  //   Return Value: []ServiceInstance
    41  //
    42  //   Method Name: CreateApplicationTask
    43  //   Endpoint: /v3/apps/:application_guid/task
    44  //   Action Name: Post
    45  //   Top Level Endpoint: apps
    46  //   Return Value: Task
    47  //
    48  // Use the following table to determine which HTTP Command equates to which
    49  // Action Name:
    50  //   HTTP Command -> Action Name
    51  //   POST -> Create OR Update*
    52  //   GET -> Get
    53  //   PUT -> Update
    54  //   DELETE -> Delete
    55  //   PATCH -> Update
    56  //
    57  // * - In some cases POSTs are updating resources, in these cases the method
    58  // should be called Update, not Create.
    59  //
    60  // Method Locations
    61  //
    62  // Methods exist in the same file as their return type, regardless of which
    63  // endpoint they use.
    64  //
    65  // Error Handling
    66  //
    67  // All error handling that requires parsing the error_code/code returned back
    68  // from the Cloud Controller should be placed in the errorWrapper. Everything
    69  // else can be handled in the individual operations. All parsed cloud
    70  // controller errors should exist in errors.go, all generic HTTP errors should
    71  // exist in the cloudcontroller's errors.go. Errors related to the individual
    72  // operation should exist at the top of that operation's file.
    73  package ccv3
    74  
    75  import (
    76  	"fmt"
    77  	"runtime"
    78  	"time"
    79  
    80  	"code.cloudfoundry.org/cli/api/cloudcontroller"
    81  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
    82  )
    83  
    84  // Warnings are a collection of warnings that the Cloud Controller can return
    85  // back from an API request.
    86  type Warnings []string
    87  
    88  // Client can be used to talk to a Cloud Controller's V3 Endpoints.
    89  type Client struct {
    90  	Info
    91  	cloudControllerURL string
    92  
    93  	connection cloudcontroller.Connection
    94  	router     *internal.Router
    95  	userAgent  string
    96  	wrappers   []ConnectionWrapper
    97  
    98  	jobPollingInterval time.Duration
    99  	jobPollingTimeout  time.Duration
   100  
   101  	clock Clock
   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 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  		clock:              new(internal.RealTime),
   127  		userAgent:          userAgent,
   128  		jobPollingInterval: config.JobPollingInterval,
   129  		jobPollingTimeout:  config.JobPollingTimeout,
   130  		wrappers:           append([]ConnectionWrapper{newErrorWrapper()}, config.Wrappers...),
   131  	}
   132  }
   133  
   134  // TestClient returns a new client explicitly meant for internal testing.  This
   135  // should not be used for production code.
   136  func TestClient(config Config, clock Clock) *Client {
   137  	client := NewClient(config)
   138  	client.clock = clock
   139  	return client
   140  }