Skip to content

Commit cfcdbc2

Browse files
caberoscaberos
authored andcommitted
new command hw create-credential
1 parent e6a4b9e commit cfcdbc2

File tree

8 files changed

+147
-4
lines changed

8 files changed

+147
-4
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 formatting
9+
10+
11+
@click.command(cls=SoftLayer.CLI.command.SLCommand, )
12+
@click.argument('identifier')
13+
@click.option('--username', '-U', help="The username part of the username/password pair")
14+
@click.option('--password', '-P', help="The password part of the username/password pair.")
15+
@click.option('--notes', '-n', help="A note string stored for this username/password pair.")
16+
@click.option('--system', help="The name of this specific piece of software.")
17+
@environment.pass_env
18+
def cli(env, identifier, username, password, notes, system):
19+
"""Create a password for a software component."""
20+
21+
mgr = SoftLayer.HardwareManager(env.client)
22+
23+
software = mgr.get_software_components(identifier)
24+
sw_id = None
25+
for sw in software:
26+
if (sw['softwareLicense']['softwareDescription']['name']).lower() == system:
27+
sw_id = sw['id']
28+
break
29+
elif system:
30+
if (sw['softwareLicense']['softwareDescription']['name']) == 'Passmark Suite':
31+
sw_id = sw['id']
32+
break
33+
34+
template = {
35+
"notes": notes,
36+
"password": password,
37+
"softwareId": sw_id,
38+
"username": username,
39+
"software": {
40+
"hardwareId": identifier,
41+
"softwareLicense": {
42+
"softwareDescription": {
43+
"name": system
44+
}
45+
}
46+
}}
47+
48+
result = mgr.create_credential(template)
49+
50+
table = formatting.KeyValueTable(['name', 'value'])
51+
table.align['name'] = 'r'
52+
table.align['value'] = 'l'
53+
table.add_row(['Software Id', result['id']])
54+
table.add_row(['Created', result['createDate']])
55+
table.add_row(['Username', result['username']])
56+
table.add_row(['Password', result['password']])
57+
table.add_row(['Notes', result['notes']])
58+
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 Hardware sensor data"""
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'])
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)