diff --git a/examples/list.py b/examples/list.py index 0246b6484..f1214d767 100644 --- a/examples/list.py +++ b/examples/list.py @@ -19,7 +19,10 @@ from examples import session def run_list(opts): sess = session.make_session(opts) cls = common.find_resource_cls(opts) - for obj in cls.list(sess): + path_args = None + if opts.data: + path_args = common.get_data_option(opts) + for obj in cls.list(sess, path_args=path_args): print(str(obj)) return diff --git a/openstack/compute/v2/server.py b/openstack/compute/v2/server.py index 69480c480..1a76b9501 100644 --- a/openstack/compute/v2/server.py +++ b/openstack/compute/v2/server.py @@ -11,6 +11,7 @@ # under the License. from openstack.compute import compute_service +from openstack.compute.v2 import server_ip from openstack import resource from openstack import utils @@ -45,6 +46,11 @@ class Server(resource.Resource): updated = resource.prop('updated') user_id = resource.prop('user_id') + def ips(self, session): + """Get server IPs.""" + path_args = {'server_id': self.id} + return server_ip.ServerIP.list(session, path_args=path_args) + def action(self, session, body): """Preform server actions given the message body.""" url = utils.urljoin(self.base_path, self.id, 'action') diff --git a/openstack/compute/v2/server_ip.py b/openstack/compute/v2/server_ip.py new file mode 100644 index 000000000..6f6d5e1cb --- /dev/null +++ b/openstack/compute/v2/server_ip.py @@ -0,0 +1,56 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import six + +from openstack.compute import compute_service +from openstack import resource + + +class ServerIP(resource.Resource): + resource_key = 'server_ip' + resources_key = 'server_ips' + base_path = '/servers/%(server_id)s/ips' + service = compute_service.ComputeService() + + # capabilities + allow_list = True + + # Properties + addr = resource.prop('addr') + network_label = resource.prop('network_label') + server_id = resource.prop('server_id') + version = resource.prop('version') + + @property + def id(self): + try: + val = self.addr + except AttributeError: + val = None + return val + + @classmethod + def list(cls, session, path_args=None, **params): + url = cls.base_path % path_args + resp = session.get(url, service=cls.service, params=params) + ray = [] + for network_label, addresses in six.iteritems(resp.body['addresses']): + for address in addresses: + record = { + 'server_id': path_args['server_id'], + 'network_label': network_label, + 'version': address['version'], + 'addr': address['addr'], + } + ray.append(cls.existing(**record)) + return ray diff --git a/openstack/tests/compute/v2/test_server_ip.py b/openstack/tests/compute/v2/test_server_ip.py new file mode 100644 index 000000000..07022917c --- /dev/null +++ b/openstack/tests/compute/v2/test_server_ip.py @@ -0,0 +1,115 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock +import testtools + +from openstack.compute.v2 import server_ip + +IDENTIFIER = 'IDENTIFIER' +EXAMPLE = { + 'addr': '1', + 'network_label': '2', + 'server_id': '3', + 'version': '4', +} +BODY = { + "addresses": { + "public": [ + { + "version": 4, + "addr": "67.23.10.132" + }, + { + "version": 6, + "addr": "::babe:67.23.10.132" + }, + { + "version": 4, + "addr": "67.23.10.131" + }, + { + "version": 6, + "addr": "::babe:4317:0A83" + } + ], + "private": [ + { + "version": 4, + "addr": "10.176.42.16" + }, + { + "version": 6, + "addr": "::babe:10.176.42.16" + } + ] + } +} + + +class TestServerIP(testtools.TestCase): + + def test_basic(self): + sot = server_ip.ServerIP() + self.assertEqual('server_ip', sot.resource_key) + self.assertEqual('server_ips', sot.resources_key) + self.assertEqual('/servers/%(server_id)s/ips', sot.base_path) + self.assertEqual('compute', sot.service.service_type) + self.assertFalse(sot.allow_create) + self.assertFalse(sot.allow_retrieve) + self.assertFalse(sot.allow_update) + self.assertFalse(sot.allow_delete) + self.assertTrue(sot.allow_list) + + def test_make_it(self): + sot = server_ip.ServerIP(EXAMPLE) + self.assertEqual(EXAMPLE['addr'], sot.id) + self.assertEqual(EXAMPLE['addr'], sot.addr) + self.assertEqual(EXAMPLE['network_label'], sot.network_label) + self.assertEqual(EXAMPLE['server_id'], sot.server_id) + self.assertEqual(EXAMPLE['version'], sot.version) + + def test_list(self): + sess = mock.Mock() + resp = mock.Mock() + resp.body = BODY + sess.get = mock.Mock(return_value=resp) + path_args = {'server_id': IDENTIFIER} + + caps = server_ip.ServerIP.list(sess, path_args=path_args) + + caps = sorted(caps, key=lambda cap: cap.id) + self.assertEqual(6, len(caps)) + self.assertEqual('10.176.42.16', caps[0].addr) + self.assertEqual('private', caps[0].network_label) + self.assertEqual(IDENTIFIER, caps[0].server_id) + self.assertEqual(4, caps[0].version) + self.assertEqual('67.23.10.131', caps[1].addr) + self.assertEqual('public', caps[1].network_label) + self.assertEqual(IDENTIFIER, caps[1].server_id) + self.assertEqual(4, caps[1].version) + self.assertEqual('67.23.10.132', caps[2].addr) + self.assertEqual('public', caps[2].network_label) + self.assertEqual(IDENTIFIER, caps[2].server_id) + self.assertEqual(4, caps[2].version) + self.assertEqual('::babe:10.176.42.16', caps[3].addr) + self.assertEqual('private', caps[3].network_label) + self.assertEqual(IDENTIFIER, caps[3].server_id) + self.assertEqual(6, caps[3].version) + self.assertEqual('::babe:4317:0A83', caps[4].addr) + self.assertEqual('public', caps[4].network_label) + self.assertEqual(IDENTIFIER, caps[4].server_id) + self.assertEqual(6, caps[4].version) + self.assertEqual('::babe:67.23.10.132', caps[5].addr) + self.assertEqual('public', caps[5].network_label) + self.assertEqual(IDENTIFIER, caps[5].server_id) + self.assertEqual(6, caps[5].version)