1
// Copyright 2014 ALTOROS
2
// Licensed under the AGPLv3, see LICENSE file for details.
11
"github.com/altoros/gosigma/https"
14
// A RequestSpec defines the type of client request
18
// RequestShort requests server to return short object representation
19
RequestShort RequestSpec = false
20
// RequestDetail requests server to return detail object representation
21
RequestDetail RequestSpec = true
24
// A LibrarySpec defines the type of drive library to use
28
// LibraryAccount requests server to use user account drive library
29
LibraryAccount LibrarySpec = false
30
// LibraryMedia requests server to use public drive library
31
LibraryMedia LibrarySpec = true
34
// A Client sends and receives requests to CloudSigma endpoint
39
operationTimeout time.Duration
42
var errEmptyUsername = errors.New("username is not allowed to be empty")
43
var errEmptyPassword = errors.New("password is not allowed to be empty")
44
var errEmptyUUID = errors.New("uuid is not allowed to be empty")
46
// NewClient returns new CloudSigma client object
47
func NewClient(endpoint string, username, password string,
48
tlsConfig *tls.Config) (*Client, error) {
50
endpoint = ResolveEndpoint(endpoint)
52
if len(username) == 0 {
53
return nil, errEmptyUsername
56
if len(password) == 0 {
57
return nil, errEmptyPassword
62
https: https.NewAuthClient(username, password, tlsConfig),
68
// ConnectTimeout sets connection timeout
69
func (c Client) ConnectTimeout(timeout time.Duration) {
70
c.https.ConnectTimeout(timeout)
73
// GetConnectTimeout returns connection timeout for the object
74
func (c Client) GetConnectTimeout() time.Duration {
75
return c.https.GetConnectTimeout()
78
// ReadWriteTimeout sets read-write timeout
79
func (c Client) ReadWriteTimeout(timeout time.Duration) {
80
c.https.ReadWriteTimeout(timeout)
83
// GetReadWriteTimeout returns connection timeout for the object
84
func (c Client) GetReadWriteTimeout() time.Duration {
85
return c.https.GetReadWriteTimeout()
88
// OperationTimeout sets timeout for cloud operations (like cloning, starting, stopping etc)
89
func (c *Client) OperationTimeout(timeout time.Duration) {
90
c.operationTimeout = timeout
93
// GetOperationTimeout gets timeout for cloud operations (like cloning, starting, stopping etc)
94
func (c Client) GetOperationTimeout() time.Duration {
95
return c.operationTimeout
98
// Logger sets logger for http traces
99
func (c *Client) Logger(logger https.Logger) {
101
c.https.Logger(logger)
104
// Servers in current account
105
func (c *Client) Servers(rqspec RequestSpec) ([]Server, error) {
106
objs, err := c.getServers(rqspec)
111
servers := make([]Server, len(objs))
112
for i := 0; i < len(objs); i++ {
113
servers[i] = &server{
122
// ServersFiltered in current account with filter applied
123
func (c *Client) ServersFiltered(rqspec RequestSpec, filter func(s Server) bool) ([]Server, error) {
124
objs, err := c.getServers(rqspec)
129
servers := make([]Server, 0, len(objs))
130
for i := 0; i < len(objs); i++ {
136
servers = append(servers, s)
143
// Server returns given server by uuid, requesting endpoint for server information
144
func (c *Client) Server(uuid string) (Server, error) {
145
obj, err := c.getServer(uuid)
158
// CreateServer in CloudSigma user account
159
func (c *Client) CreateServer(components Components) (Server, error) {
160
objs, err := c.createServer(components)
166
return nil, errors.New("no servers in response from endpoint")
177
// StartServer by uuid of server instance.
178
func (c Client) StartServer(uuid string, avoid []string) error {
179
return c.startServer(uuid, avoid)
182
// StopServer by uuid of server instance
183
func (c Client) StopServer(uuid string) error {
184
return c.stopServer(uuid)
187
// RemoveServer by uuid of server instance with an option recursively removing attached drives.
188
// See RecurseXXX constants in server.go file.
189
func (c Client) RemoveServer(uuid, recurse string) error {
190
return c.removeServer(uuid, recurse)
193
// Drives returns list of drives
194
func (c *Client) Drives(rqspec RequestSpec, libspec LibrarySpec) ([]Drive, error) {
195
objs, err := c.getDrives(rqspec, libspec)
200
drives := make([]Drive, len(objs))
201
for i := 0; i < len(objs); i++ {
212
// Drive returns given drive by uuid
213
func (c *Client) Drive(uuid string, libspec LibrarySpec) (Drive, error) {
214
obj, err := c.getDrive(uuid, libspec)
228
// CloneDrive clones given drive by uuid
229
func (c *Client) CloneDrive(uuid string, libspec LibrarySpec, params CloneParams, avoid []string) (Drive, error) {
230
obj, err := c.cloneDrive(uuid, libspec, params, avoid)
238
library: LibraryAccount,
244
// RemoveDrive removes given drive by uuid
245
func (c *Client) RemoveDrive(uuid string, libspec LibrarySpec) error {
246
return c.removeDrive(uuid, libspec)
249
// Job returns job object by uuid
250
func (c *Client) Job(uuid string) (Job, error) {
251
obj, err := c.getJob(uuid)
264
// ReadContext reads and returns context of current server
265
func (c Client) ReadContext() (Context, error) {
266
obj, err := c.readContext()
271
ctx := context{obj: obj}