Example #1
0
    def on_message(self, message):
        """Hook called when a new message is received from the browser.

        If the message is a deployment request, start the deployment process.
        Otherwise the message is propagated to the Juju API server.
        Messages sent before the client connection to the Juju API server is
        established are queued for later delivery.
        """
        data = json_decode_dict(message)
        encoded = None
        if data is not None:
            # Handle deployment requests.
            if self.deployment.requested(data):
                return self.deployment.process_request(data)
            # Handle authentication requests.
            if not self.user.is_authenticated:
                new_data = self.auth.process_request(data)
                if new_data is None:
                    # The None marker indicates that a response was sent.
                    return
                elif new_data != data:
                    encoded = escape.json_encode(new_data)
                    message = encoded.decode('utf8')
            # Handle authentication token requests.
            if self.tokens.token_requested(data):
                return self.tokens.process_token_request(
                    data, self.user, wrap_write_message(self))
        # Propagate messages to the Juju API server.
        if encoded is None:
            encoded = message.encode('utf-8')
        if self.juju_connected:
            logging.debug(self._summary + 'client -> juju: {}'.format(encoded))
            return self.juju_connection.write_message(message)
        logging.debug(self._summary + 'client -> queue: {}'.format(encoded))
        self._juju_message_queue.append(message)
Example #2
0
    def on_message(self, message):
        """Hook called when a new message is received from the browser.

        If the message is a change set request, return the resulting changes.
        Otherwise return a not implemented response.
        """
        data = json_decode_dict(message)
        if data is None:
            return
        if self.changeset.requested(data):
            return self.changeset.process_request(data)
        self.write_message({'Error': 'not implemented (sandbox mode)'})
Example #3
0
    def on_juju_message(self, message):
        """Hook called when a new message is received from the Juju API server.

        The message is propagated to the browser.
        """
        if message is None:
            # The Juju API closed the connection.
            return self.on_juju_close()
        data = json_decode_dict(message)
        if (data is not None) and self.auth.in_progress():
            encoded = escape.json_encode(self.auth.process_response(data))
            message = encoded.decode('utf8')
        else:
            encoded = message.encode('utf-8')
        logging.debug(self._summary + 'juju -> client: {}'.format(encoded))
        self.write_message(message)
Example #4
0
    def on_juju_message(self, message):
        """Hook called when a new message is received from the Juju API server.

        The message is propagated to the browser.
        """
        if message is None:
            # The Juju API closed the connection.
            return self.on_juju_close()
        data = json_decode_dict(message)
        if (data is not None) and self.auth.in_progress():
            encoded = escape.json_encode(
                self.auth.process_response(data))
            message = encoded.decode('utf8')
        else:
            encoded = message.encode('utf-8')
        logging.debug(self._summary + 'juju -> client: {}'.format(encoded))
        self.write_message(message)
Example #5
0
    def on_message(self, message):
        """Hook called when a new message is received from the browser.

        If the message is a change set request, return the resulting changes.
        If the message is a deployment request, start the deployment process.
        Otherwise the message is propagated to the Juju API server.
        Messages sent before the client connection to the Juju API server is
        established are queued for later delivery.
        """
        data = json_decode_dict(message)
        encoded = None
        if data is not None:
            # Handle change set requests.
            if self.changeset.requested(data):
                return self.changeset.process_request(data)
            # Handle deployment requests.
            if self.deployment.requested(data):
                return self.deployment.process_request(data)
            # Handle authentication requests.
            if not self.user.is_authenticated:
                new_data = self.auth.process_request(data)
                if new_data is None:
                    # The None marker indicates that a response was sent.
                    return
                elif new_data != data:
                    encoded = escape.json_encode(new_data)
                    message = encoded.decode('utf8')
            # Handle authentication token requests.
            if self.tokens.token_requested(data):
                return self.tokens.process_token_request(
                    data, self.user, wrap_write_message(self))
        # Propagate messages to the Juju API server.
        if encoded is None:
            encoded = message.encode('utf-8')
        if self.juju_connected:
            logging.debug(self._summary + 'client -> juju: {}'.format(encoded))
            return self.juju_connection.write_message(message)
        logging.debug(self._summary + 'client -> queue: {}'.format(encoded))
        self._juju_message_queue.append(message)
Example #6
0
 def test_invalid_type(self):
     # If the resulting object is not a dict-like object, a warning is
     # logged and None is returned.
     expected_log = 'JSON decoder: message is not a dict: \'"not-a-dict"\''
     with ExpectLog('', expected_log, required=True):
         self.assertIsNone(utils.json_decode_dict('"not-a-dict"'))
Example #7
0
 def test_invalid_json(self):
     # If the message is not a valid JSON string, a warning is logged and
     # None is returned.
     expected_log = "JSON decoder: message is not valid JSON: 'not-json'"
     with ExpectLog('', expected_log, required=True):
         self.assertIsNone(utils.json_decode_dict('not-json'))
Example #8
0
 def test_valid(self):
     # A valid JSON is decoded without errors.
     data = {'key1': 'value1', 'key2': 'value2'}
     message = json.dumps(data)
     self.assertEqual(data, utils.json_decode_dict(message))
Example #9
0
 def test_invalid_type(self):
     # If the resulting object is not a dict-like object, a warning is
     # logged and None is returned.
     expected_log = 'JSON decoder: message is not a dict: \'"not-a-dict"\''
     with ExpectLog('', expected_log, required=True):
         self.assertIsNone(utils.json_decode_dict('"not-a-dict"'))
Example #10
0
 def test_invalid_json(self):
     # If the message is not a valid JSON string, a warning is logged and
     # None is returned.
     expected_log = "JSON decoder: message is not valid JSON: 'not-json'"
     with ExpectLog('', expected_log, required=True):
         self.assertIsNone(utils.json_decode_dict('not-json'))
Example #11
0
 def test_valid(self):
     # A valid JSON is decoded without errors.
     data = {'key1': 'value1', 'key2': 'value2'}
     message = json.dumps(data)
     self.assertEqual(data, utils.json_decode_dict(message))
Example #12
0
 def test_valid(self):
     # A valid JSON is decoded without errors.
     data = {"key1": "value1", "key2": "value2"}
     message = json.dumps(data)
     self.assertEqual(data, utils.json_decode_dict(message))