From 103056ed33a5191f76505e1564b2d3fbde8f4856 Mon Sep 17 00:00:00 2001 From: Masahito Muroi Date: Thu, 25 Aug 2016 21:50:58 +0900 Subject: [PATCH] Support push rows to datasources congress client supports push row data into push type datasource. Example: $ openstack congress datasource row update \ $ openstack congress datasource row update push \ data '[ ["data1", "data2"], ["data3", "data4"]]' Change-Id: Icf3b0b116b20ef76957c0d6fc93c38d803e3aae0 --- congressclient/osc/v1/datasource.py | 32 ++++++++++++++++++++++ congressclient/tests/v1/test_datasource.py | 26 ++++++++++++++++++ congressclient/v1/client.py | 13 +++++++++ setup.cfg | 1 + 4 files changed, 72 insertions(+) diff --git a/congressclient/osc/v1/datasource.py b/congressclient/osc/v1/datasource.py index 72fbe7f..1814f67 100644 --- a/congressclient/osc/v1/datasource.py +++ b/congressclient/osc/v1/datasource.py @@ -18,6 +18,7 @@ from cliff import command from cliff import lister from cliff import show from oslo_log import log as logging +from oslo_serialization import jsonutils import six from congressclient.common import parseractions @@ -322,6 +323,37 @@ class DeleteDatasource(command.Command): client.delete_datasource(datasource_id) +class UpdateDatasourceRow(command.Command): + """Update rows to a datasource table.""" + + log = logging.getLogger(__name__ + '.UpdateDatasourceRow') + + def get_parser(self, prog_name): + parser = super(UpdateDatasourceRow, self).get_parser(prog_name) + parser.add_argument( + 'datasource', + metavar="", + help="Name or ID of the datasource to Update") + parser.add_argument( + 'table', + metavar="
", + help="Name or ID of the table to Update") + parser.add_argument( + 'rows', + type=jsonutils.loads, + metavar="", + help=("List of Rows should be formmated json style." + " ex. [[row1], [row2]]")) + return parser + + def take_action(self, parsed_args): + self.log.debug('take_action(%s)' % parsed_args) + client = self.app.client_manager.congressclient + body = parsed_args.rows + client.update_datasource_rows( + parsed_args.datasource, parsed_args.table, body) + + class DatasourceRequestRefresh(command.Command): """Trigger a datasource to poll.""" diff --git a/congressclient/tests/v1/test_datasource.py b/congressclient/tests/v1/test_datasource.py index abdf14d..9928758 100644 --- a/congressclient/tests/v1/test_datasource.py +++ b/congressclient/tests/v1/test_datasource.py @@ -12,6 +12,7 @@ # import mock +from oslo_serialization import jsonutils from congressclient.common import utils from congressclient.osc.v1 import datasource @@ -361,6 +362,31 @@ class TestDeleteDatasourceDriver(common.TestCongressBase): self.assertIsNone(result) +class TestUpdateDatasourceRow(common.TestCongressBase): + + def test_update_datasource_row(self): + driver = 'push' + table_name = 'table' + rows = [["data1", "data2"], + ["data3", "data4"]] + + arglist = [driver, table_name, jsonutils.dumps(rows)] + verifylist = [('datasource', driver), + ('table', table_name), + ('rows', rows)] + + mocker = mock.Mock(return_value=None) + self.app.client_manager.congressclient.update_datasource_rows = mocker + self.app.client_manager.congressclient.list_datasources = mock.Mock() + + cmd = datasource.UpdateDatasourceRow(self.app, self.namespace) + parsed_args = self.check_parser(cmd, arglist, verifylist) + with mock.patch.object(utils, 'get_resource_id_from_name', + return_value="push"): + cmd.take_action(parsed_args) + mocker.assert_called_with(driver, table_name, rows) + + class TestDatasourceRequestRefresh(common.TestCongressBase): def test_datasource_request_refresh(self): diff --git a/congressclient/v1/client.py b/congressclient/v1/client.py index 4234539..53a692d 100644 --- a/congressclient/v1/client.py +++ b/congressclient/v1/client.py @@ -138,6 +138,19 @@ class Client(object): (datasource_name, table_name)) return body + def update_datasource_rows(self, datasource_name, table_name, body=None): + """Update rows in a table of a datasource. + + Args: + datasource_name: Name or id of the datasource + table_name: Table name for updating + body: Rows for update. + """ + resp, body = self.httpclient.put(self.datasource_rows % + (datasource_name, table_name), + body=body) + return body + def list_datasource_status(self, datasource_name): resp, body = self.httpclient.get(self.datasource_status % datasource_name) diff --git a/setup.cfg b/setup.cfg index 48fa180..ebcbbcd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -44,6 +44,7 @@ openstack.congressclient.v1 = congress_datasource_request-refresh = congressclient.osc.v1.datasource:DatasourceRequestRefresh congress_datasource_table_list = congressclient.osc.v1.datasource:ListDatasourceTables congress_datasource_row_list = congressclient.osc.v1.datasource:ListDatasourceRows + congress_datasource_row_update = congressclient.osc.v1.datasource:UpdateDatasourceRow congress_datasource_status_show = congressclient.osc.v1.datasource:ShowDatasourceStatus congress_datasource_actions_show= congressclient.osc.v1.datasource:ShowDatasourceActions congress_datasource_schema_show = congressclient.osc.v1.datasource:ShowDatasourceSchema