From a28821c9f0756a8bb6dac493ab6193ee69574ab3 Mon Sep 17 00:00:00 2001 From: Swapnil Date: Wed, 19 Mar 2014 17:26:41 +0530 Subject: [PATCH 1/7] cpu upgrade feature added --- SoftLayer/CLI/modules/cci.py | 57 ++++++++++++++++++++++++++++++++++++ SoftLayer/managers/cci.py | 36 ++++++++++++++++++++++- 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/SoftLayer/CLI/modules/cci.py b/SoftLayer/CLI/modules/cci.py index f58c0d869..3abd5081a 100755 --- a/SoftLayer/CLI/modules/cci.py +++ b/SoftLayer/CLI/modules/cci.py @@ -21,6 +21,7 @@ reboot Reboots a running CCI reload Reload the OS on a CCI based on its current configuration resume Resumes a paused CCI + upgrade Upgrades parameters of a CCI For several commands, will be asked for. This can be the id, hostname or the ip address for a CCI. @@ -1016,3 +1017,59 @@ def execute(self, args): t.add_row(['transaction_id', capture['id']]) t.add_row(['all_disks', additional_disks]) return t + + + +class UpgradeCCI(CLIRunnable): + """ +usage: sl cci upgrade [options] + +Upgrade parameters of an CCI + +Options: + --cpu=CPU Number of CPU cores + --memory=MEMORY Memory in mebibytes + --network=MBPS Network port speed in Mbps + --disk=SIZE... Disks. Can be specified multiple times + --san Use SAN storage instead of local disk. Applies to + all disks specified with --disk. + --reboot Soft Reboot an instanCe +""" + + action ='upgrade' + options = ['confirm'] + def execute(self, args): + cci = CCIManager(self.client) + cpus=args.get('--cpu') + memory=args.get('--memory') + nic_speed=args.get('--network') + instance_id=args.get('') + if not instance_id: + pass + #raise error + reboot=False + if args['--reboot']: + reboot=True + local_disk=True + # 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(',') + disk=args['--disk'] + if args['--SAN']: + local_disk=False + if local_disk: + if not (len(disk)>0 and len(disk)<3): + pass + #raise error + elif not (len(disk)>0 and len(disk)<6): + pass + #raise error + + + data = self._parse_create_args(args) + + if args['--really'] or confirm( + "This action will incur charges on your account. " + "Continue?"): + cci.upgrade(instance_id,cpus,memory,nic_speed,disk,reboot,local_disk) + diff --git a/SoftLayer/managers/cci.py b/SoftLayer/managers/cci.py index 2190ae1d4..5bf4c10b0 100644 --- a/SoftLayer/managers/cci.py +++ b/SoftLayer/managers/cci.py @@ -8,7 +8,7 @@ import socket from time import sleep from itertools import repeat - +import datetime from SoftLayer.utils import NestedDict, query_filter, IdentifierMixin, lookup @@ -484,3 +484,37 @@ def capture(self, instance_id, name, additional_disks=False, notes=None): return self.guest.createArchiveTransaction( name, disks, notes, id=instance_id) + + + def upgrade(self, instance_id, cpus=None, memory=None, nic_speed=None, disk=None,reboot=False,local_disk=False): + """ + Upgrades a CCI instane + + :param int instance_id: Instane id of te CCI to be upgraded + :param int cpus: The number of virtual CPUs to upgrade to of a CCI instance. + :param bool local_disk: Flag to indicate if this should be a local disk + (default) or a SAN disk. + :param list disks: A list of disk capacities for this server + :param bool reboot: Flag to indicate weather to reboot or not upon upgrade + :param int nic_speed: The port speed to set + """ + mask="mask[capacity;prices.id;categories[name,id]]" + package=self.client['Product_Package'] + # only implemeted for Cpu at tHe moVement + cpu_items=package.getItems(id=46,mask=mask) + for item in cpu_items: + if len(filter(lambda x: x.get('id') == 3, item['categories'])) \ + and item.get('capacity') == str(cpus): + item_id=tem['prices'][0]['id'] + + orderClient=self.client['Product_Order'] + orderContainer = {} + orderContainer['complexType'] = 'SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade' + orderContainer['virtualGuests'] = [{'id': instance_id}] + orderContainer['prices'] = [{'id':item_id }] + orderContainer['properties'] = [{'name': 'MAINTENANCE_WINDOW', 'value': str(datetime.datetime.now())}] + orderClient.verifyOrder(orderContainer) + orderClient.placeOrder(orderContainer) + + if reboot: + self.guest.rebootSoft(id=int(instance_id)) From ddd2c8c6e1362e64fe687dccb42c479f5dcbafcb Mon Sep 17 00:00:00 2001 From: Swapnil Date: Thu, 20 Mar 2014 05:07:25 -0500 Subject: [PATCH 2/7] Added Exception Handling and Made pep8 compatible --- SoftLayer/CLI/modules/cci.py | 55 ++++++++++++++++++------------------ SoftLayer/managers/cci.py | 44 +++++++++++++++++------------ 2 files changed, 53 insertions(+), 46 deletions(-) diff --git a/SoftLayer/CLI/modules/cci.py b/SoftLayer/CLI/modules/cci.py index 3abd5081a..face6fb82 100755 --- a/SoftLayer/CLI/modules/cci.py +++ b/SoftLayer/CLI/modules/cci.py @@ -1019,7 +1019,6 @@ def execute(self, args): return t - class UpgradeCCI(CLIRunnable): """ usage: sl cci upgrade [options] @@ -1028,48 +1027,48 @@ class UpgradeCCI(CLIRunnable): Options: --cpu=CPU Number of CPU cores - --memory=MEMORY Memory in mebibytes + --memory=MEMORY Memory in megabytes --network=MBPS Network port speed in Mbps --disk=SIZE... Disks. Can be specified multiple times --san Use SAN storage instead of local disk. Applies to all disks specified with --disk. - --reboot Soft Reboot an instanCe + --reboot Soft Reboot of CCI """ - action ='upgrade' + action = 'upgrade' options = ['confirm'] + def execute(self, args): cci = CCIManager(self.client) - cpus=args.get('--cpu') - memory=args.get('--memory') - nic_speed=args.get('--network') - instance_id=args.get('') + cpus = args.get('--cpu') + memory = args.get('--memory') + nic_speed = args.get('--network') + instance_id = args.get('') if not instance_id: - pass - #raise error - reboot=False + raise ArgumentError('CCI ID must be provided') + reboot = False if args['--reboot']: - reboot=True - local_disk=True + reboot = True + local_disk = True + disk = "" # 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(',') - disk=args['--disk'] + disk = args['--disk'] if args['--SAN']: - local_disk=False + local_disk = False if local_disk: - if not (len(disk)>0 and len(disk)<3): - pass - #raise error - elif not (len(disk)>0 and len(disk)<6): - pass - #raise error - - - data = self._parse_create_args(args) + if not (len(disk) > 0 and len(disk) < 3): + raise ArgumentError('Maximum 2 Disks are + Allowed for Local') + elif not (len(disk) > 0 and len(disk) < 6): + raise ArgumentError('Maximum 5 Disks are Allowed for SAN') if args['--really'] or confirm( - "This action will incur charges on your account. " - "Continue?"): - cci.upgrade(instance_id,cpus,memory,nic_speed,disk,reboot,local_disk) - + "This action will incur charges on your account. " + "Continue?"): + try: + cci.upgrade(instance_id, + cpus, memory, nic_speed, disk, reboot, local_disk) + except: + raise CLIAbort('CCI Upgrade Failed') diff --git a/SoftLayer/managers/cci.py b/SoftLayer/managers/cci.py index 5bf4c10b0..a934b9b84 100644 --- a/SoftLayer/managers/cci.py +++ b/SoftLayer/managers/cci.py @@ -485,36 +485,44 @@ def capture(self, instance_id, name, additional_disks=False, notes=None): return self.guest.createArchiveTransaction( name, disks, notes, id=instance_id) - - def upgrade(self, instance_id, cpus=None, memory=None, nic_speed=None, disk=None,reboot=False,local_disk=False): + def upgrade(self, + instance_id, cpus=None, memory=None, nic_speed=None, disk=None, + reboot=False, local_disk=False): """ Upgrades a CCI instane - :param int instance_id: Instane id of te CCI to be upgraded - :param int cpus: The number of virtual CPUs to upgrade to of a CCI instance. + :param int cpus: The number of virtual CPUs to upgrade to + of a CCI instance. :param bool local_disk: Flag to indicate if this should be a local disk (default) or a SAN disk. :param list disks: A list of disk capacities for this server - :param bool reboot: Flag to indicate weather to reboot or not upon upgrade + :param bool reboot: Flag to indicate weather to reboot or + not upon upgrade :param int nic_speed: The port speed to set """ - mask="mask[capacity;prices.id;categories[name,id]]" - package=self.client['Product_Package'] - # only implemeted for Cpu at tHe moVement - cpu_items=package.getItems(id=46,mask=mask) + mask = "mask[capacity;prices.id;categories[name,id]]" + package = self.client['Product_Package'] + # Only implemeted for Cpu at the movement + if memory: + raise NotImplementedError('Memory Upgrade yet to implement') + if disk: + raise NotImplementedError('Disk Upgrade yet to implement') + if nic_speed: + raise NotImplementedError('NIC Speed Upgrade yet to implement') + cpu_items = package.getItems(id=46, mask=mask) for item in cpu_items: if len(filter(lambda x: x.get('id') == 3, item['categories'])) \ - and item.get('capacity') == str(cpus): - item_id=tem['prices'][0]['id'] - - orderClient=self.client['Product_Order'] + and item.get('capacity') == str(cpus): + item_id = item['prices'][0]['id'] + orderClient = self.client['Product_Order'] orderContainer = {} - orderContainer['complexType'] = 'SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade' - orderContainer['virtualGuests'] = [{'id': instance_id}] - orderContainer['prices'] = [{'id':item_id }] - orderContainer['properties'] = [{'name': 'MAINTENANCE_WINDOW', 'value': str(datetime.datetime.now())}] + orderContainer['complexType'] = \ + 'SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade' + orderContainer['virtualGuests'] = [{'id': int(instance_id)}] + orderContainer['prices'] = [{'id': item_id}] + orderContainer['properties'] = [{'name': 'MAINTENANCE_WINDOW', + 'value': str(datetime.datetime.now())}] orderClient.verifyOrder(orderContainer) orderClient.placeOrder(orderContainer) - if reboot: self.guest.rebootSoft(id=int(instance_id)) From 2e104410eda0078c85aacd367cf0c1969999c359 Mon Sep 17 00:00:00 2001 From: Swapnil Date: Thu, 20 Mar 2014 08:58:59 -0500 Subject: [PATCH 3/7] Slight Code Modification --- SoftLayer/CLI/modules/cci.py | 5 +++-- SoftLayer/managers/cci.py | 10 +++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/SoftLayer/CLI/modules/cci.py b/SoftLayer/CLI/modules/cci.py index face6fb82..aa1443004 100755 --- a/SoftLayer/CLI/modules/cci.py +++ b/SoftLayer/CLI/modules/cci.py @@ -1039,7 +1039,7 @@ class UpgradeCCI(CLIRunnable): options = ['confirm'] def execute(self, args): - cci = CCIManager(self.client) + cci = CCIManager(self.client) cpus = args.get('--cpu') memory = args.get('--memory') nic_speed = args.get('--network') @@ -1059,7 +1059,7 @@ def execute(self, args): local_disk = False if local_disk: if not (len(disk) > 0 and len(disk) < 3): - raise ArgumentError('Maximum 2 Disks are + raise ArgumentError('Maximum 2 Disks are \ Allowed for Local') elif not (len(disk) > 0 and len(disk) < 6): raise ArgumentError('Maximum 5 Disks are Allowed for SAN') @@ -1067,6 +1067,7 @@ def execute(self, args): if args['--really'] or confirm( "This action will incur charges on your account. " "Continue?"): + import pdb;pdb.set_trace() try: cci.upgrade(instance_id, cpus, memory, nic_speed, disk, reboot, local_disk) diff --git a/SoftLayer/managers/cci.py b/SoftLayer/managers/cci.py index a934b9b84..5d6b371d9 100644 --- a/SoftLayer/managers/cci.py +++ b/SoftLayer/managers/cci.py @@ -493,6 +493,7 @@ def upgrade(self, :param int instance_id: Instane id of te CCI to be upgraded :param int cpus: The number of virtual CPUs to upgrade to of a CCI instance. + :param int memory: RAM of the CCI to be upgraded to. :param bool local_disk: Flag to indicate if this should be a local disk (default) or a SAN disk. :param list disks: A list of disk capacities for this server @@ -500,11 +501,13 @@ def upgrade(self, not upon upgrade :param int nic_speed: The port speed to set """ + if memory: + memory = int(memory)/1024 mask = "mask[capacity;prices.id;categories[name,id]]" package = self.client['Product_Package'] # Only implemeted for Cpu at the movement - if memory: - raise NotImplementedError('Memory Upgrade yet to implement') + if cpus: + raise NotImplementedError('CPU Upgrade yet to implement') if disk: raise NotImplementedError('Disk Upgrade yet to implement') if nic_speed: @@ -512,8 +515,9 @@ def upgrade(self, cpu_items = package.getItems(id=46, mask=mask) for item in cpu_items: if len(filter(lambda x: x.get('id') == 3, item['categories'])) \ - and item.get('capacity') == str(cpus): + and item.get('capacity') == str(memory): item_id = item['prices'][0]['id'] + import pdb;pdb.set_trace() orderClient = self.client['Product_Order'] orderContainer = {} orderContainer['complexType'] = \ From b390d41c8bc2d68fddfef511163c071fe1fbac8b Mon Sep 17 00:00:00 2001 From: Swapnil Date: Fri, 21 Mar 2014 04:35:00 -0500 Subject: [PATCH 4/7] adding test cases --- SoftLayer/CLI/modules/cci.py | 10 ++++------ SoftLayer/managers/cci.py | 4 ++-- SoftLayer/tests/managers/cci_tests.py | 28 +++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/SoftLayer/CLI/modules/cci.py b/SoftLayer/CLI/modules/cci.py index aa1443004..a8e7858b2 100755 --- a/SoftLayer/CLI/modules/cci.py +++ b/SoftLayer/CLI/modules/cci.py @@ -1055,15 +1055,13 @@ def execute(self, args): if isinstance(args.get('--disk'), str): args['--disk'] = args.get('--disk').split(',') disk = args['--disk'] - if args['--SAN']: + if args['--san']: local_disk = False if local_disk: - if not (len(disk) > 0 and len(disk) < 3): - raise ArgumentError('Maximum 2 Disks are \ - Allowed for Local') - elif not (len(disk) > 0 and len(disk) < 6): + if len(disk) > 2: + raise ArgumentError('Maximum 2 Disks are Allowed for Local') + elif len(disk) > 5: raise ArgumentError('Maximum 5 Disks are Allowed for SAN') - if args['--really'] or confirm( "This action will incur charges on your account. " "Continue?"): diff --git a/SoftLayer/managers/cci.py b/SoftLayer/managers/cci.py index 5d6b371d9..a1f03449a 100644 --- a/SoftLayer/managers/cci.py +++ b/SoftLayer/managers/cci.py @@ -526,7 +526,7 @@ def upgrade(self, orderContainer['prices'] = [{'id': item_id}] orderContainer['properties'] = [{'name': 'MAINTENANCE_WINDOW', 'value': str(datetime.datetime.now())}] - orderClient.verifyOrder(orderContainer) - orderClient.placeOrder(orderContainer) + verify=orderClient.verifyOrder(orderContainer) + order=orderClient.placeOrder(orderContainer) if reboot: self.guest.rebootSoft(id=int(instance_id)) diff --git a/SoftLayer/tests/managers/cci_tests.py b/SoftLayer/tests/managers/cci_tests.py index 2adb666ee..bd339ae6d 100644 --- a/SoftLayer/tests/managers/cci_tests.py +++ b/SoftLayer/tests/managers/cci_tests.py @@ -532,6 +532,34 @@ def test_captures(self): 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 memory/RAM upgrade + self.cci.upgrade(1, memory = 2048) + + # Now test a blank upgrade + self.assertTrue(self.cci.upgrade, 1) + + # Finally, test a full Upgrade + args = { + 'cpus': 2, + 'memory': 2048, + 'disk': [25,50], + 'nic_speed': 100 } + self.cci.upgrade(1, **args) + + orderContainer = {} + orderContainer['complexType'] = \ + 'SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade' + orderContainer['virtualGuests'] = [{'id': 1}] + orderContainer['prices'] = [{'id': 11}] + orderContainer['properties'] = [{'name': 'MAINTENANCE_WINDOW', + 'value':'2014-03-21 03:12:47.942307'}] + orderClient.verifyOrder.assert_called_once_with(orderContainer) + orderClient.placeOrder.assert_called_once_with(orderContainer) + class CCIWaitReadyGoTests(unittest.TestCase): From 2d8bc5426f7eb29420886e64e19ec04bcd841ade Mon Sep 17 00:00:00 2001 From: Swapnil Date: Fri, 21 Mar 2014 09:37:34 -0500 Subject: [PATCH 5/7] made the code more structered --- SoftLayer/CLI/modules/cci.py | 44 +++++++++++++-------- SoftLayer/managers/cci.py | 56 ++++++++++++++++----------- SoftLayer/tests/managers/cci_tests.py | 7 ++-- 3 files changed, 65 insertions(+), 42 deletions(-) diff --git a/SoftLayer/CLI/modules/cci.py b/SoftLayer/CLI/modules/cci.py index a8e7858b2..f53e9eb13 100755 --- a/SoftLayer/CLI/modules/cci.py +++ b/SoftLayer/CLI/modules/cci.py @@ -1039,35 +1039,47 @@ class UpgradeCCI(CLIRunnable): options = ['confirm'] def execute(self, args): - cci = CCIManager(self.client) - cpus = args.get('--cpu') - memory = args.get('--memory') - nic_speed = args.get('--network') + cci = CCIManager(self.client) + data = {} + + data['cpus'] = args.get('--cpu') + data['memory'] = args.get('--memory') + data['nic_speed'] = args.get('--network') instance_id = args.get('') if not instance_id: raise ArgumentError('CCI ID must be provided') - reboot = False + data['reboot'] = False if args['--reboot']: - reboot = True - local_disk = True - disk = "" + data['reboot'] = True + data['local_disk'] = True + data['disk'] = "" # 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(',') - disk = args['--disk'] + data['disk'] = args['--disk'] if args['--san']: - local_disk = False - if local_disk: - if len(disk) > 2: + data['local_disk'] = False + if data['local_disk']: + if len(data['disk']) > 2: raise ArgumentError('Maximum 2 Disks are Allowed for Local') - elif len(disk) > 5: + elif len(data['disk']) > 5: raise ArgumentError('Maximum 5 Disks are Allowed for SAN') + if data['memory']: + data['memory'] = int(data['memory'])/1024 + if data['disk']: + raise NotImplementedError('Disk Upgrade yet to implement') if args['--really'] or confirm( "This action will incur charges on your account. " "Continue?"): - import pdb;pdb.set_trace() + item_id = [] try: - cci.upgrade(instance_id, - cpus, memory, nic_speed, disk, reboot, local_disk) + package_items = cci.get_package_items_for_virtual_guest() + if data['cpus']: + item_id.append({'id': cci.get_item_id_for_upgrade(package_items, 'cpus', data['cpus'])}) + if data['memory']: + item_id.append({'id': cci.get_item_id_for_upgrade(package_items, 'memory', data['memory'])}) + if data['nic_speed']: + item_id.append({'id': cci.get_item_id_for_upgrade(package_items, 'nic_speed', data['nic_speed'])}) + cci.upgrade(instance_id, item_id) except: raise CLIAbort('CCI Upgrade Failed') diff --git a/SoftLayer/managers/cci.py b/SoftLayer/managers/cci.py index a1f03449a..cd2f9f2ea 100644 --- a/SoftLayer/managers/cci.py +++ b/SoftLayer/managers/cci.py @@ -485,9 +485,9 @@ def capture(self, instance_id, name, additional_disks=False, notes=None): return self.guest.createArchiveTransaction( name, disks, notes, id=instance_id) - def upgrade(self, - instance_id, cpus=None, memory=None, nic_speed=None, disk=None, - reboot=False, local_disk=False): + def upgrade(self, instance_id, item_id): + #instance_id, cpus=None, memory=None, nic_speed=None, disk=None, + #reboot=False, local_disk=False): """ Upgrades a CCI instane :param int instance_id: Instane id of te CCI to be upgraded @@ -501,32 +501,42 @@ def upgrade(self, not upon upgrade :param int nic_speed: The port speed to set """ - if memory: - memory = int(memory)/1024 - mask = "mask[capacity;prices.id;categories[name,id]]" - package = self.client['Product_Package'] - # Only implemeted for Cpu at the movement - if cpus: - raise NotImplementedError('CPU Upgrade yet to implement') - if disk: - raise NotImplementedError('Disk Upgrade yet to implement') - if nic_speed: - raise NotImplementedError('NIC Speed Upgrade yet to implement') - cpu_items = package.getItems(id=46, mask=mask) - for item in cpu_items: - if len(filter(lambda x: x.get('id') == 3, item['categories'])) \ - and item.get('capacity') == str(memory): - item_id = item['prices'][0]['id'] - import pdb;pdb.set_trace() orderClient = self.client['Product_Order'] orderContainer = {} orderContainer['complexType'] = \ 'SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade' orderContainer['virtualGuests'] = [{'id': int(instance_id)}] - orderContainer['prices'] = [{'id': item_id}] + orderContainer['prices'] = item_id orderContainer['properties'] = [{'name': 'MAINTENANCE_WINDOW', 'value': str(datetime.datetime.now())}] - verify=orderClient.verifyOrder(orderContainer) - order=orderClient.placeOrder(orderContainer) + orderClient.verifyOrder(orderContainer) + orderClient.placeOrder(orderContainer) if reboot: self.guest.rebootSoft(id=int(instance_id)) + + + + def get_package_items_for_virtual_guest(self): + mask = "mask[capacity,prices.id,categories[name,id]]" + package = self.client['Product_Package'] + return package.getItems(id=46, mask=mask) + + + + def get_item_id_for_upgrade(self, package_items, option, value): + """ + Find the item ids for the parameters you want to upgrade to. + :param list package_items: Contains all the items related to an CCI + :param string option: Describes type of paramter to be upgraded + :param int value: The value of the parameter to be upgraded + """ + id = {'memory': 3, 'cpus': 80, 'nic_speed': 26, + 'First Disk' : 81, 'Second Disk': 82, 'Third Disk': 92, + 'Fourth Disk': 93,'Fifth Disk': 116} + f=0 + for item in package_items: + if len(filter(lambda x: x.get('id') == id[option], item['categories'])) \ + and item.get('capacity') == str(value): + if f==1: + return item['prices'][0]['id'] + else: f=f+1 diff --git a/SoftLayer/tests/managers/cci_tests.py b/SoftLayer/tests/managers/cci_tests.py index bd339ae6d..023f149c6 100644 --- a/SoftLayer/tests/managers/cci_tests.py +++ b/SoftLayer/tests/managers/cci_tests.py @@ -532,10 +532,11 @@ def test_captures(self): 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 memory/RAM upgrade self.cci.upgrade(1, memory = 2048) @@ -554,12 +555,12 @@ def test_upgrade_(self): orderContainer['complexType'] = \ 'SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade' orderContainer['virtualGuests'] = [{'id': 1}] - orderContainer['prices'] = [{'id': 11}] + orderContainer['prices'] = [{'id': 1645}] orderContainer['properties'] = [{'name': 'MAINTENANCE_WINDOW', 'value':'2014-03-21 03:12:47.942307'}] orderClient.verifyOrder.assert_called_once_with(orderContainer) orderClient.placeOrder.assert_called_once_with(orderContainer) - + class CCIWaitReadyGoTests(unittest.TestCase): From ebd63a4be89ad215232c7cd460746a71c1dcac99 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 23 Mar 2014 07:39:55 -0500 Subject: [PATCH 6/7] slight modification --- SoftLayer/CLI/modules/cci.py | 21 +++++++++++++-------- SoftLayer/managers/cci.py | 29 ++++++++++++++--------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/SoftLayer/CLI/modules/cci.py b/SoftLayer/CLI/modules/cci.py index f53e9eb13..b3fa050a1 100755 --- a/SoftLayer/CLI/modules/cci.py +++ b/SoftLayer/CLI/modules/cci.py @@ -1041,16 +1041,15 @@ class UpgradeCCI(CLIRunnable): def execute(self, args): cci = CCIManager(self.client) data = {} - data['cpus'] = args.get('--cpu') data['memory'] = args.get('--memory') data['nic_speed'] = args.get('--network') instance_id = args.get('') if not instance_id: raise ArgumentError('CCI ID must be provided') - data['reboot'] = False + reboot = False if args['--reboot']: - data['reboot'] = True + reboot = True data['local_disk'] = True data['disk'] = "" # Disks will be a comma-separated list. Let's make it a real list. @@ -1061,25 +1060,31 @@ def execute(self, args): data['local_disk'] = False if data['local_disk']: if len(data['disk']) > 2: - raise ArgumentError('Maximum 2 Disks are Allowed for Local') + raise ArgumentError( \ + 'Maximum 2 Disks are Allowed for Local') elif len(data['disk']) > 5: raise ArgumentError('Maximum 5 Disks are Allowed for SAN') if data['memory']: - data['memory'] = int(data['memory'])/1024 + data['memory'] = int(data['memory']) / 1024 if data['disk']: raise NotImplementedError('Disk Upgrade yet to implement') if args['--really'] or confirm( "This action will incur charges on your account. " "Continue?"): item_id = [] + import pdb;pdb.set_trace() try: package_items = cci.get_package_items_for_virtual_guest() if data['cpus']: - item_id.append({'id': cci.get_item_id_for_upgrade(package_items, 'cpus', data['cpus'])}) + item_id.append({'id': cci.get_item_id_for_upgrade( + package_items, 'cpus', data['cpus'])}) if data['memory']: - item_id.append({'id': cci.get_item_id_for_upgrade(package_items, 'memory', data['memory'])}) + item_id.append({'id': cci.get_item_id_for_upgrade( + package_items, 'memory', data['memory'])}) if data['nic_speed']: - item_id.append({'id': cci.get_item_id_for_upgrade(package_items, 'nic_speed', data['nic_speed'])}) + item_id.append({'id': cci.get_item_id_for_upgrade( + package_items, 'nic_speed', + data['nic_speed'])}) cci.upgrade(instance_id, item_id) except: raise CLIAbort('CCI Upgrade Failed') diff --git a/SoftLayer/managers/cci.py b/SoftLayer/managers/cci.py index cd2f9f2ea..2c3321cf9 100644 --- a/SoftLayer/managers/cci.py +++ b/SoftLayer/managers/cci.py @@ -485,9 +485,7 @@ def capture(self, instance_id, name, additional_disks=False, notes=None): return self.guest.createArchiveTransaction( name, disks, notes, id=instance_id) - def upgrade(self, instance_id, item_id): - #instance_id, cpus=None, memory=None, nic_speed=None, disk=None, - #reboot=False, local_disk=False): + def upgrade(self, instance_id, item_id, reboot = False): """ Upgrades a CCI instane :param int instance_id: Instane id of te CCI to be upgraded @@ -514,29 +512,30 @@ def upgrade(self, instance_id, item_id): if reboot: self.guest.rebootSoft(id=int(instance_id)) - - def get_package_items_for_virtual_guest(self): mask = "mask[capacity,prices.id,categories[name,id]]" package = self.client['Product_Package'] - return package.getItems(id=46, mask=mask) - - + return package.getItems(id=46, mask=mask) def get_item_id_for_upgrade(self, package_items, option, value): """ Find the item ids for the parameters you want to upgrade to. :param list package_items: Contains all the items related to an CCI :param string option: Describes type of paramter to be upgraded - :param int value: The value of the parameter to be upgraded + :param int value: The value of the parameter to be upgraded """ id = {'memory': 3, 'cpus': 80, 'nic_speed': 26, - 'First Disk' : 81, 'Second Disk': 82, 'Third Disk': 92, - 'Fourth Disk': 93,'Fifth Disk': 116} - f=0 + 'First Disk': 81, 'Second Disk': 82, 'Third Disk': 92, + 'Fourth Disk': 93, 'Fifth Disk': 116} + f = 0 for item in package_items: - if len(filter(lambda x: x.get('id') == id[option], item['categories'])) \ + if len(filter(lambda x: x.get('id') == id[option], \ + item['categories'])) \ and item.get('capacity') == str(value): - if f==1: + if option == 'cpus' or option =='nic_speed': + if f == 1: + return item['prices'][0]['id'] + else: + f = f + 1 + else: return item['prices'][0]['id'] - else: f=f+1 From e4da33999a5172f4e291d5a939b52cd7e100302e Mon Sep 17 00:00:00 2001 From: Swapnil Khanapurkar Date: Mon, 24 Mar 2014 05:32:57 -0500 Subject: [PATCH 7/7] Test cases implementation, pep8 compatible and slight code modifications --- SoftLayer/CLI/modules/cci.py | 30 +++++++++++++++--- SoftLayer/managers/cci.py | 23 -------------- SoftLayer/tests/fixtures/Virtual_Guest.py | 1 + SoftLayer/tests/managers/cci_tests.py | 37 ++++++++++++++--------- 4 files changed, 49 insertions(+), 42 deletions(-) diff --git a/SoftLayer/CLI/modules/cci.py b/SoftLayer/CLI/modules/cci.py index b3fa050a1..fa3791c79 100755 --- a/SoftLayer/CLI/modules/cci.py +++ b/SoftLayer/CLI/modules/cci.py @@ -1072,19 +1072,41 @@ def execute(self, args): "This action will incur charges on your account. " "Continue?"): item_id = [] - import pdb;pdb.set_trace() try: package_items = cci.get_package_items_for_virtual_guest() if data['cpus']: - item_id.append({'id': cci.get_item_id_for_upgrade( + item_id.append({'id': self.get_item_id_for_upgrade( package_items, 'cpus', data['cpus'])}) if data['memory']: - item_id.append({'id': cci.get_item_id_for_upgrade( + item_id.append({'id': self.get_item_id_for_upgrade( package_items, 'memory', data['memory'])}) if data['nic_speed']: - item_id.append({'id': cci.get_item_id_for_upgrade( + item_id.append({'id': self.get_item_id_for_upgrade( package_items, 'nic_speed', data['nic_speed'])}) cci.upgrade(instance_id, item_id) except: raise CLIAbort('CCI Upgrade Failed') + + def get_item_id_for_upgrade(self, package_items, option, value): + """ + Find the item ids for the parameters you want to upgrade to. + :param list package_items: Contains all the items related to an CCI + :param string option: Describes type of paramter to be upgraded + :param int value: The value of the parameter to be upgraded + """ + id = {'memory': 3, 'cpus': 80, 'nic_speed': 26, + 'First Disk': 81, 'Second Disk': 82, 'Third Disk': 92, + 'Fourth Disk': 93, 'Fifth Disk': 116} + f = 0 + for item in package_items: + if len(filter(lambda x: x.get('id') == id[option], \ + item['categories'])) \ + and item.get('capacity') == str(value): + if option == 'cpus' or option =='nic_speed': + if f == 1: + return item['prices'][0]['id'] + else: + f = f + 1 + else: + return item['prices'][0]['id'] diff --git a/SoftLayer/managers/cci.py b/SoftLayer/managers/cci.py index 2c3321cf9..0f16aad82 100644 --- a/SoftLayer/managers/cci.py +++ b/SoftLayer/managers/cci.py @@ -516,26 +516,3 @@ def get_package_items_for_virtual_guest(self): mask = "mask[capacity,prices.id,categories[name,id]]" package = self.client['Product_Package'] return package.getItems(id=46, mask=mask) - - def get_item_id_for_upgrade(self, package_items, option, value): - """ - Find the item ids for the parameters you want to upgrade to. - :param list package_items: Contains all the items related to an CCI - :param string option: Describes type of paramter to be upgraded - :param int value: The value of the parameter to be upgraded - """ - id = {'memory': 3, 'cpus': 80, 'nic_speed': 26, - 'First Disk': 81, 'Second Disk': 82, 'Third Disk': 92, - 'Fourth Disk': 93, 'Fifth Disk': 116} - f = 0 - for item in package_items: - if len(filter(lambda x: x.get('id') == id[option], \ - item['categories'])) \ - and item.get('capacity') == str(value): - if option == 'cpus' or option =='nic_speed': - if f == 1: - return item['prices'][0]['id'] - else: - f = f + 1 - else: - return item['prices'][0]['id'] diff --git a/SoftLayer/tests/fixtures/Virtual_Guest.py b/SoftLayer/tests/fixtures/Virtual_Guest.py index 8c47f75ea..db9f77fdf 100644 --- a/SoftLayer/tests/fixtures/Virtual_Guest.py +++ b/SoftLayer/tests/fixtures/Virtual_Guest.py @@ -214,5 +214,6 @@ generateOrderTemplate = {} setUserMetadata = ['meta'] reloadOperatingSystem = 'OK' +rebootSoft = True createArchiveTransaction = {} diff --git a/SoftLayer/tests/managers/cci_tests.py b/SoftLayer/tests/managers/cci_tests.py index 023f149c6..fdd8f5d85 100644 --- a/SoftLayer/tests/managers/cci_tests.py +++ b/SoftLayer/tests/managers/cci_tests.py @@ -532,35 +532,42 @@ def test_captures(self): archive.called_once_with('a', [{"device": 0, "uuid": 1}, {"device": 2, "uuid": 2}], "", id=1) - - def test_upgrade_(self): + def test_upgrade(self): # Testing Upgrade orderClient = self.client['Product_Order'] - - # test memory/RAM upgrade - self.cci.upgrade(1, memory = 2048) + + # test single upgrade + item_ids = [{'id': 1024}] + self.cci.upgrade(1, item_ids) + + # test single upgrade with rebot + item_ids = [{'id': 1024}] + self.cci.upgrade(1, item_ids, reboot = True) # Now test a blank upgrade self.assertTrue(self.cci.upgrade, 1) # Finally, test a full Upgrade - args = { - 'cpus': 2, - 'memory': 2048, - 'disk': [25,50], - 'nic_speed': 100 } - self.cci.upgrade(1, **args) + item_ids = [{'id': 1}, {'id': 2}, {'id': 3}] + self.cci.upgrade(1, item_ids) + order = self.test_ordercontainer_options() + orderClient.verifyOrder.called_once_with(order) + orderClient.placeOrder.called_once_with(order) + def test_package_ids_for_virtual_guest(self): + result = self.cci.get_package_items_for_virtual_guest()[0] + self.assertEqual(4444, result['prices'][0]['id']) + + def test_ordercontainer_options(self): orderContainer = {} orderContainer['complexType'] = \ 'SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade' orderContainer['virtualGuests'] = [{'id': 1}] orderContainer['prices'] = [{'id': 1645}] orderContainer['properties'] = [{'name': 'MAINTENANCE_WINDOW', - 'value':'2014-03-21 03:12:47.942307'}] - orderClient.verifyOrder.assert_called_once_with(orderContainer) - orderClient.placeOrder.assert_called_once_with(orderContainer) - + 'value': '2014-03-21 03:12:47.942307'}] + return orderContainer + class CCIWaitReadyGoTests(unittest.TestCase):