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