ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 2 | # 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 Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 16 | import json |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 17 | |
| 18 | from six.moves.urllib import parse as urllib |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 19 | |
Yuiko Takada | f9f744f | 2015-03-31 12:57:52 +0900 | [diff] [blame] | 20 | from tempest.api_schema.response.compute.v2_1 import flavors as schema |
ghanshyam | 59869d0 | 2015-04-22 17:23:08 +0900 | [diff] [blame] | 21 | from tempest.api_schema.response.compute.v2_1 import flavors_access \ |
| 22 | as schema_access |
| 23 | from tempest.api_schema.response.compute.v2_1 import flavors_extra_specs \ |
| 24 | as schema_extra_specs |
Ken'ichi Ohmichi | 4771cbc | 2015-01-19 23:45:23 +0000 | [diff] [blame] | 25 | from tempest.common import service_client |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 26 | |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 27 | |
Ken'ichi Ohmichi | 4771cbc | 2015-01-19 23:45:23 +0000 | [diff] [blame] | 28 | class FlavorsClientJSON(service_client.ServiceClient): |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 29 | |
| 30 | def list_flavors(self, params=None): |
| 31 | url = 'flavors' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 32 | if params: |
| 33 | url += '?%s' % urllib.urlencode(params) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 34 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 35 | resp, body = self.get(url) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 36 | body = json.loads(body) |
Yuiko Takada | f9f744f | 2015-03-31 12:57:52 +0900 | [diff] [blame] | 37 | self.validate_response(schema.list_flavors, resp, body) |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 38 | return service_client.ResponseBodyList(resp, body['flavors']) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 39 | |
| 40 | def list_flavors_with_detail(self, params=None): |
| 41 | url = 'flavors/detail' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 42 | if params: |
| 43 | url += '?%s' % urllib.urlencode(params) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 44 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 45 | resp, body = self.get(url) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 46 | body = json.loads(body) |
Yuiko Takada | f9f744f | 2015-03-31 12:57:52 +0900 | [diff] [blame] | 47 | self.validate_response(schema.list_flavors_details, resp, body) |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 48 | return service_client.ResponseBodyList(resp, body['flavors']) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 49 | |
| 50 | def get_flavor_details(self, flavor_id): |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 51 | resp, body = self.get("flavors/%s" % str(flavor_id)) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 52 | body = json.loads(body) |
Yuiko Takada | f9f744f | 2015-03-31 12:57:52 +0900 | [diff] [blame] | 53 | self.validate_response(schema.create_get_flavor_details, resp, body) |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 54 | return service_client.ResponseBody(resp, body['flavor']) |
Rohit Karajgi | 0353029 | 2012-04-24 17:00:50 -0700 | [diff] [blame] | 55 | |
rajalakshmi-ganesan | f344cc3 | 2012-12-31 20:02:27 +0530 | [diff] [blame] | 56 | def create_flavor(self, name, ram, vcpus, disk, flavor_id, **kwargs): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 57 | """Creates a new flavor or instance type.""" |
Rohit Karajgi | 0353029 | 2012-04-24 17:00:50 -0700 | [diff] [blame] | 58 | post_body = { |
Zhongyue Luo | 30a563f | 2012-09-30 23:43:50 +0900 | [diff] [blame] | 59 | 'name': name, |
| 60 | 'ram': ram, |
| 61 | 'vcpus': vcpus, |
| 62 | 'disk': disk, |
Zhongyue Luo | 30a563f | 2012-09-30 23:43:50 +0900 | [diff] [blame] | 63 | 'id': flavor_id, |
Zhongyue Luo | 30a563f | 2012-09-30 23:43:50 +0900 | [diff] [blame] | 64 | } |
rajalakshmi-ganesan | f344cc3 | 2012-12-31 20:02:27 +0530 | [diff] [blame] | 65 | if kwargs.get('ephemeral'): |
| 66 | post_body['OS-FLV-EXT-DATA:ephemeral'] = kwargs.get('ephemeral') |
| 67 | if kwargs.get('swap'): |
| 68 | post_body['swap'] = kwargs.get('swap') |
| 69 | if kwargs.get('rxtx'): |
| 70 | post_body['rxtx_factor'] = kwargs.get('rxtx') |
| 71 | if kwargs.get('is_public'): |
| 72 | post_body['os-flavor-access:is_public'] = kwargs.get('is_public') |
Rohit Karajgi | 0353029 | 2012-04-24 17:00:50 -0700 | [diff] [blame] | 73 | post_body = json.dumps({'flavor': post_body}) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 74 | resp, body = self.post('flavors', post_body) |
Rohit Karajgi | 0353029 | 2012-04-24 17:00:50 -0700 | [diff] [blame] | 75 | |
| 76 | body = json.loads(body) |
Yuiko Takada | f9f744f | 2015-03-31 12:57:52 +0900 | [diff] [blame] | 77 | self.validate_response(schema.create_get_flavor_details, resp, body) |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 78 | return service_client.ResponseBody(resp, body['flavor']) |
Rohit Karajgi | 0353029 | 2012-04-24 17:00:50 -0700 | [diff] [blame] | 79 | |
| 80 | def delete_flavor(self, flavor_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 81 | """Deletes the given flavor.""" |
Ghanshyam | e4c3fb2 | 2014-03-24 16:07:16 +0900 | [diff] [blame] | 82 | resp, body = self.delete("flavors/{0}".format(flavor_id)) |
Yuiko Takada | f9f744f | 2015-03-31 12:57:52 +0900 | [diff] [blame] | 83 | self.validate_response(schema.delete_flavor, resp, body) |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 84 | return service_client.ResponseBody(resp, body) |
rajalakshmi-ganesan | f344cc3 | 2012-12-31 20:02:27 +0530 | [diff] [blame] | 85 | |
| 86 | def is_resource_deleted(self, id): |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 87 | # Did not use get_flavor_details(id) for verification as it gives |
| 88 | # 200 ok even for deleted id. LP #981263 |
| 89 | # we can remove the loop here and use get by ID when bug gets sortedout |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 90 | flavors = self.list_flavors_with_detail() |
rajalakshmi-ganesan | f344cc3 | 2012-12-31 20:02:27 +0530 | [diff] [blame] | 91 | for flavor in flavors: |
| 92 | if flavor['id'] == id: |
| 93 | return False |
| 94 | return True |
rajalakshmi-ganesan | 6d0e7a2 | 2012-12-18 20:52:38 +0530 | [diff] [blame] | 95 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 96 | @property |
| 97 | def resource_type(self): |
| 98 | """Returns the primary type of resource this client works with.""" |
| 99 | return 'flavor' |
| 100 | |
rajalakshmi-ganesan | 6d0e7a2 | 2012-12-18 20:52:38 +0530 | [diff] [blame] | 101 | def set_flavor_extra_spec(self, flavor_id, specs): |
| 102 | """Sets extra Specs to the mentioned flavor.""" |
| 103 | post_body = json.dumps({'extra_specs': specs}) |
| 104 | resp, body = self.post('flavors/%s/os-extra_specs' % flavor_id, |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 105 | post_body) |
rajalakshmi-ganesan | 6d0e7a2 | 2012-12-18 20:52:38 +0530 | [diff] [blame] | 106 | body = json.loads(body) |
ghanshyam | 59869d0 | 2015-04-22 17:23:08 +0900 | [diff] [blame] | 107 | self.validate_response(schema_extra_specs.set_get_flavor_extra_specs, |
Ghanshyam | 639d842 | 2014-03-26 18:23:32 +0900 | [diff] [blame] | 108 | resp, body) |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 109 | return service_client.ResponseBody(resp, body['extra_specs']) |
rajalakshmi-ganesan | 6d0e7a2 | 2012-12-18 20:52:38 +0530 | [diff] [blame] | 110 | |
| 111 | def get_flavor_extra_spec(self, flavor_id): |
| 112 | """Gets extra Specs details of the mentioned flavor.""" |
| 113 | resp, body = self.get('flavors/%s/os-extra_specs' % flavor_id) |
| 114 | body = json.loads(body) |
ghanshyam | 59869d0 | 2015-04-22 17:23:08 +0900 | [diff] [blame] | 115 | self.validate_response(schema_extra_specs.set_get_flavor_extra_specs, |
Ghanshyam | 639d842 | 2014-03-26 18:23:32 +0900 | [diff] [blame] | 116 | resp, body) |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 117 | return service_client.ResponseBody(resp, body['extra_specs']) |
rajalakshmi-ganesan | 6d0e7a2 | 2012-12-18 20:52:38 +0530 | [diff] [blame] | 118 | |
lijunjbj | 9feaa21 | 2013-10-14 02:11:49 -0500 | [diff] [blame] | 119 | def get_flavor_extra_spec_with_key(self, flavor_id, key): |
Zhu Zhu | 2e1872d | 2013-10-08 21:23:29 -0500 | [diff] [blame] | 120 | """Gets extra Specs key-value of the mentioned flavor and key.""" |
lijunjbj | 9feaa21 | 2013-10-14 02:11:49 -0500 | [diff] [blame] | 121 | resp, body = self.get('flavors/%s/os-extra_specs/%s' % (str(flavor_id), |
| 122 | key)) |
| 123 | body = json.loads(body) |
ghanshyam | 59869d0 | 2015-04-22 17:23:08 +0900 | [diff] [blame] | 124 | self.validate_response( |
| 125 | schema_extra_specs.set_get_flavor_extra_specs_key, |
| 126 | resp, body) |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 127 | return service_client.ResponseBody(resp, body) |
lijunjbj | 9feaa21 | 2013-10-14 02:11:49 -0500 | [diff] [blame] | 128 | |
ivan-zhu | ae7c7c5 | 2013-10-21 22:13:22 +0800 | [diff] [blame] | 129 | def update_flavor_extra_spec(self, flavor_id, key, **kwargs): |
Zhu Zhu | 2e1872d | 2013-10-08 21:23:29 -0500 | [diff] [blame] | 130 | """Update specified extra Specs of the mentioned flavor and key.""" |
ivan-zhu | ae7c7c5 | 2013-10-21 22:13:22 +0800 | [diff] [blame] | 131 | resp, body = self.put('flavors/%s/os-extra_specs/%s' % |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 132 | (flavor_id, key), json.dumps(kwargs)) |
ivan-zhu | ae7c7c5 | 2013-10-21 22:13:22 +0800 | [diff] [blame] | 133 | body = json.loads(body) |
ghanshyam | 59869d0 | 2015-04-22 17:23:08 +0900 | [diff] [blame] | 134 | self.validate_response( |
| 135 | schema_extra_specs.set_get_flavor_extra_specs_key, |
| 136 | resp, body) |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 137 | return service_client.ResponseBody(resp, body) |
ivan-zhu | ae7c7c5 | 2013-10-21 22:13:22 +0800 | [diff] [blame] | 138 | |
rajalakshmi-ganesan | 6d0e7a2 | 2012-12-18 20:52:38 +0530 | [diff] [blame] | 139 | def unset_flavor_extra_spec(self, flavor_id, key): |
| 140 | """Unsets extra Specs from the mentioned flavor.""" |
Ghanshyam | 639d842 | 2014-03-26 18:23:32 +0900 | [diff] [blame] | 141 | resp, body = self.delete('flavors/%s/os-extra_specs/%s' % |
| 142 | (str(flavor_id), key)) |
Yuiko Takada | f9f744f | 2015-03-31 12:57:52 +0900 | [diff] [blame] | 143 | self.validate_response(schema.unset_flavor_extra_specs, resp, body) |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 144 | return service_client.ResponseBody(resp, body) |
Mitsuhiko Yamazaki | f5f4da6 | 2013-03-29 17:40:03 +0900 | [diff] [blame] | 145 | |
Zhu Zhu | 705f24a | 2013-10-11 05:52:54 -0500 | [diff] [blame] | 146 | def list_flavor_access(self, flavor_id): |
| 147 | """Gets flavor access information given the flavor id.""" |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 148 | resp, body = self.get('flavors/%s/os-flavor-access' % flavor_id) |
Zhu Zhu | 705f24a | 2013-10-11 05:52:54 -0500 | [diff] [blame] | 149 | body = json.loads(body) |
Ghanshyam | 95951ed | 2014-03-31 11:57:22 +0900 | [diff] [blame] | 150 | self.validate_response(schema_access.add_remove_list_flavor_access, |
| 151 | resp, body) |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 152 | return service_client.ResponseBodyList(resp, body['flavor_access']) |
Zhu Zhu | 705f24a | 2013-10-11 05:52:54 -0500 | [diff] [blame] | 153 | |
Mitsuhiko Yamazaki | f5f4da6 | 2013-03-29 17:40:03 +0900 | [diff] [blame] | 154 | def add_flavor_access(self, flavor_id, tenant_id): |
| 155 | """Add flavor access for the specified tenant.""" |
| 156 | post_body = { |
| 157 | 'addTenantAccess': { |
| 158 | 'tenant': tenant_id |
| 159 | } |
| 160 | } |
| 161 | post_body = json.dumps(post_body) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 162 | resp, body = self.post('flavors/%s/action' % flavor_id, post_body) |
Mitsuhiko Yamazaki | f5f4da6 | 2013-03-29 17:40:03 +0900 | [diff] [blame] | 163 | body = json.loads(body) |
Ghanshyam | 95951ed | 2014-03-31 11:57:22 +0900 | [diff] [blame] | 164 | self.validate_response(schema_access.add_remove_list_flavor_access, |
| 165 | resp, body) |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 166 | return service_client.ResponseBodyList(resp, body['flavor_access']) |
Mitsuhiko Yamazaki | f5f4da6 | 2013-03-29 17:40:03 +0900 | [diff] [blame] | 167 | |
| 168 | def remove_flavor_access(self, flavor_id, tenant_id): |
| 169 | """Remove flavor access from the specified tenant.""" |
| 170 | post_body = { |
| 171 | 'removeTenantAccess': { |
| 172 | 'tenant': tenant_id |
| 173 | } |
| 174 | } |
| 175 | post_body = json.dumps(post_body) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 176 | resp, body = self.post('flavors/%s/action' % flavor_id, post_body) |
Mitsuhiko Yamazaki | f5f4da6 | 2013-03-29 17:40:03 +0900 | [diff] [blame] | 177 | body = json.loads(body) |
Ghanshyam | 95951ed | 2014-03-31 11:57:22 +0900 | [diff] [blame] | 178 | self.validate_response(schema_access.add_remove_list_flavor_access, |
| 179 | resp, body) |
David Kranz | 2fa77b2 | 2015-02-09 11:39:50 -0500 | [diff] [blame] | 180 | return service_client.ResponseBody(resp, body['flavor_access']) |