github.com/leeclow-ops/gophercloud@v1.2.1/openstack/blockstorage/v3/snapshots/requests.go (about) 1 package snapshots 2 3 import ( 4 "github.com/leeclow-ops/gophercloud" 5 "github.com/leeclow-ops/gophercloud/pagination" 6 ) 7 8 // CreateOptsBuilder allows extensions to add additional parameters to the 9 // Create request. 10 type CreateOptsBuilder interface { 11 ToSnapshotCreateMap() (map[string]interface{}, error) 12 } 13 14 // CreateOpts contains options for creating a Snapshot. This object is passed to 15 // the snapshots.Create function. For more information about these parameters, 16 // see the Snapshot object. 17 type CreateOpts struct { 18 VolumeID string `json:"volume_id" required:"true"` 19 Force bool `json:"force,omitempty"` 20 Name string `json:"name,omitempty"` 21 Description string `json:"description,omitempty"` 22 Metadata map[string]string `json:"metadata,omitempty"` 23 } 24 25 // ToSnapshotCreateMap assembles a request body based on the contents of a 26 // CreateOpts. 27 func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) { 28 return gophercloud.BuildRequestBody(opts, "snapshot") 29 } 30 31 // Create will create a new Snapshot based on the values in CreateOpts. To 32 // extract the Snapshot object from the response, call the Extract method on the 33 // CreateResult. 34 func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 35 b, err := opts.ToSnapshotCreateMap() 36 if err != nil { 37 r.Err = err 38 return 39 } 40 resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ 41 OkCodes: []int{202}, 42 }) 43 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 44 return 45 } 46 47 // Delete will delete the existing Snapshot with the provided ID. 48 func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { 49 resp, err := client.Delete(deleteURL(client, id), nil) 50 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 51 return 52 } 53 54 // Get retrieves the Snapshot with the provided ID. To extract the Snapshot 55 // object from the response, call the Extract method on the GetResult. 56 func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { 57 resp, err := client.Get(getURL(client, id), &r.Body, nil) 58 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 59 return 60 } 61 62 // ListOptsBuilder allows extensions to add additional parameters to the List 63 // request. 64 type ListOptsBuilder interface { 65 ToSnapshotListQuery() (string, error) 66 } 67 68 // ListOpts holds options for listing Snapshots. It is passed to the snapshots.List 69 // function. 70 type ListOpts struct { 71 // AllTenants will retrieve snapshots of all tenants/projects. 72 AllTenants bool `q:"all_tenants"` 73 74 // Name will filter by the specified snapshot name. 75 Name string `q:"name"` 76 77 // Status will filter by the specified status. 78 Status string `q:"status"` 79 80 // TenantID will filter by a specific tenant/project ID. 81 // Setting AllTenants is required to use this. 82 TenantID string `q:"project_id"` 83 84 // VolumeID will filter by a specified volume ID. 85 VolumeID string `q:"volume_id"` 86 87 // Comma-separated list of sort keys and optional sort directions in the 88 // form of <key>[:<direction>]. 89 Sort string `q:"sort"` 90 91 // Requests a page size of items. 92 Limit int `q:"limit"` 93 94 // Used in conjunction with limit to return a slice of items. 95 Offset int `q:"offset"` 96 97 // The ID of the last-seen item. 98 Marker string `q:"marker"` 99 } 100 101 // ToSnapshotListQuery formats a ListOpts into a query string. 102 func (opts ListOpts) ToSnapshotListQuery() (string, error) { 103 q, err := gophercloud.BuildQueryString(opts) 104 return q.String(), err 105 } 106 107 // List returns Snapshots optionally limited by the conditions provided in 108 // ListOpts. 109 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 110 url := listURL(client) 111 if opts != nil { 112 query, err := opts.ToSnapshotListQuery() 113 if err != nil { 114 return pagination.Pager{Err: err} 115 } 116 url += query 117 } 118 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 119 return SnapshotPage{pagination.LinkedPageBase{PageResult: r}} 120 }) 121 } 122 123 // UpdateOptsBuilder allows extensions to add additional parameters to the 124 // Update request. 125 type UpdateOptsBuilder interface { 126 ToSnapshotUpdateMap() (map[string]interface{}, error) 127 } 128 129 // UpdateOpts contain options for updating an existing Snapshot. This object is passed 130 // to the snapshots.Update function. For more information about the parameters, see 131 // the Snapshot object. 132 type UpdateOpts struct { 133 Name *string `json:"name,omitempty"` 134 Description *string `json:"description,omitempty"` 135 } 136 137 // ToSnapshotUpdateMap assembles a request body based on the contents of an 138 // UpdateOpts. 139 func (opts UpdateOpts) ToSnapshotUpdateMap() (map[string]interface{}, error) { 140 return gophercloud.BuildRequestBody(opts, "snapshot") 141 } 142 143 // Update will update the Snapshot with provided information. To extract the updated 144 // Snapshot from the response, call the Extract method on the UpdateResult. 145 func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 146 b, err := opts.ToSnapshotUpdateMap() 147 if err != nil { 148 r.Err = err 149 return 150 } 151 resp, err := client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 152 OkCodes: []int{200}, 153 }) 154 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 155 return 156 } 157 158 // UpdateMetadataOptsBuilder allows extensions to add additional parameters to 159 // the Update request. 160 type UpdateMetadataOptsBuilder interface { 161 ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) 162 } 163 164 // UpdateMetadataOpts contain options for updating an existing Snapshot. This 165 // object is passed to the snapshots.Update function. For more information 166 // about the parameters, see the Snapshot object. 167 type UpdateMetadataOpts struct { 168 Metadata map[string]interface{} `json:"metadata,omitempty"` 169 } 170 171 // ToSnapshotUpdateMetadataMap assembles a request body based on the contents of 172 // an UpdateMetadataOpts. 173 func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) { 174 return gophercloud.BuildRequestBody(opts, "") 175 } 176 177 // UpdateMetadata will update the Snapshot with provided information. To 178 // extract the updated Snapshot from the response, call the ExtractMetadata 179 // method on the UpdateMetadataResult. 180 func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) { 181 b, err := opts.ToSnapshotUpdateMetadataMap() 182 if err != nil { 183 r.Err = err 184 return 185 } 186 resp, err := client.Put(updateMetadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 187 OkCodes: []int{200}, 188 }) 189 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 190 return 191 }