github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/v1/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 Size int `json:"size" required:"true"` 19 AvailabilityZone string `json:"availability_zone,omitempty"` 20 Description string `json:"display_description,omitempty"` 21 Metadata map[string]string `json:"metadata,omitempty"` 22 Name string `json:"display_name,omitempty"` 23 SnapshotID string `json:"snapshot_id,omitempty"` 24 SourceVolID string `json:"source_volid,omitempty"` 25 ImageID string `json:"imageRef,omitempty"` 26 VolumeType string `json:"volume_type,omitempty"` 27 } 28 29 // ToVolumeCreateMap assembles a request body based on the contents of a 30 // CreateOpts. 31 func (opts CreateOpts) ToVolumeCreateMap() (map[string]interface{}, error) { 32 return golangsdk.BuildRequestBody(opts, "volume") 33 } 34 35 // Create will create a new Volume based on the values in CreateOpts. To extract 36 // the Volume object from the response, call the Extract method on the 37 // CreateResult. 38 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 39 b, err := opts.ToVolumeCreateMap() 40 if err != nil { 41 r.Err = err 42 return 43 } 44 _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{ 45 OkCodes: []int{200, 201}, 46 }) 47 return 48 } 49 50 // Delete will delete the existing Volume with the provided ID. 51 func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { 52 _, r.Err = client.Delete(deleteURL(client, id), nil) 53 return 54 } 55 56 // Get retrieves the Volume with the provided ID. To extract the Volume object 57 // from the response, call the Extract method on the GetResult. 58 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 59 _, r.Err = client.Get(getURL(client, id), &r.Body, nil) 60 return 61 } 62 63 // ListOptsBuilder allows extensions to add additional parameters to the List 64 // request. 65 type ListOptsBuilder interface { 66 ToVolumeListQuery() (string, error) 67 } 68 69 // ListOpts holds options for listing Volumes. It is passed to the volumes.List 70 // function. 71 type ListOpts struct { 72 // admin-only option. Set it to true to see all tenant volumes. 73 AllTenants bool `q:"all_tenants"` 74 // List only volumes that contain Metadata. 75 Metadata map[string]string `q:"metadata"` 76 // List only volumes that have Name as the display name. 77 Name string `q:"display_name"` 78 // List only volumes that have a status of Status. 79 Status string `q:"status"` 80 } 81 82 // ToVolumeListQuery formats a ListOpts into a query string. 83 func (opts ListOpts) ToVolumeListQuery() (string, error) { 84 q, err := golangsdk.BuildQueryString(opts) 85 return q.String(), err 86 } 87 88 // List returns Volumes optionally limited by the conditions provided in ListOpts. 89 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 90 url := listURL(client) 91 if opts != nil { 92 query, err := opts.ToVolumeListQuery() 93 if err != nil { 94 return pagination.Pager{Err: err} 95 } 96 url += query 97 } 98 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 99 return VolumePage{pagination.SinglePageBase(r)} 100 }) 101 } 102 103 // UpdateOptsBuilder allows extensions to add additional parameters to the 104 // Update request. 105 type UpdateOptsBuilder interface { 106 ToVolumeUpdateMap() (map[string]interface{}, error) 107 } 108 109 // UpdateOpts contain options for updating an existing Volume. This object is passed 110 // to the volumes.Update function. For more information about the parameters, see 111 // the Volume object. 112 type UpdateOpts struct { 113 Name string `json:"display_name,omitempty"` 114 Description string `json:"display_description,omitempty"` 115 Metadata map[string]string `json:"metadata,omitempty"` 116 } 117 118 // ToVolumeUpdateMap assembles a request body based on the contents of an 119 // UpdateOpts. 120 func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) { 121 return golangsdk.BuildRequestBody(opts, "volume") 122 } 123 124 // Update will update the Volume with provided information. To extract the updated 125 // Volume from the response, call the Extract method on the UpdateResult. 126 func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 127 b, err := opts.ToVolumeUpdateMap() 128 if err != nil { 129 r.Err = err 130 return 131 } 132 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ 133 OkCodes: []int{200}, 134 }) 135 return 136 } 137 138 // IDFromName is a convienience function that returns a server's ID given its name. 139 func IDFromName(client *golangsdk.ServiceClient, name string) (string, error) { 140 count := 0 141 id := "" 142 143 listOpts := ListOpts{ 144 Name: name, 145 } 146 147 pages, err := List(client, listOpts).AllPages() 148 if err != nil { 149 return "", err 150 } 151 152 all, err := ExtractVolumes(pages) 153 if err != nil { 154 return "", err 155 } 156 157 for _, s := range all { 158 if s.Name == name { 159 count++ 160 id = s.ID 161 } 162 } 163 164 switch count { 165 case 0: 166 return "", golangsdk.ErrResourceNotFound{Name: name, ResourceType: "volume"} 167 case 1: 168 return id, nil 169 default: 170 return "", golangsdk.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "volume"} 171 } 172 }