feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 1 | package volumeactions |
| 2 | |
| 3 | import ( |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 5 | ) |
| 6 | |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 7 | // AttachOptsBuilder allows extensions to add additional parameters to the |
| 8 | // Attach request. |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 9 | type AttachOptsBuilder interface { |
| 10 | ToVolumeAttachMap() (map[string]interface{}, error) |
| 11 | } |
| 12 | |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 13 | // AttachMode describes the attachment mode for volumes. |
| 14 | type AttachMode string |
| 15 | |
| 16 | // These constants determine how a volume is attached |
| 17 | const ( |
| 18 | ReadOnly AttachMode = "ro" |
| 19 | ReadWrite AttachMode = "rw" |
| 20 | ) |
| 21 | |
| 22 | // AttachOpts contains options for attaching a Volume. |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 23 | type AttachOpts struct { |
| 24 | // The mountpoint of this volume |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 25 | MountPoint string `json:"mountpoint,omitempty"` |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 26 | // The nova instance ID, can't set simultaneously with HostName |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 27 | InstanceUUID string `json:"instance_uuid,omitempty"` |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 28 | // The hostname of baremetal host, can't set simultaneously with InstanceUUID |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 29 | HostName string `json:"host_name,omitempty"` |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 30 | // Mount mode of this volume |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 31 | Mode AttachMode `json:"mode,omitempty"` |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 32 | } |
| 33 | |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 34 | // ToVolumeAttachMap assembles a request body based on the contents of a |
| 35 | // AttachOpts. |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 36 | func (opts AttachOpts) ToVolumeAttachMap() (map[string]interface{}, error) { |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 37 | return gophercloud.BuildRequestBody(opts, "os-attach") |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 38 | } |
| 39 | |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 40 | // Attach will attach a volume based on the values in AttachOpts. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 41 | func Attach(client *gophercloud.ServiceClient, id string, opts AttachOptsBuilder) (r AttachResult) { |
| 42 | b, err := opts.ToVolumeAttachMap() |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 43 | if err != nil { |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 44 | r.Err = err |
| 45 | return |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 46 | } |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 47 | _, r.Err = client.Post(attachURL(client, id), b, nil, &gophercloud.RequestOpts{ |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 48 | OkCodes: []int{202}, |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 49 | }) |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 50 | return |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 51 | } |
| 52 | |
Eugene Yakubovich | b3a4f33 | 2016-10-13 11:01:06 -0700 | [diff] [blame^] | 53 | // BeginDetach will mark the volume as detaching |
| 54 | func BeginDetaching(client *gophercloud.ServiceClient, id string) (r BeginDetachingResult) { |
| 55 | b := map[string]interface{}{"os-begin_detaching": make(map[string]interface{})} |
| 56 | _, r.Err = client.Post(beginDetachingURL(client, id), b, nil, &gophercloud.RequestOpts{ |
| 57 | OkCodes: []int{202}, |
| 58 | }) |
| 59 | return |
| 60 | } |
| 61 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 62 | // DetachOptsBuilder allows extensions to add additional parameters to the |
| 63 | // Detach request. |
| 64 | type DetachOptsBuilder interface { |
| 65 | ToVolumeDetachMap() (map[string]interface{}, error) |
| 66 | } |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 67 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 68 | type DetachOpts struct { |
| 69 | AttachmentID string `json:"attachment_id,omitempty"` |
| 70 | } |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 71 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 72 | // ToVolumeDetachMap assembles a request body based on the contents of a |
| 73 | // DetachOpts. |
| 74 | func (opts DetachOpts) ToVolumeDetachMap() (map[string]interface{}, error) { |
| 75 | return gophercloud.BuildRequestBody(opts, "os-detach") |
| 76 | } |
| 77 | |
| 78 | // Detach will detach a volume based on volume id. |
| 79 | func Detach(client *gophercloud.ServiceClient, id string, opts DetachOptsBuilder) (r DetachResult) { |
| 80 | b, err := opts.ToVolumeDetachMap() |
| 81 | if err != nil { |
| 82 | r.Err = err |
| 83 | return |
| 84 | } |
| 85 | _, r.Err = client.Post(detachURL(client, id), b, nil, &gophercloud.RequestOpts{ |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 86 | OkCodes: []int{202}, |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 87 | }) |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 88 | return |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 89 | } |
| 90 | |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 91 | // Reserve will reserve a volume based on volume id. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 92 | func Reserve(client *gophercloud.ServiceClient, id string) (r ReserveResult) { |
| 93 | b := map[string]interface{}{"os-reserve": make(map[string]interface{})} |
| 94 | _, r.Err = client.Post(reserveURL(client, id), b, nil, &gophercloud.RequestOpts{ |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 95 | OkCodes: []int{200, 201, 202}, |
| 96 | }) |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 97 | return |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 98 | } |
| 99 | |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 100 | // Unreserve will unreserve a volume based on volume id. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 101 | func Unreserve(client *gophercloud.ServiceClient, id string) (r UnreserveResult) { |
| 102 | b := map[string]interface{}{"os-unreserve": make(map[string]interface{})} |
| 103 | _, r.Err = client.Post(unreserveURL(client, id), b, nil, &gophercloud.RequestOpts{ |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 104 | OkCodes: []int{200, 201, 202}, |
| 105 | }) |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 106 | return |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 107 | } |
| 108 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 109 | // InitializeConnectionOptsBuilder allows extensions to add additional parameters to the |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 110 | // InitializeConnection request. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 111 | type InitializeConnectionOptsBuilder interface { |
| 112 | ToVolumeInitializeConnectionMap() (map[string]interface{}, error) |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 113 | } |
| 114 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 115 | // InitializeConnectionOpts hosts options for InitializeConnection. |
| 116 | type InitializeConnectionOpts struct { |
| 117 | IP string `json:"ip,omitempty"` |
| 118 | Host string `json:"host,omitempty"` |
| 119 | Initiator string `json:"initiator,omitempty"` |
| 120 | Wwpns []string `json:"wwpns,omitempty"` |
| 121 | Wwnns string `json:"wwnns,omitempty"` |
| 122 | Multipath *bool `json:"multipath,omitempty"` |
| 123 | Platform string `json:"platform,omitempty"` |
| 124 | OSType string `json:"os_type,omitempty"` |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 125 | } |
| 126 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 127 | // ToVolumeInitializeConnectionMap assembles a request body based on the contents of a |
| 128 | // InitializeConnectionOpts. |
| 129 | func (opts InitializeConnectionOpts) ToVolumeInitializeConnectionMap() (map[string]interface{}, error) { |
| 130 | b, err := gophercloud.BuildRequestBody(opts, "connector") |
| 131 | return map[string]interface{}{"os-initialize_connection": b}, err |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 132 | } |
| 133 | |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 134 | // InitializeConnection initializes iscsi connection. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 135 | func InitializeConnection(client *gophercloud.ServiceClient, id string, opts InitializeConnectionOptsBuilder) (r InitializeConnectionResult) { |
| 136 | b, err := opts.ToVolumeInitializeConnectionMap() |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 137 | if err != nil { |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 138 | r.Err = err |
| 139 | return |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 140 | } |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 141 | _, r.Err = client.Post(initializeConnectionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 142 | OkCodes: []int{200, 201, 202}, |
| 143 | }) |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 144 | return |
| 145 | } |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 146 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 147 | // TerminateConnectionOptsBuilder allows extensions to add additional parameters to the |
| 148 | // TerminateConnection request. |
| 149 | type TerminateConnectionOptsBuilder interface { |
| 150 | ToVolumeTerminateConnectionMap() (map[string]interface{}, error) |
| 151 | } |
| 152 | |
| 153 | // TerminateConnectionOpts hosts options for TerminateConnection. |
| 154 | type TerminateConnectionOpts struct { |
| 155 | IP string `json:"ip,omitempty"` |
| 156 | Host string `json:"host,omitempty"` |
| 157 | Initiator string `json:"initiator,omitempty"` |
| 158 | Wwpns []string `json:"wwpns,omitempty"` |
| 159 | Wwnns string `json:"wwnns,omitempty"` |
| 160 | Multipath *bool `json:"multipath,omitempty"` |
| 161 | Platform string `json:"platform,omitempty"` |
| 162 | OSType string `json:"os_type,omitempty"` |
| 163 | } |
| 164 | |
| 165 | // ToVolumeTerminateConnectionMap assembles a request body based on the contents of a |
| 166 | // TerminateConnectionOpts. |
| 167 | func (opts TerminateConnectionOpts) ToVolumeTerminateConnectionMap() (map[string]interface{}, error) { |
| 168 | b, err := gophercloud.BuildRequestBody(opts, "connector") |
| 169 | return map[string]interface{}{"os-terminate_connection": b}, err |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 170 | } |
| 171 | |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 172 | // TerminateConnection terminates iscsi connection. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 173 | func TerminateConnection(client *gophercloud.ServiceClient, id string, opts TerminateConnectionOptsBuilder) (r TerminateConnectionResult) { |
| 174 | b, err := opts.ToVolumeTerminateConnectionMap() |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 175 | if err != nil { |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 176 | r.Err = err |
| 177 | return |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 178 | } |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 179 | _, r.Err = client.Post(teminateConnectionURL(client, id), b, nil, &gophercloud.RequestOpts{ |
Jamie Hannaford | 531e0cc | 2016-05-13 13:03:39 +0200 | [diff] [blame] | 180 | OkCodes: []int{202}, |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 181 | }) |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 182 | return |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 183 | } |
Mario Luan | a4d4930 | 2016-09-02 11:37:24 -0400 | [diff] [blame] | 184 | |
| 185 | // ExtendSizeOptsBuilder allows extensions to add additional parameters to the |
| 186 | // ExtendSize request. |
| 187 | type ExtendSizeOptsBuilder interface { |
| 188 | ToVolumeExtendSizeMap() (map[string]interface{}, error) |
| 189 | } |
| 190 | |
| 191 | // ExtendSizeOpts contain options for extending the size of an existing Volume. This object is passed |
| 192 | // to the volumes.ExtendSize function. |
| 193 | type ExtendSizeOpts struct { |
| 194 | // NewSize is the new size of the volume, in GB |
| 195 | NewSize int `json:"new_size" required:"true"` |
| 196 | } |
| 197 | |
| 198 | // ToVolumeExtendSizeMap assembles a request body based on the contents of an |
| 199 | // ExtendSizeOpts. |
| 200 | func (opts ExtendSizeOpts) ToVolumeExtendSizeMap() (map[string]interface{}, error) { |
| 201 | return gophercloud.BuildRequestBody(opts, "os-extend") |
| 202 | } |
| 203 | |
| 204 | // ExtendSize will extend the size of the volume based on the provided information. |
| 205 | // This operation does not return a response body. |
| 206 | func ExtendSize(client *gophercloud.ServiceClient, id string, opts ExtendSizeOptsBuilder) (r ExtendSizeResult) { |
| 207 | b, err := opts.ToVolumeExtendSizeMap() |
| 208 | if err != nil { |
| 209 | r.Err = err |
| 210 | return |
| 211 | } |
| 212 | _, r.Err = client.Post(extendSizeURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ |
| 213 | OkCodes: []int{202}, |
| 214 | }) |
| 215 | return |
| 216 | } |