github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/blockstorage/v2/volumes/requests.go (about) 1 package volumes 2 3 import ( 4 "github.com/opentelekomcloud/gophertelekomcloud" 5 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 6 ) 7 8 // CreateOptsBuilder allows extensions to add additional parameters to the 9 // Create request. 10 type CreateOptsBuilder interface { 11 ToVolumeCreateMap() (map[string]interface{}, error) 12 } 13 14 // CreateOpts contains options for creating a Volume. This object is passed to 15 // the volumes.Create function. For more information about these parameters, 16 // see the Volume object. 17 type CreateOpts struct { 18 // The size of the volume, in GB 19 Size int `json:"size" required:"true"` 20 // The availability zone 21 AvailabilityZone string `json:"availability_zone,omitempty"` 22 // ConsistencyGroupID is the ID of a consistency group 23 ConsistencyGroupID string `json:"consistencygroup_id,omitempty"` 24 // The volume description 25 Description string `json:"description,omitempty"` 26 // One or more metadata key and value pairs to associate with the volume 27 Metadata map[string]string `json:"metadata,omitempty"` 28 // The volume name 29 Name string `json:"name,omitempty"` 30 // the ID of the existing volume snapshot 31 SnapshotID string `json:"snapshot_id,omitempty"` 32 // SourceReplica is a UUID of an existing volume to replicate with 33 SourceReplica string `json:"source_replica,omitempty"` 34 // the ID of the existing volume 35 SourceVolID string `json:"source_volid,omitempty"` 36 // The ID of the image from which you want to create the volume. 37 // Required to create a bootable volume. 38 ImageID string `json:"imageRef,omitempty"` 39 // The associated volume type 40 VolumeType string `json:"volume_type,omitempty"` 41 // Shared disk 42 Multiattach bool `json:"multiattach,omitempty"` 43 } 44 45 // ToVolumeCreateMap assembles a request body based on the contents of a 46 // CreateOpts. 47 func (opts CreateOpts) ToVolumeCreateMap() (map[string]interface{}, error) { 48 return golangsdk.BuildRequestBody(opts, "volume") 49 } 50 51 // Create will create a new Volume based on the values in CreateOpts. To extract 52 // the Volume object from the response, call the Extract method on the 53 // CreateResult. 54 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 55 b, err := opts.ToVolumeCreateMap() 56 if err != nil { 57 r.Err = err 58 return 59 } 60 _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{ 61 OkCodes: []int{202}, 62 }) 63 return 64 } 65 66 // DeleteOptsBuilder is an interface by which can be able to build the query string 67 // of volume deletion. 68 type DeleteOptsBuilder interface { 69 ToVolumeDeleteQuery() (string, error) 70 } 71 72 type DeleteOpts struct { 73 // Specifies to delete all snapshots associated with the EVS disk. 74 Cascade bool `q:"cascade"` 75 } 76 77 func (opts DeleteOpts) ToVolumeDeleteQuery() (string, error) { 78 q, err := golangsdk.BuildQueryString(opts) 79 if err != nil { 80 return "", err 81 } 82 return q.String(), err 83 } 84 85 // Delete will delete the existing Volume with the provided ID 86 func Delete(client *golangsdk.ServiceClient, id string, opts DeleteOptsBuilder) (r DeleteResult) { 87 url := deleteURL(client, id) 88 if opts != nil { 89 q, err := opts.ToVolumeDeleteQuery() 90 if err != nil { 91 r.Err = err 92 return 93 } 94 url += q 95 } 96 _, r.Err = client.Delete(url, nil) 97 return 98 } 99 100 // Get retrieves the Volume with the provided ID. To extract the Volume object 101 // from the response, call the Extract method on the GetResult. 102 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 103 _, r.Err = client.Get(getURL(client, id), &r.Body, nil) 104 return 105 } 106 107 // ListOptsBuilder allows extensions to add additional parameters to the List 108 // request. 109 type ListOptsBuilder interface { 110 ToVolumeListQuery() (string, error) 111 } 112 113 // ListOpts holds options for listing Volumes. It is passed to the volumes.List 114 // function. 115 type ListOpts struct { 116 // AllTenants will retrieve volumes of all tenants/projects. 117 AllTenants bool `q:"all_tenants"` 118 119 // Metadata will filter results based on specified metadata. 120 Metadata map[string]string `q:"metadata"` 121 122 // Name will filter by the specified volume name. 123 Name string `q:"name"` 124 125 // Status will filter by the specified status. 126 Status string `q:"status"` 127 128 // TenantID will filter by a specific tenant/project ID. 129 // Setting AllTenants is required for this. 130 TenantID string `q:"project_id"` 131 132 // Comma-separated list of sort keys and optional sort directions in the 133 // form of <key>[:<direction>]. 134 Sort string `q:"sort"` 135 136 // Requests a page size of items. 137 Limit int `q:"limit"` 138 139 // Used in conjunction with limit to return a slice of items. 140 Offset int `q:"offset"` 141 142 // The ID of the last-seen item. 143 Marker string `q:"marker"` 144 } 145 146 // ToVolumeListQuery formats a ListOpts into a query string. 147 func (opts ListOpts) ToVolumeListQuery() (string, error) { 148 q, err := golangsdk.BuildQueryString(opts) 149 if err != nil { 150 return "", err 151 } 152 return q.String(), err 153 } 154 155 // List returns Volumes optionally limited by the conditions provided in ListOpts. 156 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 157 url := listURL(client) 158 if opts != nil { 159 query, err := opts.ToVolumeListQuery() 160 if err != nil { 161 return pagination.Pager{Err: err} 162 } 163 url += query 164 } 165 166 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 167 return VolumePage{pagination.LinkedPageBase{PageResult: r}} 168 }) 169 } 170 171 // UpdateOptsBuilder allows extensions to add additional parameters to the 172 // Update request. 173 type UpdateOptsBuilder interface { 174 ToVolumeUpdateMap() (map[string]interface{}, error) 175 } 176 177 // UpdateOpts contain options for updating an existing Volume. This object is passed 178 // to the volumes.Update function. For more information about the parameters, see 179 // the Volume object. 180 type UpdateOpts struct { 181 Name string `json:"name,omitempty"` 182 Description string `json:"description,omitempty"` 183 Metadata map[string]string `json:"metadata,omitempty"` 184 } 185 186 // ToVolumeUpdateMap assembles a request body based on the contents of an 187 // UpdateOpts. 188 func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) { 189 return golangsdk.BuildRequestBody(opts, "volume") 190 } 191 192 // Update will update the Volume with provided information. To extract the updated 193 // Volume from the response, call the Extract method on the UpdateResult. 194 func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 195 b, err := opts.ToVolumeUpdateMap() 196 if err != nil { 197 r.Err = err 198 return 199 } 200 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ 201 OkCodes: []int{200}, 202 }) 203 return 204 } 205 206 // IDFromName is a convienience function that returns a server's ID given its name. 207 func IDFromName(client *golangsdk.ServiceClient, name string) (string, error) { 208 count := 0 209 id := "" 210 211 listOpts := ListOpts{ 212 Name: name, 213 } 214 215 pages, err := List(client, listOpts).AllPages() 216 if err != nil { 217 return "", err 218 } 219 220 all, err := ExtractVolumes(pages) 221 if err != nil { 222 return "", err 223 } 224 225 for _, s := range all { 226 if s.Name == name { 227 count++ 228 id = s.ID 229 } 230 } 231 232 switch count { 233 case 0: 234 return "", golangsdk.ErrResourceNotFound{Name: name, ResourceType: "volume"} 235 case 1: 236 return id, nil 237 default: 238 return "", golangsdk.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "volume"} 239 } 240 }