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