blob: 4f066a638ed85e24f019cc111f9945174ebf7063 [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
50 resp, body = self.get(url, self.headers)
51 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
64 resp, body = self.get(url, self.headers)
65 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)
74 resp, body = self.get(url, self.headers)
75 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)
89 resp, body = self.post('snapshots', str(Document(snapshot)),
90 self.headers)
91 body = xml_to_json(etree.fromstring(body))
92 return resp, body
93
QingXin Mengdc95f5e2013-09-16 19:06:44 -070094 def update_snapshot(self, snapshot_id, **kwargs):
95 """Updates a snapshot."""
96 put_body = Element("snapshot", xmlns=XMLNS_11, **kwargs)
97
98 resp, body = self.put('snapshots/%s' % snapshot_id,
99 str(Document(put_body)),
100 self.headers)
101 body = xml_to_json(etree.fromstring(body))
102 return resp, body
103
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200104 # NOTE(afazekas): just for the wait function
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100105 def _get_snapshot_status(self, snapshot_id):
106 resp, body = self.get_snapshot(snapshot_id)
107 status = body['status']
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200108 # NOTE(afazekas): snapshot can reach an "error"
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100109 # state in a "normal" lifecycle
110 if (status == 'error'):
111 raise exceptions.SnapshotBuildErrorException(
Sean Dague14c68182013-04-14 15:34:30 -0400112 snapshot_id=snapshot_id)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100113
114 return status
115
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200116 # NOTE(afazkas): Wait reinvented again. It is not in the correct layer
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100117 def wait_for_snapshot_status(self, snapshot_id, status):
118 """Waits for a Snapshot to reach a given status."""
119 start_time = time.time()
120 old_value = value = self._get_snapshot_status(snapshot_id)
121 while True:
122 dtime = time.time() - start_time
123 time.sleep(self.build_interval)
124 if value != old_value:
125 LOG.info('Value transition from "%s" to "%s"'
126 'in %d second(s).', old_value,
127 value, dtime)
128 if (value == status):
129 return value
130
131 if dtime > self.build_timeout:
132 message = ('Time Limit Exceeded! (%ds)'
133 'while waiting for %s, '
134 'but we got %s.' %
135 (self.build_timeout, status, value))
136 raise exceptions.TimeoutException(message)
137 time.sleep(self.build_interval)
138 old_value = value
139 value = self._get_snapshot_status(snapshot_id)
140
141 def delete_snapshot(self, snapshot_id):
142 """Delete Snapshot."""
143 return self.delete("snapshots/%s" % str(snapshot_id))
144
145 def is_resource_deleted(self, id):
146 try:
147 self.get_snapshot(id)
148 except exceptions.NotFound:
149 return True
150 return False
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800151
152 def reset_snapshot_status(self, snapshot_id, status):
153 """Reset the specified snapshot's status."""
154 post_body = Element("os-reset_status",
155 status=status
156 )
157 url = 'snapshots/%s/action' % str(snapshot_id)
158 resp, body = self.post(url, str(Document(post_body)), self.headers)
159 if body:
160 body = xml_to_json(etree.fromstring(body))
161 return resp, body
162
163 def update_snapshot_status(self, snapshot_id, status, progress):
164 """Update the specified snapshot's status."""
165 post_body = Element("os-update_snapshot_status",
166 status=status,
167 progress=progress
168 )
169 url = 'snapshots/%s/action' % str(snapshot_id)
170 resp, body = self.post(url, str(Document(post_body)), self.headers)
171 if body:
172 body = xml_to_json(etree.fromstring(body))
173 return resp, body
huangtianhua1346d702013-12-09 18:42:35 +0800174
175 def _metadata_body(self, meta):
176 post_body = Element('metadata')
177 for k, v in meta.items():
178 data = Element('meta', key=k)
179 data.append(Text(v))
180 post_body.append(data)
181 return post_body
182
183 def _parse_key_value(self, node):
184 """Parse <foo key='key'>value</foo> data into {'key': 'value'}."""
185 data = {}
186 for node in node.getchildren():
187 data[node.get('key')] = node.text
188 return data
189
190 def create_snapshot_metadata(self, snapshot_id, metadata):
191 """Create metadata for the snapshot."""
192 post_body = self._metadata_body(metadata)
193 resp, body = self.post('snapshots/%s/metadata' % snapshot_id,
194 str(Document(post_body)),
195 self.headers)
196 body = self._parse_key_value(etree.fromstring(body))
197 return resp, body
198
199 def get_snapshot_metadata(self, snapshot_id):
200 """Get metadata of the snapshot."""
201 url = "snapshots/%s/metadata" % str(snapshot_id)
202 resp, body = self.get(url, self.headers)
203 body = self._parse_key_value(etree.fromstring(body))
204 return resp, body
205
206 def update_snapshot_metadata(self, snapshot_id, metadata):
207 """Update metadata for the snapshot."""
208 put_body = self._metadata_body(metadata)
209 url = "snapshots/%s/metadata" % str(snapshot_id)
210 resp, body = self.put(url, str(Document(put_body)), self.headers)
211 body = self._parse_key_value(etree.fromstring(body))
212 return resp, body
213
214 def update_snapshot_metadata_item(self, snapshot_id, id, meta_item):
215 """Update metadata item for the snapshot."""
216 for k, v in meta_item.items():
217 put_body = Element('meta', key=k)
218 put_body.append(Text(v))
219 url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
220 resp, body = self.put(url, str(Document(put_body)), self.headers)
221 body = xml_to_json(etree.fromstring(body))
222 return resp, body
223
224 def delete_snapshot_metadata_item(self, snapshot_id, id):
225 """Delete metadata item for the snapshot."""
226 url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
227 return self.delete(url)
wanghaofa3908c2014-01-15 19:34:03 +0800228
229 def force_delete_snapshot(self, snapshot_id):
230 """Force Delete Snapshot."""
231 post_body = Element("os-force_delete")
232 url = 'snapshots/%s/action' % str(snapshot_id)
233 resp, body = self.post(url, str(Document(post_body)), self.headers)
234 if body:
235 body = xml_to_json(etree.fromstring(body))
236 return resp, body