blob: be41957ef933a1d928d09086947ac0fd6c3a730e [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001# Copyright 2015 Fujitsu(fnst) Corporation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16from oslo_serialization import jsonutils as json
17from six.moves.urllib import parse as urllib
18
19from tempest.lib.api_schema.response.compute.v2_1 import snapshots as schema
20from tempest.lib.common import rest_client
21from tempest.lib import exceptions as lib_exc
Ghanshyamee9af302016-02-25 06:12:43 +090022from tempest.lib.services.compute import base_compute_client
Matthew Treinish9e26ca82016-02-23 11:43:20 -050023
24
Ghanshyamee9af302016-02-25 06:12:43 +090025class SnapshotsClient(base_compute_client.BaseComputeClient):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050026
27 def create_snapshot(self, volume_id, **kwargs):
28 """Create a snapshot.
29
30 Available params: see http://developer.openstack.org/
31 api-ref-compute-v2.1.html#createSnapshot
32 """
33 post_body = {
34 'volume_id': volume_id
35 }
36 post_body.update(kwargs)
37 post_body = json.dumps({'snapshot': post_body})
38 resp, body = self.post('os-snapshots', post_body)
39 body = json.loads(body)
40 self.validate_response(schema.create_get_snapshot, resp, body)
41 return rest_client.ResponseBody(resp, body)
42
43 def show_snapshot(self, snapshot_id):
44 url = "os-snapshots/%s" % snapshot_id
45 resp, body = self.get(url)
46 body = json.loads(body)
47 self.validate_response(schema.create_get_snapshot, resp, body)
48 return rest_client.ResponseBody(resp, body)
49
50 def list_snapshots(self, detail=False, params=None):
51 url = 'os-snapshots'
52
53 if detail:
54 url += '/detail'
55 if params:
56 url += '?%s' % urllib.urlencode(params)
57 resp, body = self.get(url)
58 body = json.loads(body)
59 self.validate_response(schema.list_snapshots, resp, body)
60 return rest_client.ResponseBody(resp, body)
61
62 def delete_snapshot(self, snapshot_id):
63 resp, body = self.delete("os-snapshots/%s" % snapshot_id)
64 self.validate_response(schema.delete_snapshot, resp, body)
65 return rest_client.ResponseBody(resp, body)
66
67 def is_resource_deleted(self, id):
68 try:
69 self.show_snapshot(id)
70 except lib_exc.NotFound:
71 return True
72 return False
73
74 @property
75 def resource_type(self):
76 """Return the primary type of resource this client works with."""
77 return 'snapshot'