blob: bc64117a99b39f9a8c321bba4eeedbbf23739181 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
dwallecke62b9f02012-10-10 23:34:42 -05002# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Daryl Walleck1465d612011-11-02 02:22:15 -050016import json
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050017import urllib
Daryl Walleck1465d612011-11-02 02:22:15 -050018
Yuiko Takada177ccce2014-03-13 10:02:20 +000019from tempest.api_schema.compute import flavors_access as schema_access
Haiwei Xuab924622014-03-05 04:33:51 +090020from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000021from tempest import config
22
23CONF = config.CONF
Matthew Treinisha83a16e2012-12-07 13:44:02 -050024
Daryl Walleck1465d612011-11-02 02:22:15 -050025
Haiwei Xuab924622014-03-05 04:33:51 +090026class FlavorsClientJSON(rest_client.RestClient):
Daryl Walleck1465d612011-11-02 02:22:15 -050027
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000028 def __init__(self, auth_provider):
29 super(FlavorsClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000030 self.service = CONF.compute.catalog_type
Daryl Walleck1465d612011-11-02 02:22:15 -050031
32 def list_flavors(self, params=None):
33 url = 'flavors'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050034 if params:
35 url += '?%s' % urllib.urlencode(params)
Daryl Walleck1465d612011-11-02 02:22:15 -050036
chris fattarsi5098fa22012-04-17 13:27:00 -070037 resp, body = self.get(url)
Daryl Walleck1465d612011-11-02 02:22:15 -050038 body = json.loads(body)
Daryl Walleck74d04092012-01-10 23:33:46 -060039 return resp, body['flavors']
Daryl Walleck1465d612011-11-02 02:22:15 -050040
41 def list_flavors_with_detail(self, params=None):
42 url = 'flavors/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050043 if params:
44 url += '?%s' % urllib.urlencode(params)
Daryl Walleck1465d612011-11-02 02:22:15 -050045
chris fattarsi5098fa22012-04-17 13:27:00 -070046 resp, body = self.get(url)
Daryl Walleck1465d612011-11-02 02:22:15 -050047 body = json.loads(body)
Daryl Walleck74d04092012-01-10 23:33:46 -060048 return resp, body['flavors']
Daryl Walleck1465d612011-11-02 02:22:15 -050049
50 def get_flavor_details(self, flavor_id):
chris fattarsi5098fa22012-04-17 13:27:00 -070051 resp, body = self.get("flavors/%s" % str(flavor_id))
Daryl Walleck1465d612011-11-02 02:22:15 -050052 body = json.loads(body)
53 return resp, body['flavor']
Rohit Karajgi03530292012-04-24 17:00:50 -070054
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053055 def create_flavor(self, name, ram, vcpus, disk, flavor_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -050056 """Creates a new flavor or instance type."""
Rohit Karajgi03530292012-04-24 17:00:50 -070057 post_body = {
Zhongyue Luo30a563f2012-09-30 23:43:50 +090058 'name': name,
59 'ram': ram,
60 'vcpus': vcpus,
61 'disk': disk,
Zhongyue Luo30a563f2012-09-30 23:43:50 +090062 'id': flavor_id,
Zhongyue Luo30a563f2012-09-30 23:43:50 +090063 }
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053064 if kwargs.get('ephemeral'):
65 post_body['OS-FLV-EXT-DATA:ephemeral'] = kwargs.get('ephemeral')
66 if kwargs.get('swap'):
67 post_body['swap'] = kwargs.get('swap')
68 if kwargs.get('rxtx'):
69 post_body['rxtx_factor'] = kwargs.get('rxtx')
70 if kwargs.get('is_public'):
71 post_body['os-flavor-access:is_public'] = kwargs.get('is_public')
Rohit Karajgi03530292012-04-24 17:00:50 -070072 post_body = json.dumps({'flavor': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020073 resp, body = self.post('flavors', post_body)
Rohit Karajgi03530292012-04-24 17:00:50 -070074
75 body = json.loads(body)
76 return resp, body['flavor']
77
78 def delete_flavor(self, flavor_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050079 """Deletes the given flavor."""
Rohit Karajgi03530292012-04-24 17:00:50 -070080 return self.delete("flavors/%s" % str(flavor_id))
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053081
82 def is_resource_deleted(self, id):
Attila Fazekasa8b5fe72013-08-01 16:59:06 +020083 # Did not use get_flavor_details(id) for verification as it gives
84 # 200 ok even for deleted id. LP #981263
85 # we can remove the loop here and use get by ID when bug gets sortedout
rajalakshmi-ganesanf344cc32012-12-31 20:02:27 +053086 resp, flavors = self.list_flavors_with_detail()
87 for flavor in flavors:
88 if flavor['id'] == id:
89 return False
90 return True
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +053091
92 def set_flavor_extra_spec(self, flavor_id, specs):
93 """Sets extra Specs to the mentioned flavor."""
94 post_body = json.dumps({'extra_specs': specs})
95 resp, body = self.post('flavors/%s/os-extra_specs' % flavor_id,
vponomaryovf4c27f92014-02-18 10:56:42 +020096 post_body)
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +053097 body = json.loads(body)
98 return resp, body['extra_specs']
99
100 def get_flavor_extra_spec(self, flavor_id):
101 """Gets extra Specs details of the mentioned flavor."""
102 resp, body = self.get('flavors/%s/os-extra_specs' % flavor_id)
103 body = json.loads(body)
104 return resp, body['extra_specs']
105
lijunjbj9feaa212013-10-14 02:11:49 -0500106 def get_flavor_extra_spec_with_key(self, flavor_id, key):
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500107 """Gets extra Specs key-value of the mentioned flavor and key."""
lijunjbj9feaa212013-10-14 02:11:49 -0500108 resp, body = self.get('flavors/%s/os-extra_specs/%s' % (str(flavor_id),
109 key))
110 body = json.loads(body)
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500111 return resp, body
lijunjbj9feaa212013-10-14 02:11:49 -0500112
ivan-zhuae7c7c52013-10-21 22:13:22 +0800113 def update_flavor_extra_spec(self, flavor_id, key, **kwargs):
Zhu Zhu2e1872d2013-10-08 21:23:29 -0500114 """Update specified extra Specs of the mentioned flavor and key."""
ivan-zhuae7c7c52013-10-21 22:13:22 +0800115 resp, body = self.put('flavors/%s/os-extra_specs/%s' %
vponomaryovf4c27f92014-02-18 10:56:42 +0200116 (flavor_id, key), json.dumps(kwargs))
ivan-zhuae7c7c52013-10-21 22:13:22 +0800117 body = json.loads(body)
118 return resp, body
119
rajalakshmi-ganesan6d0e7a22012-12-18 20:52:38 +0530120 def unset_flavor_extra_spec(self, flavor_id, key):
121 """Unsets extra Specs from the mentioned flavor."""
122 return self.delete('flavors/%s/os-extra_specs/%s' % (str(flavor_id),
123 key))
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900124
Zhu Zhu705f24a2013-10-11 05:52:54 -0500125 def list_flavor_access(self, flavor_id):
126 """Gets flavor access information given the flavor id."""
vponomaryovf4c27f92014-02-18 10:56:42 +0200127 resp, body = self.get('flavors/%s/os-flavor-access' % flavor_id)
Zhu Zhu705f24a2013-10-11 05:52:54 -0500128 body = json.loads(body)
Yuiko Takada177ccce2014-03-13 10:02:20 +0000129 self.validate_response(schema_access.list_flavor_access, resp, body)
Zhu Zhu705f24a2013-10-11 05:52:54 -0500130 return resp, body['flavor_access']
131
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900132 def add_flavor_access(self, flavor_id, tenant_id):
133 """Add flavor access for the specified tenant."""
134 post_body = {
135 'addTenantAccess': {
136 'tenant': tenant_id
137 }
138 }
139 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +0200140 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900141 body = json.loads(body)
142 return resp, body['flavor_access']
143
144 def remove_flavor_access(self, flavor_id, tenant_id):
145 """Remove flavor access from the specified tenant."""
146 post_body = {
147 'removeTenantAccess': {
148 'tenant': tenant_id
149 }
150 }
151 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +0200152 resp, body = self.post('flavors/%s/action' % flavor_id, post_body)
Mitsuhiko Yamazakif5f4da62013-03-29 17:40:03 +0900153 body = json.loads(body)
154 return resp, body['flavor_access']