blob: bd339b2ce4fc283b0fa7ffb88cc40f565be95eb1 [file] [log] [blame]
dwallecke62b9f02012-10-10 23:34:42 -05001# 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
Daryl Walleck1465d612011-11-02 02:22:15 -050018import json
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050019import urllib
Daryl Walleck1465d612011-11-02 02:22:15 -050020
Matthew Treinisha83a16e2012-12-07 13:44:02 -050021from tempest.common.rest_client import RestClient
22
Daryl Walleck1465d612011-11-02 02:22:15 -050023
Tiago Melloeda03b52012-08-22 23:47:29 -030024class FlavorsClientJSON(RestClient):
Daryl Walleck1465d612011-11-02 02:22:15 -050025
Daryl Walleck587385b2012-03-03 13:00:26 -060026 def __init__(self, config, username, password, auth_url, tenant_name=None):
Tiago Melloeda03b52012-08-22 23:47:29 -030027 super(FlavorsClientJSON, self).__init__(config, username, password,
28 auth_url, tenant_name)
chris fattarsi5098fa22012-04-17 13:27:00 -070029 self.service = self.config.compute.catalog_type
Daryl Walleck1465d612011-11-02 02:22:15 -050030
31 def list_flavors(self, params=None):
32 url = 'flavors'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050033 if params:
34 url += '?%s' % urllib.urlencode(params)
Daryl Walleck1465d612011-11-02 02:22:15 -050035
chris fattarsi5098fa22012-04-17 13:27:00 -070036 resp, body = self.get(url)
Daryl Walleck1465d612011-11-02 02:22:15 -050037 body = json.loads(body)
Daryl Walleck74d04092012-01-10 23:33:46 -060038 return resp, body['flavors']
Daryl Walleck1465d612011-11-02 02:22:15 -050039
40 def list_flavors_with_detail(self, params=None):
41 url = 'flavors/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050042 if params:
43 url += '?%s' % urllib.urlencode(params)
Daryl Walleck1465d612011-11-02 02:22:15 -050044
chris fattarsi5098fa22012-04-17 13:27:00 -070045 resp, body = self.get(url)
Daryl Walleck1465d612011-11-02 02:22:15 -050046 body = json.loads(body)
Daryl Walleck74d04092012-01-10 23:33:46 -060047 return resp, body['flavors']
Daryl Walleck1465d612011-11-02 02:22:15 -050048
49 def get_flavor_details(self, flavor_id):
chris fattarsi5098fa22012-04-17 13:27:00 -070050 resp, body = self.get("flavors/%s" % str(flavor_id))
Daryl Walleck1465d612011-11-02 02:22:15 -050051 body = json.loads(body)
52 return resp, body['flavor']
Rohit Karajgi03530292012-04-24 17:00:50 -070053
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053054 def create_flavor(self, name, ram, vcpus, disk, flavor_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -050055 """Creates a new flavor or instance type."""
Rohit Karajgi03530292012-04-24 17:00:50 -070056 post_body = {
Zhongyue Luo30a563f2012-09-30 23:43:50 +090057 'name': name,
58 'ram': ram,
59 'vcpus': vcpus,
60 'disk': disk,
Zhongyue Luo30a563f2012-09-30 23:43:50 +090061 'id': flavor_id,
Zhongyue Luo30a563f2012-09-30 23:43:50 +090062 }
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053063 if kwargs.get('ephemeral'):
64 post_body['OS-FLV-EXT-DATA:ephemeral'] = kwargs.get('ephemeral')
65 if kwargs.get('swap'):
66 post_body['swap'] = kwargs.get('swap')
67 if kwargs.get('rxtx'):
68 post_body['rxtx_factor'] = kwargs.get('rxtx')
69 if kwargs.get('is_public'):
70 post_body['os-flavor-access:is_public'] = kwargs.get('is_public')
Rohit Karajgi03530292012-04-24 17:00:50 -070071 post_body = json.dumps({'flavor': post_body})
72 resp, body = self.post('flavors', post_body, self.headers)
73
74 body = json.loads(body)
75 return resp, body['flavor']
76
77 def delete_flavor(self, flavor_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050078 """Deletes the given flavor."""
Rohit Karajgi03530292012-04-24 17:00:50 -070079 return self.delete("flavors/%s" % str(flavor_id))
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053080
81 def is_resource_deleted(self, id):
82 #Did not use get_flavor_details(id) for verification as it gives
83 #200 ok even for deleted id. LP #981263
84 #we can remove the loop here and use get by ID when bug gets sortedout
85 resp, flavors = self.list_flavors_with_detail()
86 for flavor in flavors:
87 if flavor['id'] == id:
88 return False
89 return True