Refactors table-create and table-delete

This commit is contained in:
Andrei V. Ostapenko 2014-05-16 12:20:47 +03:00
parent 429f34dbcf
commit f2e48b23b5
2 changed files with 49 additions and 38 deletions

View File

@ -343,6 +343,13 @@ class MagnetoDBCommand(command.OpenStackCommand):
elif v is None:
resource[k] = ''
def exclude_rows(self, info):
for row in self.excluded_rows:
try:
del info[row]
except KeyError:
pass
def add_known_arguments(self, parser):
pass
@ -363,6 +370,17 @@ class CreateCommand(MagnetoDBCommand, show.ShowOne):
parser = super(CreateCommand, self).get_parser(prog_name)
return parser
def format_output_data(self, resource):
for k, v in resource.iteritems():
if k in self._formatters:
resource[k] = self._formatters[k](v)
super(CreateCommand, self).format_output_data(resource)
def _get_resource(self, data):
for path in self.resource_path:
data = data[path]
return data
def get_data(self, parsed_args):
self.log.debug('get_data(%s)' % parsed_args)
magnetodb_client = self.get_client()
@ -370,18 +388,18 @@ class CreateCommand(MagnetoDBCommand, show.ShowOne):
_merge_args(self, parsed_args, _extra_values,
self.values_specs)
body = self.args2body(parsed_args)
body[self.resource].update(_extra_values)
obj_creator = getattr(magnetodb_client,
"create_%s" % self.resource)
obj_creator = getattr(magnetodb_client, self.method)
data = obj_creator(body)
self.format_output_data(data)
info = self.resource in data and data[self.resource] or None
if info:
resource = self._get_resource(data)
self.format_output_data(resource)
self.exclude_rows(resource)
if resource:
print(_('Created a new %s:') % self.resource,
file=self.app.stdout)
else:
info = {'': ''}
return zip(*sorted(info.iteritems()))
resource = {'': ''}
return zip(*sorted(resource.iteritems()))
class UpdateCommand(MagnetoDBCommand):
@ -437,16 +455,12 @@ class DeleteCommand(MagnetoDBCommand):
api = 'keyvalue'
resource = None
log = None
allow_names = True
def get_parser(self, prog_name):
parser = super(DeleteCommand, self).get_parser(prog_name)
if self.allow_names:
help_str = _('ID or name of %s to delete')
else:
help_str = _('ID of %s to delete')
help_str = _('Name of %s to delete')
parser.add_argument(
'id', metavar=self.resource.upper(),
'name', metavar=self.resource.upper(),
help=help_str % self.resource)
return parser
@ -455,15 +469,10 @@ class DeleteCommand(MagnetoDBCommand):
magnetodb_client = self.get_client()
obj_deleter = getattr(magnetodb_client,
"delete_%s" % self.resource)
if self.allow_names:
_id = find_resourceid_by_name_or_id(magnetodb_client,
self.resource,
parsed_args.id)
else:
_id = parsed_args.id
obj_deleter(_id)
print((_('Deleted %(resource)s: %(id)s')
% {'id': parsed_args.id,
_name = parsed_args.name
obj_deleter(_name)
print((_('Deleted %(resource)s: %(name)s')
% {'name': _name,
'resource': self.resource}),
file=self.app.stdout)
return
@ -584,13 +593,6 @@ class ShowCommand(MagnetoDBCommand, show.ShowOne):
def _add_specific_args(parser):
pass
def exclude_rows(self, info):
for row in self.excluded_rows:
try:
del info[row]
except KeyError:
pass
def format_output_data(self, resource):
for k, v in resource.iteritems():
if k in self._formatters:

View File

@ -19,6 +19,7 @@ from __future__ import print_function
import logging
from magnetodbclient.common import exceptions
from magnetodbclient.common import utils
from magnetodbclient.magnetodb import v1 as magnetodbv1
from magnetodbclient.openstack.common.gettextutils import _
@ -50,6 +51,7 @@ class ListTable(magnetodbv1.ListCommand):
class ShowTable(magnetodbv1.ShowCommand):
"""Show information of a given table."""
resource = 'table'
resource_path = ('table',)
method = 'describe_table'
excluded_rows = ('links',)
@ -57,14 +59,15 @@ class ShowTable(magnetodbv1.ShowCommand):
log = logging.getLogger(__name__ + '.ShowTable')
def add_known_arguments(self, parser):
help_str = _('Name of table to look up')
parser.add_argument(
'name', metavar='TABLE_NAME',
help=_('Name of table to look up'))
help=help_str)
class ListIndex(ShowTable):
"""List indices of a given table."""
resource = 'index'
log = logging.getLogger(__name__ + '.ListIndex')
def take_action(self, parsed_args):
@ -76,6 +79,7 @@ class ListIndex(ShowTable):
class ShowIndex(ShowTable):
"""Show information of a given index."""
resource = 'index'
resource_path = ('table', 'local_secondary_indexes')
log = logging.getLogger(__name__ + '.ShowIndex')
@ -102,15 +106,20 @@ class ShowIndex(ShowTable):
class CreateTable(magnetodbv1.CreateCommand):
"""Create a table for a given tenant."""
resource = 'Table'
resource = 'table'
resource_path = ('table_description',)
method = 'create_table'
excluded_rows = ('links',)
_formatters = {'local_secondary_indexes': _get_lsi_names}
log = logging.getLogger(__name__ + '.CreateTable')
def add_known_arguments(self, parser):
parser.add_argument(
'--request-file', metavar='FILE', dest='request_file_name',
help=_('File that contains table description to create'))
def args2body(self, parsed_args):
body = {'table': {
'name': parsed_args.name, }}
magnetodbv1.update_dict(parsed_args, body['table'],
['shared', 'tenant_id'])
return body
return utils.get_file_contents(parsed_args.request_file_name)
class DeleteTable(magnetodbv1.DeleteCommand):