def orderServer(counter): uniqueHostname = '' if (args.uniqueId != ''): uniqueHostname = args.uniqueId + "-" uniqueHostname += args.hostname + "-" + str(counter) if (args.verbose): print 'Building: ' + uniqueHostname disks = [] if (args.localDisk): disks.append(args.localDisk1Size) if (args.localDisk and args.localDisk2Size != None): disks.append(args.localDisk2Size) else: disks.append(args.sanDisk1Size) if (args.sanDisk2Size != None): disks.append(args.sanDisk2Size) if (args.sanDisk3Size != None): disks.append(args.sanDisk3Size) if (args.sanDisk4Size != None): disks.append(args.sanDisk4Size) if (args.sanDisk5Size != None): disks.append(args.sanDisk5Size) osCode = '' templateGuid = '' if (args.osCode != None and args.osCode != ''): osCode = args.osCode templateGuid = '' else: osCode = '' templateGuid = args.templateGuid disks = [] # Blank out disks since it will use the template sshKeys = [] if (args.sshKey != None and args.sshKey != ''): sshKeys.append(args.sshKey) vsManager = VSManager(client) instance = vsManager.create_instance(hostname=uniqueHostname, domain=args.domain, cpus=args.cpus, memory=args.memory, hourly=args.hourly, datacenter=args.datacenter, os_code=osCode, image_id=templateGuid, local_disk=args.localDisk, disks=disks, ssh_keys=sshKeys, nic_speed=args.nicSpeed, private=args.private, private_vlan=args.privateVlan, dedicated=args.dedicated, post_uri=args.postInstallUrl, tags=args.tag) return instance
def orderServer(hostname, domain, datacenter, cpus, memory, hourly, os_code, image_id, local_disk, disks, nic_speed, post_uri, ssh_keys, private, dedicated, private_vlan, tags): vsManager = VSManager(client) instance = vsManager.create_instance( hostname=hostname, domain=domain, datacenter=datacenter, cpus=cpus, memory=memory, hourly=hourly, os_code=os_code, image_id=image_id, local_disk=local_disk, disks=disks, # [] if image nic_speed=nic_speed, post_uri=post_uri, ssh_keys=ssh_keys, private=private, dedicated=dedicated, private_vlan=private_vlan, tags=tags) #print simplejson.dumps(instance, sort_keys=True, indent=4 * ' ') return instance
def orderServer(self, counter, hostname, domain, cpus, memory, localDisk, private, hourly, datacenter, osCode, privateVlan, nicSpeed, dedicated, templateGuid, postInstallUrl, tag): uniqueHostname = hostname + "-" + str(counter) sshKeys = [] disks = [100] if (osCode != None and osCode != ''): templateGuid = '' else: osCode = '' disks = [] # Blank out disks since it will use the template vsManager = VSManager(self.Client) instance = vsManager.create_instance( hostname = uniqueHostname, domain = domain, cpus = cpus, memory = memory, hourly = hourly, datacenter = datacenter, os_code = osCode, image_id = templateGuid, local_disk = localDisk, disks = disks, ssh_keys = sshKeys, nic_speed = nicSpeed, private = private, private_vlan = privateVlan, dedicated = dedicated, post_uri = postInstallUrl, tags = tag) return instance
def orderServer(hostname, domain, cpus, memory, disk, osCode, templateGuid, useLocalDisk, datacenter, private, dedicated, hourly, tag, privateVlan, nicSpeed, sshKey, userData, postInstallUrl): disks = [] disks.append(disk) if (osCode != None and osCode != ''): templateGuid = '' else: osCode = '' disks = [] # Blank out disks since it will use the template sshKeys = [] if (sshKey != None and sshKey != ''): sshKeys.append(sshKey) vsManager = VSManager(client) instance = vsManager.create_instance(hostname=hostname, domain=domain, cpus=cpus, memory=memory, hourly=hourly, datacenter=datacenter, os_code=osCode, image_id=templateGuid, local_disk=useLocalDisk, disks=disks, ssh_keys=sshKeys, nic_speed=nicSpeed, private=private, private_vlan=privateVlan, dedicated=dedicated, post_uri=postInstallUrl, userdata=userData, tags=tag) return instance
def orderServer(hostname,domain,datacenter,cpus,memory,hourly,os_code,image_id,local_disk,disks,nic_speed,post_uri,ssh_keys,private,dedicated,private_vlan,tags): vsManager = VSManager(client) instance = vsManager.create_instance( hostname = hostname, domain = domain, datacenter = datacenter, cpus = cpus, memory = memory, hourly = hourly, os_code = os_code, image_id = image_id, local_disk = local_disk, disks = disks, # [] if image nic_speed = nic_speed, post_uri = post_uri, ssh_keys = ssh_keys, private = private, dedicated = dedicated, private_vlan = private_vlan, tags = tags) #print simplejson.dumps(instance, sort_keys=True, indent=4 * ' ') return instance
def orderServer(hostname, domain, cpus, memory, disk, osCode, templateGuid, useLocalDisk, datacenter, private, dedicated, hourly, tag, privateVlan, nicSpeed, sshKey, userData, postInstallUrl): disks = [] disks.append(disk) if (osCode != None and osCode != ''): templateGuid = '' else: osCode = '' disks = [] # Blank out disks since it will use the template sshKeys = [] if (sshKey != None and sshKey != ''): sshKeys.append(sshKey) vsManager = VSManager(client) instance = vsManager.create_instance( hostname = hostname, domain = domain, cpus = cpus, memory = memory, hourly = hourly, datacenter = datacenter, os_code = osCode, image_id = templateGuid, local_disk = useLocalDisk, disks = disks, ssh_keys = sshKeys, nic_speed = nicSpeed, private = private, private_vlan = privateVlan, dedicated = dedicated, post_uri = postInstallUrl, userdata = userData, tags = tag) return instance
def execute(self, args): update_with_template_args(args) vsi = VSManager(self.client) self._update_with_like_args(args) # 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(',') # SSH keys may be a comma-separated list. Let's make it a real list. if isinstance(args.get('--key'), str): args['--key'] = args.get('--key').split(',') self._validate_args(args) # Do not create a virtual server with --test or --export do_create = not (args['--export'] or args['--test']) table = Table(['Item', 'cost']) table.align['Item'] = 'r' table.align['cost'] = 'r' data = self._parse_create_args(args) output = [] if args.get('--test'): result = vsi.verify_create_instance(**data) total_monthly = 0.0 total_hourly = 0.0 table = 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('--hourly'): rate = "%.2f" % float(price['hourlyRecurringFee']) else: rate = "%.2f" % float(price['recurringFee']) 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.append(table) output.append(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') 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 = vsi.create_instance(**data) table = 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'], int(args.get('--wait') or 1)) table.add_row(['ready', ready]) else: raise CLIAbort('Aborting virtual server order.') return output
class VSTests(unittest.TestCase): def setUp(self): self.client = FixtureClient() self.vs = VSManager(self.client) def test_list_instances(self): mcall = call(mask=ANY, filter={}) service = self.client['Account'] list_expected_ids = [100, 104] hourly_expected_ids = [104] monthly_expected_ids = [100] results = self.vs.list_instances(hourly=True, monthly=True) service.getVirtualGuests.assert_has_calls(mcall) for result in results: self.assertIn(result['id'], list_expected_ids) result = self.vs.list_instances(hourly=False, monthly=False) service.getVirtualGuests.assert_has_calls(mcall) for result in results: self.assertIn(result['id'], list_expected_ids) results = self.vs.list_instances(hourly=False, monthly=True) service.getMonthlyVirtualGuests.assert_has_calls(mcall) for result in results: self.assertIn(result['id'], monthly_expected_ids) results = self.vs.list_instances(hourly=True, monthly=False) service.getHourlyVirtualGuests.assert_has_calls(mcall) for result in results: self.assertIn(result['id'], hourly_expected_ids) def test_list_instances_with_filters(self): self.vs.list_instances( hourly=True, monthly=True, tags=['tag1', 'tag2'], cpus=2, memory=1024, hostname='hostname', domain='example.com', local_disk=True, datacenter='dal05', nic_speed=100, public_ip='1.2.3.4', private_ip='4.3.2.1', ) service = self.client['Account'] service.getVirtualGuests.assert_has_calls(call( filter={ 'virtualGuests': { 'datacenter': { 'name': {'operation': '_= dal05'}}, 'domain': {'operation': '_= example.com'}, 'tagReferences': { 'tag': {'name': { 'operation': 'in', 'options': [{ 'name': 'data', 'value': ['tag1', 'tag2']}]}}}, 'maxCpu': {'operation': 2}, 'localDiskFlag': {'operation': True}, 'maxMemory': {'operation': 1024}, 'hostname': {'operation': '_= hostname'}, 'networkComponents': {'maxSpeed': {'operation': 100}}, 'primaryIpAddress': {'operation': '_= 1.2.3.4'}, 'primaryBackendIpAddress': {'operation': '_= 4.3.2.1'} }}, mask=ANY, )) def test_resolve_ids_ip(self): service = self.client['Account'] _id = self.vs._get_ids_from_ip('172.16.240.2') self.assertEqual(_id, [100, 104]) _id = self.vs._get_ids_from_ip('nope') self.assertEqual(_id, []) # Now simulate a private IP test service.getVirtualGuests.side_effect = [[], [{'id': 99}]] _id = self.vs._get_ids_from_ip('10.0.1.87') self.assertEqual(_id, [99]) def test_resolve_ids_hostname(self): _id = self.vs._get_ids_from_hostname('vs-test1') self.assertEqual(_id, [100, 104]) def test_get_instance(self): result = self.vs.get_instance(100) self.client['Virtual_Guest'].getObject.assert_called_once_with( id=100, mask=ANY) self.assertEqual(Virtual_Guest.getObject, result) def test_get_create_options(self): results = self.vs.get_create_options() self.assertEqual(Virtual_Guest.getCreateObjectOptions, results) def test_cancel_instance(self): self.vs.cancel_instance(1) self.client['Virtual_Guest'].deleteObject.assert_called_once_with(id=1) def test_reload_instance(self): post_uri = 'http://test.sftlyr.ws/test.sh' self.vs.reload_instance(1, post_uri=post_uri, ssh_keys=[1701]) service = self.client['Virtual_Guest'] f = service.reloadOperatingSystem f.assert_called_once_with('FORCE', {'customProvisionScriptUri': post_uri, 'sshKeyIds': [1701]}, id=1) @patch('SoftLayer.managers.vs.VSManager._generate_create_dict') def test_create_verify(self, create_dict): create_dict.return_value = {'test': 1, 'verify': 1} self.vs.verify_create_instance(test=1, verify=1) create_dict.assert_called_once_with(test=1, verify=1) f = self.client['Virtual_Guest'].generateOrderTemplate f.assert_called_once_with({'test': 1, 'verify': 1}) @patch('SoftLayer.managers.vs.VSManager._generate_create_dict') def test_create_instance(self, create_dict): create_dict.return_value = {'test': 1, 'verify': 1} self.vs.create_instance(test=1, verify=1) create_dict.assert_called_once_with(test=1, verify=1) self.client['Virtual_Guest'].createObject.assert_called_once_with( {'test': 1, 'verify': 1}) def test_generate_os_and_image(self): self.assertRaises( ValueError, self.vs._generate_create_dict, cpus=1, memory=1, hostname='test', domain='example.com', os_code=1, image_id=1, ) def test_generate_missing(self): self.assertRaises(ValueError, self.vs._generate_create_dict) self.assertRaises(ValueError, self.vs._generate_create_dict, cpus=1) def test_generate_basic(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', os_code="STRING", ) assert_data = { 'startCpus': 1, 'maxMemory': 1, 'hostname': 'test', 'domain': 'example.com', 'localDiskFlag': True, 'operatingSystemReferenceCode': "STRING", 'hourlyBillingFlag': True, } self.assertEqual(data, assert_data) def test_generate_monthly(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', os_code="STRING", hourly=False, ) assert_data = { 'hourlyBillingFlag': False, 'startCpus': 1, 'maxMemory': 1, 'hostname': 'test', 'domain': 'example.com', 'localDiskFlag': True, 'operatingSystemReferenceCode': "STRING", } self.assertEqual(data, assert_data) def test_generate_image_id(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', image_id="45", ) assert_data = { 'startCpus': 1, 'maxMemory': 1, 'hostname': 'test', 'domain': 'example.com', 'localDiskFlag': True, 'blockDeviceTemplateGroup': {"globalIdentifier": "45"}, 'hourlyBillingFlag': True, } self.assertEqual(data, assert_data) def test_generate_dedicated(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', os_code="STRING", dedicated=True, ) assert_data = { 'startCpus': 1, 'maxMemory': 1, 'hostname': 'test', 'domain': 'example.com', 'localDiskFlag': True, 'operatingSystemReferenceCode': "STRING", 'hourlyBillingFlag': True, 'dedicatedAccountHostOnlyFlag': True, } self.assertEqual(data, assert_data) def test_generate_datacenter(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', os_code="STRING", datacenter="sng01", ) assert_data = { 'startCpus': 1, 'maxMemory': 1, 'hostname': 'test', 'domain': 'example.com', 'localDiskFlag': True, 'operatingSystemReferenceCode': "STRING", 'hourlyBillingFlag': True, 'datacenter': {"name": 'sng01'}, } self.assertEqual(data, assert_data) def test_generate_public_vlan(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', os_code="STRING", public_vlan=1, ) assert_data = { 'startCpus': 1, 'maxMemory': 1, 'hostname': 'test', 'domain': 'example.com', 'localDiskFlag': True, 'operatingSystemReferenceCode': "STRING", 'hourlyBillingFlag': True, 'primaryNetworkComponent': {"networkVlan": {"id": 1}}, } self.assertEqual(data, assert_data) def test_generate_private_vlan(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', os_code="STRING", private_vlan=1, ) assert_data = { 'startCpus': 1, 'maxMemory': 1, 'hostname': 'test', 'domain': 'example.com', 'localDiskFlag': True, 'operatingSystemReferenceCode': "STRING", 'hourlyBillingFlag': True, 'primaryBackendNetworkComponent': {"networkVlan": {"id": 1}}, } self.assertEqual(data, assert_data) def test_generate_userdata(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', os_code="STRING", userdata="ICANHAZVSI", ) assert_data = { 'startCpus': 1, 'maxMemory': 1, 'hostname': 'test', 'domain': 'example.com', 'localDiskFlag': True, 'operatingSystemReferenceCode': "STRING", 'hourlyBillingFlag': True, 'userData': [{'value': "ICANHAZVSI"}], } self.assertEqual(data, assert_data) def test_generate_network(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', os_code="STRING", nic_speed=9001, ) assert_data = { 'startCpus': 1, 'maxMemory': 1, 'hostname': 'test', 'domain': 'example.com', 'localDiskFlag': True, 'operatingSystemReferenceCode': "STRING", 'hourlyBillingFlag': True, 'networkComponents': [{'maxSpeed': 9001}], } self.assertEqual(data, assert_data) def test_generate_private_network_only(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', os_code="STRING", nic_speed=9001, private=True ) assert_data = { 'startCpus': 1, 'maxMemory': 1, 'hostname': 'test', 'domain': 'example.com', 'localDiskFlag': True, 'operatingSystemReferenceCode': "STRING", 'privateNetworkOnlyFlag': True, 'hourlyBillingFlag': True, 'networkComponents': [{'maxSpeed': 9001}], } self.assertEqual(data, assert_data) def test_generate_post_uri(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', os_code="STRING", post_uri='https://example.com/boostrap.sh', ) assert_data = { 'startCpus': 1, 'maxMemory': 1, 'hostname': 'test', 'domain': 'example.com', 'localDiskFlag': True, 'operatingSystemReferenceCode': "STRING", 'hourlyBillingFlag': True, 'postInstallScriptUri': 'https://example.com/boostrap.sh', } self.assertEqual(data, assert_data) def test_generate_sshkey(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', os_code="STRING", ssh_keys=[543], ) assert_data = { 'startCpus': 1, 'maxMemory': 1, 'hostname': 'test', 'domain': 'example.com', 'localDiskFlag': True, 'operatingSystemReferenceCode': "STRING", 'hourlyBillingFlag': True, 'sshKeys': [{'id': 543}], } self.assertEqual(data, assert_data) def test_generate_no_disks(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', os_code="STRING" ) self.assertEqual(data.get('blockDevices'), None) def test_generate_single_disk(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', os_code="STRING", disks=[50] ) assert_data = { 'blockDevices': [ {"device": "0", "diskImage": {"capacity": 50}}] } self.assertTrue(data.get('blockDevices')) self.assertEqual(data['blockDevices'], assert_data['blockDevices']) def test_generate_multi_disk(self): data = self.vs._generate_create_dict( cpus=1, memory=1, hostname='test', domain='example.com', os_code="STRING", disks=[50, 70, 100] ) assert_data = { 'blockDevices': [ {"device": "0", "diskImage": {"capacity": 50}}, {"device": "2", "diskImage": {"capacity": 70}}, {"device": "3", "diskImage": {"capacity": 100}}] } self.assertTrue(data.get('blockDevices')) self.assertEqual(data['blockDevices'], assert_data['blockDevices']) def test_change_port_speed_public(self): vs_id = 1 speed = 100 self.vs.change_port_speed(vs_id, True, speed) service = self.client['Virtual_Guest'] f = service.setPublicNetworkInterfaceSpeed f.assert_called_once_with(speed, id=vs_id) def test_change_port_speed_private(self): vs_id = 2 speed = 10 self.vs.change_port_speed(vs_id, False, speed) service = self.client['Virtual_Guest'] f = service.setPrivateNetworkInterfaceSpeed f.assert_called_once_with(speed, id=vs_id) def test_edit(self): # Test editing user data service = self.client['Virtual_Guest'] self.vs.edit(100, userdata='my data') service.setUserMetadata.assert_called_once_with(['my data'], id=100) # Now test a blank edit self.assertTrue(self.vs.edit, 100) # Finally, test a full edit args = { 'hostname': 'new-host', 'domain': 'new.sftlyr.ws', 'notes': 'random notes', } self.vs.edit(100, **args) service.editObject.assert_called_once_with(args, id=100) def test_captures(self): archive = self.client['Virtual_Guest'].createArchiveTransaction # capture only the OS disk self.vs.capture(1, 'a') archive.called_once_with('a', [{"device": 0}], "", id=1) archive.reset() # capture all the disks, minus the swap # make sure the data is carried along with it self.vs.capture(1, 'a', additional_disks=True) archive.called_once_with('a', [{"device": 0, "uuid": 1}, {"device": 2, "uuid": 2}], "", id=1) def test_upgrade(self): # Testing Upgrade orderClient = self.client['Product_Order'] # test single upgrade self.vs.upgrade(1, cpus=4, public=False) orderClient.placeOrder.called_once_with(1, cpus=4, public=False) # Now test a blank upgrade self.vs.upgrade(1) self.assertTrue(self.vs.upgrade, 1) # Testing all parameters Upgrade self.vs.upgrade(1, cpus=4, memory=2, nic_speed=1000, public=True) args = {'cpus': 4, 'memory': 2, 'nic_speed': 1000, 'public': 1000} orderClient.placeOrder.called_once_with(1, **args) def test_get_item_id_for_upgrade(self): item_id = 0 package_items = self.client['Product_Package'].getItems(id=46) for item in package_items: if ((item['categories'][0]['id'] == 3) and (item.get('capacity') == '2')): item_id = item['prices'][0]['id'] break self.assertEqual(1133, item_id)
def orderServer(counter): uniqueHostname = '' if (args.uniqueId != ''): uniqueHostname = args.uniqueId + "-" uniqueHostname += args.hostname + "-" + str(counter) if (args.verbose): print 'Building: ' + uniqueHostname disks = [] if (args.localDisk): disks.append(args.localDisk1Size) if (args.localDisk and args.localDisk2Size != None): disks.append(args.localDisk2Size) else: disks.append(args.sanDisk1Size) if (args.sanDisk2Size != None): disks.append(args.sanDisk2Size) if (args.sanDisk3Size != None): disks.append(args.sanDisk3Size) if (args.sanDisk4Size != None): disks.append(args.sanDisk4Size) if (args.sanDisk5Size != None): disks.append(args.sanDisk5Size) osCode = '' templateGuid = '' if (args.osCode != None and args.osCode != ''): osCode = args.osCode templateGuid = '' else: osCode = '' templateGuid = args.templateGuid disks = [] # Blank out disks since it will use the template sshKeys = [] if (args.sshKey != None and args.sshKey != ''): sshKeys.append(args.sshKey) vsManager = VSManager(client) instance = vsManager.create_instance( hostname = uniqueHostname, domain = args.domain, cpus = args.cpus, memory = args.memory, hourly = args.hourly, datacenter = args.datacenter, os_code = osCode, image_id = templateGuid, local_disk = args.localDisk, disks = disks, ssh_keys = sshKeys, nic_speed = args.nicSpeed, private = args.private, private_vlan = args.privateVlan, dedicated = args.dedicated, post_uri = args.postInstallUrl, tags = args.tag) return instance
if config['primary_disk'] == 100: _all_disks.append(100) else: _all_disks.append(25) if config['disk_advanced'] != '': for d in config['disk_advanced'].split(','): _all_disks.append(d) if 'os_code' in new_vsi.keys(): new_vsi['disks'] = _all_disks if config['local_only'] == True: tags.append('softlayer-network-only') new_vsi['tags'] = ','.join(tags) print 'Creating Instance: %s' % _hostname vsi = vs.create_instance(**new_vsi) print 'Created Instance: %s (%d)' % (_hostname, int(vsi['id'])) _callback() print '... Instance called-back'