Commit b3ef0fed authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

astakos: Support reading from stdin in import commands

In commands resource-import and service-import, read from standard input
with `--json -'.
parent c925f8dd
......@@ -45,10 +45,23 @@ from synnefo.util import units
from synnefo.lib.ordereddict import OrderedDict
from astakos.im.models import AstakosUser
from astakos.im.register import get_resources
import sys
DEFAULT_CONTENT_TYPE = None
def read_from_file(f_name):
if f_name == '-':
return sys.stdin.read()
else:
try:
with open(f_name) as file_desc:
return file_desc.read()
except IOError as e:
raise CommandError(e)
def get_user(email_or_id, **kwargs):
try:
if email_or_id.isdigit():
......
......@@ -40,6 +40,7 @@ from django.utils import simplejson as json
from snf_django.lib.db.transaction import commit_on_success_strict
from astakos.im.register import add_resource, ResourceException
from astakos.im.models import Service
from ._common import read_from_file
class Command(BaseCommand):
......@@ -60,17 +61,16 @@ class Command(BaseCommand):
raise CommandError(m)
else:
with open(json_file) as file_data:
m = 'Input should be a JSON list.'
try:
data = json.load(file_data)
except json.JSONDecodeError:
raise CommandError(m)
if not isinstance(data, list):
raise CommandError(m)
data = read_from_file(json_file)
m = 'Input should be a JSON list.'
try:
data = json.loads(data)
except json.JSONDecodeError:
raise CommandError(m)
if not isinstance(data, list):
raise CommandError(m)
self.add_resources(data)
@commit_on_success_strict()
def add_resources(self, resources):
output = []
......
......@@ -40,6 +40,7 @@ from django.utils import simplejson as json
from snf_django.lib.db.transaction import commit_on_success_strict
from astakos.im.register import add_service, ServiceException
from astakos.im.models import Component
from ._common import read_from_file
class Command(BaseCommand):
......@@ -61,15 +62,15 @@ class Command(BaseCommand):
raise CommandError(m)
else:
with open(json_file) as file_data:
m = ('Input should be a JSON dict mapping service names '
'to definitions.')
try:
data = json.load(file_data)
except json.JSONDecodeError:
raise CommandError(m)
if not isinstance(data, dict):
raise CommandError(m)
data = read_from_file(json_file)
m = ('Input should be a JSON dict mapping service names '
'to definitions.')
try:
data = json.loads(data)
except json.JSONDecodeError:
raise CommandError(m)
if not isinstance(data, dict):
raise CommandError(m)
self.add_services(data)
def add_services(self, data):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment