Skip to content

Commit 6622aa7

Browse files
Merge pull request #1774 from caberos/issue1773
new command hw create-credential
2 parents e6a4b9e + e25d8dd commit 6622aa7

File tree

8 files changed

+146
-4
lines changed

8 files changed

+146
-4
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Create a password for a software component"""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
6+
import SoftLayer
7+
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import exceptions
9+
from SoftLayer.CLI import formatting
10+
11+
12+
@click.command(cls=SoftLayer.CLI.command.SLCommand, )
13+
@click.argument('identifier')
14+
@click.option('--username', '-U', required=True, help="The username part of the username/password pair")
15+
@click.option('--password', '-P', required=True, help="The password part of the username/password pair.")
16+
@click.option('--notes', '-n', help="A note string stored for this username/password pair.")
17+
@click.option('--system', required=True, help="The name of this specific piece of software.")
18+
@environment.pass_env
19+
def cli(env, identifier, username, password, notes, system):
20+
"""Create a password for a software component."""
21+
22+
mgr = SoftLayer.HardwareManager(env.client)
23+
24+
software = mgr.get_software_components(identifier)
25+
sw_id = ''
26+
try:
27+
for sw_instance in software:
28+
if (sw_instance['softwareLicense']['softwareDescription']['name']).lower() == system:
29+
sw_id = sw_instance['id']
30+
except KeyError as ex:
31+
raise exceptions.CLIAbort('System id not found') from ex
32+
33+
template = {
34+
"notes": notes,
35+
"password": password,
36+
"softwareId": sw_id,
37+
"username": username,
38+
"software": {
39+
"hardwareId": identifier,
40+
"softwareLicense": {
41+
"softwareDescription": {
42+
"name": system
43+
}
44+
}
45+
}}
46+
47+
result = mgr.create_credential(template)
48+
49+
table = formatting.KeyValueTable(['name', 'value'])
50+
table.align['name'] = 'r'
51+
table.align['value'] = 'l'
52+
table.add_row(['Software Id', result['id']])
53+
table.add_row(['Created', result['createDate']])
54+
table.add_row(['Username', result['username']])
55+
table.add_row(['Password', result['password']])
56+
table.add_row(['Notes', result['notes']])
57+
env.fout(table)

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@
296296
('hardware:monitoring', 'SoftLayer.CLI.hardware.monitoring:cli'),
297297
('hardware:notifications', 'SoftLayer.CLI.hardware.notifications:cli'),
298298
('hardware:add-notification', 'SoftLayer.CLI.hardware.add_notification:cli'),
299+
('hardware:create-credential', 'SoftLayer.CLI.hardware.create_credential:cli'),
299300

300301
('securitygroup', 'SoftLayer.CLI.securitygroup'),
301302
('securitygroup:list', 'SoftLayer.CLI.securitygroup.list:cli'),

SoftLayer/fixtures/SoftLayer_Hardware.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,35 @@
104104
"upperNonCritical": "12.915",
105105
"upperNonRecoverable": "13.403"
106106
}]
107+
108+
getSoftwareComponents = [{
109+
"hardwareId": 123456,
110+
"id": 67064532,
111+
"passwords": [
112+
{
113+
"id": 77995567,
114+
"notes": "testslcli1",
115+
"password": "123456",
116+
"softwareId": 67064532,
117+
"username": "testslcli1",
118+
},
119+
{
120+
"id": 77944803,
121+
"notes": "testslcli2",
122+
"password": "test123",
123+
"softwareId": 67064532,
124+
"username": "testslcli2",
125+
}
126+
],
127+
"softwareLicense": {
128+
"id": 21854,
129+
"softwareDescriptionId": 2914,
130+
"softwareDescription": {
131+
"id": 2914,
132+
"longDescription": "Ubuntu 20.04.1-64",
133+
"name": "Ubuntu",
134+
"referenceCode": "UBUNTU_20_64",
135+
"version": "20.04.1-64"
136+
}
137+
}
138+
}]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
createObject = {
2+
"createDate": "2022-10-18T09:27:48-06:00",
3+
"id": 78026761,
4+
"notes": "test slcli",
5+
"password": "123456",
6+
"softwareId": 67064532,
7+
"username": "testslcli",
8+
"software": {
9+
"hardwareId": 1532729,
10+
"id": 67064532,
11+
"manufacturerLicenseInstance": ""
12+
}
13+
}

SoftLayer/managers/hardware.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,14 @@ def add_notification(self, hardware_id, user_id):
11021102
template = {"hardwareId": hardware_id, "userId": user_id}
11031103
return self.client.call('SoftLayer_User_Customer_Notification_Hardware', 'createObject', template)
11041104

1105+
def get_software_components(self, hardware_id):
1106+
"""Returns a piece of hardware’s installed software."""
1107+
return self.client.call('Hardware', 'getSoftwareComponents', id=hardware_id)
1108+
1109+
def create_credential(self, template):
1110+
"""Create a password for a software component"""
1111+
return self.client.call('SoftLayer_Software_Component_Password', 'createObject', template)
1112+
11051113

11061114
def _get_bandwidth_key(items, hourly=True, no_public=False, location=None):
11071115
"""Picks a valid Bandwidth Item, returns the KeyName"""

docs/cli/hardware.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,7 @@ This function updates the firmware of a server. If already at the latest version
139139
.. click:: SoftLayer.CLI.hardware.add_notification:cli
140140
:prog: hardware add-notification
141141
:show-nested:
142+
143+
.. click:: SoftLayer.CLI.hardware.create_credential:cli
144+
:prog: hardware create-credential
145+
:show-nested:

tests/CLI/modules/server_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,3 +1045,9 @@ def test_notifications(self):
10451045
def test_add_notification(self):
10461046
result = self.run_command(['hardware', 'add-notification', '100', '--users', '123456'])
10471047
self.assert_no_fail(result)
1048+
1049+
def test_create_credential(self):
1050+
result = self.run_command(['hw', 'create-credential', '123456',
1051+
'--username', 'testslcli', '--password', 'test-123456',
1052+
'--notes', 'test slcli', '--system', 'system'])
1053+
self.assert_no_fail(result)

tests/managers/hardware_tests.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,10 +557,10 @@ def test_edit(self):
557557
self.assert_called_with('SoftLayer_Hardware_Server',
558558
'editObject',
559559
args=({
560-
'hostname': 'new-host',
561-
'domain': 'new.sftlyr.ws',
562-
'notes': 'random notes',
563-
},),
560+
'hostname': 'new-host',
561+
'domain': 'new.sftlyr.ws',
562+
'notes': 'random notes',
563+
},),
564564
identifier=100)
565565

566566
def test_rescue(self):
@@ -928,6 +928,27 @@ def test_add_notification(self):
928928
self.hardware.add_notification(100, 123456)
929929
self.assert_called_with('SoftLayer_User_Customer_Notification_Hardware', 'createObject')
930930

931+
def test_get_software_component(self):
932+
self.hardware.get_software_components(123456)
933+
self.assert_called_with('SoftLayer_Hardware', 'getSoftwareComponents')
934+
935+
def test_create_credential(self):
936+
template = {
937+
"notes": 'notes',
938+
"password": 'password',
939+
"softwareId": 'sw_id',
940+
"username": 'username',
941+
"software": {
942+
"hardwareId": 123456,
943+
"softwareLicense": {
944+
"softwareDescription": {
945+
"name": 'system'
946+
}
947+
}
948+
}}
949+
self.hardware.create_credential(template)
950+
self.assert_called_with('SoftLayer_Software_Component_Password', 'createObject')
951+
931952

932953
class HardwareHelperTests(testing.TestCase):
933954

0 commit comments

Comments
 (0)