github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/evs/v2/snapshots/requests.go (about) 1 package snapshots 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 "github.com/chnsz/golangsdk/pagination" 6 ) 7 8 var requestOpts = golangsdk.RequestOpts{ 9 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 10 } 11 12 // CreateOptsBuilder allows extensions to add additional parameters to the 13 // Create request. 14 type CreateOptsBuilder interface { 15 ToSnapshotCreateMap() (map[string]interface{}, error) 16 } 17 18 // CreateOpts contains options for creating a Snapshot. 19 // This object is passed to the snapshots.Create function. 20 type CreateOpts struct { 21 VolumeID string `json:"volume_id" required:"true"` 22 Force bool `json:"force,omitempty"` 23 Name string `json:"name,omitempty"` 24 Description string `json:"description,omitempty"` 25 Metadata map[string]string `json:"metadata,omitempty"` 26 } 27 28 // ToSnapshotCreateMap assembles a request body based on the contents of a 29 // CreateOpts. 30 func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) { 31 return golangsdk.BuildRequestBody(opts, "snapshot") 32 } 33 34 // Create will create a new Snapshot based on the values in CreateOpts. To 35 // extract the Snapshot object from the response, call the Extract method on the 36 // CreateResult. 37 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 38 b, err := opts.ToSnapshotCreateMap() 39 if err != nil { 40 r.Err = err 41 return 42 } 43 _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{ 44 OkCodes: []int{202}, 45 }) 46 return 47 } 48 49 // UpdateOptsBuilder allows extensions to add additional parameters to 50 // the Update request. 51 type UpdateOptsBuilder interface { 52 ToSnapshotUpdateMap() (map[string]interface{}, error) 53 } 54 55 // UpdateOpts contain options for updating an existing Snapshot. 56 // This object is passed to the snapshots.Update function. 57 type UpdateOpts struct { 58 Name string `json:"name,omitempty"` 59 Description string `json:"description,omitempty"` 60 } 61 62 // ToSnapshotUpdateMap assembles a request body based on the contents of 63 // an UpdateOpts. 64 func (opts UpdateOpts) ToSnapshotUpdateMap() (map[string]interface{}, error) { 65 return golangsdk.BuildRequestBody(opts, "snapshot") 66 } 67 68 // Update will update the Snapshot with provided information. To 69 // extract the updated Snapshot from the response, call the ExtractMetadata 70 // method on the UpdateResult. 71 func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 72 b, err := opts.ToSnapshotUpdateMap() 73 if err != nil { 74 r.Err = err 75 return 76 } 77 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ 78 OkCodes: []int{200}, 79 }) 80 return 81 } 82 83 // Delete will delete the existing Snapshot with the provided ID. 84 func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { 85 _, r.Err = client.Delete(deleteURL(client, id), nil) 86 return 87 } 88 89 // Get retrieves the Snapshot with the provided ID. To extract the Snapshot 90 // object from the response, call the Extract method on the GetResult. 91 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 92 _, r.Err = client.Get(getURL(client, id), &r.Body, nil) 93 return 94 } 95 96 // ListOptsBuilder allows extensions to add additional parameters to the List 97 // request. 98 type ListOptsBuilder interface { 99 ToSnapshotListQuery() (string, error) 100 } 101 102 // ListOpts hold options for listing Snapshots. It is passed to the 103 // snapshots.List function. 104 type ListOpts struct { 105 // Name will filter by the specified snapshot name. 106 Name string `q:"name"` 107 108 // Status will filter by the specified status. 109 Status string `q:"status"` 110 111 // VolumeID will filter by a specified volume ID. 112 VolumeID string `q:"volume_id"` 113 114 // ID will filter by a specific snapshot ID. 115 ID string `q:"id"` 116 117 Limit int `q:"limit"` 118 Offset int `q:"offset"` 119 } 120 121 // ToSnapshotListQuery formats a ListOpts into a query string. 122 func (opts ListOpts) ToSnapshotListQuery() (string, error) { 123 q, err := golangsdk.BuildQueryString(opts) 124 return q.String(), err 125 } 126 127 // List returns Snapshots optionally limited by the conditions provided in 128 // ListOpts. 129 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 130 url := listURL(client) 131 if opts != nil { 132 query, err := opts.ToSnapshotListQuery() 133 if err != nil { 134 return pagination.Pager{Err: err} 135 } 136 url += query 137 } 138 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 139 return SnapshotPage{pagination.SinglePageBase(r)} 140 }) 141 } 142 143 func ListPage(client *golangsdk.ServiceClient, opts ListOptsBuilder) (*PagedList, error) { 144 url := listURL(client) 145 if opts != nil { 146 query, err := opts.ToSnapshotListQuery() 147 if err != nil { 148 return nil, err 149 } 150 url += query 151 } 152 153 var rst golangsdk.Result 154 _, err := client.Get(url, &rst.Body, &golangsdk.RequestOpts{ 155 MoreHeaders: requestOpts.MoreHeaders, 156 }) 157 158 if err != nil { 159 return nil, err 160 } 161 162 var r PagedList 163 err = rst.ExtractInto(&r) 164 return &r, err 165 }