github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/evs/v2/cloudvolumes/requests.go (about) 1 package cloudvolumes 2 3 import ( 4 "strings" 5 6 "github.com/huaweicloud/golangsdk" 7 "github.com/huaweicloud/golangsdk/openstack/common/structs" 8 "github.com/huaweicloud/golangsdk/pagination" 9 ) 10 11 // CreateOptsBuilder allows extensions to add additional parameters to the 12 // Create request. 13 type CreateOptsBuilder interface { 14 ToVolumeCreateMap() (map[string]interface{}, error) 15 } 16 17 // CreateOpts contains options for creating a Volume. This object is passed to 18 // the cloudvolumes.Create function. 19 type CreateOpts struct { 20 Volume VolumeOpts `json:"volume" required:"true"` 21 ChargeInfo *structs.ChargeInfo `json:"bssParam,omitempty"` 22 Scheduler *SchedulerOpts `json:"OS-SCH-HNT:scheduler_hints,omitempty"` 23 ServerID string `json:"server_id,omitempty"` 24 } 25 26 // VolumeOpts contains options for creating a Volume. 27 type VolumeOpts struct { 28 // The availability zone 29 AvailabilityZone string `json:"availability_zone" required:"true"` 30 // The associated volume type 31 VolumeType string `json:"volume_type" required:"true"` 32 // The volume name 33 Name string `json:"name,omitempty"` 34 // The volume description 35 Description string `json:"description,omitempty"` 36 // The size of the volume, in GB 37 Size int `json:"size,omitempty"` 38 // The number to be created in a batch 39 Count int `json:"count,omitempty"` 40 // The backup_id 41 BackupID string `json:"backup_id,omitempty"` 42 // the ID of the existing volume snapshot 43 SnapshotID string `json:"snapshot_id,omitempty"` 44 // the ID of the image in IMS 45 ImageID string `json:"imageRef,omitempty"` 46 // Shared disk 47 Multiattach bool `json:"multiattach,omitempty"` 48 // One or more metadata key and value pairs to associate with the volume 49 Metadata map[string]string `json:"metadata,omitempty"` 50 // One or more tag key and value pairs to associate with the volume 51 Tags map[string]string `json:"tags,omitempty"` 52 // the enterprise project id 53 EnterpriseProjectID string `json:"enterprise_project_id,omitempty"` 54 } 55 56 // SchedulerOpts contains the scheduler hints 57 type SchedulerOpts struct { 58 StorageID string `json:"dedicated_storage_id,omitempty"` 59 } 60 61 // ToVolumeCreateMap assembles a request body based on the contents of a 62 // CreateOpts. 63 func (opts CreateOpts) ToVolumeCreateMap() (map[string]interface{}, error) { 64 return golangsdk.BuildRequestBody(opts, "") 65 } 66 67 // Create will create a new Volume based on the values in CreateOpts. 68 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) { 69 b, err := opts.ToVolumeCreateMap() 70 if err != nil { 71 r.Err = err 72 return 73 } 74 75 // the version of create API is v2.1 76 newClient := *client 77 baseURL := newClient.ResourceBaseURL() 78 newClient.ResourceBase = strings.Replace(baseURL, "/v2/", "/v2.1/", 1) 79 80 _, r.Err = newClient.Post(createURL(&newClient), b, &r.Body, nil) 81 return 82 } 83 84 // ExtendOptsBuilder allows extensions to add additional parameters to the 85 // ExtendSize request. 86 type ExtendOptsBuilder interface { 87 ToVolumeExtendMap() (map[string]interface{}, error) 88 } 89 90 // ExtendOpts contains options for extending the size of an existing Volume. 91 // This object is passed to the cloudvolumes.ExtendSize function. 92 type ExtendOpts struct { 93 SizeOpts ExtendSizeOpts `json:"os-extend" required:"true"` 94 ChargeInfo *ExtendChargeOpts `json:"bssParam,omitempty"` 95 } 96 97 // ExtendSizeOpts contains the new size of the volume, in GB. 98 type ExtendSizeOpts struct { 99 NewSize int `json:"new_size" required:"true"` 100 } 101 102 // ExtendChargeOpts contains the charging parameters of the volume 103 type ExtendChargeOpts struct { 104 IsAutoPay string `json:"is_auto_pay,omitempty"` 105 } 106 107 // ToVolumeExtendMap assembles a request body based on the contents of an 108 // ExtendOpts. 109 func (opts ExtendOpts) ToVolumeExtendMap() (map[string]interface{}, error) { 110 return golangsdk.BuildRequestBody(opts, "") 111 } 112 113 // ExtendSize will extend the size of the volume based on the provided information. 114 // This operation does not return a response body. 115 func ExtendSize(client *golangsdk.ServiceClient, id string, opts ExtendOptsBuilder) (r JobResult) { 116 b, err := opts.ToVolumeExtendMap() 117 if err != nil { 118 r.Err = err 119 return 120 } 121 // the version of extend API is v2.1 122 newClient := *client 123 baseURL := newClient.ResourceBaseURL() 124 newClient.ResourceBase = strings.Replace(baseURL, "/v2/", "/v2.1/", 1) 125 126 _, r.Err = newClient.Post(actionURL(&newClient, id), b, nil, &golangsdk.RequestOpts{ 127 OkCodes: []int{202}, 128 }) 129 return 130 } 131 132 // UpdateOptsBuilder allows extensions to add additional parameters to the 133 // Update request. 134 type UpdateOptsBuilder interface { 135 ToVolumeUpdateMap() (map[string]interface{}, error) 136 } 137 138 // UpdateOpts contain options for updating an existing Volume. This object is passed 139 // to the cloudvolumes.Update function. 140 type UpdateOpts struct { 141 Name string `json:"name,omitempty"` 142 Description *string `json:"description,omitempty"` 143 } 144 145 // ToVolumeUpdateMap assembles a request body based on the contents of an 146 // UpdateOpts. 147 func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) { 148 return golangsdk.BuildRequestBody(opts, "volume") 149 } 150 151 // Update will update the Volume with provided information. To extract the updated 152 // Volume from the response, call the Extract method on the UpdateResult. 153 func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 154 b, err := opts.ToVolumeUpdateMap() 155 if err != nil { 156 r.Err = err 157 return 158 } 159 _, r.Err = client.Put(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ 160 OkCodes: []int{200}, 161 }) 162 return 163 } 164 165 // DeleteOptsBuilder is an interface by which can be able to build the query string 166 // of volume deletion. 167 type DeleteOptsBuilder interface { 168 ToVolumeDeleteQuery() (string, error) 169 } 170 171 // DeleteOpts contain options for deleting an existing Volume. This object is passed 172 // to the cloudvolumes.Delete function. 173 type DeleteOpts struct { 174 // Specifies to delete all snapshots associated with the EVS disk. 175 Cascade bool `q:"cascade"` 176 } 177 178 // ToVolumeDeleteQuery assembles a request body based on the contents of an 179 // DeleteOpts. 180 func (opts DeleteOpts) ToVolumeDeleteQuery() (string, error) { 181 q, err := golangsdk.BuildQueryString(opts) 182 return q.String(), err 183 } 184 185 // Delete will delete the existing Volume with the provided ID 186 func Delete(client *golangsdk.ServiceClient, id string, opts DeleteOptsBuilder) (r DeleteResult) { 187 url := resourceURL(client, id) 188 if opts != nil { 189 q, err := opts.ToVolumeDeleteQuery() 190 if err != nil { 191 r.Err = err 192 return 193 } 194 url += q 195 } 196 _, r.Err = client.Delete(url, &golangsdk.RequestOpts{ 197 OkCodes: []int{200}, 198 }) 199 return 200 } 201 202 // Get retrieves the Volume with the provided ID. To extract the Volume object 203 // from the response, call the Extract method on the GetResult. 204 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 205 _, r.Err = client.Get(resourceURL(client, id), &r.Body, nil) 206 return 207 } 208 209 // ListOptsBuilder allows extensions to add additional parameters to the List 210 // request. 211 type ListOptsBuilder interface { 212 ToVolumeListQuery() (string, error) 213 } 214 215 // ListOpts holds options for listing Volumes. It is passed to the volumes.List 216 // function. 217 type ListOpts struct { 218 // Name will filter by the specified volume name. 219 Name string `q:"name"` 220 221 // Status will filter by the specified status. 222 Status string `q:"status"` 223 224 // Metadata will filter results based on specified metadata. 225 Metadata map[string]string `q:"metadata"` 226 227 ID string `q:"id"` 228 229 ServerID string `q:"server_id"` 230 231 SortKey string `q:"sort_key"` 232 SortDir string `q:"sort_dir"` 233 234 // Requests a page size of items. 235 Limit int `q:"limit"` 236 237 // Used in conjunction with limit to return a slice of items. 238 Offset int `q:"offset"` 239 240 // The ID of the last-seen item. 241 Marker string `q:"marker"` 242 } 243 244 // ToVolumeListQuery formats a ListOpts into a query string. 245 func (opts ListOpts) ToVolumeListQuery() (string, error) { 246 q, err := golangsdk.BuildQueryString(opts) 247 return q.String(), err 248 } 249 250 // List returns Volumes optionally limited by the conditions provided in ListOpts. 251 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 252 url := listURL(client) 253 if opts != nil { 254 query, err := opts.ToVolumeListQuery() 255 if err != nil { 256 return pagination.Pager{Err: err} 257 } 258 url += query 259 } 260 261 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 262 return VolumePage{pagination.LinkedPageBase{PageResult: r}} 263 }) 264 }