Skip to content

Instantly share code, notes, and snippets.

@michaeldye
Last active August 29, 2015 14:27
Show Gist options
  • Save michaeldye/b430b898de0b3b1b032b to your computer and use it in GitHub Desktop.
Save michaeldye/b430b898de0b3b1b032b to your computer and use it in GitHub Desktop.
SL Baremetal verify and place order
#!/usr/bin/env python3
# -*- coding: latin-1 -*-
__author__ = 'michael dye <mdye@us.ibm.com>'
__copyright__ = '2015 IBM'
__license__ = 'EPL-1.0'
import SoftLayer
import json
import sys
import traceback
_client = SoftLayer.create_client_from_env()
_order_service = _client["Product_Order"]
def pprint(json_out):
print(json.dumps(json_out, sort_keys=True, indent=4))
def er_exit(msg="ERROR", exception=None):
my_msg = msg
if exception:
my_msg = "{}: {}\n".format(msg, exception)
traceback.print_exc(file=sys.stderr)
print(my_msg, file=sys.stderr)
sys.exit(1)
def success_exit(msg='Succeeded'):
print(msg)
print("Exiting.")
sys.exit(0)
def usage():
print("""
usage: bm_order.py 'service_name' 'JSON order'
""")
if not len(sys.argv) == 3:
usage()
er_exit('Insufficient args provided.')
# main
print('Verifying order with SoftLayer.')
try:
detail = json.loads(sys.argv[2])
except Exception as ex:
er_exit('Error in JSON syntax read from stdin', ex)
try:
order_template = _client[sys.argv[1]].generateOrderTemplate(detail)
order_output = _order_service.verifyOrder(order_template)
except Exception as ex:
er_exit('Bad order detail', ex)
pprint(order_output)
print("""
************************
continue w/ order? [y/N]
************************""")
if not sys.stdin.readline().lower().strip() == 'y':
success_exit('Operation canceled.')
else:
try:
print('Attempting order with SoftLayer.')
pprint(_order_service.placeOrder(order_template))
success_exit('Succeeded.')
except Exception as ex:
er_exit('Unable to complete order', ex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment