Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
service dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
uberj committed Jul 2, 2014
1 parent a1f7503 commit 2d40ffa
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 4 deletions.
4 changes: 1 addition & 3 deletions invtool/dispatch.py
Expand Up @@ -41,9 +41,7 @@ def handle_resp(self, nas, data, resp):
else:
return 0, ["http_status: 204 (request fulfilled)"]
elif resp.status_code == 500:
resp_list = [_("SERVER ERROR! (Please email this output to a "
"code monkey)")]
return self.error_out(nas, data, resp, resp_list=resp_list)
return self.error_out(nas, data, resp, resp_list=[])
elif resp.status_code == 400:
# Bad Request
if nas.p_json:
Expand Down
3 changes: 2 additions & 1 deletion invtool/main.py
Expand Up @@ -14,7 +14,8 @@
'invtool.csv_dispatch',
'invtool.ba_dispatch',
'invtool.sreg_dispatch',
'invtool.decommission_dispatch'
'invtool.decommission_dispatch',
'invtool.service_dispatch'
]

for d in enabled_dispatches:
Expand Down
125 changes: 125 additions & 0 deletions invtool/service_dispatch.py
@@ -0,0 +1,125 @@
import requests
import sys
import yaml

try:
import simplejson as json
except ImportError:
import json

from invtool.dispatch import Dispatch
from invtool.lib.registrar import registrar
from invtool.lib.config import REMOTE, auth


class ServiceDispatch(Dispatch):
def route(self, nas):
return getattr(self, nas.dtype)(nas)

def handle_service_resp(self, nas, query, resp):
ret_code, raw_results = self.handle_resp(nas, query, resp)
if ret_code:
return (ret_code, raw_results) # repack and go home

results = json.loads(raw_results[0])
dump = self.get_encoder(nas)

if 'errors' in results:
ret_code = 1
else:
ret_code = 0

return ret_code, [dump(results)]


class ServiceImportDispatch(ServiceDispatch):
dgroup = dtype = 'service_import'

def get_encoder(self, nas):
return lambda d: json.dumps(d, indent=4, sort_keys=True)

def build_parser(self, base_parser):
service_import = base_parser.add_parser(
'service_import', help="Import a JSON or YAML file", add_help=True
)
service_import.add_argument(
'--file-path', help="A path to a file containing "
"a JSON or YAML representation that can be used for import. "
"Piping the file into this command's STDIN also works."
)

def service_import(self, nas):
# naively read in from stdin
if nas.file_path:
try:
with open(nas.file_path, 'r') as fd:
raw_data = fd.read().strip()
except IOError, e:
return 1, [str(e)]

else:
raw_data = nas.IN.read().strip()

try:
data = json.loads(raw_data)
except ValueError:
try:
data = yaml.load(raw_data)
except ValueError:
return 1, [
"Coulnd't parse import data as valid JSON or valid YAML"
]
return self.do_import(nas, json.dumps(data))

def do_import(self, nas, json_data):
tmp_url = "/en-US/core/service/import/"
url = "{0}{1}".format(REMOTE, tmp_url)
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
resp = requests.post(url, data=json_data, headers=headers, auth=auth())
if nas.DEBUG:
sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
'get', url, json_data
))
nas.p_json = True # Do this so we can play with the response
return self.handle_service_resp(nas, json_data, resp)


class ServiceExportDispatch(ServiceDispatch):
dgroup = dtype = 'service_export'

def get_encoder(self, nas):
if nas.p_yaml:
dump = lambda d: yaml.dump(d, default_flow_style=False)
else:
dump = lambda d: json.dumps(d, indent=4, sort_keys=True)
return dump

def build_parser(self, base_parser):
service_export = base_parser.add_parser(
'service_export', help="Export services.", add_help=True
)
service_export.add_argument(
'query', type=str,
help="A search string describing the services to export"
)
service_export.add_argument(
'--yaml', action='store_true', default=False, dest='p_yaml',
help="Export in YAML format",
)

def service_export(self, nas):
tmp_url = "/core/service/export/"
url = "{0}{1}".format(REMOTE, tmp_url)
headers = {'content-type': 'application/json'}
query = {'search': nas.query}
resp = requests.get(url, params=query, headers=headers, auth=auth())
if nas.DEBUG:
sys.stderr.write('method: {0}\nurl: {1}\nparams:{2}\n'.format(
'get', url, query
))
nas.p_json = True # Do this so we can play with the response
return self.handle_service_resp(nas, query, resp)


registrar.register(ServiceImportDispatch())
registrar.register(ServiceExportDispatch())
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -2,3 +2,4 @@ requests
simplejson
argparse
keyring
PyYAML

0 comments on commit 2d40ffa

Please sign in to comment.