Skip to content

Commit c77c19f

Browse files
authored
Merge pull request #3330 from gophercloud/bp-v2-07c8148
[v2] Enhance Snapshot struct and add ListDetail function in V3 blockstorage
2 parents 908ce17 + 88b963b commit c77c19f

File tree

6 files changed

+163
-0
lines changed

6 files changed

+163
-0
lines changed

internal/acceptance/openstack/blockstorage/v3/snapshots_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,24 @@ func TestSnapshots(t *testing.T) {
7373

7474
return true, nil
7575
})
76+
th.AssertNoErr(t, err)
77+
78+
err = snapshots.ListDetail(client, listOpts).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
79+
actual, err := snapshots.ExtractSnapshots(page)
80+
th.AssertNoErr(t, err)
81+
th.AssertEquals(t, 1, len(actual))
82+
83+
var found bool
84+
for _, v := range actual {
85+
if v.ID == snapshot1.ID || v.ID == snapshot2.ID {
86+
found = true
87+
}
88+
}
7689

90+
th.AssertEquals(t, found, true)
91+
92+
return true, nil
93+
})
7794
th.AssertNoErr(t, err)
7895
}
7996

openstack/blockstorage/v3/snapshots/requests.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,21 @@ func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pa
122122
})
123123
}
124124

125+
// ListDetail returns Snapshots with additional details optionally limited by the conditions provided in ListOpts.
126+
func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
127+
url := listDetailsURL(client)
128+
if opts != nil {
129+
query, err := opts.ToSnapshotListQuery()
130+
if err != nil {
131+
return pagination.Pager{Err: err}
132+
}
133+
url += query
134+
}
135+
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
136+
return SnapshotPage{pagination.LinkedPageBase{PageResult: r}}
137+
})
138+
}
139+
125140
// UpdateOptsBuilder allows extensions to add additional parameters to the
126141
// Update request.
127142
type UpdateOptsBuilder interface {

openstack/blockstorage/v3/snapshots/results.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ type Snapshot struct {
3636

3737
// User-defined key-value pairs.
3838
Metadata map[string]string `json:"metadata"`
39+
40+
// Progress of the snapshot creation.
41+
Progress string `json:"os-extended-snapshot-attributes:progress"`
42+
43+
// Project ID that owns the snapshot.
44+
ProjectID string `json:"os-extended-snapshot-attributes:project_id"`
45+
46+
// ID of the group snapshot, if applicable.
47+
GroupSnapshotID string `json:"group_snapshot_id"`
48+
49+
// User ID that created the snapshot.
50+
UserID string `json:"user_id"`
51+
52+
// Indicates whether the snapshot consumes quota.
53+
ConsumesQuota bool `json:"consumes_quota"`
3954
}
4055

4156
// CreateResult contains the response body and error from a Create request.

openstack/blockstorage/v3/snapshots/testing/fixtures_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,62 @@ func MockListResponse(t *testing.T) {
6161
})
6262
}
6363

64+
func MockListDetailsResponse(t *testing.T) {
65+
th.Mux.HandleFunc("/snapshots/detail", func(w http.ResponseWriter, r *http.Request) {
66+
th.TestMethod(t, r, "GET")
67+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
68+
69+
w.Header().Add("Content-Type", "application/json")
70+
w.WriteHeader(http.StatusOK)
71+
72+
if err := r.ParseForm(); err != nil {
73+
t.Errorf("Failed to parse request form %v", err)
74+
}
75+
marker := r.Form.Get("marker")
76+
switch marker {
77+
case "":
78+
fmt.Fprint(w, `
79+
{
80+
"snapshots": [
81+
{
82+
"id": "289da7f8-6440-407c-9fb4-7db01ec49164",
83+
"name": "snapshot-001",
84+
"volume_id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
85+
"description": "Daily Backup",
86+
"status": "available",
87+
"size": 30,
88+
"created_at": "2017-05-30T03:35:03.000000",
89+
"os-extended-snapshot-attributes:progress": "100%",
90+
"os-extended-snapshot-attributes:project_id": "84b8950a-8594-4e5b-8dce-0dfa9c696357",
91+
"group_snapshot_id": null,
92+
"user_id": "075da7f8-6440-407c-9fb4-7db01ec49531",
93+
"consumes_quota": true
94+
},
95+
{
96+
"id": "96c3bda7-c82a-4f50-be73-ca7621794835",
97+
"name": "snapshot-002",
98+
"volume_id": "76b8950a-8594-4e5b-8dce-0dfa9c696358",
99+
"description": "Weekly Backup",
100+
"status": "available",
101+
"size": 25,
102+
"created_at": "2017-05-30T03:35:03.000000",
103+
"os-extended-snapshot-attributes:progress": "50%",
104+
"os-extended-snapshot-attributes:project_id": "84b8950a-8594-4e5b-8dce-0dfa9c696357",
105+
"group_snapshot_id": "865da7f8-6440-407c-9fb4-7db01ec40876",
106+
"user_id": "075da7f8-6440-407c-9fb4-7db01ec49531",
107+
"consumes_quota": false
108+
}
109+
]
110+
}
111+
`)
112+
case "1":
113+
fmt.Fprint(w, `{"snapshots": []}`)
114+
default:
115+
t.Fatalf("Unexpected marker: [%s]", marker)
116+
}
117+
})
118+
}
119+
64120
// MockGetResponse provides mock response for get snapshot API call
65121
func MockGetResponse(t *testing.T) {
66122
th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {

openstack/blockstorage/v3/snapshots/testing/requests_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,62 @@ func TestList(t *testing.T) {
5959
}
6060
}
6161

62+
func TestDetailList(t *testing.T) {
63+
th.SetupHTTP()
64+
defer th.TeardownHTTP()
65+
66+
MockListDetailsResponse(t)
67+
68+
count := 0
69+
70+
err := snapshots.ListDetail(client.ServiceClient(), &snapshots.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
71+
count++
72+
actual, err := snapshots.ExtractSnapshots(page)
73+
if err != nil {
74+
t.Errorf("Failed to extract snapshots: %v", err)
75+
return false, err
76+
}
77+
expected := []snapshots.Snapshot{
78+
{
79+
ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
80+
Name: "snapshot-001",
81+
VolumeID: "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
82+
Status: "available",
83+
Size: 30,
84+
CreatedAt: time.Date(2017, 5, 30, 3, 35, 3, 0, time.UTC),
85+
Description: "Daily Backup",
86+
Progress: "100%",
87+
ProjectID: "84b8950a-8594-4e5b-8dce-0dfa9c696357",
88+
GroupSnapshotID: "",
89+
UserID: "075da7f8-6440-407c-9fb4-7db01ec49531",
90+
ConsumesQuota: true,
91+
},
92+
{
93+
ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
94+
Name: "snapshot-002",
95+
VolumeID: "76b8950a-8594-4e5b-8dce-0dfa9c696358",
96+
Status: "available",
97+
Size: 25,
98+
CreatedAt: time.Date(2017, 5, 30, 3, 35, 3, 0, time.UTC),
99+
Description: "Weekly Backup",
100+
Progress: "50%",
101+
ProjectID: "84b8950a-8594-4e5b-8dce-0dfa9c696357",
102+
GroupSnapshotID: "865da7f8-6440-407c-9fb4-7db01ec40876",
103+
UserID: "075da7f8-6440-407c-9fb4-7db01ec49531",
104+
ConsumesQuota: false,
105+
},
106+
}
107+
th.CheckDeepEquals(t, expected, actual)
108+
109+
return true, nil
110+
})
111+
th.AssertNoErr(t, err)
112+
113+
if count != 1 {
114+
t.Errorf("Expected 1 page, got %d", count)
115+
}
116+
}
117+
62118
func TestGet(t *testing.T) {
63119
th.SetupHTTP()
64120
defer th.TeardownHTTP()

openstack/blockstorage/v3/snapshots/urls.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ func listURL(c *gophercloud.ServiceClient) string {
1818
return createURL(c)
1919
}
2020

21+
func listDetailsURL(c *gophercloud.ServiceClient) string {
22+
return c.ServiceURL("snapshots", "detail")
23+
}
24+
2125
func updateURL(c *gophercloud.ServiceClient, id string) string {
2226
return deleteURL(c, id)
2327
}

0 commit comments

Comments
 (0)