blob: c89f66e5e1f30bd13b9198739c5a623ad4419a7c [file] [log] [blame]
Attila Fazekas36b1fcf2013-01-31 16:41:04 +01001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import logging
16import time
17import urllib
18
19from lxml import etree
20
21from tempest.common.rest_client import RestClientXML
22from tempest import exceptions
23from tempest.services.compute.xml.common import Document
24from tempest.services.compute.xml.common import Element
25from tempest.services.compute.xml.common import xml_to_json
26from tempest.services.compute.xml.common import XMLNS_11
27
28LOG = logging.getLogger(__name__)
29
30
31class SnapshotsClientXML(RestClientXML):
32 """Client class to send CRUD Volume API requests."""
33
34 def __init__(self, config, username, password, auth_url, tenant_name=None):
35 super(SnapshotsClientXML, self).__init__(config, username, password,
36 auth_url, tenant_name)
37
38 self.service = self.config.volume.catalog_type
39 self.build_interval = self.config.volume.build_interval
40 self.build_timeout = self.config.volume.build_timeout
41
42 def list_snapshots(self, params=None):
43 """List all snapshot."""
44 url = 'snapshots'
45
46 if params:
47 url += '?%s' % urllib.urlencode(params)
48
49 resp, body = self.get(url, self.headers)
50 body = etree.fromstring(body)
51 return resp, xml_to_json(body)
52
53 def list_snapshots_with_detail(self, params=None):
54 """List all the details of snapshot."""
55 url = 'snapshots/detail'
56
57 if params:
58 url += '?%s' % urllib.urlencode(params)
59
60 resp, body = self.get(url, self.headers)
61 body = etree.fromstring(body)
62 snapshots = []
63 return resp, snapshots(xml_to_json(body))
64
65 def get_snapshot(self, snapshot_id):
66 """Returns the details of a single snapshot."""
67 url = "snapshots/%s" % str(snapshot_id)
68 resp, body = self.get(url, self.headers)
69 body = etree.fromstring(body)
70 return resp, xml_to_json(body)
71
72 def create_snapshot(self, volume_id, **kwargs):
73 """ Creates a new snapshot.
74 volume_id(Required): id of the volume.
75 force: Create a snapshot even if the volume attached (Default=False)
76 display_name: Optional snapshot Name.
77 display_description: User friendly snapshot description.
78 """
79 #NOTE(afazekas): it should use the volume namaspace
80 snapshot = Element("snapshot", xmlns=XMLNS_11, volume_id=volume_id)
81 for key, value in kwargs.items():
82 snapshot.add_attr(key, value)
83 resp, body = self.post('snapshots', str(Document(snapshot)),
84 self.headers)
85 body = xml_to_json(etree.fromstring(body))
86 return resp, body
87
88 def _get_snapshot_status(self, snapshot_id):
89 resp, body = self.get_snapshot(snapshot_id)
90 return body['status']
91
92 #NOTE(afazekas): just for the wait function
93 def _get_snapshot_status(self, snapshot_id):
94 resp, body = self.get_snapshot(snapshot_id)
95 status = body['status']
96 #NOTE(afazekas): snapshot can reach an "error"
97 # state in a "normal" lifecycle
98 if (status == 'error'):
99 raise exceptions.SnapshotBuildErrorException(
100 snapshot_id=snapshot_id)
101
102 return status
103
104 #NOTE(afazkas): Wait reinvented again. It is not in the correct layer
105 def wait_for_snapshot_status(self, snapshot_id, status):
106 """Waits for a Snapshot to reach a given status."""
107 start_time = time.time()
108 old_value = value = self._get_snapshot_status(snapshot_id)
109 while True:
110 dtime = time.time() - start_time
111 time.sleep(self.build_interval)
112 if value != old_value:
113 LOG.info('Value transition from "%s" to "%s"'
114 'in %d second(s).', old_value,
115 value, dtime)
116 if (value == status):
117 return value
118
119 if dtime > self.build_timeout:
120 message = ('Time Limit Exceeded! (%ds)'
121 'while waiting for %s, '
122 'but we got %s.' %
123 (self.build_timeout, status, value))
124 raise exceptions.TimeoutException(message)
125 time.sleep(self.build_interval)
126 old_value = value
127 value = self._get_snapshot_status(snapshot_id)
128
129 def delete_snapshot(self, snapshot_id):
130 """Delete Snapshot."""
131 return self.delete("snapshots/%s" % str(snapshot_id))
132
133 def is_resource_deleted(self, id):
134 try:
135 self.get_snapshot(id)
136 except exceptions.NotFound:
137 return True
138 return False