Skip to content

Commit 568ffea

Browse files
Merge pull request #1214 from oarosale/issue#1210
issue#1210 file block storage - adding new feature to list, assign, a…
2 parents efe2d08 + 54943d3 commit 568ffea

File tree

9 files changed

+275
-0
lines changed

9 files changed

+275
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Block Storage Subnets Control."""
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Assign block storage subnets to the given host id."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
import SoftLayer
6+
from SoftLayer.CLI import environment
7+
8+
9+
@click.command()
10+
@click.argument('access_id', type=int)
11+
@click.option('--subnet-id', multiple=True, type=int,
12+
help="ID of the subnets to assign; e.g.: --subnet-id 1234")
13+
@environment.pass_env
14+
def cli(env, access_id, subnet_id):
15+
"""Assign block storage subnets to the given host id.
16+
17+
access_id is the host_id obtained by: slcli block access-list <volume_id>
18+
19+
SoftLayer_Account::iscsiisolationdisabled must be False to use this command
20+
"""
21+
try:
22+
subnet_ids = list(subnet_id)
23+
block_manager = SoftLayer.BlockStorageManager(env.client)
24+
assigned_subnets = block_manager.assign_subnets_to_acl(access_id, subnet_ids)
25+
26+
for subnet in assigned_subnets:
27+
message = "Successfully assigned subnet id: {} to allowed host id: {}".format(subnet, access_id)
28+
click.echo(message)
29+
30+
failed_to_assign_subnets = list(set(subnet_ids) - set(assigned_subnets))
31+
for subnet in failed_to_assign_subnets:
32+
message = "Failed to assign subnet id: {} to allowed host id: {}".format(subnet, access_id)
33+
click.echo(message)
34+
35+
except SoftLayer.SoftLayerAPIError as ex:
36+
message = "Unable to assign subnets.\nReason: {}".format(ex.faultString)
37+
click.echo(message)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""List block storage assigned subnets for the given host id."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
import SoftLayer
6+
from SoftLayer.CLI import environment
7+
from SoftLayer.CLI import formatting
8+
9+
10+
COLUMNS = [
11+
'id',
12+
'createDate',
13+
'networkIdentifier',
14+
'cidr'
15+
]
16+
17+
18+
@click.command()
19+
@click.argument('access_id', type=int)
20+
@environment.pass_env
21+
def cli(env, access_id):
22+
"""List block storage assigned subnets for the given host id.
23+
24+
access_id is the host_id obtained by: slcli block access-list <volume_id>
25+
"""
26+
27+
try:
28+
block_manager = SoftLayer.BlockStorageManager(env.client)
29+
subnets = block_manager.get_subnets_in_acl(access_id)
30+
31+
table = formatting.Table(COLUMNS)
32+
for subnet in subnets:
33+
row = ["{0}".format(subnet['id']),
34+
"{0}".format(subnet['createDate']),
35+
"{0}".format(subnet['networkIdentifier']),
36+
"{0}".format(subnet['cidr'])]
37+
table.add_row(row)
38+
39+
env.fout(table)
40+
41+
except SoftLayer.SoftLayerAPIError as ex:
42+
message = "Unable to list assigned subnets for access-id: {}.\nReason: {}".format(access_id, ex.faultString)
43+
click.echo(message)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Remove block storage subnets for the given host id."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
import SoftLayer
6+
from SoftLayer.CLI import environment
7+
8+
9+
@click.command()
10+
@click.argument('access_id', type=int)
11+
@click.option('--subnet-id', multiple=True, type=int,
12+
help="ID of the subnets to remove; e.g.: --subnet-id 1234")
13+
@environment.pass_env
14+
def cli(env, access_id, subnet_id):
15+
"""Remove block storage subnets for the given host id.
16+
17+
access_id is the host_id obtained by: slcli block access-list <volume_id>
18+
19+
SoftLayer_Account::iscsiisolationdisabled must be False to use this command
20+
"""
21+
try:
22+
subnet_ids = list(subnet_id)
23+
block_manager = SoftLayer.BlockStorageManager(env.client)
24+
removed_subnets = block_manager.remove_subnets_from_acl(access_id, subnet_ids)
25+
26+
for subnet in removed_subnets:
27+
message = "Successfully removed subnet id: {} for allowed host id: {}".format(subnet, access_id)
28+
click.echo(message)
29+
30+
failed_to_remove_subnets = list(set(subnet_ids) - set(removed_subnets))
31+
for subnet in failed_to_remove_subnets:
32+
message = "Failed to remove subnet id: {} for allowed host id: {}".format(subnet, access_id)
33+
click.echo(message)
34+
35+
except SoftLayer.SoftLayerAPIError as ex:
36+
message = "Unable to remove subnets.\nReason: {}".format(ex.faultString)
37+
click.echo(message)

SoftLayer/CLI/routes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080
('block:access-list', 'SoftLayer.CLI.block.access.list:cli'),
8181
('block:access-revoke', 'SoftLayer.CLI.block.access.revoke:cli'),
8282
('block:access-password', 'SoftLayer.CLI.block.access.password:cli'),
83+
('block:subnets-list', 'SoftLayer.CLI.block.subnets.list:cli'),
84+
('block:subnets-assign', 'SoftLayer.CLI.block.subnets.assign:cli'),
85+
('block:subnets-remove', 'SoftLayer.CLI.block.subnets.remove:cli'),
8386
('block:replica-failback', 'SoftLayer.CLI.block.replication.failback:cli'),
8487
('block:replica-failover', 'SoftLayer.CLI.block.replication.failover:cli'),
8588
('block:replica-order', 'SoftLayer.CLI.block.replication.order:cli'),
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,61 @@
1+
TEST_ALLOWED_HOST = {
2+
'id': 12345,
3+
'name': 'Test Allowed Host',
4+
'accountId': 1234,
5+
'credentialId': None,
6+
'createDate': '2020-01-01 00:00:01',
7+
'iscsiAclCredentials': {
8+
'id': 129,
9+
'allowedHostId': 12345,
10+
'subnetId': 12345678
11+
},
12+
'subnetsInAcl': [{
13+
'id': 12345678,
14+
'accountId': 1234,
15+
'networkIdentifier': '10.11.12.13',
16+
'cidr': '14',
17+
'billingRecordId': None,
18+
'parentId': None,
19+
'networkVlanId': None,
20+
'createDate': '2020-01-02 00:00:01',
21+
'modifyDate': None,
22+
'subnetType': 'SECONDARY_ON_VLAN',
23+
'restrictAllocationFlag': 0,
24+
'leafFlag': 1,
25+
'ownerId': 1,
26+
'ipAddressBegin': 129123,
27+
'ipAddressEnd': 129145,
28+
'purgeFlag': 0
29+
}]
30+
}
31+
32+
getObject = TEST_ALLOWED_HOST
33+
34+
getSubnetsInAcl = [{
35+
'id': 12345678,
36+
'accountId': 1234,
37+
'networkIdentifier': '10.11.12.13',
38+
'cidr': '14',
39+
'billingRecordId': None,
40+
'parentId': None,
41+
'networkVlanId': None,
42+
'createDate': '2020-01-02 00:00:01',
43+
'modifyDate': None,
44+
'subnetType': 'SECONDARY_ON_VLAN',
45+
'restrictAllocationFlag': 0,
46+
'leafFlag': 1,
47+
'ownerId': 1,
48+
'ipAddressBegin': 129123,
49+
'ipAddressEnd': 129145,
50+
'purgeFlag': 0
51+
}]
52+
53+
assignSubnetsToAcl = [
54+
12345678
55+
]
56+
57+
removeSubnetsFromAcl = [
58+
12345678
59+
]
60+
161
setCredentialPassword = True

SoftLayer/managers/block.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,46 @@ def deauthorize_host_to_volume(self, volume_id,
208208
return self.client.call('Network_Storage', 'removeAccessFromHostList',
209209
host_templates, id=volume_id, **kwargs)
210210

211+
def assign_subnets_to_acl(self, access_id, subnet_ids):
212+
"""Assigns subnet records to ACL for the access host.
213+
214+
access_id is the host_id obtained by: slcli block access-list <volume_id>
215+
216+
:param integer access_id: id of the access host
217+
:param list subnet_ids: The ids of the subnets to be assigned
218+
:return: Returns int array of assigned subnet ids
219+
"""
220+
return self.client.call('Network_Storage_Allowed_Host',
221+
'assignSubnetsToAcl',
222+
subnet_ids,
223+
id=access_id)
224+
225+
def remove_subnets_from_acl(self, access_id, subnet_ids):
226+
"""Removes subnet records from ACL for the access host.
227+
228+
access_id is the host_id obtained by: slcli block access-list <volume_id>
229+
230+
:param integer access_id: id of the access host
231+
:param list subnet_ids: The ids of the subnets to be removed
232+
:return: Returns int array of removed subnet ids
233+
"""
234+
return self.client.call('Network_Storage_Allowed_Host',
235+
'removeSubnetsFromAcl',
236+
subnet_ids,
237+
id=access_id)
238+
239+
def get_subnets_in_acl(self, access_id):
240+
"""Returns a list of subnet records for the access host.
241+
242+
access_id is the host_id obtained by: slcli block access-list <volume_id>
243+
244+
:param integer access_id: id of the access host
245+
:return: Returns an array of SoftLayer_Network_Subnet objects
246+
"""
247+
return self.client.call('Network_Storage_Allowed_Host',
248+
'getSubnetsInAcl',
249+
id=access_id)
250+
211251
def get_replication_partners(self, volume_id):
212252
"""Acquires list of replicant volumes pertaining to the given volume.
213253

tests/CLI/modules/block_tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,23 @@ def test_deauthorize_host_to_volume(self):
438438

439439
self.assert_no_fail(result)
440440

441+
def test_assign_subnets_to_acl(self):
442+
result = self.run_command(['block', 'subnets-assign', '12345',
443+
'--subnet-id=12345678'])
444+
445+
self.assert_no_fail(result)
446+
447+
def test_remove_subnets_from_acl(self):
448+
result = self.run_command(['block', 'subnets-remove', '12345',
449+
'--subnet-id=12345678'])
450+
451+
self.assert_no_fail(result)
452+
453+
def test_get_subnets_in_acl(self):
454+
result = self.run_command(['block', 'subnets-list', '12345'])
455+
456+
self.assert_no_fail(result)
457+
441458
def test_replicant_failover(self):
442459
result = self.run_command(['block', 'replica-failover', '12345678',
443460
'--replicant-id=5678'])

tests/managers/block_tests.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,43 @@ def test_deauthorize_host_to_volume(self):
486486
'removeAccessFromHostList',
487487
identifier=50)
488488

489+
def test_assign_subnets_to_acl(self):
490+
result = self.block.assign_subnets_to_acl(
491+
12345,
492+
subnet_ids=[12345678])
493+
494+
self.assertEqual(fixtures.SoftLayer_Network_Storage_Allowed_Host.
495+
assignSubnetsToAcl, result)
496+
497+
self.assert_called_with(
498+
'SoftLayer_Network_Storage_Allowed_Host',
499+
'assignSubnetsToAcl',
500+
identifier=12345)
501+
502+
def test_remove_subnets_from_acl(self):
503+
result = self.block.remove_subnets_from_acl(
504+
12345,
505+
subnet_ids=[12345678])
506+
507+
self.assertEqual(fixtures.SoftLayer_Network_Storage_Allowed_Host.
508+
removeSubnetsFromAcl, result)
509+
510+
self.assert_called_with(
511+
'SoftLayer_Network_Storage_Allowed_Host',
512+
'removeSubnetsFromAcl',
513+
identifier=12345)
514+
515+
def test_get_subnets_in_acl(self):
516+
result = self.block.get_subnets_in_acl(12345)
517+
518+
self.assertEqual(fixtures.SoftLayer_Network_Storage_Allowed_Host.
519+
getSubnetsInAcl, result)
520+
521+
self.assert_called_with(
522+
'SoftLayer_Network_Storage_Allowed_Host',
523+
'getSubnetsInAcl',
524+
identifier=12345)
525+
489526
def test_create_snapshot(self):
490527
result = self.block.create_snapshot(123, 'hello world')
491528

0 commit comments

Comments
 (0)