This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
endpoints.go
139 lines (117 loc) · 4.91 KB
/
endpoints.go
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
130
131
132
133
134
135
136
137
138
139
/*
* Copyright (c) 2021 Enix, SAS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Authors:
* Paul Laffitte <[email protected]>
* Arthur Chaloin <[email protected]>
* Alexandre Buisine <[email protected]>
* Joe Skazinski <[email protected]>
*/
package dothill
import (
"crypto/md5"
"fmt"
"strings"
)
// Login : Called automatically, may be called manually if credentials changed
func (client *Client) Login() error {
userpass := fmt.Sprintf("%s_%s", client.Username, client.Password)
hash := fmt.Sprintf("%x", md5.Sum([]byte(userpass)))
res, _, err := client.FormattedRequest("/login/%s", hash)
if err != nil {
return err
}
client.sessionKey = res.ObjectsMap["status"].PropertiesMap["response"].Data
return nil
}
// CreateVolume : creates a volume with the given name, capacity in the given pool
func (client *Client) CreateVolume(name, size, pool string) (*Response, *ResponseStatus, error) {
return client.FormattedRequest("/create/volume/pool/\"%s\"/size/%s/tier-affinity/no-affinity/\"%s\"", pool, size, name)
}
// CreateHost : creates a host
func (client *Client) CreateHost(name, iqn string) (*Response, *ResponseStatus, error) {
return client.FormattedRequest("/create/host/id/\"%s\"/\"%s\"", iqn, name)
}
// MapVolume : map a volume to host + LUN
func (client *Client) MapVolume(name, host, access string, lun int) (*Response, *ResponseStatus, error) {
return client.FormattedRequest("/map/volume/access/%s/lun/%d/host/%s/\"%s\"", access, lun, host, name)
}
// ShowVolumes : get informations about volumes
func (client *Client) ShowVolumes(volumes ...string) (*Response, *ResponseStatus, error) {
return client.FormattedRequest("/show/volumes/\"%s\"", strings.Join(volumes, ","))
}
// UnmapVolume : unmap a volume from host
func (client *Client) UnmapVolume(name, host string) (*Response, *ResponseStatus, error) {
if host == "" {
return client.FormattedRequest("/unmap/volume/\"%s\"", name)
}
return client.FormattedRequest("/unmap/volume/host/\"%s\"/\"%s\"", host, name)
}
// ExpandVolume : extend a volume if there is enough space on the vdisk
func (client *Client) ExpandVolume(name, size string) (*Response, *ResponseStatus, error) {
return client.FormattedRequest("/expand/volume/size/\"%s\"/\"%s\"", size, name)
}
// DeleteVolume : deletes a volume
func (client *Client) DeleteVolume(name string) (*Response, *ResponseStatus, error) {
return client.FormattedRequest("/delete/volumes/\"%s\"", name)
}
// DeleteHost : deletes a host by its ID or nickname
func (client *Client) DeleteHost(name string) (*Response, *ResponseStatus, error) {
return client.FormattedRequest("/delete/host/\"%s\"", name)
}
// ShowHostMaps : list the volume mappings for given host
// If host is an empty string, mapping for all hosts is shown
func (client *Client) ShowHostMaps(host string) ([]Volume, *ResponseStatus, error) {
if len(host) > 0 {
host = fmt.Sprintf("\"%s\"", host)
}
res, status, err := client.FormattedRequest("/show/host-maps/%s", host)
if err != nil {
return nil, status, err
}
mappings := make([]Volume, 0)
for _, rootObj := range res.Objects {
if rootObj.Name != "host-view" {
continue
}
for _, object := range rootObj.Objects {
if object.Name == "volume-view" {
vol := Volume{}
vol.fillFromObject(&object)
mappings = append(mappings, vol)
}
}
}
return mappings, status, err
}
// ShowSnapshots : list snapshots
func (client *Client) ShowSnapshots(names ...string) (*Response, *ResponseStatus, error) {
if len(names) == 0 {
return client.FormattedRequest("/show/snapshots")
}
return client.FormattedRequest("/show/snapshots/%q", strings.Join(names, ","))
}
// CreateSnapshot : create a snapshot in a snap pool and the snap pool if it doesn't exsits
func (client *Client) CreateSnapshot(name string, snapshotName string) (*Response, *ResponseStatus, error) {
return client.FormattedRequest("/create/snapshots/volumes/%q/%q", name, snapshotName)
}
// DeleteSnapshot : delete a snapshot
func (client *Client) DeleteSnapshot(names ...string) (*Response, *ResponseStatus, error) {
return client.FormattedRequest("/delete/snapshot/%q", strings.Join(names, ","))
}
// CopyVolume : create an new volume by copying another one or a snapshot
func (client *Client) CopyVolume(sourceName string, destinationName string, pool string) (*Response, *ResponseStatus, error) {
return client.FormattedRequest("/copy/volume/destination-pool/%q/name/%q/%q", pool, destinationName, sourceName)
}