blob: 2e6f7cff6dc8326a4c08d5cab90964f6eb961843 [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
songwenping99d6e002021-01-05 03:07:46 +000016from urllib import parse as urllib
17
Matthew Treinish9e26ca82016-02-23 11:43:20 -050018from oslo_serialization import jsonutils as json
Matthew Treinish9e26ca82016-02-23 11:43:20 -050019
20from tempest.lib.api_schema.response.compute.v2_1 import snapshots as schema
21from tempest.lib.common import rest_client
22from tempest.lib import exceptions as lib_exc
Ghanshyamee9af302016-02-25 06:12:43 +090023from tempest.lib.services.compute import base_compute_client
Matthew Treinish9e26ca82016-02-23 11:43:20 -050024
25
Ghanshyamee9af302016-02-25 06:12:43 +090026class SnapshotsClient(base_compute_client.BaseComputeClient):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050027
28 def create_snapshot(self, volume_id, **kwargs):
29 """Create a snapshot.
30
Dong Mad12c2332016-10-19 01:36:27 -070031 For a full list of available parameters, please refer to the official
32 API reference:
Andreas Jaegerbf30ae72019-07-22 19:22:57 +020033 https://docs.openstack.org/api-ref/compute/#create-snapshot
Matthew Treinish9e26ca82016-02-23 11:43:20 -050034 """
35 post_body = {
36 'volume_id': volume_id
37 }
38 post_body.update(kwargs)
39 post_body = json.dumps({'snapshot': post_body})
40 resp, body = self.post('os-snapshots', post_body)
41 body = json.loads(body)
42 self.validate_response(schema.create_get_snapshot, resp, body)
43 return rest_client.ResponseBody(resp, body)
44
45 def show_snapshot(self, snapshot_id):
46 url = "os-snapshots/%s" % snapshot_id
47 resp, body = self.get(url)
48 body = json.loads(body)
49 self.validate_response(schema.create_get_snapshot, resp, body)
50 return rest_client.ResponseBody(resp, body)
51
52 def list_snapshots(self, detail=False, params=None):
zhufl4ffa4c12017-03-08 15:22:40 +080053 """List snapshots.
54
55 For a full list of available parameters, please refer to the official
56 API reference:
Andreas Jaegerbf30ae72019-07-22 19:22:57 +020057 https://docs.openstack.org/api-ref/compute/#list-snapshots
zhufl4ffa4c12017-03-08 15:22:40 +080058 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050059 url = 'os-snapshots'
60
61 if detail:
62 url += '/detail'
63 if params:
64 url += '?%s' % urllib.urlencode(params)
65 resp, body = self.get(url)
66 body = json.loads(body)
67 self.validate_response(schema.list_snapshots, resp, body)
68 return rest_client.ResponseBody(resp, body)
69
70 def delete_snapshot(self, snapshot_id):
71 resp, body = self.delete("os-snapshots/%s" % snapshot_id)
72 self.validate_response(schema.delete_snapshot, resp, body)
73 return rest_client.ResponseBody(resp, body)
74
75 def is_resource_deleted(self, id):
76 try:
77 self.show_snapshot(id)
78 except lib_exc.NotFound:
79 return True
80 return False
81
82 @property
83 def resource_type(self):
84 """Return the primary type of resource this client works with."""
85 return 'snapshot'