Adds batch write command. Refactoring
This commit is contained in:
parent
6459ed5df1
commit
53fcf4e58a
|
@ -425,7 +425,9 @@ class UpdateCommand(MagnetoDBCommand, show.ShowOne):
|
|||
if path in data:
|
||||
data = data[path]
|
||||
else:
|
||||
return {}
|
||||
return {'': ''}
|
||||
if not data:
|
||||
return {'': ''}
|
||||
return data
|
||||
|
||||
def run(self, parsed_args):
|
||||
|
@ -514,10 +516,13 @@ class ListCommand(MagnetoDBCommand, lister.Lister):
|
|||
return data
|
||||
|
||||
def _get_resource(self, data):
|
||||
collection = _get_resource_plural(self.resource_path[0])
|
||||
data = data[collection]
|
||||
for path in self.resource_path[1:]:
|
||||
data = data[path]
|
||||
for path in self.resource_path:
|
||||
if path in data:
|
||||
data = data[path]
|
||||
else:
|
||||
return []
|
||||
if not data:
|
||||
return []
|
||||
return data
|
||||
|
||||
def retrieve_list(self, parsed_args):
|
||||
|
@ -600,21 +605,23 @@ class ShowCommand(MagnetoDBCommand, show.ShowOne):
|
|||
if path in data:
|
||||
data = data[path]
|
||||
else:
|
||||
return {}
|
||||
return {'': ''}
|
||||
if not data:
|
||||
return {'': ''}
|
||||
return data
|
||||
|
||||
def call_server(self, magnetodb_client, name, parsed_args, body):
|
||||
obj_shower = getattr(magnetodb_client, self.method)
|
||||
data = obj_shower(name)
|
||||
return data
|
||||
|
||||
def get_data(self, parsed_args):
|
||||
self.log.debug('get_data(%s)', parsed_args)
|
||||
magnetodb_client = self.get_client()
|
||||
|
||||
_name = parsed_args.name
|
||||
|
||||
_name = getattr(parsed_args, 'name', None)
|
||||
body = self.args2body(parsed_args)
|
||||
obj_shower = getattr(magnetodb_client, self.method)
|
||||
if body:
|
||||
data = obj_shower(_name, body)
|
||||
else:
|
||||
data = obj_shower(_name)
|
||||
data = self.call_server(magnetodb_client, _name, parsed_args, body)
|
||||
if data:
|
||||
resource = self._get_resource(data, parsed_args)
|
||||
self.format_output_data(resource)
|
||||
|
|
|
@ -24,20 +24,6 @@ from magnetodbclient.magnetodb import v1 as magnetodbv1
|
|||
from magnetodbclient.openstack.common.gettextutils import _
|
||||
|
||||
|
||||
def _format_table_name(table):
|
||||
try:
|
||||
return table['href']
|
||||
except Exception:
|
||||
return ''
|
||||
|
||||
|
||||
def _get_lsi_names(indexes):
|
||||
index_names = []
|
||||
for index in indexes:
|
||||
index_names.append(index['index_name'])
|
||||
return index_names
|
||||
|
||||
|
||||
class GetItem(magnetodbv1.ShowCommand):
|
||||
"""Gets item from a given table by key."""
|
||||
|
||||
|
@ -47,10 +33,9 @@ class GetItem(magnetodbv1.ShowCommand):
|
|||
log = logging.getLogger(__name__ + '.GetItem')
|
||||
|
||||
def add_known_arguments(self, parser):
|
||||
help_str = _('Name of table to look up')
|
||||
parser.add_argument(
|
||||
'name', metavar='TABLE_NAME',
|
||||
help=help_str)
|
||||
help=_('Name of table to look up'))
|
||||
parser.add_argument(
|
||||
'--request-file', metavar='FILE', dest='request_file_name',
|
||||
help=_('File that contains item description to put in table'))
|
||||
|
@ -58,6 +43,11 @@ class GetItem(magnetodbv1.ShowCommand):
|
|||
def args2body(self, parsed_args):
|
||||
return utils.get_file_contents(parsed_args.request_file_name)
|
||||
|
||||
def call_server(self, magnetodb_client, name, parsed_args, body):
|
||||
obj_shower = getattr(magnetodb_client, self.method)
|
||||
data = obj_shower(name, body)
|
||||
return data
|
||||
|
||||
|
||||
class PutItem(magnetodbv1.CreateCommand):
|
||||
"""Puts item to a given table."""
|
||||
|
@ -108,7 +98,7 @@ class UpdateItem(magnetodbv1.UpdateCommand):
|
|||
resource_path = ('attributes',)
|
||||
resource = 'item'
|
||||
|
||||
# NOTE(aostapenko) Update item is not supported on server side
|
||||
# NOTE(aostapenko) Update item is not supported on server side yet
|
||||
# remove this method in future
|
||||
def run(self, parsed_args):
|
||||
print("Update item is not supported now on server side")
|
||||
|
@ -129,7 +119,7 @@ class Query(magnetodbv1.ListCommand):
|
|||
"""Query table that belong to a given tenant."""
|
||||
|
||||
resource = 'item'
|
||||
resource_path = ('item',)
|
||||
resource_path = ('items',)
|
||||
method = 'query'
|
||||
log = logging.getLogger(__name__ + '.Query')
|
||||
|
||||
|
@ -154,6 +144,27 @@ class Scan(Query):
|
|||
"""Scan table that belong to a given tenant."""
|
||||
|
||||
resource = 'item'
|
||||
resource_path = ('item',)
|
||||
resource_path = ('items',)
|
||||
method = 'scan'
|
||||
log = logging.getLogger(__name__ + '.Scan')
|
||||
|
||||
|
||||
class BatchWrite(magnetodbv1.ShowCommand):
|
||||
"""Batch write command."""
|
||||
|
||||
resource_path = ('unprocessed_items',)
|
||||
method = 'batch_write_item'
|
||||
log = logging.getLogger(__name__ + '.GetItem')
|
||||
|
||||
def add_known_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
'--request-file', metavar='FILE', dest='request_file_name',
|
||||
help=_('File that contains item description to put in table'))
|
||||
|
||||
def args2body(self, parsed_args):
|
||||
return utils.get_file_contents(parsed_args.request_file_name)
|
||||
|
||||
def call_server(self, magnetodb_client, name, parsed_args, body):
|
||||
obj_shower = getattr(magnetodb_client, self.method)
|
||||
data = obj_shower(body)
|
||||
return data
|
||||
|
|
|
@ -41,7 +41,7 @@ def _get_lsi_names(indexes):
|
|||
class ListTable(magnetodbv1.ListCommand):
|
||||
"""List tables that belong to a given tenant."""
|
||||
|
||||
resource_path = ('table',)
|
||||
resource_path = ('tables',)
|
||||
method = 'list_tables'
|
||||
log = logging.getLogger(__name__ + '.ListTable')
|
||||
_formatters = {'Table Name': _format_table_name}
|
||||
|
|
|
@ -82,6 +82,7 @@ COMMAND_V1 = {
|
|||
'item-update': item.UpdateItem,
|
||||
'query': item.Query,
|
||||
'scan': item.Scan,
|
||||
'batch-write': item.BatchWrite,
|
||||
}
|
||||
|
||||
COMMANDS = {'1': COMMAND_V1}
|
||||
|
|
Loading…
Reference in New Issue