Skip to content

Commit caafd40

Browse files
Merge pull request #926 from camporter/ssh_key_add_improvements
Improve error handling when adding SSH keys
2 parents 02812ce + 33ecb4a commit caafd40

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

SoftLayer/CLI/sshkey/add.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import SoftLayer
88
from SoftLayer.CLI import environment
9+
from SoftLayer.CLI import exceptions
910

1011

1112
@click.command()
@@ -19,6 +20,16 @@
1920
def cli(env, label, in_file, key, note):
2021
"""Add a new SSH key."""
2122

23+
if in_file is None and key is None:
24+
raise exceptions.ArgumentError(
25+
'Either [-f | --in-file] or [-k | --key] arguments are required to add a key'
26+
)
27+
28+
if in_file and key:
29+
raise exceptions.ArgumentError(
30+
'[-f | --in-file] is not allowed with [-k | --key]'
31+
)
32+
2233
if key:
2334
key_text = key
2435
else:

SoftLayer/managers/sshkey.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def add_key(self, key, label, notes=None):
2828
2929
:param string key: The SSH key to add
3030
:param string label: The label for the key
31+
:param string notes: Additional notes for the key
3132
:returns: A dictionary of the new key's information.
3233
"""
3334
order = {

tests/CLI/modules/sshkey_tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,25 @@
1111

1212
import mock
1313

14+
from SoftLayer.CLI import exceptions
1415
from SoftLayer import testing
1516

1617

1718
class SshKeyTests(testing.TestCase):
19+
def test_add_without_key_errors(self):
20+
result = self.run_command(['sshkey', 'add', 'key1'])
21+
22+
self.assertEqual(result.exit_code, 2)
23+
self.assertIsInstance(result.exception, exceptions.ArgumentError)
24+
25+
def test_add_with_key_file_and_key_argument_errors(self):
26+
path = os.path.join(testing.FIXTURE_PATH, 'id_rsa.pub')
27+
result = self.run_command(['sshkey', 'add', 'key1',
28+
'--key=some_key',
29+
'--in-file=%s' % path])
30+
31+
self.assertEqual(result.exit_code, 2)
32+
self.assertIsInstance(result.exception, exceptions.ArgumentError)
1833

1934
def test_add_by_option(self):
2035
service = self.client['Security_Ssh_Key']

0 commit comments

Comments
 (0)