示例#1
0
    def show(self, args):
        """Displays trace-results by given trace id in HTML or JSON format."""

        trace = None

        if os.path.exists(args.trace):
            trace = json.load(open(args.trace))
        else:
            try:
                import ceilometerclient.client
                import ceilometerclient.exc
                import ceilometerclient.shell
            except ImportError:
                raise ImportError(
                    "To use this command, you should install "
                    "'ceilometerclient' manually. Use command:\n "
                    "'pip install ceilometerclient'.")
            try:
                client = ceilometerclient.client.get_client(
                    args.ceilometer_api_version, **args.__dict__)
                notifications = ceiloparser.get_notifications(
                    client, args.trace)
            except Exception as e:
                if hasattr(e, "http_status") and e.http_status == 401:
                    msg = "Invalid OpenStack Identity credentials."
                else:
                    msg = "Something has gone wrong. See logs for more details"
                raise exc.CommandError(msg)

            if notifications:
                trace = ceiloparser.parse_notifications(notifications)

        if not trace:
            msg = ("Trace with UUID %s not found. "
                   "There are 3 possible reasons: \n"
                   " 1) You are using not admin credentials\n"
                   " 2) You specified wrong trace id\n"
                   " 3) You specified wrong HMAC Key in original calling\n"
                   " 4) Ceilometer didn't enable profiler notification topic" %
                   args.trace)
            raise exc.CommandError(msg)

        if args.use_json:
            output = json.dumps(trace)
        elif args.use_html:
            with open(os.path.join(os.path.dirname(__file__),
                                   "template.html")) as html_template:
                output = html_template.read().replace(
                    "$DATA", json.dumps(trace, indent=2))
        else:
            raise exc.CommandError("You should choose one of the following "
                                   "output-formats: --json or --html.")

        if args.file_name:
            with open(args.file_name, "w+") as output_file:
                output_file.write(output)
        else:
            print(output)
示例#2
0
    def show(self, args):
        """Displays trace-results by given trace id in HTML or JSON format."""

        trace = None

        if os.path.exists(args.trace):
            trace = json.load(open(args.trace))
        else:
            try:
                import ceilometerclient.client
                import ceilometerclient.exc
                import ceilometerclient.shell
            except ImportError:
                raise ImportError(
                    "To use this command, you should install "
                    "'ceilometerclient' manually. Use command:\n "
                    "'pip install ceilometerclient'.")
            try:
                client = ceilometerclient.client.get_client(
                    args.ceilometer_api_version, **args.__dict__)
                notifications = ceiloparser.get_notifications(
                    client, args.trace)
            except Exception as e:
                if hasattr(e, "http_status") and e.http_status == 401:
                    msg = "Invalid OpenStack Identity credentials."
                else:
                    msg = "Something has gone wrong. See logs for more details"
                raise exc.CommandError(msg)

            if notifications:
                trace = ceiloparser.parse_notifications(notifications)

        if not trace:
            msg = ("Trace with UUID %s not found. "
                   "There are 3 possible reasons: \n"
                   " 1) You are using not admin credentials\n"
                   " 2) You specified wrong trace id\n"
                   " 3) You specified wrong HMAC Key in original calling"
                   % args.trace)
            raise exc.CommandError(msg)

        if args.use_json:
            output = json.dumps(trace)
        elif args.use_html:
            with open(os.path.join(os.path.dirname(__file__),
                                   "template.html")) as html_template:
                output = html_template.read().replace(
                    "$DATA", json.dumps(trace, indent=2))
        else:
            raise exc.CommandError("You should choose one of the following "
                                   "output-formats: --json or --html.")

        if args.file_name:
            with open(args.file_name, "w+") as output_file:
                output_file.write(output)
        else:
            print (output)
    def test_get_notifications(self):
        mock_ceil_client = mock.MagicMock()
        results = [mock.MagicMock(), mock.MagicMock()]
        mock_ceil_client.events.list.return_value = results
        base_id = "10"

        result = ceilometer.get_notifications(mock_ceil_client, base_id)

        expected_filter = [{"field": "base_id", "op": "eq", "value": base_id}]
        mock_ceil_client.events.list.assert_called_once_with(expected_filter, limit=100000)
        self.assertEqual(result, [results[0].to_dict(), results[1].to_dict()])
示例#4
0
    def test_get_notifications(self):
        mock_ceil_client = mock.MagicMock()
        results = [mock.MagicMock(), mock.MagicMock()]
        mock_ceil_client.events.list.return_value = results
        base_id = "10"

        result = ceilometer.get_notifications(mock_ceil_client, base_id)

        expected_filter = [{"field": "base_id", "op": "eq", "value": base_id}]
        mock_ceil_client.events.list.assert_called_once_with(expected_filter,
                                                             limit=100000)
        self.assertEqual(result, [results[0].to_dict(), results[1].to_dict()])
    def test_get_notifications(self):
        mock_ceil_client = mock.MagicMock()
        results = [mock.MagicMock(), mock.MagicMock()]
        mock_ceil_client.query_samples.query.return_value = results
        base_id = "10"

        result = ceilometer.get_notifications(mock_ceil_client, base_id)

        expected_filter = '{"=": {"resource_id": "profiler-%s"}}' % base_id
        mock_ceil_client.query_samples.query.assert_called_once_with(
            expected_filter, None, None)
        self.assertEqual(result, [results[0].to_dict(), results[1].to_dict()])