Minor refactor
This commit is contained in:
parent
8fa481f0d3
commit
4ff631aa30
|
@ -200,3 +200,10 @@ def safe_encode_dict(data):
|
|||
return (k, _safe_encode_without_obj(v))
|
||||
|
||||
return dict(map(_encode_item, data.items()))
|
||||
|
||||
|
||||
def get_file_contents(fname):
|
||||
with open(fname, 'r') as f:
|
||||
content = f.read()
|
||||
body = json.loads(content)
|
||||
return body
|
||||
|
|
|
@ -14,10 +14,13 @@
|
|||
# under the License.
|
||||
#
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
from magnetodbclient.common import exceptions
|
||||
from magnetodbclient.common import utils
|
||||
from magnetodbclient.magnetodb import v1 as magnetodb
|
||||
from magnetodbclient.openstack.common.gettextutils import _
|
||||
|
||||
|
@ -33,9 +36,19 @@ class CreateTable(magnetodb.CreateCommand):
|
|||
'--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
|
||||
def get_data(self, parsed_args):
|
||||
self.log.debug('get_data(%s)' % parsed_args)
|
||||
magnetodb_client = self.get_client()
|
||||
body = utils.get_file_contents(parsed_args.desc_file_name)
|
||||
obj_creator = getattr(magnetodb_client, "create_%s" % self.resource)
|
||||
data = obj_creator(body)
|
||||
self.format_output_data(data)
|
||||
info = self.resource in data and data[self.resource] or None
|
||||
if info:
|
||||
print(_('Created a new %s:') % self.resource, file=self.app.stdout)
|
||||
else:
|
||||
info = {'': ''}
|
||||
return zip(*sorted(info.iteritems()))
|
||||
|
||||
|
||||
class DeleteTable(magnetodb.DeleteCommand):
|
||||
|
|
|
@ -125,11 +125,11 @@ class Client(object):
|
|||
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"
|
||||
put_item_path = table_path + "/put_item"
|
||||
delete_item_path = table_path + "/delete_item"
|
||||
get_item_path = table_path + "/get_item"
|
||||
query_path = table_path + "/query"
|
||||
scan_path = table_path + "/scan"
|
||||
batch_write_item_path = base_path + "/batch_write_item"
|
||||
batch_read_item_path = base_path + "/batch_read_item"
|
||||
|
||||
|
@ -138,8 +138,7 @@ class Client(object):
|
|||
|
||||
def create_table(self, request_body):
|
||||
"""Create table."""
|
||||
url = self.tables_path
|
||||
return self.post(url, request_body)
|
||||
return self.post(self.tables_path, request_body)
|
||||
|
||||
def delete_table(self, table_name):
|
||||
"""Delete the specified table."""
|
||||
|
@ -155,33 +154,27 @@ class Client(object):
|
|||
|
||||
def put_item(self, table_name, request_body):
|
||||
"""Put item to the specified table."""
|
||||
url = self.table_path % table_name + self.put_item_path
|
||||
return self.post(url, request_body)
|
||||
return self.post(self.put_item_path % table_name, request_body)
|
||||
|
||||
def update_item(self, table_name, request_body):
|
||||
"""Update item."""
|
||||
url = self.table_path % table_name + self.put_item_path
|
||||
return self.post(url, request_body)
|
||||
return self.post(self.put_item_path % table_name, request_body)
|
||||
|
||||
def delete_item(self, table_name, request_body):
|
||||
"""Delete item."""
|
||||
url = self.table_path % table_name + self.delete_item_path
|
||||
return self.post(url, request_body)
|
||||
return self.post(self.delete_item_path % table_name, request_body)
|
||||
|
||||
def get_item(self, table_name, request_body):
|
||||
"""Get item."""
|
||||
url = self.table_path % table_name + self.get_item_path
|
||||
return self.post(url, request_body)
|
||||
return self.post(self.get_item_path % table_name, request_body)
|
||||
|
||||
def query(self, table_name, request_body):
|
||||
"""Query the specified table."""
|
||||
url = self.table_path % table_name + self.query_path
|
||||
return self.post(url, request_body)
|
||||
return self.post(self.query_path % table_name, request_body)
|
||||
|
||||
def scan(self, table_name, request_body):
|
||||
"""Scan the specified table."""
|
||||
url = self.table_path % table_name + self.scan_path
|
||||
return self.post(url, request_body)
|
||||
return self.post(self.scan_path % table_name, request_body)
|
||||
|
||||
def batch_write_item(self, request_items):
|
||||
"""Batch write item."""
|
||||
|
|
Loading…
Reference in New Issue