Adds table-create cli command

This commit is contained in:
Andrei V. Ostapenko 2014-04-27 22:57:40 +03:00
parent d8d4369d77
commit 7b0e01cda3
7 changed files with 86 additions and 68 deletions

View File

@ -113,7 +113,6 @@ HTTP_EXCEPTION_MAP = {
# Exceptions mapped to MagnetoDB server exceptions
# These are defined if a user of client library needs specific exception.
# Exception name should be <MagnetoDB Exception Name> + 'Client'
# e.g., NetworkNotFound -> NetworkNotFoundClient
# Exceptions from client library
@ -144,7 +143,7 @@ class RequestURITooLong(MagnetoDBClientException):
class ConnectionFailed(MagnetoDBClientException):
message = _("Connection to neutron failed: %(reason)s")
message = _("Connection to magnetodb failed: %(reason)s")
class SslCertificateValidationError(MagnetoDBClientException):

View File

@ -20,7 +20,7 @@ import argparse
import logging
import re
from cliff.formatters import table
from cliff.formatters import table as cliff_table
from cliff import lister
from cliff import show
@ -321,7 +321,7 @@ def update_dict(obj, dict, attributes):
dict[attribute] = getattr(obj, attribute)
class TableFormater(table.TableFormatter):
class TableFormater(cliff_table.TableFormatter):
"""This class is used to keep consistency with prettytable 0.6.
https://bugs.launchpad.net/python-magnetodbclient/+bug/1165962
@ -340,14 +340,6 @@ class MagnetoDBCommand(command.OpenStackCommand):
values_specs = []
json_indent = None
def __init__(self, app, app_args):
super(MagnetoDBCommand, self).__init__(app, app_args)
# NOTE(markmcclain): This is no longer supported in cliff version 1.5.2
# see https://bugs.launchpad.net/python-neutronclient/+bug/1265926
#if hasattr(self, 'formatters'):
#self.formatters['table'] = TableFormater()
def get_client(self):
return self.app.client_manager.magnetodb
@ -411,11 +403,7 @@ class CreateCommand(MagnetoDBCommand, show.ShowOne):
self.log.debug('get_data(%s)' % parsed_args)
magnetodb_client = self.get_client()
magnetodb_client.format = parsed_args.request_format
_extra_values = parse_args_to_dict(self.values_specs)
_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)
data = obj_creator(body)

View File

@ -0,0 +1,45 @@
# Copyright 2012 OpenStack Foundation.
# All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
import argparse
import logging
from magnetodbclient.common import exceptions
from magnetodbclient.magnetodb import v1 as magnetodb
from magnetodbclient.openstack.common.gettextutils import _
class CreateTable(magnetodb.CreateCommand):
"""Create a table for a given tenant."""
resource = 'table'
log = logging.getLogger(__name__ + '.CreateTable')
def add_known_arguments(self, parser):
parser.add_argument(
'--description-file', metavar='FILE', dest='desc_file_name',
help=_('File that contains table description to create'))
def args2body(self, parsed_args):
body = {'file': parsed_args.desc_file_name, }
return body
class DeleteTable(magnetodb.DeleteCommand):
"""Delete a given table."""
log = logging.getLogger(__name__ + '.DeleteTable')
resource = 'table'

View File

@ -31,6 +31,7 @@ from cliff import commandmanager
from magnetodbclient.common import clientmanager
from magnetodbclient.common import exceptions as exc
from magnetodbclient.common import utils
from magnetodbclient.magnetodb.v1 import table
from magnetodbclient.openstack.common.gettextutils import _
from magnetodbclient.openstack.common import strutils
from magnetodbclient.version import __version__
@ -67,7 +68,9 @@ def env(*_vars, **kwargs):
return kwargs.get('default', '')
COMMAND_V1 = {}
COMMAND_V1 = {
'table-create': table.CreateTable,
}
COMMANDS = {'1': COMMAND_V1}
@ -163,8 +166,8 @@ class MagnetoDBShell(app.App):
'--os-auth-strategy', metavar='<auth-strategy>',
default=env('OS_AUTH_STRATEGY', default='keystone'),
help=_('Authentication strategy (Env: OS_AUTH_STRATEGY'
', default keystone). For now, any other value will'
' disable the authentication'))
', default keystone). For now, any other value will'
' disable the authentication'))
parser.add_argument(
'--os_auth_strategy',
help=argparse.SUPPRESS)
@ -476,7 +479,7 @@ class MagnetoDBShell(app.App):
def main(argv=sys.argv[1:]):
try:
return MagnetoDBShell(MAGNETODB_API_VERSION).run(
map(strutils.safe_decode, argv))
map(strutils.safe_decode, argv))
except exc.MagnetoDBClientException:
return 1
except Exception as e:

View File

@ -122,90 +122,74 @@ class Client(object):
...
"""
base_path = "/%(tenant_id)s/data"
tables_path = "/tables"
table_path = "/tables/%s"
base_path = "/data"
tables_path = base_path + "/tables"
table_path = base_path + "/tables/%s"
put_item_path = "/put_item"
delete_item_path = "/delete_item"
get_item_path = "/get_item"
query_path = "/query"
scan_path = "/scan"
batch_write_item_path = "/batch_write_item"
batch_read_item_path = "/batch_read_item"
batch_write_item_path = base_path + "/batch_write_item"
batch_read_item_path = base_path + "/batch_read_item"
# 8192 Is the default max URI len for eventlet.wsgi.server
MAX_URI_LEN = 8192
def get_base_path(self, tenant_id):
return self.base_path % {'tenant_id': tenant_id}
def create_table(self, tenant_id, request_body):
def create_table(self, request_body):
"""Create table."""
url = self.get_base_path(tenant_id) + self.tables_path
url = self.tables_path
return self.post(url, request_body)
def delete_table(self, tenant_id, table_name):
def delete_table(self, table_name):
"""Delete the specified table."""
url = self.get_base_path(tenant_id) + self.table_path % table_name
return self.delete(url)
return self.delete(self.table_path % table_name)
def list_tables(self, tenant_id):
def list_tables(self):
"""List tables."""
url = self.get_base_path(tenant_id) + self.tables_path
return self.get(url)
return self.get(self.tables_path)
def describe_table(self, tenant_id, table_name):
def describe_table(self, table_name):
"""Describe the specified table."""
url = self.get_base_path(tenant_id) + self.table_path % table_name
return self.get(url)
return self.get(self.table_path % table_name)
def put_item(self, tenant_id, table_name, request_body):
def put_item(self, table_name, request_body):
"""Put item to the specified table."""
url = self.get_base_path(tenant_id) + self.table_path % table_name
url += self.put_item_path
url = self.table_path % table_name + self.put_item_path
return self.post(url, request_body)
def update_item(self, tenant_id, table_name, request_body):
def update_item(self, table_name, request_body):
"""Update item."""
url = self.get_base_path(tenant_id) + self.table_path % table_name
url += self.put_item_path
url = self.table_path % table_name + self.put_item_path
return self.post(url, request_body)
def delete_item(self, tenant_id, table_name, request_body):
def delete_item(self, table_name, request_body):
"""Delete item."""
url = self.get_base_path(tenant_id) + self.table_path % table_name
url += self.delete_item_path
url = self.table_path % table_name + self.delete_item_path
return self.post(url, request_body)
def get_item(self, tenant_id, table_name, request_body):
def get_item(self, table_name, request_body):
"""Get item."""
url = self.get_base_path(tenant_id) + self.table_path % table_name
url += self.get_item_path
url = self.table_path % table_name + self.get_item_path
return self.post(url, request_body)
def query(self, tenant_id, table_name, request_body):
def query(self, table_name, request_body):
"""Query the specified table."""
url = self.get_base_path(tenant_id) + self.table_path % table_name
url += self.query_path
url = self.table_path % table_name + self.query_path
return self.post(url, request_body)
def scan(self, tenant_id, table_name, request_body):
def scan(self, table_name, request_body):
"""Scan the specified table."""
url = self.get_base_path(tenant_id) + self.table_path % table_name
url += self.scan_path
url = self.table_path % table_name + self.scan_path
return self.post(url, request_body)
def batch_write_item(self, tenant_id, request_items):
def batch_write_item(self, request_items):
"""Batch write item."""
url = self.get_base_path(tenant_id)
url += self.batch_write_item_path
return self.post(url, request_items)
return self.post(self.batch_write_item_path, request_items)
def batch_read_item(self, tenant_id, request_items):
def batch_read_item(self, request_items):
"""Batch read item."""
url = self.get_base_path(tenant_id)
url += self.batch_read_item_path
return self.post(url, request_items)
return self.post(self.batch_read_item_path, request_items)
def __init__(self, **kwargs):
"""Initialize a new client for the MagnetoDB v1 API."""

View File

@ -16,7 +16,7 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# @author: Carl Baldwin, Hewlett-Packard
import pbr.version
from pbr import version
__version__ = pbr.version.VersionInfo('python-neutronclient').version_string()
__version__ = version.VersionInfo('python-magnetodbclient').version_string()

View File

@ -3,7 +3,6 @@ argparse
cliff>=1.4.3
httplib2>=0.7.5
iso8601>=0.1.9
netaddr>=0.7.6
simplejson>=2.0.9
six>=1.5.2
Babel>=1.3