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