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>') 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