Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Command: Hardware notifications #1756

Merged
merged 2 commits into from Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions SoftLayer/CLI/hardware/add_notification.py
@@ -0,0 +1,31 @@
"""Create a user hardware notification entry."""
# :license: MIT, see LICENSE for more details.

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@click.option('--users', multiple=True,
help='UserId to be notified on monitoring failure.')
@environment.pass_env
def cli(env, identifier, users):
"""Create a user hardware notification entry."""

hardware = SoftLayer.HardwareManager(env.client)

table = formatting.KeyValueTable(['Id', 'Hostmane', 'Username', 'Email', 'FirstName', 'Lastname'])
table.align['Id'] = 'r'
table.align['Username'] = 'l'

for user in users:
notification = hardware.add_notification(identifier, user)
table.add_row([notification['id'], notification['hardware']['fullyQualifiedDomainName'],
notification['user']['username'], notification['user']['email'],
notification['user']['firstName'], notification['user']['lastName']])

env.fout(table)
30 changes: 30 additions & 0 deletions SoftLayer/CLI/hardware/notifications.py
@@ -0,0 +1,30 @@
"""Get all hardware notifications associated with the passed hardware ID."""
# :license: MIT, see LICENSE for more details.

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting


@click.command(cls=SoftLayer.CLI.command.SLCommand, )
@click.argument('identifier')
@environment.pass_env
def cli(env, identifier):
"""Get all hardware notifications."""

hardware = SoftLayer.HardwareManager(env.client)

notifications = hardware.get_notifications(identifier)

table = formatting.KeyValueTable(['Domain', 'Hostmane', 'Username', 'Email', 'FirstName', 'Lastname'])
table.align['Domain'] = 'r'
table.align['Username'] = 'l'

for notification in notifications:
table.add_row([notification['hardware']['fullyQualifiedDomainName'], notification['hardware']['hostname'],
notification['user']['username'], notification['user']['email'],
notification['user']['firstName'], notification['user']['lastName']])

env.fout(table)
2 changes: 2 additions & 0 deletions SoftLayer/CLI/routes.py
Expand Up @@ -292,6 +292,8 @@
('hardware:upgrade', 'SoftLayer.CLI.hardware.upgrade:cli'),
('hardware:sensor', 'SoftLayer.CLI.hardware.sensor:cli'),
('hardware:monitoring', 'SoftLayer.CLI.hardware.monitoring:cli'),
('hardware:notifications', 'SoftLayer.CLI.hardware.notifications:cli'),
('hardware:add-notification', 'SoftLayer.CLI.hardware.add_notification:cli'),

('securitygroup', 'SoftLayer.CLI.securitygroup'),
('securitygroup:list', 'SoftLayer.CLI.securitygroup.list:cli'),
Expand Down
@@ -0,0 +1,70 @@
findByHardwareId = [
{
'hardwareId': 147258,
'id': 1232569,
'userId': 3698,
'hardware': {
'accountId': 963258,
'domain': 'testedit.com',
'fullyQualifiedDomainName': 'testslcli.testedit.com',
'hostname': 'bardcabero',
'id': 1403539,
'notes': 'My golang note',
'provisionDate': '2020-04-27T16:10:56-06:00',
},
'user': {
'accountId': 307608,
'email': 'cgallo@us.ibm.com',
'firstName': 'CHRISTOPHER',
'id': 167758,
'lastName': 'GALLO',
'username': 'SL307608',
}
},
{
'hardwareId': 1403539,
'id': 1408587,
'userId': 9734826,
'hardware': {
'accountId': 963258,
'domain': 'testedit.com',
'fullyQualifiedDomainName': 'testslcli.testedit.com',
'hostname': 'bardcabero',
'id': 1403539,
'notes': 'My golang note',
'provisionDate': '2020-04-27T16:10:56-06:00',
},
'user': {
'accountId': 307608,
'email': 'Brian.Flores@ibm.com',
'firstName': 'Brian',
'id': 9734826,
'lastName': 'Flores',
'username': '307608_brian.flores@ibm.com',
}
}
]

createObject = {
'hardwareId': 1403539,
'id': 1408593,
'userId': 7650493,
'hardware': {
'accountId': 307608,
'domain': 'testedit.com',
'fullyQualifiedDomainName': 'bardcabero.testedit.com',
'hostname': 'bardcabero',
'id': 1403539,
'notes': 'My golang note',

},
'user': {
'accountId': 307608,
'email': 'daniel.cabero@jalasoft.com',
'firstName': 'daniel',
'id': 7650493,
'lastName': 'cabero',
'username': 'sl307608-dcabero'

}
}
10 changes: 10 additions & 0 deletions SoftLayer/managers/hardware.py
Expand Up @@ -1092,6 +1092,16 @@ def get_sensors(self, hardware_id):
"""Returns Hardware sensor data"""
return self.client.call('Hardware', 'getSensorData', id=hardware_id)

def get_notifications(self, hardware_id):
"""Returns all hardware notifications."""
return self.client.call('SoftLayer_User_Customer_Notification_Hardware', 'findByHardwareId', hardware_id)

def add_notification(self, hardware_id, user_id):
"""Create a user hardware notification entry"""

template = {"hardwareId": hardware_id, "userId": user_id}
return self.client.call('SoftLayer_User_Customer_Notification_Hardware', 'createObject', template)


def _get_bandwidth_key(items, hourly=True, no_public=False, location=None):
"""Picks a valid Bandwidth Item, returns the KeyName"""
Expand Down
8 changes: 8 additions & 0 deletions docs/cli/hardware.rst
Expand Up @@ -131,3 +131,11 @@ This function updates the firmware of a server. If already at the latest version
.. click:: SoftLayer.CLI.hardware.sensor:cli
:prog: hardware sensor
:show-nested:

.. click:: SoftLayer.CLI.hardware.notifications:cli
:prog: hardware notifications
:show-nested:

.. click:: SoftLayer.CLI.hardware.add_notification:cli
:prog: hardware add-notification
:show-nested:
44 changes: 26 additions & 18 deletions tests/CLI/modules/server_tests.py
Expand Up @@ -704,19 +704,19 @@ def test_dns_sync_both(self, confirm_mock):
'getResourceRecords')
getResourceRecords.return_value = []
createAargs = ({
'type': 'a',
'host': 'hardware-test1',
'domainId': 12345, # from SoftLayer_Account::getDomains
'data': '172.16.1.100',
'ttl': 7200
},)
'type': 'a',
'host': 'hardware-test1',
'domainId': 12345, # from SoftLayer_Account::getDomains
'data': '172.16.1.100',
'ttl': 7200
},)
createPTRargs = ({
'type': 'ptr',
'host': '100',
'domainId': 123456,
'data': 'hardware-test1.test.sftlyr.ws',
'ttl': 7200
},)
'type': 'ptr',
'host': '100',
'domainId': 123456,
'data': 'hardware-test1.test.sftlyr.ws',
'ttl': 7200
},)

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

Expand Down Expand Up @@ -759,12 +759,12 @@ def test_dns_sync_v6(self, confirm_mock):
}
}
createV6args = ({
'type': 'aaaa',
'host': 'hardware-test1',
'domainId': 12345, # from SoftLayer_Account::getDomains
'data': '2607:f0d0:1b01:0023:0000:0000:0000:0004',
'ttl': 7200
},)
'type': 'aaaa',
'host': 'hardware-test1',
'domainId': 12345, # from SoftLayer_Account::getDomains
'data': '2607:f0d0:1b01:0023:0000:0000:0000:0004',
'ttl': 7200
},)
server.return_value = test_server
result = self.run_command(['hw', 'dns-sync', '--aaaa-record', '1000'])
self.assert_no_fail(result)
Expand Down Expand Up @@ -1026,3 +1026,11 @@ def test_check_for_closing(self, confirm_mock):
'--flavor', 'B1_2X8X25', '--datacenter', 'mex01', '--os', 'UBUNTU_LATEST'])
self.assert_no_fail(result)
self.assertNotIn('Warning: Closed soon: mex01', result.output)

def test_notifications(self):
result = self.run_command(['hardware', 'notifications', '100'])
self.assert_no_fail(result)

def test_add_notification(self):
result = self.run_command(['hardware', 'add-notification', '100', '--users', '123456'])
self.assert_no_fail(result)
8 changes: 8 additions & 0 deletions tests/managers/hardware_tests.py
Expand Up @@ -920,6 +920,14 @@ def test_sensor(self):
self.hardware.get_sensors(100)
self.assert_called_with('SoftLayer_Hardware', 'getSensorData')

def test_notification(self):
self.hardware.get_notifications(100)
self.assert_called_with('SoftLayer_User_Customer_Notification_Hardware', 'findByHardwareId')

def test_add_notification(self):
self.hardware.add_notification(100, 123456)
self.assert_called_with('SoftLayer_User_Customer_Notification_Hardware', 'createObject')


class HardwareHelperTests(testing.TestCase):

Expand Down