|
| 1 | +"""View and Order a quote""" |
| 2 | +# :license: MIT, see LICENSE for more details. |
| 3 | +import click |
| 4 | + |
| 5 | +from SoftLayer.CLI import environment |
| 6 | +from SoftLayer.CLI import formatting |
| 7 | +from SoftLayer.CLI import helpers |
| 8 | +from SoftLayer.managers import ImageManager as ImageManager |
| 9 | +from SoftLayer.managers import ordering |
| 10 | +from SoftLayer.managers import SshKeyManager as SshKeyManager |
| 11 | + |
| 12 | + |
| 13 | +def _parse_create_args(client, args): |
| 14 | + """Converts CLI arguments to args for VSManager.create_instance. |
| 15 | +
|
| 16 | + :param dict args: CLI arguments |
| 17 | + """ |
| 18 | + data = {} |
| 19 | + |
| 20 | + if args.get('quantity'): |
| 21 | + data['quantity'] = int(args.get('quantity')) |
| 22 | + if args.get('postinstall'): |
| 23 | + data['provisionScripts'] = [args.get('postinstall')] |
| 24 | + if args.get('complex_type'): |
| 25 | + data['complexType'] = args.get('complex_type') |
| 26 | + |
| 27 | + if args.get('fqdn'): |
| 28 | + servers = [] |
| 29 | + for name in args.get('fqdn'): |
| 30 | + fqdn = name.split(".", 1) |
| 31 | + servers.append({'hostname': fqdn[0], 'domain': fqdn[1]}) |
| 32 | + data['hardware'] = servers |
| 33 | + |
| 34 | + if args.get('image'): |
| 35 | + if args.get('image').isdigit(): |
| 36 | + image_mgr = ImageManager(client) |
| 37 | + image_details = image_mgr.get_image(args.get('image'), mask="id,globalIdentifier") |
| 38 | + data['imageTemplateGlobalIdentifier'] = image_details['globalIdentifier'] |
| 39 | + else: |
| 40 | + data['imageTemplateGlobalIdentifier'] = args['image'] |
| 41 | + |
| 42 | + userdata = None |
| 43 | + if args.get('userdata'): |
| 44 | + userdata = args['userdata'] |
| 45 | + elif args.get('userfile'): |
| 46 | + with open(args['userfile'], 'r') as userfile: |
| 47 | + userdata = userfile.read() |
| 48 | + if userdata: |
| 49 | + for hardware in data['hardware']: |
| 50 | + hardware['userData'] = [{'value': userdata}] |
| 51 | + |
| 52 | + # Get the SSH keys |
| 53 | + if args.get('key'): |
| 54 | + keys = [] |
| 55 | + for key in args.get('key'): |
| 56 | + resolver = SshKeyManager(client).resolve_ids |
| 57 | + key_id = helpers.resolve_id(resolver, key, 'SshKey') |
| 58 | + keys.append(key_id) |
| 59 | + data['sshKeys'] = keys |
| 60 | + |
| 61 | + return data |
| 62 | + |
| 63 | + |
| 64 | +@click.command() |
| 65 | +@click.argument('quote') |
| 66 | +@click.option('--verify', is_flag=True, default=False, show_default=True, |
| 67 | + help="If specified, will only show what the quote will order, will NOT place an order") |
| 68 | +@click.option('--quantity', type=int, default=None, |
| 69 | + help="The quantity of the item being ordered if different from quoted value") |
| 70 | +@click.option('--complex-type', default='SoftLayer_Container_Product_Order_Hardware_Server', show_default=True, |
| 71 | + help=("The complex type of the order. Starts with 'SoftLayer_Container_Product_Order'.")) |
| 72 | +@click.option('--userdata', '-u', help="User defined metadata string") |
| 73 | +@click.option('--userfile', '-F', type=click.Path(exists=True, readable=True, resolve_path=True), |
| 74 | + help="Read userdata from file") |
| 75 | +@click.option('--postinstall', '-i', help="Post-install script to download") |
| 76 | +@helpers.multi_option('--key', '-k', help="SSH keys to add to the root user") |
| 77 | +@helpers.multi_option('--fqdn', required=True, |
| 78 | + help="<hostname>.<domain.name.tld> formatted name to use. Specify one fqdn per server") |
| 79 | +@click.option('--image', help="Image ID. See: 'slcli image list' for reference") |
| 80 | +@environment.pass_env |
| 81 | +def cli(env, quote, **args): |
| 82 | + """View and Order a quote |
| 83 | +
|
| 84 | + \f |
| 85 | + :note: |
| 86 | + The hostname and domain are split out from the fully qualified domain name. |
| 87 | +
|
| 88 | + If you want to order multiple servers, you need to specify each FQDN. Postinstall, userdata, and |
| 89 | + sshkeys are applied to all servers in an order. |
| 90 | +
|
| 91 | + :: |
| 92 | +
|
| 93 | + slcli order quote 12345 --fqdn testing.tester.com \\ |
| 94 | + --complex-type SoftLayer_Container_Product_Order_Virtual_Guest -k sshKeyNameLabel\\ |
| 95 | + -i https://domain.com/runthis.sh --userdata DataGoesHere |
| 96 | +
|
| 97 | + """ |
| 98 | + table = formatting.Table([ |
| 99 | + 'Id', 'Name', 'Created', 'Expiration', 'Status' |
| 100 | + ]) |
| 101 | + create_args = _parse_create_args(env.client, args) |
| 102 | + |
| 103 | + manager = ordering.OrderingManager(env.client) |
| 104 | + quote_details = manager.get_quote_details(quote) |
| 105 | + |
| 106 | + package = quote_details['order']['items'][0]['package'] |
| 107 | + create_args['packageId'] = package['id'] |
| 108 | + |
| 109 | + if args.get('verify'): |
| 110 | + result = manager.verify_quote(quote, create_args) |
| 111 | + verify_table = formatting.Table(['keyName', 'description', 'cost']) |
| 112 | + verify_table.align['keyName'] = 'l' |
| 113 | + verify_table.align['description'] = 'l' |
| 114 | + for price in result['prices']: |
| 115 | + cost_key = 'hourlyRecurringFee' if result['useHourlyPricing'] is True else 'recurringFee' |
| 116 | + verify_table.add_row([ |
| 117 | + price['item']['keyName'], |
| 118 | + price['item']['description'], |
| 119 | + price[cost_key] if cost_key in price else formatting.blank() |
| 120 | + ]) |
| 121 | + env.fout(verify_table) |
| 122 | + else: |
| 123 | + result = manager.order_quote(quote, create_args) |
| 124 | + table = formatting.KeyValueTable(['name', 'value']) |
| 125 | + table.align['name'] = 'r' |
| 126 | + table.align['value'] = 'l' |
| 127 | + table.add_row(['id', result['orderId']]) |
| 128 | + table.add_row(['created', result['orderDate']]) |
| 129 | + table.add_row(['status', result['placedOrder']['status']]) |
| 130 | + env.fout(table) |
0 commit comments