github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v3/attachments/requests.go (about) 1 package attachments 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // CreateOptsBuilder allows extensions to add additional parameters to the 9 // Create request. 10 type CreateOptsBuilder interface { 11 ToAttachmentCreateMap() (map[string]interface{}, error) 12 } 13 14 // CreateOpts contains options for creating a Volume attachment. This object is 15 // passed to the Create function. For more information about these parameters, 16 // see the Attachment object. 17 type CreateOpts struct { 18 // VolumeUUID is the UUID of the Cinder volume to create the attachment 19 // record for. 20 VolumeUUID string `json:"volume_uuid"` 21 // InstanceUUID is the ID of the Server to create the attachment for. 22 // When attaching to a Nova Server this is the Nova Server (Instance) 23 // UUID. 24 InstanceUUID string `json:"instance_uuid"` 25 // Connector is an optional map containing all of the needed atachment 26 // information for exmaple initiator IQN, etc. 27 Connector map[string]interface{} `json:"connector,omitempty"` 28 // Mode is an attachment mode. Acceptable values are read-only ('ro') 29 // and read-and-write ('rw'). Available only since 3.54 microversion. 30 // For APIs from 3.27 till 3.53 use Connector["mode"] = "rw|ro". 31 Mode string `json:"mode,omitempty"` 32 } 33 34 // ToAttachmentCreateMap assembles a request body based on the contents of a 35 // CreateOpts. 36 func (opts CreateOpts) ToAttachmentCreateMap() (map[string]interface{}, error) { 37 return gophercloud.BuildRequestBody(opts, "attachment") 38 } 39 40 // Create will create a new Attachment based on the values in CreateOpts. To 41 // extract the Attachment object from the response, call the Extract method on 42 // the CreateResult. 43 func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 44 b, err := opts.ToAttachmentCreateMap() 45 if err != nil { 46 r.Err = err 47 return 48 } 49 resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ 50 OkCodes: []int{200, 202}, 51 }) 52 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 53 return 54 } 55 56 // Delete will delete the existing Attachment with the provided ID. 57 func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { 58 resp, err := client.Delete(deleteURL(client, id), &gophercloud.RequestOpts{ 59 OkCodes: []int{200}, 60 }) 61 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 62 return 63 } 64 65 // Get retrieves the Attachment with the provided ID. To extract the Attachment 66 // object from the response, call the Extract method on the GetResult. 67 func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { 68 resp, err := client.Get(getURL(client, id), &r.Body, nil) 69 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 70 return 71 } 72 73 // ListOptsBuilder allows extensions to add additional parameters to the List 74 // request. 75 type ListOptsBuilder interface { 76 ToAttachmentListQuery() (string, error) 77 } 78 79 // ListOpts holds options for listing Attachments. It is passed to the attachments.List 80 // function. 81 type ListOpts struct { 82 // AllTenants will retrieve attachments of all tenants/projects. 83 AllTenants bool `q:"all_tenants"` 84 85 // Status will filter by the specified status. 86 Status string `q:"status"` 87 88 // ProjectID will filter by a specific tenant/project ID. 89 ProjectID string `q:"project_id"` 90 91 // VolumeID will filter by a specific volume ID. 92 VolumeID string `q:"volume_id"` 93 94 // InstanceID will filter by a specific instance ID. 95 InstanceID string `q:"instance_id"` 96 97 // Comma-separated list of sort keys and optional sort directions in the 98 // form of <key>[:<direction>]. 99 Sort string `q:"sort"` 100 101 // Requests a page size of items. 102 Limit int `q:"limit"` 103 104 // Used in conjunction with limit to return a slice of items. 105 Offset int `q:"offset"` 106 107 // The ID of the last-seen item. 108 Marker string `q:"marker"` 109 } 110 111 // ToAttachmentListQuery formats a ListOpts into a query string. 112 func (opts ListOpts) ToAttachmentListQuery() (string, error) { 113 q, err := gophercloud.BuildQueryString(opts) 114 return q.String(), err 115 } 116 117 // List returns Attachments optionally limited by the conditions provided in 118 // ListOpts. 119 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 120 url := listURL(client) 121 if opts != nil { 122 query, err := opts.ToAttachmentListQuery() 123 if err != nil { 124 return pagination.Pager{Err: err} 125 } 126 url += query 127 } 128 129 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 130 return AttachmentPage{pagination.LinkedPageBase{PageResult: r}} 131 }) 132 } 133 134 // UpdateOptsBuilder allows extensions to add additional parameters to the 135 // Update request. 136 type UpdateOptsBuilder interface { 137 ToAttachmentUpdateMap() (map[string]interface{}, error) 138 } 139 140 // UpdateOpts contain options for updating an existing Attachment. 141 // This is used to finalize an attachment that was created without a 142 // connector (reserve). 143 type UpdateOpts struct { 144 Connector map[string]interface{} `json:"connector"` 145 } 146 147 // ToAttachmentUpdateMap assembles a request body based on the contents of an 148 // UpdateOpts. 149 func (opts UpdateOpts) ToAttachmentUpdateMap() (map[string]interface{}, error) { 150 return gophercloud.BuildRequestBody(opts, "attachment") 151 } 152 153 // Update will update the Attachment with provided information. To extract the 154 // updated Attachment from the response, call the Extract method on the 155 // UpdateResult. 156 func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 157 b, err := opts.ToAttachmentUpdateMap() 158 if err != nil { 159 r.Err = err 160 return 161 } 162 resp, err := client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 163 OkCodes: []int{200}, 164 }) 165 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 166 return 167 } 168 169 // Complete will complete an attachment for a cinder volume. 170 // Available starting in the 3.44 microversion. 171 func Complete(client *gophercloud.ServiceClient, id string) (r CompleteResult) { 172 b := map[string]interface{}{ 173 "os-complete": nil, 174 } 175 resp, err := client.Post(completeURL(client, id), b, nil, &gophercloud.RequestOpts{ 176 OkCodes: []int{204}, 177 }) 178 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 179 return 180 }