blob: c22b398d9b093d521869867eec2441a512241a60 [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
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050020import urllib
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053021
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070022from tempest.common.rest_client import RestClient
23from tempest import exceptions
24
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053025
Matthew Treinish9854d5b2012-09-20 10:22:13 -040026class VolumesClientJSON(RestClient):
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070027 """
28 Client class to send CRUD Volume API requests to a Cinder endpoint
29 """
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053030
chris fattarsi5098fa22012-04-17 13:27:00 -070031 def __init__(self, config, username, password, auth_url, tenant_name=None):
Matthew Treinish9854d5b2012-09-20 10:22:13 -040032 super(VolumesClientJSON, self).__init__(config, username, password,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080033 auth_url, tenant_name)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070034
35 self.service = self.config.volume.catalog_type
36 self.build_interval = self.config.volume.build_interval
37 self.build_timeout = self.config.volume.build_timeout
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053038
39 def list_volumes(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050040 """List all the volumes created."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070041 url = 'volumes'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050042 if params:
43 url += '?%s' % urllib.urlencode(params)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053044
chris fattarsi5098fa22012-04-17 13:27:00 -070045 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053046 body = json.loads(body)
47 return resp, body['volumes']
48
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053049 def list_volumes_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050050 """List the details of all volumes."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070051 url = 'volumes/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050052 if params:
53 url += '?%s' % urllib.urlencode(params)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053054
chris fattarsi5098fa22012-04-17 13:27:00 -070055 resp, body = self.get(url)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053056 body = json.loads(body)
57 return resp, body['volumes']
58
Attila Fazekasb8aa7592013-01-26 01:25:45 +010059 def get_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050060 """Returns the details of a single volume."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070061 url = "volumes/%s" % str(volume_id)
Attila Fazekasb8aa7592013-01-26 01:25:45 +010062 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053063 body = json.loads(body)
64 return resp, body['volume']
65
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053066 def create_volume(self, size, **kwargs):
67 """
68 Creates a new Volume.
69 size(Required): Size of volume in GB.
70 Following optional keyword arguments are accepted:
71 display_name: Optional Volume Name.
72 metadata: A dictionary of values to be used as metadata.
Rohan Rhishikesh Kanadec316f0a2012-12-04 05:44:39 -080073 volume_type: Optional Name of volume_type for the volume
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010074 snapshot_id: When specified the volume is created from this snapshot
Giulio Fidente36836c42013-04-05 15:43:51 +020075 imageRef: When specified the volume is created from this image
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053076 """
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010077 post_body = {'size': size}
78 post_body.update(kwargs)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053079 post_body = json.dumps({'volume': post_body})
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070080 resp, body = self.post('volumes', post_body, self.headers)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053081 body = json.loads(body)
82 return resp, body['volume']
83
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053084 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050085 """Deletes the Specified Volume."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070086 return self.delete("volumes/%s" % str(volume_id))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053087
Giulio Fidente884e9da2013-06-21 17:25:42 +020088 def upload_volume(self, volume_id, image_name):
89 """Uploads a volume in Glance."""
90 post_body = {
91 'image_name': image_name,
92 }
93 post_body = json.dumps({'os-volume_upload_image': post_body})
94 url = 'volumes/%s/action' % (volume_id)
95 resp, body = self.post(url, post_body, self.headers)
96 body = json.loads(body)
97 return resp, body['os-volume_upload_image']
98
Rohit Karajgia42fe442012-09-21 03:08:33 -070099 def attach_volume(self, volume_id, instance_uuid, mountpoint):
Sean Daguef237ccb2013-01-04 15:19:14 -0500100 """Attaches a volume to a given instance on a given mountpoint."""
Rohit Karajgia42fe442012-09-21 03:08:33 -0700101 post_body = {
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900102 'instance_uuid': instance_uuid,
103 'mountpoint': mountpoint,
104 }
Rohit Karajgia42fe442012-09-21 03:08:33 -0700105 post_body = json.dumps({'os-attach': post_body})
106 url = 'volumes/%s/action' % (volume_id)
107 resp, body = self.post(url, post_body, self.headers)
108 return resp, body
109
110 def detach_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500111 """Detaches a volume from an instance."""
Rohit Karajgia42fe442012-09-21 03:08:33 -0700112 post_body = {}
113 post_body = json.dumps({'os-detach': post_body})
114 url = 'volumes/%s/action' % (volume_id)
115 resp, body = self.post(url, post_body, self.headers)
116 return resp, body
117
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530118 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -0500119 """Waits for a Volume to reach a given status."""
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530120 resp, body = self.get_volume(volume_id)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700121 volume_name = body['display_name']
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530122 volume_status = body['status']
123 start = int(time.time())
124
125 while volume_status != status:
126 time.sleep(self.build_interval)
127 resp, body = self.get_volume(volume_id)
128 volume_status = body['status']
129 if volume_status == 'error':
rajalakshmi-ganesane3bb58f2012-05-16 12:01:15 +0530130 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530131
132 if int(time.time()) - start >= self.build_timeout:
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800133 message = ('Volume %s failed to reach %s status within '
134 'the required time (%s s).' %
135 (volume_name, status, self.build_timeout))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530136 raise exceptions.TimeoutException(message)
David Kranz6aceb4a2012-06-05 14:05:45 -0400137
138 def is_resource_deleted(self, id):
139 try:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100140 self.get_volume(id)
David Kranz6aceb4a2012-06-05 14:05:45 -0400141 except exceptions.NotFound:
142 return True
143 return False