blob: df8d6fbd327b8d9004814d0038af156824d989dd [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
Dong Mad12c2332016-10-19 01:36:27 -070030 For a full list of available parameters, please refer to the official
31 API reference:
zhufl4ffa4c12017-03-08 15:22:40 +080032 https://developer.openstack.org/api-ref/compute/#create-snapshot
Matthew Treinish9e26ca82016-02-23 11:43:20 -050033 """
34 post_body = {
35 'volume_id': volume_id
36 }
37 post_body.update(kwargs)
38 post_body = json.dumps({'snapshot': post_body})
39 resp, body = self.post('os-snapshots', post_body)
40 body = json.loads(body)
41 self.validate_response(schema.create_get_snapshot, resp, body)
42 return rest_client.ResponseBody(resp, body)
43
44 def show_snapshot(self, snapshot_id):
45 url = "os-snapshots/%s" % snapshot_id
46 resp, body = self.get(url)
47 body = json.loads(body)
48 self.validate_response(schema.create_get_snapshot, resp, body)
49 return rest_client.ResponseBody(resp, body)
50
51 def list_snapshots(self, detail=False, params=None):
zhufl4ffa4c12017-03-08 15:22:40 +080052 """List snapshots.
53
54 For a full list of available parameters, please refer to the official
55 API reference:
56 https://developer.openstack.org/api-ref/compute/#list-snapshots
57 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050058 url = 'os-snapshots'
59
60 if detail:
61 url += '/detail'
62 if params:
63 url += '?%s' % urllib.urlencode(params)
64 resp, body = self.get(url)
65 body = json.loads(body)
66 self.validate_response(schema.list_snapshots, resp, body)
67 return rest_client.ResponseBody(resp, body)
68
69 def delete_snapshot(self, snapshot_id):
70 resp, body = self.delete("os-snapshots/%s" % snapshot_id)
71 self.validate_response(schema.delete_snapshot, resp, body)
72 return rest_client.ResponseBody(resp, body)
73
74 def is_resource_deleted(self, id):
75 try:
76 self.show_snapshot(id)
77 except lib_exc.NotFound:
78 return True
79 return False
80
81 @property
82 def resource_type(self):
83 """Return the primary type of resource this client works with."""
84 return 'snapshot'