blob: 17f6cba0acd5a04d0c708eca6c6fbfa77758561e [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 json
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010016import time
17import urllib
18
Mitsuhiko Yamazaki46818aa2013-04-18 17:49:17 +090019from tempest.common import log as logging
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010020from tempest.common.rest_client import RestClient
21from tempest import exceptions
22
23LOG = logging.getLogger(__name__)
24
25
26class SnapshotsClientJSON(RestClient):
27 """Client class to send CRUD Volume API requests."""
28
29 def __init__(self, config, username, password, auth_url, tenant_name=None):
30 super(SnapshotsClientJSON, self).__init__(config, username, password,
31 auth_url, tenant_name)
32
33 self.service = self.config.volume.catalog_type
34 self.build_interval = self.config.volume.build_interval
35 self.build_timeout = self.config.volume.build_timeout
36
37 def list_snapshots(self, params=None):
38 """List all the snapshot."""
39 url = 'snapshots'
40 if params:
41 url += '?%s' % urllib.urlencode(params)
42
43 resp, body = self.get(url)
44 body = json.loads(body)
45 return resp, body['snapshots']
46
47 def list_snapshot_with_detail(self, params=None):
48 """List the details of all snapshots."""
49 url = 'snapshots/detail'
50 if params:
51 url += '?%s' % urllib.urlencode(params)
52
53 resp, body = self.get(url)
54 body = json.loads(body)
55 return resp, body['snapshots']
56
57 def get_snapshot(self, snapshot_id):
58 """Returns the details of a single snapshot."""
59 url = "snapshots/%s" % str(snapshot_id)
60 resp, body = self.get(url)
61 body = json.loads(body)
62 return resp, body['snapshot']
63
64 def create_snapshot(self, volume_id, **kwargs):
65 """
66 Creates a new snapshot.
67 volume_id(Required): id of the volume.
68 force: Create a snapshot even if the volume attached (Default=False)
69 display_name: Optional snapshot Name.
70 display_description: User friendly snapshot description.
71 """
72 post_body = {'volume_id': volume_id}
73 post_body.update(kwargs)
74 post_body = json.dumps({'snapshot': post_body})
75 resp, body = self.post('snapshots', post_body, self.headers)
76 body = json.loads(body)
77 return resp, body['snapshot']
78
79 #NOTE(afazekas): just for the wait function
80 def _get_snapshot_status(self, snapshot_id):
81 resp, body = self.get_snapshot(snapshot_id)
82 status = body['status']
83 #NOTE(afazekas): snapshot can reach an "error"
84 # state in a "normal" lifecycle
85 if (status == 'error'):
86 raise exceptions.SnapshotBuildErrorException(
Sean Dague14c68182013-04-14 15:34:30 -040087 snapshot_id=snapshot_id)
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010088
89 return status
90
91 #NOTE(afazkas): Wait reinvented again. It is not in the correct layer
92 def wait_for_snapshot_status(self, snapshot_id, status):
93 """Waits for a Snapshot to reach a given status."""
94 start_time = time.time()
95 old_value = value = self._get_snapshot_status(snapshot_id)
96 while True:
97 dtime = time.time() - start_time
98 time.sleep(self.build_interval)
99 if value != old_value:
100 LOG.info('Value transition from "%s" to "%s"'
101 'in %d second(s).', old_value,
102 value, dtime)
103 if (value == status):
104 return value
105
106 if dtime > self.build_timeout:
107 message = ('Time Limit Exceeded! (%ds)'
108 'while waiting for %s, '
109 'but we got %s.' %
110 (self.build_timeout, status, value))
111 raise exceptions.TimeoutException(message)
112 time.sleep(self.build_interval)
113 old_value = value
114 value = self._get_snapshot_status(snapshot_id)
115
116 def delete_snapshot(self, snapshot_id):
117 """Delete Snapshot."""
118 return self.delete("snapshots/%s" % str(snapshot_id))
119
120 def is_resource_deleted(self, id):
121 try:
122 self.get_snapshot(id)
123 except exceptions.NotFound:
124 return True
125 return False