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 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 53 | // DetachOptsBuilder allows extensions to add additional parameters to the |
| 54 | // Detach request. |
| 55 | type DetachOptsBuilder interface { |
| 56 | ToVolumeDetachMap() (map[string]interface{}, error) |
| 57 | } |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 58 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 59 | type DetachOpts struct { |
| 60 | AttachmentID string `json:"attachment_id,omitempty"` |
| 61 | } |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 62 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 63 | // ToVolumeDetachMap assembles a request body based on the contents of a |
| 64 | // DetachOpts. |
| 65 | func (opts DetachOpts) ToVolumeDetachMap() (map[string]interface{}, error) { |
| 66 | return gophercloud.BuildRequestBody(opts, "os-detach") |
| 67 | } |
| 68 | |
| 69 | // Detach will detach a volume based on volume id. |
| 70 | func Detach(client *gophercloud.ServiceClient, id string, opts DetachOptsBuilder) (r DetachResult) { |
| 71 | b, err := opts.ToVolumeDetachMap() |
| 72 | if err != nil { |
| 73 | r.Err = err |
| 74 | return |
| 75 | } |
| 76 | _, r.Err = client.Post(detachURL(client, id), b, nil, &gophercloud.RequestOpts{ |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 77 | OkCodes: []int{202}, |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 78 | }) |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 79 | return |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 80 | } |
| 81 | |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 82 | // Reserve will reserve a volume based on volume id. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 83 | func Reserve(client *gophercloud.ServiceClient, id string) (r ReserveResult) { |
| 84 | b := map[string]interface{}{"os-reserve": make(map[string]interface{})} |
| 85 | _, r.Err = client.Post(reserveURL(client, id), b, nil, &gophercloud.RequestOpts{ |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 86 | OkCodes: []int{200, 201, 202}, |
| 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 | // Unreserve will unreserve a volume based on volume id. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 92 | func Unreserve(client *gophercloud.ServiceClient, id string) (r UnreserveResult) { |
| 93 | b := map[string]interface{}{"os-unreserve": make(map[string]interface{})} |
| 94 | _, r.Err = client.Post(unreserveURL(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 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 100 | // InitializeConnectionOptsBuilder allows extensions to add additional parameters to the |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 101 | // InitializeConnection request. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 102 | type InitializeConnectionOptsBuilder interface { |
| 103 | ToVolumeInitializeConnectionMap() (map[string]interface{}, error) |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 104 | } |
| 105 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 106 | // InitializeConnectionOpts hosts options for InitializeConnection. |
| 107 | type InitializeConnectionOpts struct { |
| 108 | IP string `json:"ip,omitempty"` |
| 109 | Host string `json:"host,omitempty"` |
| 110 | Initiator string `json:"initiator,omitempty"` |
| 111 | Wwpns []string `json:"wwpns,omitempty"` |
| 112 | Wwnns string `json:"wwnns,omitempty"` |
| 113 | Multipath *bool `json:"multipath,omitempty"` |
| 114 | Platform string `json:"platform,omitempty"` |
| 115 | OSType string `json:"os_type,omitempty"` |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 116 | } |
| 117 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 118 | // ToVolumeInitializeConnectionMap assembles a request body based on the contents of a |
| 119 | // InitializeConnectionOpts. |
| 120 | func (opts InitializeConnectionOpts) ToVolumeInitializeConnectionMap() (map[string]interface{}, error) { |
| 121 | b, err := gophercloud.BuildRequestBody(opts, "connector") |
| 122 | return map[string]interface{}{"os-initialize_connection": b}, err |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 123 | } |
| 124 | |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 125 | // InitializeConnection initializes iscsi connection. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 126 | func InitializeConnection(client *gophercloud.ServiceClient, id string, opts InitializeConnectionOptsBuilder) (r InitializeConnectionResult) { |
| 127 | b, err := opts.ToVolumeInitializeConnectionMap() |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 128 | if err != nil { |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 129 | r.Err = err |
| 130 | return |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 131 | } |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 132 | _, r.Err = client.Post(initializeConnectionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 133 | OkCodes: []int{200, 201, 202}, |
| 134 | }) |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 135 | return |
| 136 | } |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 137 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 138 | // TerminateConnectionOptsBuilder allows extensions to add additional parameters to the |
| 139 | // TerminateConnection request. |
| 140 | type TerminateConnectionOptsBuilder interface { |
| 141 | ToVolumeTerminateConnectionMap() (map[string]interface{}, error) |
| 142 | } |
| 143 | |
| 144 | // TerminateConnectionOpts hosts options for TerminateConnection. |
| 145 | type TerminateConnectionOpts struct { |
| 146 | IP string `json:"ip,omitempty"` |
| 147 | Host string `json:"host,omitempty"` |
| 148 | Initiator string `json:"initiator,omitempty"` |
| 149 | Wwpns []string `json:"wwpns,omitempty"` |
| 150 | Wwnns string `json:"wwnns,omitempty"` |
| 151 | Multipath *bool `json:"multipath,omitempty"` |
| 152 | Platform string `json:"platform,omitempty"` |
| 153 | OSType string `json:"os_type,omitempty"` |
| 154 | } |
| 155 | |
| 156 | // ToVolumeTerminateConnectionMap assembles a request body based on the contents of a |
| 157 | // TerminateConnectionOpts. |
| 158 | func (opts TerminateConnectionOpts) ToVolumeTerminateConnectionMap() (map[string]interface{}, error) { |
| 159 | b, err := gophercloud.BuildRequestBody(opts, "connector") |
| 160 | return map[string]interface{}{"os-terminate_connection": b}, err |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 161 | } |
| 162 | |
Pengfei Ni | 8bfbfb0 | 2016-04-29 16:04:12 +0800 | [diff] [blame] | 163 | // TerminateConnection terminates iscsi connection. |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 164 | func TerminateConnection(client *gophercloud.ServiceClient, id string, opts TerminateConnectionOptsBuilder) (r TerminateConnectionResult) { |
| 165 | b, err := opts.ToVolumeTerminateConnectionMap() |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 166 | if err != nil { |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 167 | r.Err = err |
| 168 | return |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 169 | } |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 170 | _, r.Err = client.Post(teminateConnectionURL(client, id), b, nil, &gophercloud.RequestOpts{ |
Jamie Hannaford | 531e0cc | 2016-05-13 13:03:39 +0200 | [diff] [blame] | 171 | OkCodes: []int{202}, |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 172 | }) |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 173 | return |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame] | 174 | } |
Mario Luan | a4d4930 | 2016-09-02 11:37:24 -0400 | [diff] [blame] | 175 | |
| 176 | // ExtendSizeOptsBuilder allows extensions to add additional parameters to the |
| 177 | // ExtendSize request. |
| 178 | type ExtendSizeOptsBuilder interface { |
| 179 | ToVolumeExtendSizeMap() (map[string]interface{}, error) |
| 180 | } |
| 181 | |
| 182 | // ExtendSizeOpts contain options for extending the size of an existing Volume. This object is passed |
| 183 | // to the volumes.ExtendSize function. |
| 184 | type ExtendSizeOpts struct { |
| 185 | // NewSize is the new size of the volume, in GB |
| 186 | NewSize int `json:"new_size" required:"true"` |
| 187 | } |
| 188 | |
| 189 | // ToVolumeExtendSizeMap assembles a request body based on the contents of an |
| 190 | // ExtendSizeOpts. |
| 191 | func (opts ExtendSizeOpts) ToVolumeExtendSizeMap() (map[string]interface{}, error) { |
| 192 | return gophercloud.BuildRequestBody(opts, "os-extend") |
| 193 | } |
| 194 | |
| 195 | // ExtendSize will extend the size of the volume based on the provided information. |
| 196 | // This operation does not return a response body. |
| 197 | func ExtendSize(client *gophercloud.ServiceClient, id string, opts ExtendSizeOptsBuilder) (r ExtendSizeResult) { |
| 198 | b, err := opts.ToVolumeExtendSizeMap() |
| 199 | if err != nil { |
| 200 | r.Err = err |
| 201 | return |
| 202 | } |
| 203 | _, r.Err = client.Post(extendSizeURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ |
| 204 | OkCodes: []int{202}, |
| 205 | }) |
| 206 | return |
| 207 | } |