Skip to content

Commit cd9db74

Browse files
Merge pull request #1712 from caberos/issue1710
New Command: user device-access
2 parents 75fa575 + e4cc32a commit cd9db74

File tree

7 files changed

+185
-0
lines changed

7 files changed

+185
-0
lines changed

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@
363363
('user:edit-details', 'SoftLayer.CLI.user.edit_details:cli'),
364364
('user:create', 'SoftLayer.CLI.user.create:cli'),
365365
('user:delete', 'SoftLayer.CLI.user.delete:cli'),
366+
('user:device-access', 'SoftLayer.CLI.user.device_access:cli'),
366367
('user:vpn-manual', 'SoftLayer.CLI.user.vpn_manual:cli'),
367368
('user:vpn-subnet', 'SoftLayer.CLI.user.vpn_subnet:cli'),
368369

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""List User Device access."""
2+
import click
3+
4+
import SoftLayer
5+
from SoftLayer.CLI import environment
6+
from SoftLayer.CLI import formatting
7+
8+
9+
@click.command(cls=SoftLayer.CLI.command.SLCommand, )
10+
@click.argument('identifier')
11+
@environment.pass_env
12+
def cli(env, identifier):
13+
"""User Device access."""
14+
15+
mgr = SoftLayer.UserManager(env.client)
16+
all_permissions = mgr.get_user_permissions(identifier)
17+
18+
# verify the table in table
19+
table = formatting.Table(['Name', 'Value'])
20+
permission_table = formatting.Table(['KeyName', 'Name'])
21+
for permission in all_permissions:
22+
if 'ALL_' in permission['key']:
23+
permission_table.add_row([permission.get('keyName'), permission.get('name')])
24+
25+
hardwares = mgr.get_user_hardware(identifier)
26+
dedicatedhosts = mgr.get_user_dedicated_host(identifier)
27+
virtual_guests = mgr.get_user_virtuals(identifier)
28+
hardware_table = formatting.KeyValueTable(['Id', 'Device Name', 'Device type', 'Public Ip', 'Private Ip', 'notes'])
29+
virtual_table = formatting.KeyValueTable(['Id', 'Device Name', 'Device type', 'Public Ip', 'Private Ip', 'notes'])
30+
dedicated_table = formatting.KeyValueTable(['Id', 'Device Name', 'Device type', 'notes'])
31+
32+
hardware_table.align['Device Name'] = 'l'
33+
dedicated_table.align['Device Name'] = 'l'
34+
virtual_table.align['Device Name'] = 'l'
35+
for hardware in hardwares:
36+
hardware_table.add_row([hardware.get('id'),
37+
hardware.get('fullyQualifiedDomainName'),
38+
'Bare Metal',
39+
hardware.get('primaryIpAddress'),
40+
hardware.get('primaryBackendIpAddress'),
41+
hardware.get('notes') or '-'])
42+
for host in dedicatedhosts:
43+
dedicated_table.add_row([host.get('id'),
44+
host.get('name'),
45+
'Dedicated Host',
46+
host.get('notes') or '-'])
47+
for virtual in virtual_guests:
48+
virtual_table.add_row([virtual.get('id'),
49+
virtual.get('fullyQualifiedDomainName'),
50+
'virtual Guests',
51+
virtual.get('primaryIpAddress'),
52+
virtual.get('primaryBackendIpAddress'),
53+
virtual.get('notes') or '-'])
54+
55+
table.add_row(['Permission', permission_table])
56+
table.add_row(['Hardware', hardware_table])
57+
table.add_row(['Dedicated Host', dedicated_table])
58+
table.add_row(['Virtual Guest', virtual_table])
59+
60+
env.fout(table)

SoftLayer/fixtures/SoftLayer_User_Customer.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,79 @@
8686
editObject = True
8787
addApiAuthenticationKey = True
8888
updateVpnUser = True
89+
90+
getHardware = [{
91+
"domain": "testedit.com",
92+
"fullyQualifiedDomainName": "test.testedit.com",
93+
"hardwareStatusId": 5,
94+
"hostname": "test",
95+
"id": 1403539,
96+
"manufacturerSerialNumber": "J33H9TX",
97+
"notes": "My golang note",
98+
"provisionDate": "2020-04-27T16:10:56-06:00",
99+
"serialNumber": "SL01FJUI",
100+
"globalIdentifier": "81434794-af69-44d5-bb97-6b6f43454eee",
101+
"hardwareStatus": {
102+
"id": 5,
103+
"status": "ACTIVE"
104+
},
105+
"networkManagementIpAddress": "10.93.138.222",
106+
"primaryBackendIpAddress": "10.93.138.202",
107+
"primaryIpAddress": "169.48.191.244",
108+
"privateIpAddress": "10.93.138.202"
109+
}]
110+
getDedicatedHosts = [
111+
{
112+
'createDate': '2021-11-18T15:13:57-06:00',
113+
'diskCapacity': 1200,
114+
'id': 656700,
115+
'memoryCapacity': 242,
116+
'modifyDate': '2022-04-26T10:49:48-06:00',
117+
'name': 'dedicatedhost01'
118+
},
119+
{
120+
'accountId': 307608,
121+
'cpuCount': 56,
122+
'createDate': '2022-02-18T12:47:30-06:00',
123+
'diskCapacity': 1200,
124+
'id': 691394,
125+
'memoryCapacity': 242,
126+
'modifyDate': '2022-04-18T11:24:20-06:00',
127+
'name': 'test'
128+
}
129+
]
130+
getVirtualGuests = [
131+
{
132+
"fullyQualifiedDomainName": "KVM-Test.cgallo.com",
133+
"hostname": "KVM-Test",
134+
"id": 121401696,
135+
"maxCpu": 2,
136+
"maxCpuUnits": "CORE",
137+
"maxMemory": 4096,
138+
"modifyDate": "2022-01-25T23:23:13-06:00",
139+
"provisionDate": "2021-06-09T14:51:54-06:00",
140+
"startCpus": 2,
141+
"typeId": 1,
142+
"uuid": "15951561-6171-0dfc-f3d2-be039e51cc10",
143+
"globalIdentifier": "a245a7dd-acd1-4d1a-9356-cc1ac6b55b98",
144+
"primaryBackendIpAddress": "10.208.73.53",
145+
"primaryIpAddress": "169.48.96.27",
146+
},
147+
{
148+
"createDate": "2020-11-16T09:01:57-06:00",
149+
"deviceStatusId": 8,
150+
"domain": "softlayer.test",
151+
"fullyQualifiedDomainName": "SuspendVsTest.softlayer.test",
152+
"hostname": "SuspendVsTest",
153+
"id": 112238162,
154+
"maxCpu": 8,
155+
"maxCpuUnits": "CORE",
156+
"maxMemory": 16384,
157+
"modifyDate": "2022-01-25T14:15:37-06:00",
158+
"provisionDate": "2020-11-16T09:09:04-06:00",
159+
"startCpus": 8,
160+
"typeId": 1,
161+
"uuid": "d8908a64-f4d4-5637-49c7-650572d47120",
162+
"globalIdentifier": "7fe777af-d38b-47c2-9f1c-b1ec26751b58",
163+
"primaryBackendIpAddress": "10.74.54.76",
164+
}]

SoftLayer/managers/user.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,30 @@ def get_overrides_list(self, user_id, subnet_ids):
392392

393393
return overrides_list
394394

395+
def get_user_hardware(self, user_id):
396+
"""User Hardware list.
397+
398+
:param int user_id:
399+
:return: List hardware relate to user
400+
"""
401+
return self.user_service.getHardware(id=user_id)
402+
403+
def get_user_dedicated_host(self, user_id):
404+
"""User dedicate host list.
405+
406+
:param int user_id:
407+
:return: List dedicated host relate to user
408+
"""
409+
return self.user_service.getDedicatedHosts(id=user_id)
410+
411+
def get_user_virtuals(self, user_id):
412+
"""User virtual guest list.
413+
414+
:param int user_id:
415+
:return: List virtual guest relate to user
416+
"""
417+
return self.user_service.getVirtualGuests(id=user_id)
418+
395419

396420
def _keyname_search(haystack, needle):
397421
for item in haystack:

docs/cli/users.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ Version 5.6.0 introduces the ability to interact with user accounts from the cli
4040
:prog: user delete
4141
:show-nested:
4242

43+
.. click:: SoftLayer.CLI.user.device_access:cli
44+
:prog: user device-access
45+
:show-nested:
46+
4347
.. click:: SoftLayer.CLI.user.vpn_manual:cli
4448
:prog: user vpn-manual
4549
:show-nested:

tests/CLI/modules/user_tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,11 @@ def test_edit_notification_off_failure(self, click):
336336
click.secho.assert_called_with('Failed to update notifications: Test notification', fg='red')
337337
self.assert_no_fail(result)
338338
self.assert_called_with('SoftLayer_Email_Subscription', 'disable', identifier=111)
339+
340+
def test_devices_access(self):
341+
result = self.run_command(['user', 'device-access', '111'])
342+
self.assert_no_fail(result)
343+
self.assert_called_with('SoftLayer_User_Customer', 'getPermissions')
344+
self.assert_called_with('SoftLayer_User_Customer', 'getHardware')
345+
self.assert_called_with('SoftLayer_User_Customer', 'getDedicatedHosts')
346+
self.assert_called_with('SoftLayer_User_Customer', 'getVirtualGuests')

tests/managers/user_tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,15 @@ def test_gather_notifications_fail(self):
294294
self.manager.gather_notifications,
295295
['Test not exit'])
296296
self.assertEqual("Test not exit is not a valid notification name", str(ex))
297+
298+
def test_get_hardware(self):
299+
self.manager.get_user_hardware(1234)
300+
self.assert_called_with('SoftLayer_User_Customer', 'getHardware')
301+
302+
def test_get_dedicated_host(self):
303+
self.manager.get_user_dedicated_host(1234)
304+
self.assert_called_with('SoftLayer_User_Customer', 'getDedicatedHosts')
305+
306+
def test_get_virtual(self):
307+
self.manager.get_user_virtuals(1234)
308+
self.assert_called_with('SoftLayer_User_Customer', 'getVirtualGuests')

0 commit comments

Comments
 (0)