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