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