def on_get(self, req, resp): client = req.env['sl_client'] hardware = HardwareManager(client) nodes = [] hw_items = set([ 'id', 'hostname', 'domain', 'hardwareStatus', 'globalIdentifier', 'fullyQualifiedDomainName', 'processorPhysicalCoreAmount', 'memoryCapacity', 'primaryBackendIpAddress', 'primaryIpAddress', 'datacenter', ]) server_items = set([ 'activeTransaction[id, transactionStatus[friendlyName,name]]', ]) mask = '[mask[%s],' \ ' mask(SoftLayer_Hardware_Server)[%s]]' % \ (','.join(hw_items), ','.join(server_items)) for hw in hardware.list_hardware(mask=mask): nodes.append({ "uuid": hw['id'], "instance_uuid": hw['id'], # Get the actual power state of every device??? N+1 alert! # "power_state": '', "provision_state": hw['hardwareStatus']['status'], }) resp.body = {'nodes': nodes}
def execute(self, args): data = {} if args['--userdata'] and args['--userfile']: raise ArgumentError('[-u | --userdata] not allowed with ' '[-F | --userfile]') if args['--userfile']: if not os.path.exists(args['--userfile']): raise ArgumentError( 'File does not exist [-u | --userfile] = %s' % args['--userfile']) if args.get('--userdata'): data['userdata'] = args['--userdata'] elif args.get('--userfile'): with open(args['--userfile'], 'r') as userfile: data['userdata'] = userfile.read() data['hostname'] = args.get('--hostname') data['domain'] = args.get('--domain') mgr = HardwareManager(self.client) hw_id = resolve_id(mgr.resolve_ids, args.get('<identifier>'), 'hardware') if not mgr.edit(hw_id, **data): raise CLIAbort("Failed to update hardware")
def execute(self, args): table = KeyValueTable(['Name', 'Value']) table.align['Name'] = 'r' table.align['Value'] = 'l' show_all = True for opt_name in self.options: if args.get("--" + opt_name): show_all = False break mgr = HardwareManager(self.client) bmi_options = mgr.get_bare_metal_create_options() if args['--all']: show_all = True if args['--datacenter'] or show_all: results = self.get_create_options(bmi_options, 'datacenter')[0] table.add_row([results[0], listing(sorted(results[1]))]) if args['--cpu'] or args['--memory'] or show_all: results = self.get_create_options(bmi_options, 'cpu') memory_cpu_table = Table(['memory', 'cpu']) for result in results: memory_cpu_table.add_row([ result[0], listing( [item[0] for item in sorted( result[1], key=lambda x: int(x[0]) )])]) table.add_row(['memory/cpu', memory_cpu_table]) if args['--os'] or show_all: results = self.get_create_options(bmi_options, 'os') for result in results: table.add_row([ result[0], listing( [item[0] for item in sorted(result[1])], separator=linesep )]) if args['--disk'] or show_all: results = self.get_create_options(bmi_options, 'disk')[0] table.add_row([results[0], listing( [item[0] for item in sorted(results[1])])]) if args['--nic'] or show_all: results = self.get_create_options(bmi_options, 'nic') for result in results: table.add_row([result[0], listing( [item[0] for item in sorted(result[1],)])]) return table
def execute(self, args): public = args['public'] mgr = HardwareManager(self.client) hw_id = resolve_id(mgr.resolve_ids, args.get('<identifier>'), 'hardware') mgr.change_port_speed(hw_id, public, args['--speed'])
def global_search(search): client = get_client() match_string = '<span class="text-primary">%s</span>' % search term = re.compile(search, re.IGNORECASE) results = [] if 'vm' in app.config['installed_blueprints']: cci = CCIManager(client) # if hostname_regex.match(term): for vm in cci.list_instances(): if term.match(vm['hostname']) or \ term.match(vm.get('primaryIpAddress', '')): text = '%s (%s)' % (vm['fullyQualifiedDomainName'], vm.get('primaryIpAddress', 'No Public IP')) text = term.sub(match_string, text) results.append({ 'label': '<strong>VM:</strong> ' + text, 'value': url_for('vm_module.view', vm_id=vm['id']) }) if 'servers' in app.config['installed_blueprints']: hw = HardwareManager(client) for svr in hw.list_hardware(): if term.match(svr['hostname']) or \ term.match(svr.get('primaryIpAddress', '')): text = '%s (%s)' % (svr['fullyQualifiedDomainName'], svr.get('primaryIpAddress', 'No Public IP')) text = term.sub(match_string, text) results.append({ 'label': '<strong>Server:</strong> ' + text, 'value': url_for('server_module.view', server_id=svr['id']) }) if 'sshkeys' in app.config['installed_blueprints']: ssh = SshKeyManager(client) for key in ssh.list_keys(): if term.match(key['label']) or term.match(key['fingerprint']): text = '%s (%s)' % (key['label'], key['fingerprint']) text = term.sub(match_string, text) results.append({ 'label': '<strong>SSH Key:</strong> ' + text, 'value': url_for('ssh_module.view', key_id=key['id']) }) return results
def execute(self, args): hw = HardwareManager(self.client) hw_id = resolve_id(hw.resolve_ids, args.get("<identifier>"), "hardware") immediate = args.get("--immediate", False) if args["--really"] or no_going_back(hw_id): hw.cancel_metal(hw_id, immediate) else: CLIAbort("Aborted")
def execute(self, args): mgr = HardwareManager(self.client) hw_id = resolve_id( mgr.resolve_ids, args.get('<identifier>'), 'hardware') immediate = args.get('--immediate', False) if args['--really'] or no_going_back(hw_id): mgr.cancel_metal(hw_id, immediate) else: CLIAbort('Aborted')
def execute(self, args): table = Table(['Code', 'Reason']) table.align['Code'] = 'r' table.align['Reason'] = 'l' mgr = HardwareManager(self.client) for code, reason in mgr.get_cancellation_reasons().items(): table.add_row([code, reason]) return table
def execute(client, args): t = Table(['Code', 'Reason']) t.align['Code'] = 'r' t.align['Reason'] = 'l' mgr = HardwareManager(client) reasons = mgr.get_cancellation_reasons().iteritems() for code, reason in reasons: t.add_row([code, reason]) return t
def execute(self, args): table = Table(['Code', 'Chassis']) table.align['Code'] = 'r' table.align['Chassis'] = 'l' mgr = HardwareManager(self.client) chassis = mgr.get_available_dedicated_server_packages() for chassis in chassis: table.add_row([chassis[0], chassis[1]]) return table
def execute(self, args): t = KeyValueTable(["Name", "Value"]) t.align["Name"] = "r" t.align["Value"] = "l" show_all = True for opt_name in self.options: if args.get("--" + opt_name): show_all = False break mgr = HardwareManager(self.client) bmi_options = mgr.get_bare_metal_create_options() if args["--all"]: show_all = True if args["--datacenter"] or show_all: results = self.get_create_options(bmi_options, "datacenter")[0] t.add_row([results[0], listing(sorted(results[1]))]) if args["--cpu"] or args["--memory"] or show_all: results = self.get_create_options(bmi_options, "cpu") memory_cpu_table = Table(["memory", "cpu"]) for result in results: memory_cpu_table.add_row( [result[0], listing([item[0] for item in sorted(result[1], key=lambda x: int(x[0]))])] ) t.add_row(["memory/cpu", memory_cpu_table]) if args["--os"] or show_all: results = self.get_create_options(bmi_options, "os") for result in results: t.add_row([result[0], listing([item[0] for item in sorted(result[1])], separator=linesep)]) if args["--disk"] or show_all: results = self.get_create_options(bmi_options, "disk")[0] t.add_row([results[0], listing([item[0] for item in sorted(results[1])])]) if args["--nic"] or show_all: results = self.get_create_options(bmi_options, "nic") for result in results: t.add_row([result[0], listing([item[0] for item in sorted(result[1])])]) return t
def execute(self, args): hardware = HardwareManager(self.client) hardware_id = resolve_id( hardware.resolve_ids, args.get('<identifier>'), 'hardware') keys = [] if args.get('--key'): for key in args.get('--key'): key_id = resolve_id(SshKeyManager(self.client).resolve_ids, key, 'SshKey') keys.append(key_id) if args['--really'] or no_going_back(hardware_id): hardware.reload(hardware_id, args['--postinstall'], keys) else: CLIAbort('Aborted')
def global_search(search): client = get_client() match_string = '<span class="text-primary">%s</span>' % search term = re.compile(search, re.IGNORECASE) results = [] if 'vm' in app.config['installed_blueprints']: cci = CCIManager(client) # if hostname_regex.match(term): for vm in cci.list_instances(): if term.match(vm['hostname']) or \ term.match(vm.get('primaryIpAddress', '')): text = '%s (%s)' % (vm['fullyQualifiedDomainName'], vm.get('primaryIpAddress', 'No Public IP')) text = term.sub(match_string, text) results.append({'label': '<strong>VM:</strong> ' + text, 'value': url_for('vm_module.view', vm_id=vm['id'])}) if 'servers' in app.config['installed_blueprints']: hw = HardwareManager(client) for svr in hw.list_hardware(): if term.match(svr['hostname']) or \ term.match(svr.get('primaryIpAddress', '')): text = '%s (%s)' % (svr['fullyQualifiedDomainName'], svr.get('primaryIpAddress', 'No Public IP')) text = term.sub(match_string, text) results.append({'label': '<strong>Server:</strong> ' + text, 'value': url_for('server_module.view', server_id=svr['id'])}) if 'sshkeys' in app.config['installed_blueprints']: ssh = SshKeyManager(client) for key in ssh.list_keys(): if term.match(key['label']) or term.match(key['fingerprint']): text = '%s (%s)' % (key['label'], key['fingerprint']) text = term.sub(match_string, text) results.append({'label': '<strong>SSH Key:</strong> ' + text, 'value': url_for('ssh_module.view', key_id=key['id'])}) return results
def execute(self, args): mgr = HardwareManager(self.client) hw_id = resolve_id( mgr.resolve_ids, args.get('<identifier>'), 'hardware') comment = args.get('--comment') if not comment and not args['--really']: comment = self.env.input("(Optional) Add a cancellation comment:") reason = args.get('--reason') if args['--really'] or no_going_back(hw_id): mgr.cancel_hardware(hw_id, reason, comment) else: CLIAbort('Aborted')
def execute(self, args): manager = HardwareManager(self.client) tags = None if args.get('--tags'): tags = [tag.strip() for tag in args.get('--tags').split(',')] servers = manager.list_hardware( hostname=args.get('--hostname'), domain=args.get('--domain'), cpus=args.get('--cpu'), memory=args.get('--memory'), datacenter=args.get('--datacenter'), nic_speed=args.get('--network'), tags=tags) table = Table([ 'id', 'datacenter', 'host', 'cores', 'memory', 'primary_ip', 'backend_ip', 'active_transaction' ]) table.sortby = args.get('--sortby') or 'host' for server in servers: server = NestedDict(server) table.add_row([ server['id'], server['datacenter']['name'] or blank(), server['fullyQualifiedDomainName'], server['processorPhysicalCoreAmount'], gb(server['memoryCapacity'] or 0), server['primaryIpAddress'] or blank(), server['primaryBackendIpAddress'] or blank(), active_txn(server), ]) return table
def get_hardware_manager(): return HardwareManager(get_client())
def _process_args(self, args, ds_options): """ Helper method to centralize argument processing without convoluting code flow of the main execute method. """ mgr = HardwareManager(self.client) order = { 'hostname': args['--hostname'], 'domain': args['--domain'], 'bare_metal': False, 'package_id': args['--chassis'], } # Determine if this is a "Bare Metal Instance" or regular server bmc = False if args['--chassis'] == str(mgr.get_bare_metal_package_id()): bmc = True # Convert the OS code back into a price ID os_price = self._get_price_id_from_options(ds_options, 'os', args['--os']) if os_price: order['os'] = os_price else: raise CLIAbort('Invalid operating system specified.') order['location'] = args['--datacenter'] or 'FIRST_AVAILABLE' if bmc: order['server'] = self._get_cpu_and_memory_price_ids( ds_options, args['--cpu'], args['--memory']) order['bare_metal'] = True if args['--billing'] == 'hourly': order['hourly'] = True else: order['server'] = args['--cpu'] order['ram'] = self._get_price_id_from_options( ds_options, 'memory', int(args['--memory'])) # Set the disk sizes disk_prices = [] disk_number = 0 for disk in args.get('--disk'): disk_price = self._get_disk_price(ds_options, disk, disk_number) disk_number += 1 if disk_price: disk_prices.append(disk_price) if not disk_prices: disk_prices.append(self._get_default_value(ds_options, 'disk0')) order['disks'] = disk_prices # Set the disk controller price if not bmc: if args.get('--controller'): dc_price = self._get_price_id_from_options( ds_options, 'disk_controller', args.get('--controller')) else: dc_price = self._get_price_id_from_options(ds_options, 'disk_controller', 'None') order['disk_controller'] = dc_price # Set the port speed port_speed = args.get('--network') or '100' nic_price = self._get_price_id_from_options(ds_options, 'nic', port_speed) if nic_price: order['port_speed'] = nic_price else: raise CLIAbort('Invalid NIC speed specified.') if args.get('--postinstall'): order['post_uri'] = args.get('--postinstall') # Get the SSH keys if args.get('--key'): keys = [] for key in args.get('--key'): key_id = resolve_id(SshKeyManager(self.client).resolve_ids, key, 'SshKey') keys.append(key_id) order['ssh_keys'] = keys if args.get('--vlan_public'): order['public_vlan'] = args['--vlan_public'] if args.get('--vlan_private'): order['private_vlan'] = args['--vlan_private'] return order
def execute(self, args): mgr = HardwareManager(self.client) table = KeyValueTable(['Name', 'Value']) table.align['Name'] = 'r' table.align['Value'] = 'l' chassis_id = args.get('<chassis_id>') ds_options = mgr.get_dedicated_server_create_options(chassis_id) show_all = True for opt_name in self.options: if args.get("--" + opt_name): show_all = False break if args['--all']: show_all = True if args['--datacenter'] or show_all: results = self.get_create_options(ds_options, 'datacenter')[0] table.add_row([results[0], listing(sorted(results[1]))]) if args['--cpu'] or show_all: results = self.get_create_options(ds_options, 'cpu') cpu_table = Table(['id', 'description']) for result in sorted(results): cpu_table.add_row([result[1], result[0]]) table.add_row(['cpu', cpu_table]) if args['--memory'] or show_all: results = self.get_create_options(ds_options, 'memory')[0] table.add_row([results[0], listing( item[0] for item in sorted(results[1]))]) if args['--os'] or show_all: results = self.get_create_options(ds_options, 'os') for result in results: table.add_row([ result[0], listing( [item[0] for item in sorted(result[1])], separator=linesep )]) if args['--disk'] or show_all: results = self.get_create_options(ds_options, 'disk')[0] table.add_row([ results[0], listing( [item[0] for item in sorted(results[1])], separator=linesep )]) if args['--nic'] or show_all: results = self.get_create_options(ds_options, 'nic') for result in results: table.add_row([result[0], listing( item[0] for item in sorted(result[1],))]) if args['--controller'] or show_all: results = self.get_create_options(ds_options, 'disk_controller')[0] table.add_row([results[0], listing( item[0] for item in sorted(results[1],))]) return table
class HardwareTests(unittest.TestCase): def setUp(self): self.client = FixtureClient() self.hardware = HardwareManager(self.client) def test_list_hardware(self): mcall = call(mask=ANY, filter={}) results = self.hardware.list_hardware() self.client['Account'].getHardware.assert_has_calls(mcall) self.assertEqual(results, Account.getHardware) def test_list_hardware_with_filters(self): results = self.hardware.list_hardware( tags=['tag1', 'tag2'], cpus=2, memory=1, hostname='hostname', domain='example.com', datacenter='dal05', nic_speed=100, public_ip='1.2.3.4', private_ip='4.3.2.1', ) self.client['Account'].getHardware.assert_has_calls(call( filter={ 'hardware': { 'datacenter': {'name': {'operation': '_= dal05'}}, 'domain': {'operation': '_= example.com'}, 'tagReferences': { 'tag': {'name': { 'operation': 'in', 'options': [ {'name': 'data', 'value': ['tag1', 'tag2']}] }} }, 'memoryCapacity': {'operation': 1}, 'processorPhysicalCoreAmount': {'operation': 2}, 'hostname': {'operation': '_= hostname'}, 'primaryIpAddress': {'operation': '_= 1.2.3.4'}, 'networkComponents': {'maxSpeed': {'operation': 100}}, 'primaryBackendIpAddress': {'operation': '_= 4.3.2.1'}} }, mask=ANY, )) self.assertEqual(results, Account.getHardware) def test_resolve_ids_ip(self): _id = self.hardware._get_ids_from_ip('172.16.1.100') self.assertEqual(_id, [1000, 1001]) _id = self.hardware._get_ids_from_ip('nope') self.assertEqual(_id, []) # Now simulate a private IP test self.client['Account'].getHardware.side_effect = [[], [{'id': 99}]] _id = self.hardware._get_ids_from_ip('10.0.1.87') self.assertEqual(_id, [99]) def test_resolve_ids_hostname(self): _id = self.hardware._get_ids_from_hostname('hardware-test1') self.assertEqual(_id, [1000, 1001]) def test_get_hardware(self): result = self.hardware.get_hardware(1000) self.client['Hardware_Server'].getObject.assert_called_once_with( id=1000, mask=ANY) self.assertEqual(Hardware_Server.getObject, result) def test_reload(self): post_uri = 'http://test.sftlyr.ws/test.sh' self.hardware.reload(1, post_uri=post_uri, ssh_keys=[1701]) f = self.client['Hardware_Server'].reloadOperatingSystem f.assert_called_once_with('FORCE', {'customProvisionScriptUri': post_uri, 'sshKeyIds': [1701]}, id=1) def test_get_bare_metal_create_options_returns_none_on_error(self): self.client['Product_Package'].getAllObjects.return_value = [ {'name': 'No Matching Instances', 'id': 0}] self.assertIsNone(self.hardware.get_bare_metal_create_options()) def test_get_bare_metal_create_options(self): package_id = 50 self.hardware.get_bare_metal_create_options() f1 = self.client['Product_Package'].getRegions f1.assert_called_once_with(id=package_id) f2 = self.client['Product_Package'].getConfiguration f2.assert_called_once_with(id=package_id, mask='mask[itemCategory[group]]') f3 = self.client['Product_Package'].getCategories f3.assert_called_once_with(id=package_id) def test_generate_create_dict_with_all_bare_metal_options(self): args = { 'server': 100, 'hostname': 'unicorn', 'domain': 'giggles.woo', 'disks': [500], 'location': 'Wyrmshire', 'os': 200, 'port_speed': 600, 'bare_metal': True, 'hourly': True, 'public_vlan': 10234, 'private_vlan': 20468, } expected = { 'hardware': [ { 'domain': 'giggles.woo', 'bareMetalInstanceFlag': True, 'hostname': 'unicorn', 'primaryBackendNetworkComponent': {'networkVlan': {'id': 20468}}, 'primaryNetworkComponent': {'networkVlan': {'id': 10234}}, } ], 'prices': [ {'id': 100}, {'id': 500}, {'id': 200}, {'id': 600}, {'id': 12000} ], 'useHourlyPricing': True, 'location': 'Wyrmshire', 'packageId': 50 } data = self.hardware._generate_create_dict(**args) self.assertEqual(expected, data) def test_generate_create_dict_with_all_dedicated_server_options(self): args = { 'server': 100, 'hostname': 'unicorn', 'domain': 'giggles.woo', 'disks': [1000, 1000, 1000, 1000], 'location': 'Wyrmshire', 'os': 200, 'port_speed': 600, 'bare_metal': False, 'package_id': 13, 'ram': 1400, 'disk_controller': 1500, 'ssh_keys': [3000, 3001], 'public_vlan': 10234, 'private_vlan': 20468, } expected = { 'hardware': [ { 'domain': 'giggles.woo', 'bareMetalInstanceFlag': False, 'hostname': 'unicorn', 'primaryBackendNetworkComponent': {'networkVlan': {'id': 20468}}, 'primaryNetworkComponent': {'networkVlan': {'id': 10234}}, } ], 'prices': [ {'id': 100}, {'id': 1000}, {'id': 1000}, {'id': 1000}, {'id': 1000}, {'id': 200}, {'id': 600}, {'id': 1400}, {'id': 1500}], 'sshKeys': [{'sshKeyIds': [3000, 3001]}], 'location': 'Wyrmshire', 'packageId': 13 } data = self.hardware._generate_create_dict(**args) self.assertEqual(expected, data) @patch('SoftLayer.managers.hardware.HardwareManager._generate_create_dict') def test_verify_order(self, create_dict): create_dict.return_value = {'test': 1, 'verify': 1} self.hardware.verify_order(test=1, verify=1) create_dict.assert_called_once_with(test=1, verify=1) f = self.client['Product_Order'].verifyOrder f.assert_called_once_with({'test': 1, 'verify': 1}) @patch('SoftLayer.managers.hardware.HardwareManager._generate_create_dict') def test_place_order(self, create_dict): create_dict.return_value = {'test': 1, 'verify': 1} self.hardware.place_order(test=1, verify=1) create_dict.assert_called_once_with(test=1, verify=1) f = self.client['Product_Order'].placeOrder f.assert_called_once_with({'test': 1, 'verify': 1}) def test_cancel_metal_immediately(self): b_id = 6327 result = self.hardware.cancel_metal(b_id, immediate=True) f = self.client['Billing_Item'].cancelService f.assert_called_once_with(id=b_id) self.assertEqual(result, Billing_Item.cancelService) def test_cancel_metal_on_anniversary(self): b_id = 6327 result = self.hardware.cancel_metal(b_id, False) f = self.client['Billing_Item'].cancelServiceOnAnniversaryDate f.assert_called_once_with(id=b_id) self.assertEqual(result, Billing_Item.cancelServiceOnAnniversaryDate) def test_cancel_hardware_without_reason(self): hw_id = 987 result = self.hardware.cancel_hardware(hw_id) reasons = self.hardware.get_cancellation_reasons() f = self.client['Ticket'].createCancelServerTicket f.assert_called_once_with(hw_id, reasons['unneeded'], '', True, 'HARDWARE') self.assertEqual(result, Ticket.createCancelServerTicket) def test_cancel_hardware_with_reason_and_comment(self): hw_id = 987 reason = 'sales' comment = 'Test Comment' self.hardware.cancel_hardware(hw_id, reason, comment) reasons = self.hardware.get_cancellation_reasons() f = self.client['Ticket'].createCancelServerTicket f.assert_called_once_with(hw_id, reasons[reason], comment, True, 'HARDWARE') def test_change_port_speed_public(self): hw_id = 1 speed = 100 self.hardware.change_port_speed(hw_id, True, speed) f = self.client['Hardware_Server'].setPublicNetworkInterfaceSpeed f.assert_called_once_with(speed, id=hw_id) def test_change_port_speed_private(self): hw_id = 2 speed = 10 self.hardware.change_port_speed(hw_id, False, speed) f = self.client['Hardware_Server'].setPrivateNetworkInterfaceSpeed f.assert_called_once_with(speed, id=hw_id) def test_get_available_dedicated_server_packages(self): self.hardware.get_available_dedicated_server_packages() f = self.client['Product_Package'].getObject f.assert_has_calls([call(id=13, mask='mask[id, name, description]')]) def test_get_dedicated_server_options(self): package_id = 13 self.hardware.get_dedicated_server_create_options(package_id) f1 = self.client['Product_Package'].getRegions f1.assert_called_once_with(id=package_id) f2 = self.client['Product_Package'].getConfiguration f2.assert_called_once_with(id=package_id, mask='mask[itemCategory[group]]') f3 = self.client['Product_Package'].getCategories f3.assert_called_once_with(id=package_id) def test_get_default_value_returns_none_for_unknown_category(self): package_options = {'categories': ['Cat1', 'Cat2']} self.assertEqual(None, get_default_value(package_options, 'Unknown Category')) def test_get_default_value(self): price_id = 9876 package_options = {'categories': {'Cat1': { 'items': [{ 'prices': [{ 'setupFee': 0, 'recurringFee': 0, }], 'price_id': price_id, }] }}} self.assertEqual(price_id, get_default_value(package_options, 'Cat1')) def test_edit(self): # Test editing user data self.hardware.edit(100, userdata='my data') service = self.client['Hardware_Server'] service.setUserMetadata.assert_called_once_with(['my data'], id=100) # Now test a blank edit self.assertTrue(self.hardware.edit, 100) # Finally, test a full edit args = { 'hostname': 'new-host', 'domain': 'new.sftlyr.ws', 'notes': 'random notes', } self.hardware.edit(100, **args) service.editObject.assert_called_once_with(args, id=100)
def setUp(self): self.client = MagicMock() self.hardware = HardwareManager(self.client)
def setUp(self): self.client = FixtureClient() self.hardware = HardwareManager(self.client)
def execute(self, args): update_with_template_args(args) mgr = HardwareManager(self.client) # Disks will be a comma-separated list. Let's make it a real list. if isinstance(args.get("--disk"), str): args["--disk"] = args.get("--disk").split(",") # Do the same thing for SSH keys if isinstance(args.get("--key"), str): args["--key"] = args.get("--key").split(",") self._validate_args(args) bmi_options = mgr.get_bare_metal_create_options() order = {"hostname": args["--hostname"], "domain": args["--domain"], "bare_metal": True} # Validate the CPU/Memory combination and get the price ID server_core = self._get_cpu_and_memory_price_ids(bmi_options, args["--cpu"], args["--memory"]) if server_core: order["server"] = server_core else: raise CLIAbort("Invalid CPU/memory combination specified.") order["hourly"] = args["--hourly"] # Convert the OS code back into a price ID os_price = self._get_price_id_from_options(bmi_options, "os", args["--os"]) if os_price: order["os"] = os_price else: raise CLIAbort("Invalid operating system specified.") order["location"] = args["--datacenter"] or "FIRST_AVAILABLE" # Set the disk size disk_prices = [] for disk in args.get("--disk"): disk_price = self._get_price_id_from_options(bmi_options, "disk", disk) if disk_price: disk_prices.append(disk_price) if not disk_prices: disk_prices.append(self._get_default_value(bmi_options, "disk0")) order["disks"] = disk_prices # Set the port speed port_speed = args.get("--network") or 100 nic_price = self._get_price_id_from_options(bmi_options, "nic", str(port_speed)) if nic_price: order["port_speed"] = nic_price else: raise CLIAbort("Invalid network speed specified.") # Get the SSH keys if args.get("--key"): keys = [] for key in args.get("--key"): key_id = resolve_id(SshKeyManager(self.client).resolve_ids, key, "SshKey") keys.append(key_id) order["ssh_keys"] = keys if args.get("--vlan_public"): order["public_vlan"] = args["--vlan_public"] if args.get("--vlan_private"): order["private_vlan"] = args["--vlan_private"] # Begin output t = Table(["Item", "cost"]) t.align["Item"] = "r" t.align["cost"] = "r" if args.get("--test"): result = mgr.verify_order(**order) total_monthly = 0.0 total_hourly = 0.0 for price in result["prices"]: monthly_fee = float(price.get("recurringFee", 0.0)) hourly_fee = float(price.get("hourlyRecurringFee", 0.0)) total_monthly += monthly_fee total_hourly += hourly_fee if args.get("--hourly"): rate = "%.2f" % hourly_fee else: rate = "%.2f" % monthly_fee t.add_row([price["item"]["description"], rate]) if args.get("--hourly"): total = total_hourly else: total = total_monthly billing_rate = "monthly" if args.get("--hourly"): billing_rate = "hourly" t.add_row(["Total %s cost" % billing_rate, "%.2f" % total]) output = SequentialOutput() output.append(t) output.append( FormattedItem( "", " -- ! Prices reflected here are retail and do not " "take account level discounts and are not guaranteed.", ) ) elif args["--really"] or confirm("This action will incur charges on your account. Continue?"): result = mgr.place_order(**order) t = KeyValueTable(["name", "value"]) t.align["name"] = "r" t.align["value"] = "l" t.add_row(["id", result["orderId"]]) t.add_row(["created", result["orderDate"]]) output = t else: raise CLIAbort("Aborting bare metal instance order.") return output
def execute(self, args): hardware = HardwareManager(self.client) table = KeyValueTable(['Name', 'Value']) table.align['Name'] = 'r' table.align['Value'] = 'l' hardware_id = resolve_id( hardware.resolve_ids, args.get('<identifier>'), 'hardware') result = hardware.get_hardware(hardware_id) result = NestedDict(result) table.add_row(['id', result['id']]) table.add_row(['hostname', result['fullyQualifiedDomainName']]) table.add_row(['status', result['hardwareStatus']['status']]) table.add_row(['datacenter', result['datacenter']['name'] or blank()]) table.add_row(['cores', result['processorPhysicalCoreAmount']]) table.add_row(['memory', gb(result['memoryCapacity'])]) table.add_row(['public_ip', result['primaryIpAddress'] or blank()]) table.add_row( ['private_ip', result['primaryBackendIpAddress'] or blank()]) table.add_row(['ipmi_ip', result['networkManagementIpAddress'] or blank()]) table.add_row([ 'os', FormattedItem( result['operatingSystem']['softwareLicense'] ['softwareDescription']['referenceCode'] or blank(), result['operatingSystem']['softwareLicense'] ['softwareDescription']['name'] or blank() )]) table.add_row(['created', result['provisionDate'] or blank()]) vlan_table = 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'): user_strs = [] for item in result['operatingSystem']['passwords']: user_strs.append( "%s %s" % (item['username'], item['password'])) table.add_row(['users', listing(user_strs)]) tag_row = [] for tag in result['tagReferences']: tag_row.append(tag['tag']['name']) if tag_row: table.add_row(['tags', listing(tag_row, separator=',')]) if not result['privateNetworkOnlyFlag']: ptr_domains = self.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']]) return table
def execute(self, args): update_with_template_args(args) mgr = HardwareManager(self.client) # Disks will be a comma-separated list. Let's make it a real list. if isinstance(args.get('--disk'), str): args['--disk'] = args.get('--disk').split(',') # Do the same thing for SSH keys if isinstance(args.get('--key'), str): args['--key'] = args.get('--key').split(',') self._validate_args(args) ds_options = mgr.get_dedicated_server_create_options(args['--chassis']) order = { 'hostname': args['--hostname'], 'domain': args['--domain'], 'bare_metal': False, 'package_id': args['--chassis'], } # Convert the OS code back into a price ID os_price = self._get_price_id_from_options(ds_options, 'os', args['--os']) if os_price: order['os'] = os_price else: raise CLIAbort('Invalid operating system specified.') order['location'] = args['--datacenter'] or 'FIRST_AVAILABLE' order['server'] = args['--cpu'] order['ram'] = self._get_price_id_from_options(ds_options, 'memory', int(args['--memory'])) # Set the disk sizes disk_prices = [] disk_number = 0 for disk in args.get('--disk'): disk_price = self._get_disk_price(ds_options, disk, disk_number) disk_number += 1 if disk_price: disk_prices.append(disk_price) if not disk_prices: disk_prices.append(self._get_default_value(ds_options, 'disk0')) order['disks'] = disk_prices # Set the disk controller price if args.get('--controller'): dc_price = self._get_price_id_from_options( ds_options, 'disk_controller', args.get('--controller')) else: dc_price = self._get_price_id_from_options(ds_options, 'disk_controller', 'None') order['disk_controller'] = dc_price # Set the port speed port_speed = args.get('--network') or '100' nic_price = self._get_price_id_from_options(ds_options, 'nic', port_speed) if nic_price: order['port_speed'] = nic_price else: raise CLIAbort('Invalid NIC speed specified.') if args.get('--postinstall'): order['post_uri'] = args.get('--postinstall') # Get the SSH keys if args.get('--key'): keys = [] for key in args.get('--key'): key_id = resolve_id(SshKeyManager(self.client).resolve_ids, key, 'SshKey') keys.append(key_id) order['ssh_keys'] = keys if args.get('--vlan_public'): order['public_vlan'] = args['--vlan_public'] if args.get('--vlan_private'): order['private_vlan'] = args['--vlan_private'] # 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 = 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(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') export_to_template(export_file, args, exclude=['--wait', '--test']) return 'Successfully exported options to a template file.' if do_create: if args['--really'] or confirm( "This action will incur charges on your account. " "Continue?"): result = mgr.place_order(**order) table = 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 else: raise CLIAbort('Aborting dedicated server order.') return output
def execute(self, args): update_with_template_args(args) mgr = HardwareManager(self.client) # Disks will be a comma-separated list. Let's make it a real list. if isinstance(args.get('--disk'), str): args['--disk'] = args.get('--disk').split(',') # Do the same thing for SSH keys if isinstance(args.get('--key'), str): args['--key'] = args.get('--key').split(',') self._validate_args(args) ds_options = mgr.get_dedicated_server_create_options(args['--chassis']) order = self._process_args(args, ds_options) # 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 = 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(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') export_to_template(export_file, args, exclude=['--wait', '--test']) return 'Successfully exported options to a template file.' if do_create: if args['--really'] or confirm( "This action will incur charges on your account. " "Continue?"): result = mgr.place_order(**order) table = 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 else: raise CLIAbort('Aborting dedicated server order.') return output
def execute(self, args): update_with_template_args(args) mgr = HardwareManager(self.client) # Disks will be a comma-separated list. Let's make it a real list. if isinstance(args.get('--disk'), str): args['--disk'] = args.get('--disk').split(',') # Do the same thing for SSH keys if isinstance(args.get('--key'), str): args['--key'] = args.get('--key').split(',') self._validate_args(args) bmi_options = mgr.get_bare_metal_create_options() order = { 'hostname': args['--hostname'], 'domain': args['--domain'], 'bare_metal': True, } # Validate the CPU/Memory combination and get the price ID server_core = self._get_cpu_and_memory_price_ids(bmi_options, args['--cpu'], args['--memory']) if server_core: order['server'] = server_core else: raise CLIAbort('Invalid CPU/memory combination specified.') order['hourly'] = args['--hourly'] # Convert the OS code back into a price ID os_price = self._get_price_id_from_options(bmi_options, 'os', args['--os']) if os_price: order['os'] = os_price else: raise CLIAbort('Invalid operating system specified.') order['location'] = args['--datacenter'] or 'FIRST_AVAILABLE' # Set the disk size disk_prices = [] for disk in args.get('--disk'): disk_price = self._get_price_id_from_options(bmi_options, 'disk', disk) if disk_price: disk_prices.append(disk_price) if not disk_prices: disk_prices.append(self._get_default_value(bmi_options, 'disk0')) order['disks'] = disk_prices # Set the port speed port_speed = args.get('--network') or 100 nic_price = self._get_price_id_from_options(bmi_options, 'nic', str(port_speed)) if nic_price: order['port_speed'] = nic_price else: raise CLIAbort('Invalid network speed specified.') # Get the SSH keys if args.get('--key'): keys = [] for key in args.get('--key'): key_id = resolve_id(SshKeyManager(self.client).resolve_ids, key, 'SshKey') keys.append(key_id) order['ssh_keys'] = keys if args.get('--vlan_public'): order['public_vlan'] = args['--vlan_public'] if args.get('--vlan_private'): order['private_vlan'] = args['--vlan_private'] # Begin output table = Table(['Item', 'cost']) table.align['Item'] = 'r' table.align['cost'] = 'r' if args.get('--test'): result = mgr.verify_order(**order) total_monthly = 0.0 total_hourly = 0.0 for price in result['prices']: monthly_fee = float(price.get('recurringFee', 0.0)) hourly_fee = float(price.get('hourlyRecurringFee', 0.0)) total_monthly += monthly_fee total_hourly += hourly_fee if args.get('--hourly'): rate = "%.2f" % hourly_fee else: rate = "%.2f" % monthly_fee table.add_row([price['item']['description'], rate]) if args.get('--hourly'): total = total_hourly else: total = total_monthly billing_rate = 'monthly' if args.get('--hourly'): billing_rate = 'hourly' table.add_row(['Total %s cost' % billing_rate, "%.2f" % total]) output = SequentialOutput() output.append(table) output.append(FormattedItem( '', ' -- ! Prices reflected here are retail and do not ' 'take account level discounts and are not guaranteed.') ) elif args['--really'] or confirm( "This action will incur charges on your account. Continue?"): result = mgr.place_order(**order) table = 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 else: raise CLIAbort('Aborting bare metal instance order.') return output
def execute(self, args): mgr = HardwareManager(self.client) table = KeyValueTable(['Name', 'Value']) table.align['Name'] = 'r' table.align['Value'] = 'l' chassis_id = args.get('<chassis_id>') found = False for chassis in mgr.get_available_dedicated_server_packages(): if chassis_id == str(chassis[0]): found = True break if not found: raise CLIAbort('Invalid chassis specified.') ds_options = mgr.get_dedicated_server_create_options(chassis_id) show_all = True for opt_name in self.options: if args.get("--" + opt_name): show_all = False break if args['--all']: show_all = True # Determine if this is a "Bare Metal Instance" or regular server bmc = False if chassis_id == str(mgr.get_bare_metal_package_id()): bmc = True if args['--datacenter'] or show_all: results = self.get_create_options(ds_options, 'datacenter')[0] table.add_row([results[0], listing(sorted(results[1]))]) if (args['--cpu'] or show_all) and not bmc: results = self.get_create_options(ds_options, 'cpu') cpu_table = Table(['ID', 'Description']) cpu_table.align['ID'] = 'r' cpu_table.align['Description'] = 'l' for result in sorted(results, key=lambda x: x[1]): cpu_table.add_row([result[1], result[0]]) table.add_row(['cpu', cpu_table]) if (args['--memory'] or show_all) and not bmc: results = self.get_create_options(ds_options, 'memory')[0] table.add_row([results[0], listing( item[0] for item in sorted(results[1]))]) if bmc and (show_all or args['--memory'] or args['--cpu']): results = self.get_create_options(ds_options, 'server_core') memory_cpu_table = Table(['memory', 'cpu']) for result in results: memory_cpu_table.add_row([ result[0], listing( [item[0] for item in sorted( result[1], key=lambda x: int(x[0]) )])]) table.add_row(['memory/cpu', memory_cpu_table]) if args['--os'] or show_all: results = self.get_create_options(ds_options, 'os') for result in results: table.add_row([ result[0], listing( [item[0] for item in sorted(result[1])], separator=linesep )]) if args['--disk'] or show_all: results = self.get_create_options(ds_options, 'disk')[0] table.add_row([ results[0], listing( [item[0] for item in sorted(results[1])], separator=linesep )]) if args['--nic'] or show_all: results = self.get_create_options(ds_options, 'nic') for result in results: table.add_row([result[0], listing( item[0] for item in sorted(result[1],))]) if (args['--controller'] or show_all) and not bmc: results = self.get_create_options(ds_options, 'disk_controller')[0] table.add_row([results[0], listing( item[0] for item in sorted(results[1],))]) return table