Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 1 | # 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 | |
songwenping | 99d6e00 | 2021-01-05 03:07:46 +0000 | [diff] [blame] | 16 | from urllib import parse as urllib |
| 17 | |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 18 | from oslo_serialization import jsonutils as json |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 19 | |
| 20 | from tempest.lib.api_schema.response.compute.v2_1 import snapshots as schema |
| 21 | from tempest.lib.common import rest_client |
| 22 | from tempest.lib import exceptions as lib_exc |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 23 | from tempest.lib.services.compute import base_compute_client |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 24 | |
| 25 | |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 26 | class SnapshotsClient(base_compute_client.BaseComputeClient): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 27 | |
| 28 | def create_snapshot(self, volume_id, **kwargs): |
| 29 | """Create a snapshot. |
| 30 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 31 | For a full list of available parameters, please refer to the official |
| 32 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 33 | https://docs.openstack.org/api-ref/compute/#create-snapshot |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 34 | """ |
| 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): |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 53 | """List snapshots. |
| 54 | |
| 55 | For a full list of available parameters, please refer to the official |
| 56 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 57 | https://docs.openstack.org/api-ref/compute/#list-snapshots |
zhufl | 4ffa4c1 | 2017-03-08 15:22:40 +0800 | [diff] [blame] | 58 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 59 | 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' |