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 | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 17 | import urllib |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 18 | |
Yuiko Takada | 177ccce | 2014-03-13 10:02:20 +0000 | [diff] [blame] | 19 | from tempest.api_schema.compute import flavors_access as schema_access |
Haiwei Xu | ab92462 | 2014-03-05 04:33:51 +0900 | [diff] [blame] | 20 | from tempest.common import rest_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 21 | from tempest import config |
| 22 | |
| 23 | CONF = config.CONF |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 24 | |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 25 | |
Haiwei Xu | ab92462 | 2014-03-05 04:33:51 +0900 | [diff] [blame] | 26 | class FlavorsClientJSON(rest_client.RestClient): |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 27 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 28 | def __init__(self, auth_provider): |
| 29 | super(FlavorsClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 30 | self.service = CONF.compute.catalog_type |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 31 | |
| 32 | def list_flavors(self, params=None): |
| 33 | url = 'flavors' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 34 | if params: |
| 35 | url += '?%s' % urllib.urlencode(params) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 36 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 37 | resp, body = self.get(url) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 38 | body = json.loads(body) |
Daryl Walleck | 74d0409 | 2012-01-10 23:33:46 -0600 | [diff] [blame] | 39 | return resp, body['flavors'] |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 40 | |
| 41 | def list_flavors_with_detail(self, params=None): |
| 42 | url = 'flavors/detail' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 43 | if params: |
| 44 | url += '?%s' % urllib.urlencode(params) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 45 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 46 | resp, body = self.get(url) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 47 | body = json.loads(body) |
Daryl Walleck | 74d0409 | 2012-01-10 23:33:46 -0600 | [diff] [blame] | 48 | return 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) |
| 53 | return 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) |
| 76 | return resp, body['flavor'] |
| 77 | |
| 78 | def delete_flavor(self, flavor_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 79 | """Deletes the given flavor.""" |
Rohit Karajgi | 0353029 | 2012-04-24 17:00:50 -0700 | [diff] [blame] | 80 | return self.delete("flavors/%s" % str(flavor_id)) |
rajalakshmi-ganesan | f344cc3 | 2012-12-31 20:02:27 +0530 | [diff] [blame] | 81 | |
| 82 | def is_resource_deleted(self, id): |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 83 | # 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-ganesan | f344cc3 | 2012-12-31 20:02:27 +0530 | [diff] [blame] | 86 | resp, flavors = self.list_flavors_with_detail() |
| 87 | for flavor in flavors: |
| 88 | if flavor['id'] == id: |
| 89 | return False |
| 90 | return True |
rajalakshmi-ganesan | 6d0e7a2 | 2012-12-18 20:52:38 +0530 | [diff] [blame] | 91 | |
| 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, |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 96 | post_body) |
rajalakshmi-ganesan | 6d0e7a2 | 2012-12-18 20:52:38 +0530 | [diff] [blame] | 97 | 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 | |
lijunjbj | 9feaa21 | 2013-10-14 02:11:49 -0500 | [diff] [blame] | 106 | def get_flavor_extra_spec_with_key(self, flavor_id, key): |
Zhu Zhu | 2e1872d | 2013-10-08 21:23:29 -0500 | [diff] [blame] | 107 | """Gets extra Specs key-value of the mentioned flavor and key.""" |
lijunjbj | 9feaa21 | 2013-10-14 02:11:49 -0500 | [diff] [blame] | 108 | resp, body = self.get('flavors/%s/os-extra_specs/%s' % (str(flavor_id), |
| 109 | key)) |
| 110 | body = json.loads(body) |
Zhu Zhu | 2e1872d | 2013-10-08 21:23:29 -0500 | [diff] [blame] | 111 | return resp, body |
lijunjbj | 9feaa21 | 2013-10-14 02:11:49 -0500 | [diff] [blame] | 112 | |
ivan-zhu | ae7c7c5 | 2013-10-21 22:13:22 +0800 | [diff] [blame] | 113 | def update_flavor_extra_spec(self, flavor_id, key, **kwargs): |
Zhu Zhu | 2e1872d | 2013-10-08 21:23:29 -0500 | [diff] [blame] | 114 | """Update specified extra Specs of the mentioned flavor and key.""" |
ivan-zhu | ae7c7c5 | 2013-10-21 22:13:22 +0800 | [diff] [blame] | 115 | resp, body = self.put('flavors/%s/os-extra_specs/%s' % |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 116 | (flavor_id, key), json.dumps(kwargs)) |
ivan-zhu | ae7c7c5 | 2013-10-21 22:13:22 +0800 | [diff] [blame] | 117 | body = json.loads(body) |
| 118 | return resp, body |
| 119 | |
rajalakshmi-ganesan | 6d0e7a2 | 2012-12-18 20:52:38 +0530 | [diff] [blame] | 120 | 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 Yamazaki | f5f4da6 | 2013-03-29 17:40:03 +0900 | [diff] [blame] | 124 | |
Zhu Zhu | 705f24a | 2013-10-11 05:52:54 -0500 | [diff] [blame] | 125 | def list_flavor_access(self, flavor_id): |
| 126 | """Gets flavor access information given the flavor id.""" |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 127 | resp, body = self.get('flavors/%s/os-flavor-access' % flavor_id) |
Zhu Zhu | 705f24a | 2013-10-11 05:52:54 -0500 | [diff] [blame] | 128 | body = json.loads(body) |
Yuiko Takada | 177ccce | 2014-03-13 10:02:20 +0000 | [diff] [blame] | 129 | self.validate_response(schema_access.list_flavor_access, resp, body) |
Zhu Zhu | 705f24a | 2013-10-11 05:52:54 -0500 | [diff] [blame] | 130 | return resp, body['flavor_access'] |
| 131 | |
Mitsuhiko Yamazaki | f5f4da6 | 2013-03-29 17:40:03 +0900 | [diff] [blame] | 132 | 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) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 140 | resp, body = self.post('flavors/%s/action' % flavor_id, post_body) |
Mitsuhiko Yamazaki | f5f4da6 | 2013-03-29 17:40:03 +0900 | [diff] [blame] | 141 | 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) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 152 | resp, body = self.post('flavors/%s/action' % flavor_id, post_body) |
Mitsuhiko Yamazaki | f5f4da6 | 2013-03-29 17:40:03 +0900 | [diff] [blame] | 153 | body = json.loads(body) |
| 154 | return resp, body['flavor_access'] |