diff --git a/ironic_lib/json_rpc/client.py b/ironic_lib/json_rpc/client.py index e0aff7b8..d7099de7 100644 --- a/ironic_lib/json_rpc/client.py +++ b/ironic_lib/json_rpc/client.py @@ -82,18 +82,34 @@ class Client(object): return _can_send_version(version, self.version_cap) def prepare(self, topic, version=None): + """Prepare the client to transmit a request. + + :param topic: Topic which is being addressed. Typically this + is the hostname of the remote json-rpc service. + :param version: The RPC API version to utilize. + """ + host = topic.split('.', 1)[1] + # NOTE(TheJulia): Originally, I was worried about ip addresses being + # used, but looking at the topic split, it would have never really + # worked. + host, port = netutils.parse_host_port(host) return _CallContext( host, self.serializer, version=version, version_cap=self.version_cap, - allowed_exception_namespaces=self.allowed_exception_namespaces) + allowed_exception_namespaces=self.allowed_exception_namespaces, + port=port) class _CallContext(object): """Wrapper object for compatibility with oslo.messaging API.""" def __init__(self, host, serializer, version=None, version_cap=None, - allowed_exception_namespaces=()): + allowed_exception_namespaces=(), port=None): + if not port: + self.port = CONF.json_rpc.port + else: + self.port = int(port) self.host = host self.serializer = serializer self.version = version @@ -190,7 +206,7 @@ class _CallContext(object): scheme = 'https' url = '%s://%s:%d' % (scheme, netutils.escape_ipv6(self.host), - CONF.json_rpc.port) + self.port) LOG.debug("RPC %s to %s with %s", method, url, strutils.mask_dict_password(body)) try: @@ -200,7 +216,6 @@ class _CallContext(object): raise LOG.debug('RPC %s to %s returned %s', method, url, strutils.mask_password(result.text or '')) - if not cast: result = result.json() self._handle_error(result.get('error')) diff --git a/ironic_lib/tests/test_json_rpc.py b/ironic_lib/tests/test_json_rpc.py index 106e54a6..71cffc0a 100644 --- a/ironic_lib/tests/test_json_rpc.py +++ b/ironic_lib/tests/test_json_rpc.py @@ -395,6 +395,7 @@ class TestClient(base.IronicLibTestCase): } cctx = self.client.prepare('foo.2001:db8::1') self.assertEqual('2001:db8::1', cctx.host) + self.assertEqual(8089, cctx.port) result = cctx.call(self.context, 'do_something', answer=42) self.assertEqual(42, result) mock_session.return_value.post.assert_called_once_with( @@ -404,18 +405,35 @@ class TestClient(base.IronicLibTestCase): 'params': {'answer': 42, 'context': self.ctx_json}, 'id': self.context.request_id}) + def test_call_ipv6_success_rfc2732(self, mock_session): + response = mock_session.return_value.post.return_value + response.json.return_value = { + 'jsonrpc': '2.0', + 'result': 42 + } + cctx = self.client.prepare('foo.[2001:db8::1]:8192') + self.assertEqual('2001:db8::1', cctx.host) + result = cctx.call(self.context, 'do_something', answer=42) + self.assertEqual(42, result) + mock_session.return_value.post.assert_called_once_with( + 'http://[2001:db8::1]:8192', + json={'jsonrpc': '2.0', + 'method': 'do_something', + 'params': {'answer': 42, 'context': self.ctx_json}, + 'id': self.context.request_id}) + def test_call_success_with_version(self, mock_session): response = mock_session.return_value.post.return_value response.json.return_value = { 'jsonrpc': '2.0', 'result': 42 } - cctx = self.client.prepare('foo.example.com', version='1.42') + cctx = self.client.prepare('foo.example.com:8192', version='1.42') self.assertEqual('example.com', cctx.host) result = cctx.call(self.context, 'do_something', answer=42) self.assertEqual(42, result) mock_session.return_value.post.assert_called_once_with( - 'http://example.com:8089', + 'http://example.com:8192', json={'jsonrpc': '2.0', 'method': 'do_something', 'params': {'answer': 42, 'context': self.ctx_json, diff --git a/releasenotes/notes/add-port-to-rpc-client-2f2f0cd60547843f.yaml b/releasenotes/notes/add-port-to-rpc-client-2f2f0cd60547843f.yaml new file mode 100644 index 00000000..3a7b6ec7 --- /dev/null +++ b/releasenotes/notes/add-port-to-rpc-client-2f2f0cd60547843f.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Adds the capability for the ``json_rpc`` client to identify and utilize + a specific port from the supplied ``topic`` field as opposed to the + default configured port.