def deserialize_msg(msg):
    # NOTE(russellb): Hang on to your hats, this road is about to
    # get a little bumpy.
    #
    # Robustness Principle:
    #    "Be strict in what you send, liberal in what you accept."
    #
    # At this point we have to do a bit of guessing about what it
    # is we just received.  Here is the set of possibilities:
    #
    # 1) We received a dict.  This could be 2 things:
    #
    #   a) Inspect it to see if it looks like a standard message envelope.
    #      If so, great!
    #
    #   b) If it doesn't look like a standard message envelope, it could either
    #      be a notification, or a message from before we added a message
    #      envelope (referred to as version 1.0).
    #      Just return the message as-is.
    #
    # 2) It's any other non-dict type.  Just return it and hope for the best.
    #    This case covers return values from rpc.call() from before message
    #    envelopes were used.  (messages to call a method were always a dict)

    if not isinstance(msg, dict):
        # See #2 above.
        return msg

    base_envelope_keys = (_VERSION_KEY, _MESSAGE_KEY)
    if not all(map(lambda key: key in msg, base_envelope_keys)):
        #  See #1.b above.
        return msg

    # At this point we think we have the message envelope
    # format we were expecting. (#1.a above)

    if not utils.version_is_compatible(_RPC_ENVELOPE_VERSION,
                                       msg[_VERSION_KEY]):
        raise UnsupportedRpcEnvelopeVersion(version=msg[_VERSION_KEY])

    raw_msg = jsonutils.loads(msg[_MESSAGE_KEY])

    return raw_msg
def deserialize_msg(msg):
    # NOTE(russellb): Hang on to your hats, this road is about to
    # get a little bumpy.
    #
    # Robustness Principle:
    #    "Be strict in what you send, liberal in what you accept."
    #
    # At this point we have to do a bit of guessing about what it
    # is we just received.  Here is the set of possibilities:
    #
    # 1) We received a dict.  This could be 2 things:
    #
    #   a) Inspect it to see if it looks like a standard message envelope.
    #      If so, great!
    #
    #   b) If it doesn't look like a standard message envelope, it could either
    #      be a notification, or a message from before we added a message
    #      envelope (referred to as version 1.0).
    #      Just return the message as-is.
    #
    # 2) It's any other non-dict type.  Just return it and hope for the best.
    #    This case covers return values from rpc.call() from before message
    #    envelopes were used.  (messages to call a method were always a dict)

    if not isinstance(msg, dict):
        # See #2 above.
        return msg

    base_envelope_keys = (_VERSION_KEY, _MESSAGE_KEY)
    if not all(map(lambda key: key in msg, base_envelope_keys)):
        #  See #1.b above.
        return msg

    # At this point we think we have the message envelope
    # format we were expecting. (#1.a above)

    if not utils.version_is_compatible(_RPC_ENVELOPE_VERSION,
                                       msg[_VERSION_KEY]):
        raise UnsupportedRpcEnvelopeVersion(version=msg[_VERSION_KEY])

    raw_msg = jsonutils.loads(msg[_MESSAGE_KEY])

    return raw_msg
Beispiel #3
0
 def _check_version_cap(self, version):
     if not utils.version_is_compatible(self.version_cap, version):
         raise RPCVersionCapError(version=version,
                                  version_cap=self.version_cap)
Beispiel #4
0
 def test_version_is_compatible_no_rev_is_zero(self):
     self.assertTrue(utils.version_is_compatible('1.23.0', '1.23'))
Beispiel #5
0
 def test_version_is_compatible_older_rev(self):
     self.assertTrue(utils.version_is_compatible('1.24', '1.23.1'))
Beispiel #6
0
 def test_version_is_compatible_newer_minor(self):
     self.assertTrue(utils.version_is_compatible('1.24', '1.23'))
Beispiel #7
0
 def _is_compatible(target, version):
     endpoint_version = target.version or '1.0'
     return utils.version_is_compatible(endpoint_version, version)
Beispiel #8
0
 def _check_version_cap(self, version):
     if not utils.version_is_compatible(self.version_cap, version):
         raise RPCVersionCapError(version=version,
                                  version_cap=self.version_cap)
Beispiel #9
0
 def test_version_is_compatible_newer_minor(self):
     self.assertTrue(utils.version_is_compatible('1.24', '1.23'))
Beispiel #10
0
 def test_version_is_compatible_same(self):
     self.assertTrue(utils.version_is_compatible('1.23', '1.23'))
Beispiel #11
0
 def test_version_is_compatible_no_rev_is_zero(self):
     self.assertTrue(utils.version_is_compatible('1.23.0', '1.23'))
Beispiel #12
0
 def test_version_is_compatible_older_rev(self):
     self.assertTrue(utils.version_is_compatible('1.24', '1.23.1'))
Beispiel #13
0
 def test_version_is_compatible_newer_rev_both(self):
     self.assertFalse(utils.version_is_compatible('1.23.1', '1.23.2'))
Beispiel #14
0
 def test_version_is_compatible_major_difference2(self):
     self.assertFalse(utils.version_is_compatible('1.23', '2.23'))
Beispiel #15
0
 def test_version_is_compatible_older_minor(self):
     self.assertFalse(utils.version_is_compatible('1.22', '1.23'))
Beispiel #16
0
 def can_send_version(self, version=_marker):
     """Check to see if a version is compatible with the version cap."""
     version = self.target.version if version is self._marker else version
     return (not self.version_cap or
             utils.version_is_compatible(self.version_cap,
                                         self.target.version))
Beispiel #17
0
 def test_version_is_compatible_older_minor(self):
     self.assertFalse(utils.version_is_compatible('1.22', '1.23'))
Beispiel #18
0
 def test_version_is_compatible_major_difference2(self):
     self.assertFalse(utils.version_is_compatible('1.23', '2.23'))
Beispiel #19
0
 def can_send_version(self, version=_marker):
     """Check to see if a version is compatible with the version cap."""
     version = self.target.version if version is self._marker else version
     return (not self.version_cap or utils.version_is_compatible(
         self.version_cap, self.target.version))
Beispiel #20
0
 def test_version_is_compatible_newer_rev_both(self):
     self.assertFalse(utils.version_is_compatible('1.23.1', '1.23.2'))
Beispiel #21
0
 def _is_compatible(target, version):
     endpoint_version = target.version or '1.0'
     return utils.version_is_compatible(endpoint_version, version)
Beispiel #22
0
 def test_version_is_compatible_same(self):
     self.assertTrue(utils.version_is_compatible('1.23', '1.23'))