From 5d3da1ad2db093b896e41d06336185d4f45f6114 Mon Sep 17 00:00:00 2001 From: Ramkishor Chaladi Date: Wed, 13 Sep 2023 19:53:04 +0530 Subject: [PATCH] added force feature for hardware poweron and poweroff --- SoftLayer/CLI/hardware/power.py | 22 ++++++++++++++++------ tests/CLI/modules/server_tests.py | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/SoftLayer/CLI/hardware/power.py b/SoftLayer/CLI/hardware/power.py index 4947535f5..06ccc3840 100644 --- a/SoftLayer/CLI/hardware/power.py +++ b/SoftLayer/CLI/hardware/power.py @@ -12,16 +12,18 @@ @click.command(cls=SoftLayer.CLI.command.SLCommand, ) @click.argument('identifier') +@click.option('--force', default=False, is_flag=True, help="Force modify") @environment.pass_env -def power_off(env, identifier): +def power_off(env, identifier, force): """Power off an active server.""" mgr = SoftLayer.HardwareManager(env.client) hw_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'hardware') - if not (env.skip_confirmations or - formatting.confirm('This will power off the server with id %s ' - 'Continue?' % hw_id)): - raise exceptions.CLIAbort('Aborted.') + if not force: + if not (env.skip_confirmations or + formatting.confirm('This will power off the server with id %s ' + 'Continue?' % hw_id)): + raise exceptions.CLIAbort('Aborted.') env.client['Hardware_Server'].powerOff(id=hw_id) @@ -53,12 +55,20 @@ def reboot(env, identifier, hard): @click.command(cls=SoftLayer.CLI.command.SLCommand, ) @click.argument('identifier') +@click.option('--force', default=False, is_flag=True, help="Force modify") @environment.pass_env -def power_on(env, identifier): +def power_on(env, identifier, force): """Power on a server.""" mgr = SoftLayer.HardwareManager(env.client) hw_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'hardware') + + if not force: + if not (env.skip_confirmations or + formatting.confirm('This will power off the server with id %s. ' + 'Continue?' % hw_id)): + raise exceptions.CLIAbort('Aborted.') + env.client['Hardware_Server'].powerOn(id=hw_id) diff --git a/tests/CLI/modules/server_tests.py b/tests/CLI/modules/server_tests.py index 1f17e0c2a..33c125675 100644 --- a/tests/CLI/modules/server_tests.py +++ b/tests/CLI/modules/server_tests.py @@ -321,6 +321,20 @@ def test_server_power_on(self): self.assert_called_with('SoftLayer_Hardware_Server', 'powerOn', identifier=12345) + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_harware_power_on_force(self, confirm_mock): + confirm_mock.return_value = False + result = self.run_command(['hardware', 'power-on', '12345']) + self.assertEqual(2, result.exit_code) + self.assertEqual('Aborted.', result.exception.message) + + @mock.patch('SoftLayer.CLI.formatting.confirm') + def test_harware_power_off_force(self, confirm_mock): + confirm_mock.return_value = False + result = self.run_command(['hardware', 'power-off', '12345']) + self.assertEqual(2, result.exit_code) + self.assertEqual('Aborted.', result.exception.message) + def test_server_power_cycle(self): result = self.run_command(['--really', 'server', 'power-cycle', '12345'])