Skip to content

Commit 4b68924

Browse files
Merge pull request #1439 from FernandoOjeda/ft/authorize_storage
Authorize block and file storage to a hardware server
2 parents c897382 + 3853a1f commit 4b68924

File tree

8 files changed

+112
-0
lines changed

8 files changed

+112
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Authorize File or Block Storage to a Hardware Server"""
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 exceptions
9+
from SoftLayer.CLI import helpers
10+
11+
12+
@click.command()
13+
@click.argument('identifier')
14+
@click.option('--username-storage', '-u', type=click.STRING,
15+
help="The storage username to be added to the hardware server")
16+
@environment.pass_env
17+
def cli(env, identifier, username_storage):
18+
"""Authorize File or Block Storage to a Hardware Server."""
19+
hardware = SoftLayer.HardwareManager(env.client)
20+
hardware_id = helpers.resolve_id(hardware.resolve_ids, identifier, 'hardware')
21+
22+
if not hardware.authorize_storage(hardware_id, username_storage):
23+
raise exceptions.CLIAbort('Authorize Storage Failed')
24+
env.fout('Successfully Storage Added.')

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@
256256
('hardware:rescue', 'SoftLayer.CLI.hardware.power:rescue'),
257257
('hardware:ready', 'SoftLayer.CLI.hardware.ready:cli'),
258258
('hardware:toggle-ipmi', 'SoftLayer.CLI.hardware.toggle_ipmi:cli'),
259+
('hardware:authorize-storage', 'SoftLayer.CLI.hardware.authorize_storage:cli'),
259260
('hardware:dns-sync', 'SoftLayer.CLI.hardware.dns:cli'),
260261
('hardware:storage', 'SoftLayer.CLI.hardware.storage:cli'),
261262
('hardware:upgrade', 'SoftLayer.CLI.hardware.upgrade:cli'),

SoftLayer/fixtures/SoftLayer_Account.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,25 @@
10211021
}
10221022
]
10231023

1024+
getNetworkStorage = [
1025+
{
1026+
"accountId": 1111111,
1027+
"capacityGb": 20,
1028+
"createDate": "2016-01-21T12:11:07-06:00",
1029+
"id": 1234567,
1030+
"nasType": "ISCSI",
1031+
"username": "SL01SEL301234-11",
1032+
},
1033+
{
1034+
"accountId": 1111111,
1035+
"capacityGb": 20,
1036+
"createDate": "2015-04-29T07:55:55-06:00",
1037+
"id": 4917123,
1038+
"nasType": "NAS",
1039+
"username": "SL01SEV1234567_111"
1040+
}
1041+
]
1042+
10241043
getRouters = [
10251044
{
10261045
"accountId": 1,

SoftLayer/fixtures/SoftLayer_Hardware.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,5 @@
5757
{'tag': {'name': 'a tag'}}
5858
],
5959
}
60+
61+
allowAccessToNetworkStorageList = True

SoftLayer/managers/hardware.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,34 @@ def get_hardware_item_prices(self, location):
788788
return self.client.call('SoftLayer_Product_Package', 'getItemPrices', mask=object_mask, filter=object_filter,
789789
id=package['id'])
790790

791+
def authorize_storage(self, hardware_id, username_storage):
792+
"""Authorize File or Block Storage to a Hardware Server.
793+
794+
:param int hardware_id: Hardware server id.
795+
:param string username_storage: Storage username.
796+
797+
:return: bool.
798+
"""
799+
_filter = {"networkStorage": {"username": {"operation": username_storage}}}
800+
801+
storage_result = self.client.call('Account', 'getNetworkStorage', filter=_filter)
802+
803+
if len(storage_result) == 0:
804+
raise SoftLayerError("The Storage with username: %s was not found, please"
805+
" enter a valid storage username" % username_storage)
806+
807+
storage_template = [
808+
{
809+
"id": storage_result[0]['id'],
810+
"username": username_storage
811+
}
812+
]
813+
814+
result = self.client.call('Hardware', 'allowAccessToNetworkStorageList',
815+
storage_template, id=hardware_id)
816+
817+
return result
818+
791819
def upgrade(self, instance_id, memory=None,
792820
nic_speed=None, drive_controller=None,
793821
public_bandwidth=None, test=False):

docs/cli/hardware.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ This function updates the firmware of a server. If already at the latest version
120120
:prog: hardware guests
121121
:show-nested:
122122

123+
.. click:: SoftLayer.CLI.hardware.authorize_storage:cli
124+
:prog: hardware authorize-storage
125+
:show-nested:
126+
123127
.. click:: SoftLayer.CLI.hardware.upgrade:cli
124128
:prog: hardware upgrade
125129
:show-nested:

tests/CLI/modules/server_tests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,27 @@ def test_hardware_guests_empty(self):
904904
self.assertEqual(result.exit_code, 2)
905905
self.assertIsInstance(result.exception, exceptions.CLIAbort)
906906

907+
@mock.patch('SoftLayer.CLI.formatting.confirm')
908+
def test_authorize_hw_no_confirm(self, confirm_mock):
909+
confirm_mock.return_value = False
910+
result = self.run_command(['hw', 'authorize-storage', '-u', '1234'])
911+
912+
self.assertEqual(result.exit_code, 2)
913+
914+
@mock.patch('SoftLayer.CLI.formatting.confirm')
915+
def test_authorize_hw_empty(self, confirm_mock):
916+
confirm_mock.return_value = True
917+
storage_result = self.set_mock('SoftLayer_Account', 'getNetworkStorage')
918+
storage_result.return_value = []
919+
result = self.run_command(['hw', 'authorize-storage', '--username-storage=#', '1234'])
920+
921+
self.assertEqual(str(result.exception), "The Storage with username: # was not found, "
922+
"please enter a valid storage username")
923+
924+
def test_authorize_hw(self):
925+
result = self.run_command(['hw', 'authorize-storage', '--username-storage=SL01SEL301234-11', '1234'])
926+
self.assert_no_fail(result)
927+
907928
def test_upgrade_no_options(self, ):
908929
result = self.run_command(['hw', 'upgrade', '100'])
909930
self.assertEqual(result.exit_code, 2)
@@ -928,4 +949,5 @@ def test_upgrade(self, confirm_mock):
928949
confirm_mock.return_value = True
929950
result = self.run_command(['hw', 'upgrade', '100', '--memory=32', '--public-bandwidth=500',
930951
'--drive-controller=RAID', '--network=10000 Redundant'])
952+
931953
self.assert_no_fail(result)

tests/managers/hardware_tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,18 @@ def test_get_hardware_guests(self):
841841

842842
self.assertEqual("NSX-T Manager", result[0]['hostname'])
843843

844+
def test_authorize_storage(self):
845+
options = self.hardware.authorize_storage(1234, "SL01SEL301234-11")
846+
847+
self.assertEqual(True, options)
848+
849+
def test_authorize_storage_empty(self):
850+
mock = self.set_mock('SoftLayer_Account', 'getNetworkStorage')
851+
mock.return_value = []
852+
self.assertRaises(SoftLayer.exceptions.SoftLayerError,
853+
self.hardware.authorize_storage,
854+
1234, "#")
855+
844856
def test_get_price_id_memory_capacity(self):
845857
upgrade_prices = [
846858
{'categories': [{'categoryCode': 'ram'}], 'item': {'capacity': 1}, 'id': 99}

0 commit comments

Comments
 (0)