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