github.com/gophercloud/gophercloud@v1.11.0/openstack/sharedfilesystems/v2/snapshots/requests.go (about) 1 package snapshots 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/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 the options for create a Snapshot. This object is 15 // passed to snapshots.Create(). For more information about these parameters, 16 // please refer to the Snapshot object, or the shared file systems API v2 17 // documentation 18 type CreateOpts struct { 19 // The UUID of the share from which to create a snapshot 20 ShareID string `json:"share_id" required:"true"` 21 // Defines the snapshot name 22 Name string `json:"name,omitempty"` 23 // Defines the snapshot description 24 Description string `json:"description,omitempty"` 25 // DisplayName is equivalent to Name. The API supports using both 26 // This is an inherited attribute from the block storage API 27 DisplayName string `json:"display_name,omitempty"` 28 // DisplayDescription is equivalent to Description. The API supports using both 29 // This is an inherited attribute from the block storage API 30 DisplayDescription string `json:"display_description,omitempty"` 31 } 32 33 // ToSnapshotCreateMap assembles a request body based on the contents of a 34 // CreateOpts. 35 func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) { 36 return gophercloud.BuildRequestBody(opts, "snapshot") 37 } 38 39 // Create will create a new Snapshot based on the values in CreateOpts. To extract 40 // the Snapshot object from the response, call the Extract method on the 41 // CreateResult. 42 func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 43 b, err := opts.ToSnapshotCreateMap() 44 if err != nil { 45 r.Err = err 46 return 47 } 48 resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ 49 OkCodes: []int{200, 201, 202}, 50 }) 51 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 52 return 53 } 54 55 // ListOpts holds options for listing Snapshots. It is passed to the 56 // snapshots.List function. 57 type ListOpts struct { 58 // (Admin only). Defines whether to list the requested resources for all projects. 59 AllTenants bool `q:"all_tenants"` 60 // The snapshot name. 61 Name string `q:"name"` 62 // Filter by a snapshot description. 63 Description string `q:"description"` 64 // Filters by a share from which the snapshot was created. 65 ShareID string `q:"share_id"` 66 // Filters by a snapshot size in GB. 67 Size int `q:"size"` 68 // Filters by a snapshot status. 69 Status string `q:"status"` 70 // The maximum number of snapshots to return. 71 Limit int `q:"limit"` 72 // The offset to define start point of snapshot or snapshot group listing. 73 Offset int `q:"offset"` 74 // The key to sort a list of snapshots. 75 SortKey string `q:"sort_key"` 76 // The direction to sort a list of snapshots. 77 SortDir string `q:"sort_dir"` 78 // The UUID of the project in which the snapshot was created. Useful with all_tenants parameter. 79 ProjectID string `q:"project_id"` 80 // The name pattern that can be used to filter snapshots, snapshot snapshots, snapshot networks or snapshot groups. 81 NamePattern string `q:"name~"` 82 // The description pattern that can be used to filter snapshots, snapshot snapshots, snapshot networks or snapshot groups. 83 DescriptionPattern string `q:"description~"` 84 } 85 86 // ListOptsBuilder allows extensions to add additional parameters to the List 87 // request. 88 type ListOptsBuilder interface { 89 ToSnapshotListQuery() (string, error) 90 } 91 92 // ToSnapshotListQuery formats a ListOpts into a query string. 93 func (opts ListOpts) ToSnapshotListQuery() (string, error) { 94 q, err := gophercloud.BuildQueryString(opts) 95 return q.String(), err 96 } 97 98 // ListDetail returns []Snapshot optionally limited by the conditions provided in ListOpts. 99 func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 100 url := listDetailURL(client) 101 if opts != nil { 102 query, err := opts.ToSnapshotListQuery() 103 if err != nil { 104 return pagination.Pager{Err: err} 105 } 106 url += query 107 } 108 109 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 110 p := SnapshotPage{pagination.MarkerPageBase{PageResult: r}} 111 p.MarkerPageBase.Owner = p 112 return p 113 }) 114 } 115 116 // Delete will delete an existing Snapshot with the given UUID. 117 func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { 118 resp, err := client.Delete(deleteURL(client, id), nil) 119 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 120 return 121 } 122 123 // Get will get a single snapshot with given UUID 124 func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { 125 resp, err := client.Get(getURL(client, id), &r.Body, nil) 126 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 127 return 128 } 129 130 // UpdateOptsBuilder allows extensions to add additional parameters to the 131 // Update request. 132 type UpdateOptsBuilder interface { 133 ToSnapshotUpdateMap() (map[string]interface{}, error) 134 } 135 136 // UpdateOpts contain options for updating an existing Snapshot. This object is passed 137 // to the snapshot.Update function. For more information about the parameters, see 138 // the Snapshot object. 139 type UpdateOpts struct { 140 // Snapshot name. Manila snapshot update logic doesn't have a "name" alias. 141 DisplayName *string `json:"display_name,omitempty"` 142 // Snapshot description. Manila snapshot update logic doesn't have a "description" alias. 143 DisplayDescription *string `json:"display_description,omitempty"` 144 } 145 146 // ToSnapshotUpdateMap assembles a request body based on the contents of an 147 // UpdateOpts. 148 func (opts UpdateOpts) ToSnapshotUpdateMap() (map[string]interface{}, error) { 149 return gophercloud.BuildRequestBody(opts, "snapshot") 150 } 151 152 // Update will update the Snapshot with provided information. To extract the updated 153 // Snapshot from the response, call the Extract method on the UpdateResult. 154 func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 155 b, err := opts.ToSnapshotUpdateMap() 156 if err != nil { 157 r.Err = err 158 return 159 } 160 resp, err := client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ 161 OkCodes: []int{200}, 162 }) 163 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 164 return 165 } 166 167 // ResetStatusOptsBuilder allows extensions to add additional parameters to the 168 // ResetStatus request. 169 type ResetStatusOptsBuilder interface { 170 ToSnapshotResetStatusMap() (map[string]interface{}, error) 171 } 172 173 // ResetStatusOpts contains options for resetting a Snapshot status. 174 // For more information about these parameters, please, refer to the shared file systems API v2, 175 // Snapshot Actions, ResetStatus share documentation. 176 type ResetStatusOpts struct { 177 // Status is a snapshot status to reset to. Can be "available", "error", 178 // "creating", "deleting", "manage_starting", "manage_error", 179 // "unmanage_starting", "unmanage_error" or "error_deleting". 180 Status string `json:"status"` 181 } 182 183 // ToSnapshotResetStatusMap assembles a request body based on the contents of a 184 // ResetStatusOpts. 185 func (opts ResetStatusOpts) ToSnapshotResetStatusMap() (map[string]interface{}, error) { 186 return gophercloud.BuildRequestBody(opts, "reset_status") 187 } 188 189 // ResetStatus will reset the existing snapshot status. ResetStatusResult contains only the error. 190 // To extract it, call the ExtractErr method on the ResetStatusResult. 191 func ResetStatus(client *gophercloud.ServiceClient, id string, opts ResetStatusOptsBuilder) (r ResetStatusResult) { 192 b, err := opts.ToSnapshotResetStatusMap() 193 if err != nil { 194 r.Err = err 195 return 196 } 197 198 resp, err := client.Post(resetStatusURL(client, id), b, nil, &gophercloud.RequestOpts{ 199 OkCodes: []int{202}, 200 }) 201 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 202 return 203 } 204 205 // ForceDelete will delete the existing snapshot in any state. ForceDeleteResult contains only the error. 206 // To extract it, call the ExtractErr method on the ForceDeleteResult. 207 func ForceDelete(client *gophercloud.ServiceClient, id string) (r ForceDeleteResult) { 208 b := map[string]interface{}{ 209 "force_delete": nil, 210 } 211 resp, err := client.Post(forceDeleteURL(client, id), b, nil, &gophercloud.RequestOpts{ 212 OkCodes: []int{202}, 213 }) 214 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 215 return 216 }