diff --git a/SoftLayer/managers/hardware.py b/SoftLayer/managers/hardware.py index 4a52a5236..f5b01646b 100644 --- a/SoftLayer/managers/hardware.py +++ b/SoftLayer/managers/hardware.py @@ -9,7 +9,6 @@ import socket import time - import SoftLayer from SoftLayer.decoration import retry from SoftLayer.managers import ordering @@ -86,14 +85,17 @@ def cancel_hardware(self, hardware_id, reason='unneeded', comment='', immediate= raise SoftLayer.SoftLayerError("Unable to cancel hardware with running transaction") if 'billingItem' not in hw_billing: - raise SoftLayer.SoftLayerError("Ticket #%s already exists for this server" % - hw_billing['openCancellationTicket']['id']) + if utils.lookup(hw_billing, 'openCancellationTicket', 'id'): + raise SoftLayer.SoftLayerError("Ticket #%s already exists for this server" % + hw_billing['openCancellationTicket']['id']) + raise SoftLayer.SoftLayerError("Cannot locate billing for the server. " + "The server may already be cancelled.") billing_id = hw_billing['billingItem']['id'] if immediate and not hw_billing['hourlyBillingFlag']: - LOGGER.warning("Immediate cancelation of montly servers is not guaranteed." - "Please check the cancelation ticket for updates.") + LOGGER.warning("Immediate cancellation of monthly servers is not guaranteed." + "Please check the cancellation ticket for updates.") result = self.client.call('Billing_Item', 'cancelItem', False, False, cancel_reason, comment, id=billing_id) diff --git a/tests/managers/hardware_tests.py b/tests/managers/hardware_tests.py index 461094be6..120f60601 100644 --- a/tests/managers/hardware_tests.py +++ b/tests/managers/hardware_tests.py @@ -291,6 +291,15 @@ def test_cancel_hardware_no_billing_item(self): 6327) self.assertEqual("Ticket #1234 already exists for this server", str(ex)) + def test_cancel_hardwareno_billing_item_or_ticket(self): + mock = self.set_mock('SoftLayer_Hardware_Server', 'getObject') + mock.return_value = {'id': 987} + + ex = self.assertRaises(SoftLayer.SoftLayerError, + self.hardware.cancel_hardware, + 6327) + self.assertEqual("Cannot locate billing for the server. The server may already be cancelled.", str(ex)) + def test_cancel_hardware_monthly_now(self): mock = self.set_mock('SoftLayer_Hardware_Server', 'getObject') mock.return_value = {'id': 987, 'billingItem': {'id': 1234},