blob: cfa02bdd145f03ba1bbaa3167bc8cacda02b05b4 [file] [log] [blame]
Attila Fazekas36b1fcf2013-01-31 16:41:04 +01001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13import json
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010014import time
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010015
Doug Hellmann583ce2c2015-03-11 14:55:46 +000016from oslo_log import log as logging
Matthew Treinish89128142015-04-23 10:44:30 -040017from six.moves.urllib import parse as urllib
Masayuki Igawabfa07602015-01-20 18:47:17 +090018from tempest_lib import exceptions as lib_exc
19
Joseph Lanoux6809bab2014-12-18 14:57:18 +000020from tempest.common import service_client
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010021from tempest import exceptions
22
Matthew Treinish684d8992014-01-30 16:27:40 +000023
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010024LOG = logging.getLogger(__name__)
25
26
Ken'ichi Ohmichif85e9bd2015-01-27 12:51:47 +000027class BaseSnapshotsClientJSON(service_client.ServiceClient):
Zhi Kun Liu38641c62014-07-10 20:12:48 +080028 """Base Client class to send CRUD Volume API requests."""
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010029
Ken'ichi Ohmichia39d0be2014-12-17 08:46:11 +000030 create_resp = 200
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010031
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000032 def list_snapshots(self, detail=False, params=None):
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010033 """List all the snapshot."""
34 url = 'snapshots'
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000035 if detail:
36 url += '/detail'
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010037 if params:
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000038 url += '?%s' % urllib.urlencode(params)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010039
40 resp, body = self.get(url)
41 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000042 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000043 return service_client.ResponseBodyList(resp, body['snapshots'])
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010044
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000045 def show_snapshot(self, snapshot_id):
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010046 """Returns the details of a single snapshot."""
47 url = "snapshots/%s" % str(snapshot_id)
48 resp, body = self.get(url)
49 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000050 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000051 return service_client.ResponseBody(resp, body['snapshot'])
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010052
53 def create_snapshot(self, volume_id, **kwargs):
54 """
55 Creates a new snapshot.
56 volume_id(Required): id of the volume.
57 force: Create a snapshot even if the volume attached (Default=False)
58 display_name: Optional snapshot Name.
59 display_description: User friendly snapshot description.
60 """
61 post_body = {'volume_id': volume_id}
62 post_body.update(kwargs)
63 post_body = json.dumps({'snapshot': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020064 resp, body = self.post('snapshots', post_body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010065 body = json.loads(body)
Zhi Kun Liu38641c62014-07-10 20:12:48 +080066 self.expected_success(self.create_resp, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000067 return service_client.ResponseBody(resp, body['snapshot'])
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010068
QingXin Mengdc95f5e2013-09-16 19:06:44 -070069 def update_snapshot(self, snapshot_id, **kwargs):
70 """Updates a snapshot."""
71 put_body = json.dumps({'snapshot': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020072 resp, body = self.put('snapshots/%s' % snapshot_id, put_body)
QingXin Mengdc95f5e2013-09-16 19:06:44 -070073 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000074 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +000075 return service_client.ResponseBody(resp, body['snapshot'])
QingXin Mengdc95f5e2013-09-16 19:06:44 -070076
Attila Fazekasa8b5fe72013-08-01 16:59:06 +020077 # NOTE(afazekas): just for the wait function
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010078 def _get_snapshot_status(self, snapshot_id):
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000079 body = self.show_snapshot(snapshot_id)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010080 status = body['status']
Attila Fazekasa8b5fe72013-08-01 16:59:06 +020081 # NOTE(afazekas): snapshot can reach an "error"
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010082 # state in a "normal" lifecycle
83 if (status == 'error'):
84 raise exceptions.SnapshotBuildErrorException(
Sean Dague14c68182013-04-14 15:34:30 -040085 snapshot_id=snapshot_id)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010086
87 return status
88
Attila Fazekasa8b5fe72013-08-01 16:59:06 +020089 # NOTE(afazkas): Wait reinvented again. It is not in the correct layer
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010090 def wait_for_snapshot_status(self, snapshot_id, status):
91 """Waits for a Snapshot to reach a given status."""
92 start_time = time.time()
93 old_value = value = self._get_snapshot_status(snapshot_id)
94 while True:
95 dtime = time.time() - start_time
96 time.sleep(self.build_interval)
97 if value != old_value:
98 LOG.info('Value transition from "%s" to "%s"'
99 'in %d second(s).', old_value,
100 value, dtime)
101 if (value == status):
102 return value
103
104 if dtime > self.build_timeout:
105 message = ('Time Limit Exceeded! (%ds)'
106 'while waiting for %s, '
107 'but we got %s.' %
108 (self.build_timeout, status, value))
109 raise exceptions.TimeoutException(message)
110 time.sleep(self.build_interval)
111 old_value = value
112 value = self._get_snapshot_status(snapshot_id)
113
114 def delete_snapshot(self, snapshot_id):
115 """Delete Snapshot."""
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000116 resp, body = self.delete("snapshots/%s" % str(snapshot_id))
117 self.expected_success(202, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000118 return service_client.ResponseBody(resp, body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100119
120 def is_resource_deleted(self, id):
121 try:
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +0000122 self.show_snapshot(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900123 except lib_exc.NotFound:
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100124 return True
125 return False
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800126
Matt Riedemannd2b96512014-10-13 10:18:16 -0700127 @property
128 def resource_type(self):
129 """Returns the primary type of resource this client works with."""
130 return 'volume-snapshot'
131
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800132 def reset_snapshot_status(self, snapshot_id, status):
133 """Reset the specified snapshot's status."""
134 post_body = json.dumps({'os-reset_status': {"status": status}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200135 resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000136 self.expected_success(202, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000137 return service_client.ResponseBody(resp, body)
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800138
139 def update_snapshot_status(self, snapshot_id, status, progress):
140 """Update the specified snapshot's status."""
141 post_body = {
142 'status': status,
143 'progress': progress
144 }
145 post_body = json.dumps({'os-update_snapshot_status': post_body})
146 url = 'snapshots/%s/action' % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200147 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000148 self.expected_success(202, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000149 return service_client.ResponseBody(resp, body)
huangtianhua1346d702013-12-09 18:42:35 +0800150
151 def create_snapshot_metadata(self, snapshot_id, metadata):
152 """Create metadata for the snapshot."""
153 put_body = json.dumps({'metadata': metadata})
154 url = "snapshots/%s/metadata" % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200155 resp, body = self.post(url, put_body)
huangtianhua1346d702013-12-09 18:42:35 +0800156 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000157 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000158 return service_client.ResponseBody(resp, body['metadata'])
huangtianhua1346d702013-12-09 18:42:35 +0800159
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +0000160 def show_snapshot_metadata(self, snapshot_id):
huangtianhua1346d702013-12-09 18:42:35 +0800161 """Get metadata of the snapshot."""
162 url = "snapshots/%s/metadata" % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200163 resp, body = self.get(url)
huangtianhua1346d702013-12-09 18:42:35 +0800164 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000165 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000166 return service_client.ResponseBody(resp, body['metadata'])
huangtianhua1346d702013-12-09 18:42:35 +0800167
168 def update_snapshot_metadata(self, snapshot_id, metadata):
169 """Update metadata for the snapshot."""
170 put_body = json.dumps({'metadata': metadata})
171 url = "snapshots/%s/metadata" % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200172 resp, body = self.put(url, put_body)
huangtianhua1346d702013-12-09 18:42:35 +0800173 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000174 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000175 return service_client.ResponseBody(resp, body['metadata'])
huangtianhua1346d702013-12-09 18:42:35 +0800176
177 def update_snapshot_metadata_item(self, snapshot_id, id, meta_item):
178 """Update metadata item for the snapshot."""
179 put_body = json.dumps({'meta': meta_item})
180 url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200181 resp, body = self.put(url, put_body)
huangtianhua1346d702013-12-09 18:42:35 +0800182 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000183 self.expected_success(200, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000184 return service_client.ResponseBody(resp, body['meta'])
huangtianhua1346d702013-12-09 18:42:35 +0800185
186 def delete_snapshot_metadata_item(self, snapshot_id, id):
187 """Delete metadata item for the snapshot."""
188 url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200189 resp, body = self.delete(url)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000190 self.expected_success(200, resp.status)
Joseph Lanoux2cdd5502015-01-16 14:46:51 +0000191 return service_client.ResponseBody(resp, body)
wanghaofa3908c2014-01-15 19:34:03 +0800192
193 def force_delete_snapshot(self, snapshot_id):
194 """Force Delete Snapshot."""
195 post_body = json.dumps({'os-force_delete': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200196 resp, body = self.post('snapshots/%s/action' % snapshot_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000197 self.expected_success(202, resp.status)
Joseph Lanoux6809bab2014-12-18 14:57:18 +0000198 return service_client.ResponseBody(resp, body)
Zhi Kun Liu38641c62014-07-10 20:12:48 +0800199
200
201class SnapshotsClientJSON(BaseSnapshotsClientJSON):
202 """Client class to send CRUD Volume V1 API requests."""