def execute(self, args): vsi = SoftLayer.VSManager(self.client) table = formatting.KeyValueTable(['Name', 'Value']) table.align['Name'] = 'r' table.align['Value'] = 'l' vs_id = helpers.resolve_id(vsi.resolve_ids, args.get('<identifier>'), 'VS') result = vsi.get_instance(vs_id) result = utils.NestedDict(result) table.add_row(['id', result['id']]) table.add_row(['hostname', result['fullyQualifiedDomainName']]) table.add_row([ 'status', formatting.FormattedItem( result['status']['keyName'] or formatting.blank(), result['status']['name'] or formatting.blank()) ]) table.add_row(['active_transaction', formatting.active_txn(result)]) table.add_row([ 'state', formatting.FormattedItem( utils.lookup(result, 'powerState', 'keyName'), utils.lookup(result, 'powerState', 'name'), ) ]) table.add_row( ['datacenter', result['datacenter']['name'] or formatting.blank()]) operating_system = utils.lookup(result, 'operatingSystem', 'softwareLicense', 'softwareDescription') or {} table.add_row([ 'os', formatting.FormattedItem( operating_system.get('version') or formatting.blank(), operating_system.get('name') or formatting.blank()) ]) table.add_row([ 'os_version', operating_system.get('version') or formatting.blank() ]) table.add_row(['cores', result['maxCpu']]) table.add_row(['memory', formatting.mb_to_gb(result['maxMemory'])]) table.add_row( ['public_ip', result['primaryIpAddress'] or formatting.blank()]) table.add_row([ 'private_ip', result['primaryBackendIpAddress'] or formatting.blank() ]) table.add_row(['private_only', result['privateNetworkOnlyFlag']]) table.add_row(['private_cpu', result['dedicatedAccountHostOnlyFlag']]) table.add_row(['created', result['createDate']]) table.add_row(['modified', result['modifyDate']]) table.add_row([ 'owner', formatting.FormattedItem( utils.lookup(result, 'billingItem', 'orderItem', 'order', 'userRecord', 'username') or formatting.blank(), ) ]) vlan_table = formatting.Table(['type', 'number', 'id']) for vlan in result['networkVlans']: vlan_table.add_row( [vlan['networkSpace'], vlan['vlanNumber'], vlan['id']]) table.add_row(['vlans', vlan_table]) if result.get('notes'): table.add_row(['notes', result['notes']]) if args.get('--price'): table.add_row( ['price rate', result['billingItem']['recurringFee']]) if args.get('--passwords'): pass_table = formatting.Table(['username', 'password']) for item in result['operatingSystem']['passwords']: pass_table.add_row([item['username'], item['password']]) table.add_row(['users', pass_table]) tag_row = [] for tag in result['tagReferences']: tag_row.append(tag['tag']['name']) if tag_row: table.add_row(['tags', formatting.listing(tag_row, separator=',')]) # Test to see if this actually has a primary (public) ip address if result['primaryIpAddress']: ptr_domains = ( self.client['Virtual_Guest'].getReverseDomainRecords(id=vs_id)) for ptr_domain in ptr_domains: for ptr in ptr_domain['resourceRecords']: table.add_row(['ptr', ptr['data']]) return table
def cli(env, **args): """Order/create virtual servers.""" vsi = SoftLayer.VSManager(env.client) _validate_args(env, args) # Do not create a virtual server with test or export do_create = not (args['export'] or args['test']) table = formatting.Table(['Item', 'cost']) table.align['Item'] = 'r' table.align['cost'] = 'r' data = _parse_create_args(env.client, args) output = [] if args.get('test'): result = vsi.verify_create_instance(**data) total_monthly = 0.0 total_hourly = 0.0 table = formatting.Table(['Item', 'cost']) table.align['Item'] = 'r' table.align['cost'] = 'r' for price in result['prices']: total_monthly += float(price.get('recurringFee', 0.0)) total_hourly += float(price.get('hourlyRecurringFee', 0.0)) if args.get('billing') == 'hourly': rate = "%.2f" % float(price['hourlyRecurringFee']) elif args.get('billing') == 'monthly': rate = "%.2f" % float(price['recurringFee']) table.add_row([price['item']['description'], rate]) total = 0 if args.get('billing') == 'hourly': total = total_hourly elif args.get('billing') == 'monthly': total = total_monthly billing_rate = 'monthly' if args.get('billing') == 'hourly': billing_rate = 'hourly' table.add_row(['Total %s cost' % billing_rate, "%.2f" % total]) output.append(table) output.append( formatting.FormattedItem( None, ' -- ! Prices reflected here are retail and do not ' 'take account level discounts and are not guaranteed.')) if args['export']: export_file = args.pop('export') template.export_to_template(export_file, args, exclude=['wait', 'test']) env.fout('Successfully exported options to a template file.') if do_create: if not (env.skip_confirmations or formatting.confirm( "This action will incur charges on your account. Continue?")): raise exceptions.CLIAbort('Aborting virtual server order.') result = vsi.create_instance(**data) table = formatting.KeyValueTable(['name', 'value']) table.align['name'] = 'r' table.align['value'] = 'l' table.add_row(['id', result['id']]) table.add_row(['created', result['createDate']]) table.add_row(['guid', result['globalIdentifier']]) output.append(table) if args.get('wait'): ready = vsi.wait_for_ready(result['id'], args.get('wait') or 1) table.add_row(['ready', ready]) if ready is False: env.out(env.fmt(output)) raise exceptions.CLIHalt(code=1) env.fout(output)
def test_format_output_formatted_item(self): item = formatting.FormattedItem('test', 'test_formatted') ret = formatting.format_output(item, 'table') self.assertEqual('test_formatted', ret)
def cli(env, identifier, passwords, price): """Get details for a hardware device.""" hardware = SoftLayer.HardwareManager(env.client) table = formatting.KeyValueTable(['Name', 'Value']) table.align['Name'] = 'r' table.align['Value'] = 'l' hardware_id = helpers.resolve_id(hardware.resolve_ids, identifier, 'hardware') result = hardware.get_hardware(hardware_id) result = utils.NestedDict(result) table.add_row(['id', result['id']]) table.add_row(['guid', result['globalIdentifier'] or formatting.blank()]) table.add_row(['hostname', result['hostname']]) table.add_row(['domain', result['domain']]) table.add_row(['fqdn', result['fullyQualifiedDomainName']]) table.add_row(['status', result['hardwareStatus']['status']]) table.add_row( ['datacenter', result['datacenter']['name'] or formatting.blank()]) table.add_row(['cores', result['processorPhysicalCoreAmount']]) table.add_row(['memory', formatting.gb(result['memoryCapacity'])]) table.add_row( ['public_ip', result['primaryIpAddress'] or formatting.blank()]) table.add_row([ 'private_ip', result['primaryBackendIpAddress'] or formatting.blank() ]) table.add_row([ 'ipmi_ip', result['networkManagementIpAddress'] or formatting.blank() ]) table.add_row([ 'os', formatting.FormattedItem( result['operatingSystem']['softwareLicense']['softwareDescription'] ['referenceCode'] or formatting.blank(), result['operatingSystem'] ['softwareLicense']['softwareDescription']['name'] or formatting.blank()) ]) table.add_row(['created', result['provisionDate'] or formatting.blank()]) if utils.lookup(result, 'billingItem') != []: table.add_row([ 'owner', formatting.FormattedItem( utils.lookup(result, 'billingItem', 'orderItem', 'order', 'userRecord', 'username') or formatting.blank(), ) ]) else: table.add_row(['owner', formatting.blank()]) vlan_table = formatting.Table(['type', 'number', 'id']) for vlan in result['networkVlans']: vlan_table.add_row( [vlan['networkSpace'], vlan['vlanNumber'], vlan['id']]) table.add_row(['vlans', vlan_table]) if result.get('notes'): table.add_row(['notes', result['notes']]) if price: table.add_row([ 'price rate', utils.lookup(result, 'billingItem', 'nextInvoiceTotalRecurringAmount') ]) if passwords: pass_table = formatting.Table(['username', 'password']) for item in result['operatingSystem']['passwords']: pass_table.add_row([item['username'], item['password']]) table.add_row(['users', pass_table]) pass_table = formatting.Table(['ipmi_username', 'password']) for item in result['remoteManagementAccounts']: pass_table.add_row([item['username'], item['password']]) table.add_row(['remote users', pass_table]) tag_row = [] for tag_detail in result['tagReferences']: tag = utils.lookup(tag_detail, 'tag', 'name') if tag is not None: tag_row.append(tag) if tag_row: table.add_row(['tags', formatting.listing(tag_row, separator=',')]) # Test to see if this actually has a primary (public) ip address try: if not result['privateNetworkOnlyFlag']: ptr_domains = ( env.client['Hardware_Server'].getReverseDomainRecords( id=hardware_id)) for ptr_domain in ptr_domains: for ptr in ptr_domain['resourceRecords']: table.add_row(['ptr', ptr['data']]) except SoftLayer.SoftLayerAPIError: pass return table
def cli(env, identifier, passwords=False, price=False): """Get details for a virtual server.""" vsi = SoftLayer.VSManager(env.client) table = formatting.KeyValueTable(['name', 'value']) table.align['name'] = 'r' table.align['value'] = 'l' vs_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS') result = vsi.get_instance(vs_id) result = utils.NestedDict(result) local_disks = vsi.get_local_disks(vs_id) table_local_disks = get_local_storage_table(local_disks) table.add_row(['id', result['id']]) table.add_row(['guid', result['globalIdentifier']]) table.add_row(['hostname', result['hostname']]) table.add_row(['domain', result['domain']]) table.add_row(['fqdn', result['fullyQualifiedDomainName']]) table.add_row([ 'status', formatting.FormattedItem(result['status']['keyName'], result['status']['name']) ]) table.add_row([ 'state', formatting.FormattedItem( utils.lookup(result, 'powerState', 'keyName'), utils.lookup(result, 'powerState', 'name'), ) ]) table.add_row(['active_transaction', formatting.active_txn(result)]) table.add_row( ['datacenter', result['datacenter']['name'] or formatting.blank()]) _cli_helper_dedicated_host(env, result, table) operating_system = utils.lookup(result, 'operatingSystem', 'softwareLicense', 'softwareDescription') or {} table.add_row(['os', operating_system.get('name', '-')]) table.add_row(['os_version', operating_system.get('version', '-')]) table.add_row(['cores', result['maxCpu']]) table.add_row(['memory', formatting.mb_to_gb(result['maxMemory'])]) table.add_row(['drives', table_local_disks]) table.add_row(['public_ip', result.get('primaryIpAddress', '-')]) table.add_row(['private_ip', result.get('primaryBackendIpAddress', '-')]) table.add_row(['private_only', result['privateNetworkOnlyFlag']]) table.add_row(['private_cpu', result['dedicatedAccountHostOnlyFlag']]) table.add_row(['transient', result.get('transientGuestFlag', False)]) table.add_row(['created', result['createDate']]) table.add_row(['modified', result['modifyDate']]) last_transaction = '' if result.get('lastTransaction'): last_transaction = "{} ({})".format( utils.lookup(result, 'lastTransaction', 'transactionGroup', 'name'), utils.clean_time( utils.lookup(result, 'lastTransaction', 'modifyDate'))) table.add_row(['last_transaction', last_transaction]) table.add_row( ['billing', 'Hourly' if result['hourlyBillingFlag'] else 'Monthly']) table.add_row([ 'preset', utils.lookup(result, 'billingItem', 'orderItem', 'preset', 'keyName') or '-' ]) table.add_row(_get_owner_row(result)) table.add_row(_get_vlan_table(result)) bandwidth = vsi.get_bandwidth_allocation(vs_id) table.add_row(['Bandwidth', _bw_table(bandwidth)]) security_table = _get_security_table(result) if security_table is not None: table.add_row(['security_groups', security_table]) table.add_row(['notes', result.get('notes', '-')]) if price: total_price = utils.lookup(result, 'billingItem', 'nextInvoiceTotalRecurringAmount') or 0 total_price += sum( p['nextInvoiceTotalRecurringAmount'] for p in utils.lookup(result, 'billingItem', 'children') or []) table.add_row(['price_rate', total_price]) if passwords: pass_table = formatting.Table(['software', 'username', 'password']) for component in result['softwareComponents']: for item in component['passwords']: pass_table.add_row([ utils.lookup(component, 'softwareLicense', 'softwareDescription', 'name'), item['username'], item['password'], ]) table.add_row(['users', pass_table]) table.add_row(['tags', formatting.tags(result['tagReferences'])]) # Test to see if this actually has a primary (public) ip address try: if not result['privateNetworkOnlyFlag']: ptr_domains = env.client.call( 'Virtual_Guest', 'getReverseDomainRecords', id=vs_id, ) for ptr_domain in ptr_domains: for ptr in ptr_domain['resourceRecords']: table.add_row(['ptr', ptr['data']]) except SoftLayer.SoftLayerAPIError: pass env.fout(table)
def cli(env, identifier, passwords=False, price=False): """Get details for a virtual server.""" vsi = SoftLayer.VSManager(env.client) table = formatting.KeyValueTable(['name', 'value']) table.align['name'] = 'r' table.align['value'] = 'l' vs_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS') result = vsi.get_instance(vs_id) result = utils.NestedDict(result) table.add_row(['id', result['id']]) table.add_row(['guid', result['globalIdentifier']]) table.add_row(['hostname', result['hostname']]) table.add_row(['domain', result['domain']]) table.add_row(['fqdn', result['fullyQualifiedDomainName']]) table.add_row([ 'status', formatting.FormattedItem( result['status']['keyName'] or formatting.blank(), result['status']['name'] or formatting.blank()) ]) table.add_row([ 'state', formatting.FormattedItem( utils.lookup(result, 'powerState', 'keyName'), utils.lookup(result, 'powerState', 'name'), ) ]) table.add_row(['active_transaction', formatting.active_txn(result)]) table.add_row( ['datacenter', result['datacenter']['name'] or formatting.blank()]) operating_system = utils.lookup(result, 'operatingSystem', 'softwareLicense', 'softwareDescription') or {} table.add_row(['os', operating_system.get('name') or formatting.blank()]) table.add_row( ['os_version', operating_system.get('version') or formatting.blank()]) table.add_row(['cores', result['maxCpu']]) table.add_row(['memory', formatting.mb_to_gb(result['maxMemory'])]) table.add_row( ['public_ip', result['primaryIpAddress'] or formatting.blank()]) table.add_row([ 'private_ip', result['primaryBackendIpAddress'] or formatting.blank() ]) table.add_row(['private_only', result['privateNetworkOnlyFlag']]) table.add_row(['private_cpu', result['dedicatedAccountHostOnlyFlag']]) table.add_row(['created', result['createDate']]) table.add_row(['modified', result['modifyDate']]) if utils.lookup(result, 'billingItem') != []: table.add_row([ 'owner', formatting.FormattedItem( utils.lookup(result, 'billingItem', 'orderItem', 'order', 'userRecord', 'username') or formatting.blank(), ) ]) else: table.add_row(['owner', formatting.blank()]) vlan_table = formatting.Table(['type', 'number', 'id']) for vlan in result['networkVlans']: vlan_table.add_row( [vlan['networkSpace'], vlan['vlanNumber'], vlan['id']]) table.add_row(['vlans', vlan_table]) if result.get('notes'): table.add_row(['notes', result['notes']]) if price: total_price = utils.lookup(result, 'billingItem', 'nextInvoiceTotalRecurringAmount') or 0 total_price += sum( p['nextInvoiceTotalRecurringAmount'] for p in utils.lookup(result, 'billingItem', 'children') or []) table.add_row(['price_rate', total_price]) if passwords: pass_table = formatting.Table(['software', 'username', 'password']) for component in result['softwareComponents']: for item in component['passwords']: pass_table.add_row([ utils.lookup(component, 'softwareLicense', 'softwareDescription', 'name'), item['username'], item['password'], ]) table.add_row(['users', pass_table]) table.add_row(['tags', formatting.tags(result['tagReferences'])]) # Test to see if this actually has a primary (public) ip address try: if not result['privateNetworkOnlyFlag']: ptr_domains = env.client.call( 'Virtual_Guest', 'getReverseDomainRecords', id=vs_id, ) for ptr_domain in ptr_domains: for ptr in ptr_domain['resourceRecords']: table.add_row(['ptr', ptr['data']]) except SoftLayer.SoftLayerAPIError: pass env.fout(table)
"""Compute images.""" # :license: MIT, see LICENSE for more details. from SoftLayer.CLI import formatting MASK = ('id,accountId,name,globalIdentifier,parentId,publicFlag,flexImageFlag,' 'imageType') DETAIL_MASK = MASK + (',children[id,blockDevicesDiskSpaceTotal,datacenter],' 'note,createDate,status') PUBLIC_TYPE = formatting.FormattedItem('PUBLIC', 'Public') PRIVATE_TYPE = formatting.FormattedItem('PRIVATE', 'Private')
def cli(env, **args): """Order/create a dedicated server.""" mgr = SoftLayer.HardwareManager(env.client) # Get the SSH keys ssh_keys = [] for key in args.get('key'): resolver = SoftLayer.SshKeyManager(env.client).resolve_ids key_id = helpers.resolve_id(resolver, key, 'SshKey') ssh_keys.append(key_id) order = { 'hostname': args['hostname'], 'domain': args['domain'], 'size': args['size'], 'location': args.get('datacenter'), 'ssh_keys': ssh_keys, 'post_uri': args.get('postinstall'), 'os': args['os'], 'hourly': args.get('billing') == 'hourly', 'port_speed': args.get('port_speed'), 'no_public': args.get('no_public') or False, 'extras': args.get('extra'), } # Do not create hardware server with --test or --export do_create = not (args['export'] or args['test']) output = None if args.get('test'): result = mgr.verify_order(**order) table = formatting.Table(['Item', 'cost']) table.align['Item'] = 'r' table.align['cost'] = 'r' total = 0.0 for price in result['prices']: total += float(price.get('recurringFee', 0.0)) rate = "%.2f" % float(price['recurringFee']) table.add_row([price['item']['description'], rate]) table.add_row(['Total monthly cost', "%.2f" % total]) output = [] output.append(table) output.append( formatting.FormattedItem( '', ' -- ! Prices reflected here are retail and do not ' 'take account level discounts and are not guaranteed.')) if args['export']: export_file = args.pop('export') template.export_to_template(export_file, args, exclude=['wait', 'test']) env.fout('Successfully exported options to a template file.') return if do_create: if not (env.skip_confirmations or formatting.confirm( "This action will incur charges on your account. " "Continue?")): raise exceptions.CLIAbort('Aborting dedicated server order.') result = mgr.place_order(**order) table = formatting.KeyValueTable(['name', 'value']) table.align['name'] = 'r' table.align['value'] = 'l' table.add_row(['id', result['orderId']]) table.add_row(['created', result['orderDate']]) output = table env.fout(output)