Skip to content

Commit

Permalink
Added YAML mode; added support for JSON POST data (needed for pagerules)
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed May 17, 2016
1 parent 24bcf9d commit 821b144
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions cli4/cli4.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import re
import json
import yaml
import getopt

def convert_zones_to_identifier(cf, zone_name):
Expand Down Expand Up @@ -92,13 +93,13 @@ def convert_virtual_dns_to_identifier(cf, virtual_dns_name):

def cli4(args):
verbose = False
quiet = False
output = 'json'
method = 'GET'

usage = 'usage: cli4 [-h|--help] [-v|--verbose] [-q|--quiet] [--get|--patch|--post|-put|--delete] [item=value ...] /command...'
usage = 'usage: cli4 [-h|--help] [-v|--verbose] [-q|--quiet] [-j|--json] [-y|--yaml] [--get|--patch|--post|-put|--delete] [item=value ...] /command...'

try:
opts, args = getopt.getopt(args, 'hvq', ['help', 'verbose', 'quiet', 'get', 'patch', 'post', 'put', 'delete'])
opts, args = getopt.getopt(args, 'hvqjy', ['help', 'verbose', 'quiet', 'json', 'yaml', 'get', 'patch', 'post', 'put', 'delete'])
except getopt.GetoptError:
exit(usage)
for opt, arg in opts:
Expand All @@ -107,7 +108,9 @@ def cli4(args):
elif opt in ('-v', '--verbose'):
verbose = True
elif opt in ('-q', '--quiet'):
quiet = True
output = None
elif opt in ('-y', '--yaml'):
output = 'yaml'
elif opt in ('--get'):
method = 'GET'
elif opt in ('-P', '--patch'):
Expand All @@ -131,8 +134,14 @@ def cli4(args):
value = False
elif value[0] is '=' and digits_only.match(value[1:]):
value = int(value[1:])
elif value[0] is '[' and value[-1] is ']':
value = value[1:-1].split(',')
elif (value[0] is '{' and value[-1] is '}') or (value[0] is '[' and value[-1] is ']'):
# a json structure - used in pagerules
try:
#value = json.loads(value) - changed to yaml code to remove unicode strings
value = yaml.safe_load(value)
# success
except ValueError:
exit('cli4: %s="%s" - can\'t parse json value' % (tag, value))
params[tag] = value

# what's left is the command itself
Expand Down Expand Up @@ -215,6 +224,8 @@ def cli4(args):
except Exception as e:
exit('cli4: /%s - %s - api error' % (command, e))

if not quiet:
if output == 'json':
print json.dumps(r, indent=4, sort_keys=True)
if output == 'yaml':
print yaml.dump(r)

0 comments on commit 821b144

Please sign in to comment.