-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeis.go
More file actions
129 lines (114 loc) · 4.01 KB
/
deis.go
File metadata and controls
129 lines (114 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Package deis offers a SDK for interacting with the Deis controller API.
//
// This package works by creating a client, which contains session information,
// such as the controller url and user token. The client is then passed to api methods,
// which use it to make requests.
//
// Basic Example
//
// This example creates a client and then lists the apps that the user has access to:
//
// import (
// deis "github.com/deis/controller-sdk-go"
// "github.com/deis/controller-sdk-go/apps"
// )
//
// // Verify SSL, Controller URL, API Token
// client, err := deis.New(true, "deis.test.io", "abc123")
// if err != nil {
// log.Fatal(err)
// }
// apps, _, err := apps.List(client, 100)
// if err != nil {
// log.Fatal(err)
// }
//
// Authentication
//
// If you don't already have a token for a user, you can retrieve one with a
// username and password.
//
// import (
// deis "github.com/deis/controller-sdk-go"
// "github.com/deis/controller-sdk-go/apps"
// )
//
// // Create a client with a blank token to pass to login.
// client, err := deis.New(true, "deis.test.io", "")
// if err != nil {
// log.Fatal(err)
// }
// token, err := auth.Login(client, "user", "password")
// if err != nil {
// log.Fatal(err)
// }
// // Set the client to use the retrieved token
// client.Token = token
//
// Learning More
//
// See the godoc for the SDK's subpackages to learn more about specific SDK actions.
package deis
import (
"errors"
"fmt"
"net/http"
"net/url"
"github.com/goware/urlx"
)
// Client oversees the interaction between the deis and controller
type Client struct {
// HTTPClient is the transport that is used to communicate with the API.
HTTPClient *http.Client
// VerifySSL determines whether or not to verify SSL connections.
// This should be true unless you know the controller is using untrusted SSL keys.
VerifySSL bool
// ControllerURL is the URL used to communicate with the controller.
ControllerURL *url.URL
// UserAgent is the user agent used when making requests.
UserAgent string
// API Version used by the controller, set after a http request.
ControllerAPIVersion string
// Version of the deis platform in use, set after a http request.
DeisVersion string
// Token is used to authenticate the request against the API.
Token string
}
// APIVersion is the api version compatible with the SDK.
//
// In general, using an SDK that is a minor version out of date with the target controller
// is probably safe, as the deis controller api follows semantic versioning and is backward
// compatible. However, using a SDK that is newer or a major version different than the
// controller is unsafe.
//
// If the SDK detects an API version mismatch, it will return ErrAPIMismatch.
const APIVersion = "2.2"
var (
// ErrAPIMismatch occurs when the sdk is using a different api version than the deis.
ErrAPIMismatch = errors.New("API Version Mismatch between server and deis")
// DefaultUserAgent is used as the default user agent when making requests.
DefaultUserAgent = fmt.Sprintf("Deis Go SDK V%s", APIVersion)
)
// IsErrAPIMismatch returns true if err is an ErrAPIMismatch, false otherwise
func IsErrAPIMismatch(err error) bool {
return err == ErrAPIMismatch
}
// New creates a new client to communicate with the api.
// The controllerURL is the url of the controller component, by default deis.<cluster url>.com
// verifySSL determines whether or not to verify SSL connections.
// This should be true unless you know the controller is using untrusted SSL keys.
func New(verifySSL bool, controllerURL string, token string) (*Client, error) {
// urlx, unlike the native url library, uses sane defaults when URL parsing,
// preventing issues like missing schemes.
u, err := urlx.Parse(controllerURL)
if err != nil {
return nil, err
}
return &Client{
HTTPClient: createHTTPClient(verifySSL),
VerifySSL: verifySSL,
ControllerURL: u,
Token: token,
UserAgent: DefaultUserAgent,
}, nil
}