blob: 863d9a82edb779ee0d98a7f90f9ba9a64ef597c1 [file] [log] [blame]
Rohit Karajgidd47d7e2012-07-31 04:11:01 -07001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2012 OpenStack, LLC
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053018import json
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053019import time
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053020
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070021from tempest.common.rest_client import RestClient
22from tempest import exceptions
23
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053024
chris fattarsi5098fa22012-04-17 13:27:00 -070025class VolumesClient(RestClient):
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070026 """
27 Client class to send CRUD Volume API requests to a Cinder endpoint
28 """
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053029
chris fattarsi5098fa22012-04-17 13:27:00 -070030 def __init__(self, config, username, password, auth_url, tenant_name=None):
31 super(VolumesClient, self).__init__(config, username, password,
32 auth_url, tenant_name)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070033
34 self.service = self.config.volume.catalog_type
35 self.build_interval = self.config.volume.build_interval
36 self.build_timeout = self.config.volume.build_timeout
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053037
38 def list_volumes(self, params=None):
39 """List all the volumes created"""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070040 url = 'volumes'
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053041 if params != None:
42 param_list = []
43 for param, value in params.iteritems():
44 param_list.append("%s=%s&" % (param, value))
45
46 url += '?' + ' '.join(param_list)
chris fattarsi5098fa22012-04-17 13:27:00 -070047 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053048 body = json.loads(body)
49 return resp, body['volumes']
50
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053051 def list_volumes_with_detail(self, params=None):
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070052 """List the details of all volumes"""
53 url = 'volumes/detail'
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053054 if params != None:
55 param_list = []
56 for param, value in params.iteritems():
57 param_list.append("%s=%s&" % (param, value))
58
59 url = '?' + ' '.join(param_list)
60
chris fattarsi5098fa22012-04-17 13:27:00 -070061 resp, body = self.get(url)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053062 body = json.loads(body)
63 return resp, body['volumes']
64
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053065 def get_volume(self, volume_id):
66 """Returns the details of a single volume"""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070067 url = "volumes/%s" % str(volume_id)
chris fattarsi5098fa22012-04-17 13:27:00 -070068 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053069 body = json.loads(body)
70 return resp, body['volume']
71
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053072 def create_volume(self, size, **kwargs):
73 """
74 Creates a new Volume.
75 size(Required): Size of volume in GB.
76 Following optional keyword arguments are accepted:
77 display_name: Optional Volume Name.
78 metadata: A dictionary of values to be used as metadata.
79 """
80 post_body = {
81 'size': size,
82 'display_name': kwargs.get('display_name'),
83 'metadata': kwargs.get('metadata'),
84 }
85
86 post_body = json.dumps({'volume': post_body})
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070087 resp, body = self.post('volumes', post_body, self.headers)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053088 body = json.loads(body)
89 return resp, body['volume']
90
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053091 def delete_volume(self, volume_id):
92 """Deletes the Specified Volume"""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070093 return self.delete("volumes/%s" % str(volume_id))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053094
Rohit Karajgia42fe442012-09-21 03:08:33 -070095 def attach_volume(self, volume_id, instance_uuid, mountpoint):
96 """Attaches a volume to a given instance on a given mountpoint"""
97 post_body = {
98 'instance_uuid': instance_uuid,
99 'mountpoint': mountpoint
100 }
101 post_body = json.dumps({'os-attach': post_body})
102 url = 'volumes/%s/action' % (volume_id)
103 resp, body = self.post(url, post_body, self.headers)
104 return resp, body
105
106 def detach_volume(self, volume_id):
107 """Detaches a volume from an instance"""
108 post_body = {}
109 post_body = json.dumps({'os-detach': post_body})
110 url = 'volumes/%s/action' % (volume_id)
111 resp, body = self.post(url, post_body, self.headers)
112 return resp, body
113
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530114 def wait_for_volume_status(self, volume_id, status):
115 """Waits for a Volume to reach a given status"""
116 resp, body = self.get_volume(volume_id)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700117 volume_name = body['display_name']
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530118 volume_status = body['status']
119 start = int(time.time())
120
121 while volume_status != status:
122 time.sleep(self.build_interval)
123 resp, body = self.get_volume(volume_id)
124 volume_status = body['status']
125 if volume_status == 'error':
rajalakshmi-ganesane3bb58f2012-05-16 12:01:15 +0530126 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530127
128 if int(time.time()) - start >= self.build_timeout:
129 message = 'Volume %s failed to reach %s status within '\
130 'the required time (%s s).' % (volume_name, status,
131 self.build_timeout)
132 raise exceptions.TimeoutException(message)
David Kranz6aceb4a2012-06-05 14:05:45 -0400133
134 def is_resource_deleted(self, id):
135 try:
136 self.get_volume(id)
137 except exceptions.NotFound:
138 return True
139 return False