Skip to content

Commit 7fdc73a

Browse files
Merge pull request #1818 from BrianSantivanez/issue1816
New Command: `slcli user grant-access`
2 parents 2685756 + 66dd10d commit 7fdc73a

File tree

7 files changed

+112
-0
lines changed

7 files changed

+112
-0
lines changed

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@
376376
('user:vpn-manual', 'SoftLayer.CLI.user.vpn_manual:cli'),
377377
('user:vpn-subnet', 'SoftLayer.CLI.user.vpn_subnet:cli'),
378378
('user:remove-access', 'SoftLayer.CLI.user.remove_access:cli'),
379+
('user:grant-access', 'SoftLayer.CLI.user.grant_access:cli'),
379380

380381
('vlan', 'SoftLayer.CLI.vlan'),
381382
('vlan:create', 'SoftLayer.CLI.vlan.create:cli'),

SoftLayer/CLI/user/grant_access.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""Grants a user access to a given device"""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
import SoftLayer
6+
7+
from SoftLayer.CLI.command import SLCommand as SLCommand
8+
from SoftLayer.CLI import environment
9+
from SoftLayer.CLI import exceptions
10+
11+
12+
@click.command(cls=SLCommand, )
13+
@click.argument('identifier')
14+
@click.option('--hardware', help="Hardware ID")
15+
@click.option('--virtual', help="Virtual Guest ID")
16+
@click.option('--dedicated', help="dedicated host ID")
17+
@environment.pass_env
18+
def cli(env, identifier, hardware, virtual, dedicated):
19+
"""Grants a user access to a given device.
20+
21+
Example: slcli user grant-access 123456 --hardware 123456789
22+
"""
23+
24+
mgr = SoftLayer.UserManager(env.client)
25+
result = False
26+
if hardware:
27+
result = mgr.grant_hardware_access(identifier, hardware)
28+
if result:
29+
click.secho(f"User {identifier} has been given access to hardware {hardware}", fg='green')
30+
31+
if virtual:
32+
result = mgr.grant_virtual_access(identifier, virtual)
33+
if result:
34+
click.secho(f"User {identifier} has been given access to hardware {virtual}", fg='green')
35+
36+
if dedicated:
37+
result = mgr.grant_dedicated_access(identifier, dedicated)
38+
if result:
39+
click.secho(f"User {identifier} has been given access to hardware {dedicated}", fg='green')
40+
41+
if not result:
42+
raise exceptions.CLIAbort('A device option is required.\n'
43+
'E.g slcli user grant-access 123456 --hardware 91803794\n'
44+
' slcli user grant-access 123456 --dedicated 91803793\n'
45+
' slcli user grant-access 123456 --virtual 91803792')

SoftLayer/fixtures/SoftLayer_User_Customer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@
8989
removeDedicatedHostAccess = True
9090
removeHardwareAccess = True
9191
removeVirtualGuestAccess = True
92+
addDedicatedHostAccess = True
93+
addHardwareAccess = True
94+
addVirtualGuestAccess = True
9295

9396
getHardware = [{
9497
"domain": "testedit.com",

SoftLayer/managers/user.py

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

393393
return overrides_list
394394

395+
def grant_hardware_access(self, user_id, hardware_id):
396+
"""Grants the user access to a single hardware device.
397+
398+
:param int user_id:
399+
:param int hardware_id
400+
401+
:returns: true
402+
"""
403+
return self.user_service.addHardwareAccess(hardware_id, id=user_id)
404+
405+
def grant_virtual_access(self, user_id, virtual_id):
406+
"""Grants the user access to a single VS device.
407+
408+
:param int user_id:
409+
:param int virtual_id
410+
411+
:returns: true
412+
"""
413+
return self.user_service.addVirtualGuestAccess(virtual_id, id=user_id)
414+
415+
def grant_dedicated_access(self, user_id, dedicated_id):
416+
"""Grants the user access to a single dedicated host device.
417+
418+
:param int user_id:
419+
:param int dedicated_id
420+
421+
:returns: true
422+
"""
423+
return self.user_service.addDedicatedHostAccess(dedicated_id, id=user_id)
424+
395425
def remove_hardware_access(self, user_id, hardware_id):
396426
"""Remove hardware from a portal user’s hardware access list.
397427

docs/cli/users.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,8 @@ Version 5.6.0 introduces the ability to interact with user accounts from the cli
5656
:prog: user remove-access
5757
:show-nested:
5858

59+
.. click:: SoftLayer.CLI.user.grant_access:cli
60+
:prog: user grant-access
61+
:show-nested:
62+
5963

tests/CLI/modules/user_tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,23 @@ def test_devices_access(self):
345345
self.assert_called_with('SoftLayer_User_Customer', 'getDedicatedHosts')
346346
self.assert_called_with('SoftLayer_User_Customer', 'getVirtualGuests')
347347

348+
def test_grant_access_hardware(self):
349+
result = self.run_command(['user', 'grant-access', '123456', '--hardware', '147258'])
350+
self.assert_no_fail(result)
351+
352+
def test_grant_access_virtual(self):
353+
result = self.run_command(['user', 'grant-access', '123456', '--virtual', '987456'])
354+
self.assert_no_fail(result)
355+
356+
def test_grant_access_dedicated(self):
357+
result = self.run_command(['user', 'grant-access', '123456', '--dedicated', '369852'])
358+
self.assert_no_fail(result)
359+
360+
def test_grant_without_device(self):
361+
result = self.run_command(['user', 'grant-access', '123456'])
362+
self.assertEqual(2, result.exit_code)
363+
self.assertIn('A device option is required.', result.exception.message)
364+
348365
def test_remove_access_hardware(self):
349366
result = self.run_command(['user', 'remove-access', '123456', '--hardware', '147258'])
350367
self.assert_no_fail(result)

tests/managers/user_tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,18 @@ def test_get_virtual(self):
307307
self.manager.get_user_virtuals(1234)
308308
self.assert_called_with('SoftLayer_User_Customer', 'getVirtualGuests')
309309

310+
def test_grant_hardware(self):
311+
self.manager.grant_hardware_access(123456, 369852)
312+
self.assert_called_with('SoftLayer_User_Customer', 'addHardwareAccess')
313+
314+
def test_grant_virtual(self):
315+
self.manager.grant_virtual_access(123456, 369852)
316+
self.assert_called_with('SoftLayer_User_Customer', 'addVirtualGuestAccess')
317+
318+
def test_grant_dedicated(self):
319+
self.manager.grant_dedicated_access(123456, 369852)
320+
self.assert_called_with('SoftLayer_User_Customer', 'addDedicatedHostAccess')
321+
310322
def test_remove_hardware(self):
311323
self.manager.remove_hardware_access(123456, 369852)
312324
self.assert_called_with('SoftLayer_User_Customer', 'removeHardwareAccess')

0 commit comments

Comments
 (0)