Skip to content

Commit be65545

Browse files
Merge pull request #1756 from caberos/issue1752
New Command: Hardware notifications
2 parents 7df7756 + 6877108 commit be65545

File tree

8 files changed

+185
-18
lines changed

8 files changed

+185
-18
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Create a user hardware notification entry."""
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('--users', multiple=True,
14+
help='UserId to be notified on monitoring failure.')
15+
@environment.pass_env
16+
def cli(env, identifier, users):
17+
"""Create a user hardware notification entry."""
18+
19+
hardware = SoftLayer.HardwareManager(env.client)
20+
21+
table = formatting.KeyValueTable(['Id', 'Hostmane', 'Username', 'Email', 'FirstName', 'Lastname'])
22+
table.align['Id'] = 'r'
23+
table.align['Username'] = 'l'
24+
25+
for user in users:
26+
notification = hardware.add_notification(identifier, user)
27+
table.add_row([notification['id'], notification['hardware']['fullyQualifiedDomainName'],
28+
notification['user']['username'], notification['user']['email'],
29+
notification['user']['firstName'], notification['user']['lastName']])
30+
31+
env.fout(table)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Get all hardware notifications associated with the passed hardware ID."""
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+
@environment.pass_env
14+
def cli(env, identifier):
15+
"""Get all hardware notifications."""
16+
17+
hardware = SoftLayer.HardwareManager(env.client)
18+
19+
notifications = hardware.get_notifications(identifier)
20+
21+
table = formatting.KeyValueTable(['Domain', 'Hostmane', 'Username', 'Email', 'FirstName', 'Lastname'])
22+
table.align['Domain'] = 'r'
23+
table.align['Username'] = 'l'
24+
25+
for notification in notifications:
26+
table.add_row([notification['hardware']['fullyQualifiedDomainName'], notification['hardware']['hostname'],
27+
notification['user']['username'], notification['user']['email'],
28+
notification['user']['firstName'], notification['user']['lastName']])
29+
30+
env.fout(table)

SoftLayer/CLI/routes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@
292292
('hardware:upgrade', 'SoftLayer.CLI.hardware.upgrade:cli'),
293293
('hardware:sensor', 'SoftLayer.CLI.hardware.sensor:cli'),
294294
('hardware:monitoring', 'SoftLayer.CLI.hardware.monitoring:cli'),
295+
('hardware:notifications', 'SoftLayer.CLI.hardware.notifications:cli'),
296+
('hardware:add-notification', 'SoftLayer.CLI.hardware.add_notification:cli'),
295297

296298
('securitygroup', 'SoftLayer.CLI.securitygroup'),
297299
('securitygroup:list', 'SoftLayer.CLI.securitygroup.list:cli'),
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
findByHardwareId = [
2+
{
3+
'hardwareId': 147258,
4+
'id': 1232569,
5+
'userId': 3698,
6+
'hardware': {
7+
'accountId': 963258,
8+
'domain': 'testedit.com',
9+
'fullyQualifiedDomainName': 'testslcli.testedit.com',
10+
'hostname': 'bardcabero',
11+
'id': 1403539,
12+
'notes': 'My golang note',
13+
'provisionDate': '2020-04-27T16:10:56-06:00',
14+
},
15+
'user': {
16+
'accountId': 307608,
17+
'email': 'cgallo@us.ibm.com',
18+
'firstName': 'CHRISTOPHER',
19+
'id': 167758,
20+
'lastName': 'GALLO',
21+
'username': 'SL307608',
22+
}
23+
},
24+
{
25+
'hardwareId': 1403539,
26+
'id': 1408587,
27+
'userId': 9734826,
28+
'hardware': {
29+
'accountId': 963258,
30+
'domain': 'testedit.com',
31+
'fullyQualifiedDomainName': 'testslcli.testedit.com',
32+
'hostname': 'bardcabero',
33+
'id': 1403539,
34+
'notes': 'My golang note',
35+
'provisionDate': '2020-04-27T16:10:56-06:00',
36+
},
37+
'user': {
38+
'accountId': 307608,
39+
'email': 'Brian.Flores@ibm.com',
40+
'firstName': 'Brian',
41+
'id': 9734826,
42+
'lastName': 'Flores',
43+
'username': '307608_brian.flores@ibm.com',
44+
}
45+
}
46+
]
47+
48+
createObject = {
49+
'hardwareId': 1403539,
50+
'id': 1408593,
51+
'userId': 7650493,
52+
'hardware': {
53+
'accountId': 307608,
54+
'domain': 'testedit.com',
55+
'fullyQualifiedDomainName': 'bardcabero.testedit.com',
56+
'hostname': 'bardcabero',
57+
'id': 1403539,
58+
'notes': 'My golang note',
59+
60+
},
61+
'user': {
62+
'accountId': 307608,
63+
'email': 'daniel.cabero@jalasoft.com',
64+
'firstName': 'daniel',
65+
'id': 7650493,
66+
'lastName': 'cabero',
67+
'username': 'sl307608-dcabero'
68+
69+
}
70+
}

SoftLayer/managers/hardware.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,16 @@ def get_sensors(self, hardware_id):
10921092
"""Returns Hardware sensor data"""
10931093
return self.client.call('Hardware', 'getSensorData', id=hardware_id)
10941094

1095+
def get_notifications(self, hardware_id):
1096+
"""Returns all hardware notifications."""
1097+
return self.client.call('SoftLayer_User_Customer_Notification_Hardware', 'findByHardwareId', hardware_id)
1098+
1099+
def add_notification(self, hardware_id, user_id):
1100+
"""Create a user hardware notification entry"""
1101+
1102+
template = {"hardwareId": hardware_id, "userId": user_id}
1103+
return self.client.call('SoftLayer_User_Customer_Notification_Hardware', 'createObject', template)
1104+
10951105

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

docs/cli/hardware.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,11 @@ This function updates the firmware of a server. If already at the latest version
131131
.. click:: SoftLayer.CLI.hardware.sensor:cli
132132
:prog: hardware sensor
133133
:show-nested:
134+
135+
.. click:: SoftLayer.CLI.hardware.notifications:cli
136+
:prog: hardware notifications
137+
:show-nested:
138+
139+
.. click:: SoftLayer.CLI.hardware.add_notification:cli
140+
:prog: hardware add-notification
141+
:show-nested:

tests/CLI/modules/server_tests.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -704,19 +704,19 @@ def test_dns_sync_both(self, confirm_mock):
704704
'getResourceRecords')
705705
getResourceRecords.return_value = []
706706
createAargs = ({
707-
'type': 'a',
708-
'host': 'hardware-test1',
709-
'domainId': 12345, # from SoftLayer_Account::getDomains
710-
'data': '172.16.1.100',
711-
'ttl': 7200
712-
},)
707+
'type': 'a',
708+
'host': 'hardware-test1',
709+
'domainId': 12345, # from SoftLayer_Account::getDomains
710+
'data': '172.16.1.100',
711+
'ttl': 7200
712+
},)
713713
createPTRargs = ({
714-
'type': 'ptr',
715-
'host': '100',
716-
'domainId': 123456,
717-
'data': 'hardware-test1.test.sftlyr.ws',
718-
'ttl': 7200
719-
},)
714+
'type': 'ptr',
715+
'host': '100',
716+
'domainId': 123456,
717+
'data': 'hardware-test1.test.sftlyr.ws',
718+
'ttl': 7200
719+
},)
720720

721721
result = self.run_command(['hw', 'dns-sync', '1000'])
722722

@@ -759,12 +759,12 @@ def test_dns_sync_v6(self, confirm_mock):
759759
}
760760
}
761761
createV6args = ({
762-
'type': 'aaaa',
763-
'host': 'hardware-test1',
764-
'domainId': 12345, # from SoftLayer_Account::getDomains
765-
'data': '2607:f0d0:1b01:0023:0000:0000:0000:0004',
766-
'ttl': 7200
767-
},)
762+
'type': 'aaaa',
763+
'host': 'hardware-test1',
764+
'domainId': 12345, # from SoftLayer_Account::getDomains
765+
'data': '2607:f0d0:1b01:0023:0000:0000:0000:0004',
766+
'ttl': 7200
767+
},)
768768
server.return_value = test_server
769769
result = self.run_command(['hw', 'dns-sync', '--aaaa-record', '1000'])
770770
self.assert_no_fail(result)
@@ -1026,3 +1026,11 @@ def test_check_for_closing(self, confirm_mock):
10261026
'--flavor', 'B1_2X8X25', '--datacenter', 'mex01', '--os', 'UBUNTU_LATEST'])
10271027
self.assert_no_fail(result)
10281028
self.assertNotIn('Warning: Closed soon: mex01', result.output)
1029+
1030+
def test_notifications(self):
1031+
result = self.run_command(['hardware', 'notifications', '100'])
1032+
self.assert_no_fail(result)
1033+
1034+
def test_add_notification(self):
1035+
result = self.run_command(['hardware', 'add-notification', '100', '--users', '123456'])
1036+
self.assert_no_fail(result)

tests/managers/hardware_tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,14 @@ def test_sensor(self):
920920
self.hardware.get_sensors(100)
921921
self.assert_called_with('SoftLayer_Hardware', 'getSensorData')
922922

923+
def test_notification(self):
924+
self.hardware.get_notifications(100)
925+
self.assert_called_with('SoftLayer_User_Customer_Notification_Hardware', 'findByHardwareId')
926+
927+
def test_add_notification(self):
928+
self.hardware.add_notification(100, 123456)
929+
self.assert_called_with('SoftLayer_User_Customer_Notification_Hardware', 'createObject')
930+
923931

924932
class HardwareHelperTests(testing.TestCase):
925933

0 commit comments

Comments
 (0)