@@ -93,6 +93,88 @@ def find( | |||
| 93 | 93 | ||
| 94 | 94 | return ret | |
| 95 | 95 | ||
| 96 | + # Flaoting IPs | ||
| 97 | + | ||
| 98 | + def floating_ip_create( | ||
| 99 | + self, | ||
| 100 | + pool=None, | ||
| 101 | + ): | ||
| 102 | + """Create a new floating ip | ||
| 103 | + | ||
| 104 | + https://developer.openstack.org/api-ref/compute/#create-allocate-floating-ip-address | ||
| 105 | + | ||
| 106 | + :param pool: Name of floating IP pool | ||
| 107 | + """ | ||
| 108 | + | ||
| 109 | + url = "/os-floating-ips" | ||
| 110 | + | ||
| 111 | + try: | ||
| 112 | + return self.create( | ||
| 113 | + url, | ||
| 114 | + json={'pool': pool}, | ||
| 115 | + )['floating_ip'] | ||
| 116 | + except ( | ||
| 117 | + ksa_exceptions.NotFound, | ||
| 118 | + ksa_exceptions.BadRequest, | ||
| 119 | + ): | ||
| 120 | + msg = _("%s not found") % pool | ||
| 121 | + raise exceptions.NotFound(msg) | ||
| 122 | + | ||
| 123 | + def floating_ip_delete( | ||
| 124 | + self, | ||
| 125 | + floating_ip_id=None, | ||
| 126 | + ): | ||
| 127 | + """Delete a floating IP | ||
| 128 | + | ||
| 129 | + https://developer.openstack.org/api-ref/compute/#delete-deallocate-floating-ip-address | ||
| 130 | + | ||
| 131 | + :param string security_group: | ||
| 132 | + Floating IP ID | ||
| 133 | + """ | ||
| 134 | + | ||
| 135 | + url = "/os-floating-ips" | ||
| 136 | + | ||
| 137 | + if floating_ip_id is not None: | ||
| 138 | + return self.delete('/%s/%s' % (url, floating_ip_id)) | ||
| 139 | + | ||
| 140 | + return None | ||
| 141 | + | ||
| 142 | + def floating_ip_find( | ||
| 143 | + self, | ||
| 144 | + floating_ip=None, | ||
| 145 | + ): | ||
| 146 | + """Return a security group given name or ID | ||
| 147 | + | ||
| 148 | + https://developer.openstack.org/api-ref/compute/#list-floating-ip-addresses | ||
| 149 | + | ||
| 150 | + :param string floating_ip: | ||
| 151 | + Floating IP address | ||
| 152 | + :returns: A dict of the floating IP attributes | ||
| 153 | + """ | ||
| 154 | + | ||
| 155 | + url = "/os-floating-ips" | ||
| 156 | + | ||
| 157 | + return self.find( | ||
| 158 | + url, | ||
| 159 | + attr='ip', | ||
| 160 | + value=floating_ip, | ||
| 161 | + ) | ||
| 162 | + | ||
| 163 | + def floating_ip_list( | ||
| 164 | + self, | ||
| 165 | + ): | ||
| 166 | + """Get floating IPs | ||
| 167 | + | ||
| 168 | + https://developer.openstack.org/api-ref/compute/#show-floating-ip-address-details | ||
| 169 | + | ||
| 170 | + :returns: | ||
| 171 | + list of security groups names | ||
| 172 | + """ | ||
| 173 | + | ||
| 174 | + url = "/os-floating-ips" | ||
| 175 | + | ||
| 176 | + return self.list(url)["floating_ips"] | ||
| 177 | + | ||
| 96 | 178 | # Security Groups | |
| 97 | 179 | ||
| 98 | 180 | def security_group_create( | |
@@ -190,9 +190,9 @@ def take_action_network(self, client, parsed_args): | |||
| 190 | 190 | return (display_columns, data) | |
| 191 | 191 | ||
| 192 | 192 | def take_action_compute(self, client, parsed_args): | |
| 193 | - obj = client.floating_ips.create(parsed_args.network) | ||
| 194 | - columns = _get_columns(obj._info) | ||
| 195 | - data = utils.get_dict_properties(obj._info, columns) | ||
| 193 | + obj = client.api.floating_ip_create(parsed_args.network) | ||
| 194 | + columns = _get_columns(obj) | ||
| 195 | + data = utils.get_dict_properties(obj, columns) | ||
| 196 | 196 | return (columns, data) | |
| 197 | 197 | ||
| 198 | 198 | ||
@@ -245,13 +245,7 @@ def take_action_network(self, client, parsed_args): | |||
| 245 | 245 | client.delete_ip(obj) | |
| 246 | 246 | ||
| 247 | 247 | def take_action_compute(self, client, parsed_args): | |
| 248 | - obj = utils.find_resource(client.floating_ips, self.r) | ||
| 249 | - client.floating_ips.delete(obj.id) | ||
| 250 | - | ||
| 251 | - def take_action(self, parsed_args): | ||
| 252 | - """Implements a naive cache for the list of floating IPs""" | ||
| 253 | - | ||
| 254 | - super(DeleteFloatingIP, self).take_action(parsed_args) | ||
| 248 | + client.api.floating_ip_delete(self.r) | ||
| 255 | 249 | ||
| 256 | 250 | ||
| 257 | 251 | class DeleteIPFloating(DeleteFloatingIP): | |
@@ -414,10 +408,10 @@ def take_action_compute(self, client, parsed_args): | |||
| 414 | 408 | 'Pool', | |
| 415 | 409 | ) | |
| 416 | 410 | ||
| 417 | - data = client.floating_ips.list() | ||
| 411 | + data = client.api.floating_ip_list() | ||
| 418 | 412 | ||
| 419 | 413 | return (headers, | |
| 420 | - (utils.get_item_properties( | ||
| 414 | + (utils.get_dict_properties( | ||
| 421 | 415 | s, columns, | |
| 422 | 416 | formatters={}, | |
| 423 | 417 | ) for s in data)) | |
@@ -510,12 +504,9 @@ def take_action_network(self, client, parsed_args): | |||
| 510 | 504 | return (display_columns, data) | |
| 511 | 505 | ||
| 512 | 506 | def take_action_compute(self, client, parsed_args): | |
| 513 | - obj = utils.find_resource( | ||
| 514 | - client.floating_ips, | ||
| 515 | - parsed_args.floating_ip, | ||
| 516 | - ) | ||
| 517 | - columns = _get_columns(obj._info) | ||
| 518 | - data = utils.get_dict_properties(obj._info, columns) | ||
| 507 | + obj = client.api.floating_ip_find(parsed_args.floating_ip) | ||
| 508 | + columns = _get_columns(obj) | ||
| 509 | + data = utils.get_dict_properties(obj, columns) | ||
| 519 | 510 | return (columns, data) | |
| 520 | 511 | ||
| 521 | 512 | ||
@@ -34,6 +34,117 @@ def setUp(self): | |||
| 34 | 34 | self.requests_mock = self.useFixture(fixture.Fixture()) | |
| 35 | 35 | ||
| 36 | 36 | ||
| 37 | + class TestFloatingIP(TestComputeAPIv2): | ||
| 38 | + | ||
| 39 | + FAKE_FLOATING_IP_RESP = { | ||
| 40 | + 'id': 1, | ||
| 41 | + 'ip': '203.0.113.11', # TEST-NET-3 | ||
| 42 | + 'fixed_ip': '198.51.100.11', # TEST-NET-2 | ||
| 43 | + 'pool': 'nova', | ||
| 44 | + 'instance_id': None, | ||
| 45 | + } | ||
| 46 | + FAKE_FLOATING_IP_RESP_2 = { | ||
| 47 | + 'id': 2, | ||
| 48 | + 'ip': '203.0.113.12', # TEST-NET-3 | ||
| 49 | + 'fixed_ip': '198.51.100.12', # TEST-NET-2 | ||
| 50 | + 'pool': 'nova', | ||
| 51 | + 'instance_id': None, | ||
| 52 | + } | ||
| 53 | + LIST_FLOATING_IP_RESP = [ | ||
| 54 | + FAKE_FLOATING_IP_RESP, | ||
| 55 | + FAKE_FLOATING_IP_RESP_2, | ||
| 56 | + ] | ||
| 57 | + | ||
| 58 | + def test_floating_ip_create(self): | ||
| 59 | + self.requests_mock.register_uri( | ||
| 60 | + 'POST', | ||
| 61 | + FAKE_URL + '/os-floating-ips', | ||
| 62 | + json={'floating_ip': self.FAKE_FLOATING_IP_RESP}, | ||
| 63 | + status_code=200, | ||
| 64 | + ) | ||
| 65 | + ret = self.api.floating_ip_create('nova') | ||
| 66 | + self.assertEqual(self.FAKE_FLOATING_IP_RESP, ret) | ||
| 67 | + | ||
| 68 | + def test_floating_ip_create_not_found(self): | ||
| 69 | + self.requests_mock.register_uri( | ||
| 70 | + 'POST', | ||
| 71 | + FAKE_URL + '/os-floating-ips', | ||
| 72 | + status_code=404, | ||
| 73 | + ) | ||
| 74 | + self.assertRaises( | ||
| 75 | + osc_lib_exceptions.NotFound, | ||
| 76 | + self.api.floating_ip_create, | ||
| 77 | + 'not-nova', | ||
| 78 | + ) | ||
| 79 | + | ||
| 80 | + def test_floating_ip_delete(self): | ||
| 81 | + self.requests_mock.register_uri( | ||
| 82 | + 'DELETE', | ||
| 83 | + FAKE_URL + '/os-floating-ips/1', | ||
| 84 | + status_code=202, | ||
| 85 | + ) | ||
| 86 | + ret = self.api.floating_ip_delete('1') | ||
| 87 | + self.assertEqual(202, ret.status_code) | ||
| 88 | + self.assertEqual("", ret.text) | ||
| 89 | + | ||
| 90 | + def test_floating_ip_delete_none(self): | ||
| 91 | + ret = self.api.floating_ip_delete() | ||
| 92 | + self.assertIsNone(ret) | ||
| 93 | + | ||
| 94 | + def test_floating_ip_find_id(self): | ||
| 95 | + self.requests_mock.register_uri( | ||
| 96 | + 'GET', | ||
| 97 | + FAKE_URL + '/os-floating-ips/1', | ||
| 98 | + json={'floating_ip': self.FAKE_FLOATING_IP_RESP}, | ||
| 99 | + status_code=200, | ||
| 100 | + ) | ||
| 101 | + ret = self.api.floating_ip_find('1') | ||
| 102 | + self.assertEqual(self.FAKE_FLOATING_IP_RESP, ret) | ||
| 103 | + | ||
| 104 | + def test_floating_ip_find_ip(self): | ||
| 105 | + self.requests_mock.register_uri( | ||
| 106 | + 'GET', | ||
| 107 | + FAKE_URL + '/os-floating-ips/' + self.FAKE_FLOATING_IP_RESP['ip'], | ||
| 108 | + status_code=404, | ||
| 109 | + ) | ||
| 110 | + self.requests_mock.register_uri( | ||
| 111 | + 'GET', | ||
| 112 | + FAKE_URL + '/os-floating-ips', | ||
| 113 | + json={'floating_ips': self.LIST_FLOATING_IP_RESP}, | ||
| 114 | + status_code=200, | ||
| 115 | + ) | ||
| 116 | + ret = self.api.floating_ip_find(self.FAKE_FLOATING_IP_RESP['ip']) | ||
| 117 | + self.assertEqual(self.FAKE_FLOATING_IP_RESP, ret) | ||
| 118 | + | ||
| 119 | + def test_floating_ip_find_not_found(self): | ||
| 120 | + self.requests_mock.register_uri( | ||
| 121 | + 'GET', | ||
| 122 | + FAKE_URL + '/os-floating-ips/1.2.3.4', | ||
| 123 | + status_code=404, | ||
| 124 | + ) | ||
| 125 | + self.requests_mock.register_uri( | ||
| 126 | + 'GET', | ||
| 127 | + FAKE_URL + '/os-floating-ips', | ||
| 128 | + json={'floating_ips': self.LIST_FLOATING_IP_RESP}, | ||
| 129 | + status_code=200, | ||
| 130 | + ) | ||
| 131 | + self.assertRaises( | ||
| 132 | + osc_lib_exceptions.NotFound, | ||
| 133 | + self.api.floating_ip_find, | ||
| 134 | + '1.2.3.4', | ||
| 135 | + ) | ||
| 136 | + | ||
| 137 | + def test_floating_ip_list(self): | ||
| 138 | + self.requests_mock.register_uri( | ||
| 139 | + 'GET', | ||
| 140 | + FAKE_URL + '/os-floating-ips', | ||
| 141 | + json={'floating_ips': self.LIST_FLOATING_IP_RESP}, | ||
| 142 | + status_code=200, | ||
| 143 | + ) | ||
| 144 | + ret = self.api.floating_ip_list() | ||
| 145 | + self.assertEqual(self.LIST_FLOATING_IP_RESP, ret) | ||
| 146 | + | ||
| 147 | + | ||
| 37 | 148 | class TestSecurityGroup(TestComputeAPIv2): | |
| 38 | 149 | ||
| 39 | 150 | FAKE_SECURITY_GROUP_RESP = { | |
@@ -1016,11 +1016,7 @@ def create_one_floating_ip(attrs=None): | |||
| 1016 | 1016 | # Overwrite default attributes. | |
| 1017 | 1017 | floating_ip_attrs.update(attrs) | |
| 1018 | 1018 | ||
| 1019 | - floating_ip = fakes.FakeResource( | ||
| 1020 | - info=copy.deepcopy(floating_ip_attrs), | ||
| 1021 | - loaded=True) | ||
| 1022 | - | ||
| 1023 | - return floating_ip | ||
| 1019 | + return floating_ip_attrs | ||
| 1024 | 1020 | ||
| 1025 | 1021 | @staticmethod | |
| 1026 | 1022 | def create_floating_ips(attrs=None, count=2): | |
0 commit comments