blob: 9abe04275af2212ebb87ff8dbcd41b6a7d4ea4ea [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
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010013import time
14import urllib
15
16from lxml import etree
17
18from tempest.common.rest_client import RestClientXML
Matthew Treinish684d8992014-01-30 16:27:40 +000019from tempest import config
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010020from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040021from tempest.openstack.common import log as logging
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010022from tempest.services.compute.xml.common import Document
23from tempest.services.compute.xml.common import Element
huangtianhua1346d702013-12-09 18:42:35 +080024from tempest.services.compute.xml.common import Text
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010025from tempest.services.compute.xml.common import xml_to_json
26from tempest.services.compute.xml.common import XMLNS_11
27
Matthew Treinish684d8992014-01-30 16:27:40 +000028CONF = config.CONF
29
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010030LOG = logging.getLogger(__name__)
31
32
33class SnapshotsClientXML(RestClientXML):
34 """Client class to send CRUD Volume API requests."""
35
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000036 def __init__(self, auth_provider):
37 super(SnapshotsClientXML, self).__init__(auth_provider)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010038
Matthew Treinish684d8992014-01-30 16:27:40 +000039 self.service = CONF.volume.catalog_type
40 self.build_interval = CONF.volume.build_interval
41 self.build_timeout = CONF.volume.build_timeout
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010042
43 def list_snapshots(self, params=None):
44 """List all snapshot."""
45 url = 'snapshots'
46
47 if params:
48 url += '?%s' % urllib.urlencode(params)
49
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020050 resp, body = self.get(url)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010051 body = etree.fromstring(body)
Giulio Fidentee92956b2013-06-06 18:38:48 +020052 snapshots = []
53 for snap in body:
54 snapshots.append(xml_to_json(snap))
55 return resp, snapshots
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010056
57 def list_snapshots_with_detail(self, params=None):
58 """List all the details of snapshot."""
59 url = 'snapshots/detail'
60
61 if params:
62 url += '?%s' % urllib.urlencode(params)
63
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020064 resp, body = self.get(url)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010065 body = etree.fromstring(body)
66 snapshots = []
Giulio Fidentee92956b2013-06-06 18:38:48 +020067 for snap in body:
68 snapshots.append(xml_to_json(snap))
69 return resp, snapshots
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010070
71 def get_snapshot(self, snapshot_id):
72 """Returns the details of a single snapshot."""
73 url = "snapshots/%s" % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020074 resp, body = self.get(url)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010075 body = etree.fromstring(body)
76 return resp, xml_to_json(body)
77
78 def create_snapshot(self, volume_id, **kwargs):
Attila Fazekasb2902af2013-02-16 16:22:44 +010079 """Creates a new snapshot.
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010080 volume_id(Required): id of the volume.
81 force: Create a snapshot even if the volume attached (Default=False)
82 display_name: Optional snapshot Name.
83 display_description: User friendly snapshot description.
84 """
Chang Bo Guocc1623c2013-09-13 20:11:27 -070085 # NOTE(afazekas): it should use the volume namespace
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010086 snapshot = Element("snapshot", xmlns=XMLNS_11, volume_id=volume_id)
87 for key, value in kwargs.items():
88 snapshot.add_attr(key, value)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020089 resp, body = self.post('snapshots', str(Document(snapshot)))
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010090 body = xml_to_json(etree.fromstring(body))
91 return resp, body
92
QingXin Mengdc95f5e2013-09-16 19:06:44 -070093 def update_snapshot(self, snapshot_id, **kwargs):
94 """Updates a snapshot."""
95 put_body = Element("snapshot", xmlns=XMLNS_11, **kwargs)
96
97 resp, body = self.put('snapshots/%s' % snapshot_id,
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020098 str(Document(put_body)))
QingXin Mengdc95f5e2013-09-16 19:06:44 -070099 body = xml_to_json(etree.fromstring(body))
100 return resp, body
101
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200102 # NOTE(afazekas): just for the wait function
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100103 def _get_snapshot_status(self, snapshot_id):
104 resp, body = self.get_snapshot(snapshot_id)
105 status = body['status']
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200106 # NOTE(afazekas): snapshot can reach an "error"
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100107 # state in a "normal" lifecycle
108 if (status == 'error'):
109 raise exceptions.SnapshotBuildErrorException(
Sean Dague14c68182013-04-14 15:34:30 -0400110 snapshot_id=snapshot_id)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100111
112 return status
113
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200114 # NOTE(afazkas): Wait reinvented again. It is not in the correct layer
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100115 def wait_for_snapshot_status(self, snapshot_id, status):
116 """Waits for a Snapshot to reach a given status."""
117 start_time = time.time()
118 old_value = value = self._get_snapshot_status(snapshot_id)
119 while True:
120 dtime = time.time() - start_time
121 time.sleep(self.build_interval)
122 if value != old_value:
123 LOG.info('Value transition from "%s" to "%s"'
124 'in %d second(s).', old_value,
125 value, dtime)
126 if (value == status):
127 return value
128
129 if dtime > self.build_timeout:
130 message = ('Time Limit Exceeded! (%ds)'
131 'while waiting for %s, '
132 'but we got %s.' %
133 (self.build_timeout, status, value))
134 raise exceptions.TimeoutException(message)
135 time.sleep(self.build_interval)
136 old_value = value
137 value = self._get_snapshot_status(snapshot_id)
138
139 def delete_snapshot(self, snapshot_id):
140 """Delete Snapshot."""
141 return self.delete("snapshots/%s" % str(snapshot_id))
142
143 def is_resource_deleted(self, id):
144 try:
145 self.get_snapshot(id)
146 except exceptions.NotFound:
147 return True
148 return False
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800149
150 def reset_snapshot_status(self, snapshot_id, status):
151 """Reset the specified snapshot's status."""
152 post_body = Element("os-reset_status",
153 status=status
154 )
155 url = 'snapshots/%s/action' % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200156 resp, body = self.post(url, str(Document(post_body)))
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800157 if body:
158 body = xml_to_json(etree.fromstring(body))
159 return resp, body
160
161 def update_snapshot_status(self, snapshot_id, status, progress):
162 """Update the specified snapshot's status."""
163 post_body = Element("os-update_snapshot_status",
164 status=status,
165 progress=progress
166 )
167 url = 'snapshots/%s/action' % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200168 resp, body = self.post(url, str(Document(post_body)))
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800169 if body:
170 body = xml_to_json(etree.fromstring(body))
171 return resp, body
huangtianhua1346d702013-12-09 18:42:35 +0800172
173 def _metadata_body(self, meta):
174 post_body = Element('metadata')
175 for k, v in meta.items():
176 data = Element('meta', key=k)
177 data.append(Text(v))
178 post_body.append(data)
179 return post_body
180
181 def _parse_key_value(self, node):
182 """Parse <foo key='key'>value</foo> data into {'key': 'value'}."""
183 data = {}
184 for node in node.getchildren():
185 data[node.get('key')] = node.text
186 return data
187
188 def create_snapshot_metadata(self, snapshot_id, metadata):
189 """Create metadata for the snapshot."""
190 post_body = self._metadata_body(metadata)
191 resp, body = self.post('snapshots/%s/metadata' % snapshot_id,
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200192 str(Document(post_body)))
huangtianhua1346d702013-12-09 18:42:35 +0800193 body = self._parse_key_value(etree.fromstring(body))
194 return resp, body
195
196 def get_snapshot_metadata(self, snapshot_id):
197 """Get metadata of the snapshot."""
198 url = "snapshots/%s/metadata" % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200199 resp, body = self.get(url)
huangtianhua1346d702013-12-09 18:42:35 +0800200 body = self._parse_key_value(etree.fromstring(body))
201 return resp, body
202
203 def update_snapshot_metadata(self, snapshot_id, metadata):
204 """Update metadata for the snapshot."""
205 put_body = self._metadata_body(metadata)
206 url = "snapshots/%s/metadata" % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200207 resp, body = self.put(url, str(Document(put_body)))
huangtianhua1346d702013-12-09 18:42:35 +0800208 body = self._parse_key_value(etree.fromstring(body))
209 return resp, body
210
211 def update_snapshot_metadata_item(self, snapshot_id, id, meta_item):
212 """Update metadata item for the snapshot."""
213 for k, v in meta_item.items():
214 put_body = Element('meta', key=k)
215 put_body.append(Text(v))
216 url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200217 resp, body = self.put(url, str(Document(put_body)))
huangtianhua1346d702013-12-09 18:42:35 +0800218 body = xml_to_json(etree.fromstring(body))
219 return resp, body
220
221 def delete_snapshot_metadata_item(self, snapshot_id, id):
222 """Delete metadata item for the snapshot."""
223 url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
224 return self.delete(url)
wanghaofa3908c2014-01-15 19:34:03 +0800225
226 def force_delete_snapshot(self, snapshot_id):
227 """Force Delete Snapshot."""
228 post_body = Element("os-force_delete")
229 url = 'snapshots/%s/action' % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200230 resp, body = self.post(url, str(Document(post_body)))
wanghaofa3908c2014-01-15 19:34:03 +0800231 if body:
232 body = xml_to_json(etree.fromstring(body))
233 return resp, body