blob: 9ad86d2b125a15831348109ada76bfdeee168376 [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
vponomaryov960eeb42014-02-22 18:25:25 +020018from tempest.common import rest_client
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
Yuiko Takada4d41c2f2014-03-07 11:58:31 +000022from tempest.services.compute.xml import common
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010023
Matthew Treinish684d8992014-01-30 16:27:40 +000024CONF = config.CONF
25
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010026LOG = logging.getLogger(__name__)
27
28
vponomaryov960eeb42014-02-22 18:25:25 +020029class SnapshotsClientXML(rest_client.RestClient):
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010030 """Client class to send CRUD Volume API requests."""
vponomaryov960eeb42014-02-22 18:25:25 +020031 TYPE = "xml"
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010032
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000033 def __init__(self, auth_provider):
34 super(SnapshotsClientXML, self).__init__(auth_provider)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010035
Matthew Treinish684d8992014-01-30 16:27:40 +000036 self.service = CONF.volume.catalog_type
37 self.build_interval = CONF.volume.build_interval
38 self.build_timeout = CONF.volume.build_timeout
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010039
40 def list_snapshots(self, params=None):
41 """List all snapshot."""
42 url = 'snapshots'
43
44 if params:
45 url += '?%s' % urllib.urlencode(params)
46
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020047 resp, body = self.get(url)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010048 body = etree.fromstring(body)
Giulio Fidentee92956b2013-06-06 18:38:48 +020049 snapshots = []
50 for snap in body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +000051 snapshots.append(common.xml_to_json(snap))
Giulio Fidentee92956b2013-06-06 18:38:48 +020052 return resp, snapshots
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010053
54 def list_snapshots_with_detail(self, params=None):
55 """List all the details of snapshot."""
56 url = 'snapshots/detail'
57
58 if params:
59 url += '?%s' % urllib.urlencode(params)
60
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020061 resp, body = self.get(url)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010062 body = etree.fromstring(body)
63 snapshots = []
Giulio Fidentee92956b2013-06-06 18:38:48 +020064 for snap in body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +000065 snapshots.append(common.xml_to_json(snap))
Giulio Fidentee92956b2013-06-06 18:38:48 +020066 return resp, snapshots
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010067
68 def get_snapshot(self, snapshot_id):
69 """Returns the details of a single snapshot."""
70 url = "snapshots/%s" % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020071 resp, body = self.get(url)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010072 body = etree.fromstring(body)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +000073 return resp, common.xml_to_json(body)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010074
75 def create_snapshot(self, volume_id, **kwargs):
Attila Fazekasb2902af2013-02-16 16:22:44 +010076 """Creates a new snapshot.
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010077 volume_id(Required): id of the volume.
78 force: Create a snapshot even if the volume attached (Default=False)
79 display_name: Optional snapshot Name.
80 display_description: User friendly snapshot description.
81 """
Chang Bo Guocc1623c2013-09-13 20:11:27 -070082 # NOTE(afazekas): it should use the volume namespace
Yuiko Takada4d41c2f2014-03-07 11:58:31 +000083 snapshot = common.Element("snapshot", xmlns=common.XMLNS_11,
84 volume_id=volume_id)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010085 for key, value in kwargs.items():
86 snapshot.add_attr(key, value)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +000087 resp, body = self.post('snapshots',
88 str(common.Document(snapshot)))
89 body = common.xml_to_json(etree.fromstring(body))
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010090 return resp, body
91
QingXin Mengdc95f5e2013-09-16 19:06:44 -070092 def update_snapshot(self, snapshot_id, **kwargs):
93 """Updates a snapshot."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +000094 put_body = common.Element("snapshot", xmlns=common.XMLNS_11, **kwargs)
QingXin Mengdc95f5e2013-09-16 19:06:44 -070095
96 resp, body = self.put('snapshots/%s' % snapshot_id,
Yuiko Takada4d41c2f2014-03-07 11:58:31 +000097 str(common.Document(put_body)))
98 body = common.xml_to_json(etree.fromstring(body))
QingXin Mengdc95f5e2013-09-16 19:06:44 -070099 return resp, body
100
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200101 # NOTE(afazekas): just for the wait function
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100102 def _get_snapshot_status(self, snapshot_id):
103 resp, body = self.get_snapshot(snapshot_id)
104 status = body['status']
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200105 # NOTE(afazekas): snapshot can reach an "error"
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100106 # state in a "normal" lifecycle
107 if (status == 'error'):
108 raise exceptions.SnapshotBuildErrorException(
Sean Dague14c68182013-04-14 15:34:30 -0400109 snapshot_id=snapshot_id)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100110
111 return status
112
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200113 # NOTE(afazkas): Wait reinvented again. It is not in the correct layer
Attila Fazekas36b1fcf2013-01-31 16:41:04 +0100114 def wait_for_snapshot_status(self, snapshot_id, status):
115 """Waits for a Snapshot to reach a given status."""
116 start_time = time.time()
117 old_value = value = self._get_snapshot_status(snapshot_id)
118 while True:
119 dtime = time.time() - start_time
120 time.sleep(self.build_interval)
121 if value != old_value:
122 LOG.info('Value transition from "%s" to "%s"'
123 'in %d second(s).', old_value,
124 value, dtime)
125 if (value == status):
126 return value
127
128 if dtime > self.build_timeout:
129 message = ('Time Limit Exceeded! (%ds)'
130 'while waiting for %s, '
131 'but we got %s.' %
132 (self.build_timeout, status, value))
133 raise exceptions.TimeoutException(message)
134 time.sleep(self.build_interval)
135 old_value = value
136 value = self._get_snapshot_status(snapshot_id)
137
138 def delete_snapshot(self, snapshot_id):
139 """Delete Snapshot."""
140 return self.delete("snapshots/%s" % str(snapshot_id))
141
142 def is_resource_deleted(self, id):
143 try:
144 self.get_snapshot(id)
145 except exceptions.NotFound:
146 return True
147 return False
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800148
149 def reset_snapshot_status(self, snapshot_id, status):
150 """Reset the specified snapshot's status."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000151 post_body = common.Element("os-reset_status", status=status)
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800152 url = 'snapshots/%s/action' % str(snapshot_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000153 resp, body = self.post(url, str(common.Document(post_body)))
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800154 if body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000155 body = common.xml_to_json(etree.fromstring(body))
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800156 return resp, body
157
158 def update_snapshot_status(self, snapshot_id, status, progress):
159 """Update the specified snapshot's status."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000160 post_body = common.Element("os-update_snapshot_status",
161 status=status,
162 progress=progress
163 )
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800164 url = 'snapshots/%s/action' % str(snapshot_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000165 resp, body = self.post(url, str(common.Document(post_body)))
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800166 if body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000167 body = common.xml_to_json(etree.fromstring(body))
zhangyanzid4d3c6d2013-11-06 09:27:13 +0800168 return resp, body
huangtianhua1346d702013-12-09 18:42:35 +0800169
170 def _metadata_body(self, meta):
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000171 post_body = common.Element('metadata')
huangtianhua1346d702013-12-09 18:42:35 +0800172 for k, v in meta.items():
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000173 data = common.Element('meta', key=k)
174 data.append(common.Text(v))
huangtianhua1346d702013-12-09 18:42:35 +0800175 post_body.append(data)
176 return post_body
177
178 def _parse_key_value(self, node):
179 """Parse <foo key='key'>value</foo> data into {'key': 'value'}."""
180 data = {}
181 for node in node.getchildren():
182 data[node.get('key')] = node.text
183 return data
184
185 def create_snapshot_metadata(self, snapshot_id, metadata):
186 """Create metadata for the snapshot."""
187 post_body = self._metadata_body(metadata)
188 resp, body = self.post('snapshots/%s/metadata' % snapshot_id,
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000189 str(common.Document(post_body)))
huangtianhua1346d702013-12-09 18:42:35 +0800190 body = self._parse_key_value(etree.fromstring(body))
191 return resp, body
192
193 def get_snapshot_metadata(self, snapshot_id):
194 """Get metadata of the snapshot."""
195 url = "snapshots/%s/metadata" % str(snapshot_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200196 resp, body = self.get(url)
huangtianhua1346d702013-12-09 18:42:35 +0800197 body = self._parse_key_value(etree.fromstring(body))
198 return resp, body
199
200 def update_snapshot_metadata(self, snapshot_id, metadata):
201 """Update metadata for the snapshot."""
202 put_body = self._metadata_body(metadata)
203 url = "snapshots/%s/metadata" % str(snapshot_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000204 resp, body = self.put(url, str(common.Document(put_body)))
huangtianhua1346d702013-12-09 18:42:35 +0800205 body = self._parse_key_value(etree.fromstring(body))
206 return resp, body
207
208 def update_snapshot_metadata_item(self, snapshot_id, id, meta_item):
209 """Update metadata item for the snapshot."""
210 for k, v in meta_item.items():
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000211 put_body = common.Element('meta', key=k)
212 put_body.append(common.Text(v))
huangtianhua1346d702013-12-09 18:42:35 +0800213 url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000214 resp, body = self.put(url, str(common.Document(put_body)))
215 body = common.xml_to_json(etree.fromstring(body))
huangtianhua1346d702013-12-09 18:42:35 +0800216 return resp, body
217
218 def delete_snapshot_metadata_item(self, snapshot_id, id):
219 """Delete metadata item for the snapshot."""
220 url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id))
221 return self.delete(url)
wanghaofa3908c2014-01-15 19:34:03 +0800222
223 def force_delete_snapshot(self, snapshot_id):
224 """Force Delete Snapshot."""
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000225 post_body = common.Element("os-force_delete")
wanghaofa3908c2014-01-15 19:34:03 +0800226 url = 'snapshots/%s/action' % str(snapshot_id)
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000227 resp, body = self.post(url, str(common.Document(post_body)))
wanghaofa3908c2014-01-15 19:34:03 +0800228 if body:
Yuiko Takada4d41c2f2014-03-07 11:58:31 +0000229 body = common.xml_to_json(etree.fromstring(body))
wanghaofa3908c2014-01-15 19:34:03 +0800230 return resp, body