File: requests.go

package info (click to toggle)
golang-github-rackspace-gophercloud 1.0.0%2Bgit20161013.1012.e00690e8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,148 kB
  • ctags: 6,414
  • sloc: sh: 16; makefile: 6
file content (201 lines) | stat: -rw-r--r-- 5,059 bytes parent folder | download | duplicates (2)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package volumeactions

import (
	"github.com/rackspace/gophercloud"
)

// AttachOptsBuilder allows extensions to add additional parameters to the
// Attach request.
type AttachOptsBuilder interface {
	ToVolumeAttachMap() (map[string]interface{}, error)
}

// AttachMode describes the attachment mode for volumes.
type AttachMode string

// These constants determine how a volume is attached
const (
	ReadOnly  AttachMode = "ro"
	ReadWrite AttachMode = "rw"
)

// AttachOpts contains options for attaching a Volume.
type AttachOpts struct {
	// The mountpoint of this volume
	MountPoint string
	// The nova instance ID, can't set simultaneously with HostName
	InstanceUUID string
	// The hostname of baremetal host, can't set simultaneously with InstanceUUID
	HostName string
	// Mount mode of this volume
	Mode AttachMode
}

// ToVolumeAttachMap assembles a request body based on the contents of a
// AttachOpts.
func (opts AttachOpts) ToVolumeAttachMap() (map[string]interface{}, error) {
	v := make(map[string]interface{})

	if opts.MountPoint != "" {
		v["mountpoint"] = opts.MountPoint
	}
	if opts.Mode != "" {
		v["mode"] = opts.Mode
	}
	if opts.InstanceUUID != "" {
		v["instance_uuid"] = opts.InstanceUUID
	}
	if opts.HostName != "" {
		v["host_name"] = opts.HostName
	}

	return map[string]interface{}{"os-attach": v}, nil
}

// Attach will attach a volume based on the values in AttachOpts.
func Attach(client *gophercloud.ServiceClient, id string, opts AttachOptsBuilder) AttachResult {
	var res AttachResult

	reqBody, err := opts.ToVolumeAttachMap()
	if err != nil {
		res.Err = err
		return res
	}

	_, res.Err = client.Post(attachURL(client, id), reqBody, nil, &gophercloud.RequestOpts{
		OkCodes: []int{202},
	})

	return res
}

// Attach will detach a volume based on volume id.
func Detach(client *gophercloud.ServiceClient, id string) DetachResult {
	var res DetachResult

	v := make(map[string]interface{})
	reqBody := map[string]interface{}{"os-detach": v}

	_, res.Err = client.Post(detachURL(client, id), reqBody, nil, &gophercloud.RequestOpts{
		OkCodes: []int{202},
	})

	return res
}

// Reserve will reserve a volume based on volume id.
func Reserve(client *gophercloud.ServiceClient, id string) ReserveResult {
	var res ReserveResult

	v := make(map[string]interface{})
	reqBody := map[string]interface{}{"os-reserve": v}

	_, res.Err = client.Post(reserveURL(client, id), reqBody, nil, &gophercloud.RequestOpts{
		OkCodes: []int{200, 201, 202},
	})

	return res
}

// Unreserve will unreserve a volume based on volume id.
func Unreserve(client *gophercloud.ServiceClient, id string) UnreserveResult {
	var res UnreserveResult

	v := make(map[string]interface{})
	reqBody := map[string]interface{}{"os-unreserve": v}

	_, res.Err = client.Post(unreserveURL(client, id), reqBody, nil, &gophercloud.RequestOpts{
		OkCodes: []int{200, 201, 202},
	})

	return res
}

// ConnectorOptsBuilder allows extensions to add additional parameters to the
// InitializeConnection request.
type ConnectorOptsBuilder interface {
	ToConnectorMap() (map[string]interface{}, error)
}

// ConnectorOpts hosts options for InitializeConnection.
type ConnectorOpts struct {
	IP        string
	Host      string
	Initiator string
	Wwpns     []string
	Wwnns     string
	Multipath bool
	Platform  string
	OSType    string
}

// ToConnectorMap assembles a request body based on the contents of a
// ConnectorOpts.
func (opts ConnectorOpts) ToConnectorMap() (map[string]interface{}, error) {
	v := make(map[string]interface{})

	if opts.IP != "" {
		v["ip"] = opts.IP
	}
	if opts.Host != "" {
		v["host"] = opts.Host
	}
	if opts.Initiator != "" {
		v["initiator"] = opts.Initiator
	}
	if opts.Wwpns != nil {
		v["wwpns"] = opts.Wwpns
	}
	if opts.Wwnns != "" {
		v["wwnns"] = opts.Wwnns
	}

	v["multipath"] = opts.Multipath

	if opts.Platform != "" {
		v["platform"] = opts.Platform
	}
	if opts.OSType != "" {
		v["os_type"] = opts.OSType
	}

	return map[string]interface{}{"connector": v}, nil
}

// InitializeConnection initializes iscsi connection.
func InitializeConnection(client *gophercloud.ServiceClient, id string, opts *ConnectorOpts) InitializeConnectionResult {
	var res InitializeConnectionResult

	connctorMap, err := opts.ToConnectorMap()
	if err != nil {
		res.Err = err
		return res
	}

	reqBody := map[string]interface{}{"os-initialize_connection": connctorMap}

	_, res.Err = client.Post(initializeConnectionURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200, 201, 202},
	})

	return res
}

// TerminateConnection terminates iscsi connection.
func TerminateConnection(client *gophercloud.ServiceClient, id string, opts *ConnectorOpts) TerminateConnectionResult {
	var res TerminateConnectionResult

	connctorMap, err := opts.ToConnectorMap()
	if err != nil {
		res.Err = err
		return res
	}

	reqBody := map[string]interface{}{"os-terminate_connection": connctorMap}

	_, res.Err = client.Post(teminateConnectionURL(client, id), reqBody, nil, &gophercloud.RequestOpts{
		OkCodes: []int{202},
	})

	return res
}