blob: 2209fc7d8920b3dc3bf1b2cd5df06eb37a152040 [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):
Attila Fazekasb2902af2013-02-16 16:22:44 +010073 """Creates a new snapshot.
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010074 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
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010088 #NOTE(afazekas): just for the wait function
89 def _get_snapshot_status(self, snapshot_id):
90 resp, body = self.get_snapshot(snapshot_id)
91 status = body['status']
92 #NOTE(afazekas): snapshot can reach an "error"
93 # state in a "normal" lifecycle
94 if (status == 'error'):
95 raise exceptions.SnapshotBuildErrorException(
Sean Dague14c68182013-04-14 15:34:30 -040096 snapshot_id=snapshot_id)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010097
98 return status
99
100 #NOTE(afazkas): Wait reinvented again. It is not in the correct layer
101 def wait_for_snapshot_status(self, snapshot_id, status):
102 """Waits for a Snapshot to reach a given status."""
103 start_time = time.time()
104 old_value = value = self._get_snapshot_status(snapshot_id)
105 while True:
106 dtime = time.time() - start_time
107 time.sleep(self.build_interval)
108 if value != old_value:
109 LOG.info('Value transition from "%s" to "%s"'
110 'in %d second(s).', old_value,
111 value, dtime)
112 if (value == status):
113 return value
114
115 if dtime > self.build_timeout:
116 message = ('Time Limit Exceeded! (%ds)'
117 'while waiting for %s, '
118 'but we got %s.' %
119 (self.build_timeout, status, value))
120 raise exceptions.TimeoutException(message)
121 time.sleep(self.build_interval)
122 old_value = value
123 value = self._get_snapshot_status(snapshot_id)
124
125 def delete_snapshot(self, snapshot_id):
126 """Delete Snapshot."""
127 return self.delete("snapshots/%s" % str(snapshot_id))
128
129 def is_resource_deleted(self, id):
130 try:
131 self.get_snapshot(id)
132 except exceptions.NotFound:
133 return True
134 return False