Skip to content

Commit c187167

Browse files
Merge pull request #1484 from ATGE/issues/1481
Fix Incomplete notes field for file and block
2 parents a511285 + c1de463 commit c187167

File tree

7 files changed

+115
-49
lines changed

7 files changed

+115
-49
lines changed

SoftLayer/CLI/block/list.py

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import SoftLayer
66
from SoftLayer.CLI import columns as column_helper
77
from SoftLayer.CLI import environment
8-
from SoftLayer.CLI import formatting
9-
8+
from SoftLayer.CLI import storage_utils
109

1110
COLUMNS = [
1211
column_helper.Column('id', ('id',), mask="id"),
@@ -18,7 +17,7 @@
1817
'storage_type',
1918
lambda b: b['storageType']['keyName'].split('_').pop(0)
2019
if 'storageType' in b and 'keyName' in b['storageType']
21-
and isinstance(b['storageType']['keyName'], str)
20+
and isinstance(b['storageType']['keyName'], str)
2221
else '-',
2322
mask="storageType.keyName"),
2423
column_helper.Column('capacity_gb', ('capacityGb',), mask="capacityGb"),
@@ -52,8 +51,6 @@
5251
'notes'
5352
]
5453

55-
DEFAULT_NOTES_SIZE = 20
56-
5754

5855
@click.command()
5956
@click.option('--username', '-u', help='Volume username')
@@ -78,24 +75,5 @@ def cli(env, sortby, columns, datacenter, username, storage_type, order):
7875
order=order,
7976
mask=columns.mask())
8077

81-
table = formatting.Table(columns.columns)
82-
table.sortby = sortby
83-
84-
_reduce_notes(block_volumes)
85-
86-
for block_volume in block_volumes:
87-
table.add_row([value or formatting.blank()
88-
for value in columns.row(block_volume)])
89-
78+
table = storage_utils.build_output_table(env, block_volumes, columns, sortby)
9079
env.fout(table)
91-
92-
93-
def _reduce_notes(block_volumes):
94-
"""Reduces the size of the notes in a volume list.
95-
96-
:param block_volumes: An list of block volumes
97-
"""
98-
for block_volume in block_volumes:
99-
if len(block_volume.get('notes', '')) > DEFAULT_NOTES_SIZE:
100-
shortened_notes = block_volume['notes'][:DEFAULT_NOTES_SIZE]
101-
block_volume['notes'] = shortened_notes

SoftLayer/CLI/environment.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def fmt(self, output, fmt=None):
5252
fmt = self.format
5353
return formatting.format_output(output, fmt)
5454

55+
def format_output_is_json(self):
56+
"""Return True if format output is json or jsonraw"""
57+
return 'json' in self.format
58+
5559
def fout(self, output, newline=True):
5660
"""Format the input and output to the console (stdout)."""
5761
if output is not None:

SoftLayer/CLI/file/list.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import SoftLayer
66
from SoftLayer.CLI import columns as column_helper
77
from SoftLayer.CLI import environment
8-
from SoftLayer.CLI import formatting
8+
from SoftLayer.CLI import storage_utils
99

1010
COLUMNS = [
1111
column_helper.Column('id', ('id',), mask="id"),
@@ -76,24 +76,5 @@ def cli(env, sortby, columns, datacenter, username, storage_type, order):
7676
order=order,
7777
mask=columns.mask())
7878

79-
table = formatting.Table(columns.columns)
80-
table.sortby = sortby
81-
82-
_reduce_notes(file_volumes)
83-
84-
for file_volume in file_volumes:
85-
table.add_row([value or formatting.blank()
86-
for value in columns.row(file_volume)])
87-
79+
table = storage_utils.build_output_table(env, file_volumes, columns, sortby)
8880
env.fout(table)
89-
90-
91-
def _reduce_notes(file_volumes):
92-
"""Reduces the size of the notes in a volume list.
93-
94-
:param file_volumes: An list of file volumes
95-
"""
96-
for file_volume in file_volumes:
97-
if len(file_volume.get('notes', '')) > DEFAULT_NOTES_SIZE:
98-
shortened_notes = file_volume['notes'][:DEFAULT_NOTES_SIZE]
99-
file_volume['notes'] = shortened_notes

SoftLayer/CLI/storage_utils.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,43 @@
22
# :license: MIT, see LICENSE for more details.
33

44
from SoftLayer.CLI import columns as column_helper
5+
from SoftLayer.CLI import formatting
6+
7+
DEFAULT_NOTES_SIZE = 20
8+
9+
10+
def reduce_notes(volumes, env):
11+
"""Reduces all long notes found in the volumes list just if the format output is different from a JSON format.
12+
13+
:param list volumes: An list of storage volumes
14+
:param env :A environment console.
15+
"""
16+
if env.format_output_is_json():
17+
return
18+
19+
for volume in volumes:
20+
if len(volume.get('notes', '')) > DEFAULT_NOTES_SIZE:
21+
shortened_notes = volume['notes'][:DEFAULT_NOTES_SIZE]
22+
volume['notes'] = shortened_notes
23+
24+
25+
def build_output_table(env, volumes, columns, sortby):
26+
"""Builds a formatting table for a list of volumes.
27+
28+
:param env :A Environment console.
29+
:param list volumes: An list of storage volumes
30+
:param columns :A ColumnFormatter for column names
31+
:param str sortby :A string to sort by.
32+
"""
33+
table = formatting.Table(columns.columns)
34+
if sortby in table.columns:
35+
table.sortby = sortby
36+
37+
reduce_notes(volumes, env)
38+
for volume in volumes:
39+
table.add_row([value or formatting.blank()
40+
for value in columns.row(volume)])
41+
return table
542

643

744
def _format_name(obj):
@@ -96,7 +133,6 @@ def _format_name(obj):
96133
"""),
97134
]
98135

99-
100136
DEFAULT_COLUMNS = [
101137
'id',
102138
'name',

tests/CLI/environment_tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,7 @@ def test_print_unicode(self, echo):
8181
]
8282
self.env.fout(output)
8383
self.assertEqual(2, echo.call_count)
84+
85+
def test_format_output_is_json(self):
86+
self.env.format = 'jsonraw'
87+
self.assertTrue(self.env.format_output_is_json())

tests/CLI/modules/block_tests.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
:license: MIT, see LICENSE for more details.
66
"""
77
from SoftLayer.CLI import exceptions
8+
from SoftLayer.CLI import formatting
89
from SoftLayer import SoftLayerAPIError
910
from SoftLayer import testing
1011

11-
1212
import json
1313
from unittest import mock as mock
1414

@@ -135,14 +135,45 @@ def test_volume_list(self):
135135
'IOPs': None,
136136
'ip_addr': '10.1.2.3',
137137
'lunId': None,
138-
'notes': "{'status': 'availabl",
138+
'notes': "{'status': 'available'}",
139139
'rep_partner_count': None,
140140
'storage_type': 'ENDURANCE',
141141
'username': 'username',
142142
'active_transactions': None
143143
}],
144144
json.loads(result.output))
145145

146+
@mock.patch('SoftLayer.BlockStorageManager.list_block_volumes')
147+
def test_volume_list_notes_format_output_json(self, list_mock):
148+
note_mock = 'test ' * 5
149+
list_mock.return_value = [
150+
{'notes': note_mock}
151+
]
152+
153+
result = self.run_command(['--format', 'json', 'block', 'volume-list', '--columns', 'notes'])
154+
155+
self.assert_no_fail(result)
156+
self.assertEqual(
157+
[{
158+
'notes': note_mock,
159+
}],
160+
json.loads(result.output))
161+
162+
@mock.patch('SoftLayer.BlockStorageManager.list_block_volumes')
163+
def test_volume_list_reduced_notes_format_output_table(self, list_mock):
164+
note_mock = 'test ' * 10
165+
expected_reduced_note = 'test ' * 4
166+
list_mock.return_value = [
167+
{'notes': note_mock}
168+
]
169+
expected_table = formatting.Table(['notes'])
170+
expected_table.add_row([expected_reduced_note])
171+
expected_output = formatting.format_output(expected_table)+'\n'
172+
result = self.run_command(['--format', 'table', 'block', 'volume-list', '--columns', 'notes'])
173+
174+
self.assert_no_fail(result)
175+
self.assertEqual(expected_output, result.output)
176+
146177
def test_volume_list_order(self):
147178
result = self.run_command(['block', 'volume-list', '--order=1234567'])
148179

tests/CLI/modules/file_tests.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
:license: MIT, see LICENSE for more details.
66
"""
77
from SoftLayer.CLI import exceptions
8+
from SoftLayer.CLI import formatting
89
from SoftLayer import SoftLayerError
910
from SoftLayer import testing
1011

@@ -63,6 +64,37 @@ def test_volume_list_order(self):
6364
json_result = json.loads(result.output)
6465
self.assertEqual(json_result[0]['id'], 1)
6566

67+
@mock.patch('SoftLayer.FileStorageManager.list_file_volumes')
68+
def test_volume_list_notes_format_output_json(self, list_mock):
69+
note_mock = 'test ' * 5
70+
list_mock.return_value = [
71+
{'notes': note_mock}
72+
]
73+
74+
result = self.run_command(['--format', 'json', 'file', 'volume-list', '--columns', 'notes'])
75+
76+
self.assert_no_fail(result)
77+
self.assertEqual(
78+
[{
79+
'notes': note_mock,
80+
}],
81+
json.loads(result.output))
82+
83+
@mock.patch('SoftLayer.FileStorageManager.list_file_volumes')
84+
def test_volume_list_reduced_notes_format_output_table(self, list_mock):
85+
note_mock = 'test ' * 10
86+
expected_reduced_note = 'test ' * 4
87+
list_mock.return_value = [
88+
{'notes': note_mock}
89+
]
90+
expected_table = formatting.Table(['notes'])
91+
expected_table.add_row([expected_reduced_note])
92+
expected_output = formatting.format_output(expected_table) + '\n'
93+
result = self.run_command(['--format', 'table', 'file', 'volume-list', '--columns', 'notes'])
94+
95+
self.assert_no_fail(result)
96+
self.assertEqual(expected_output, result.output)
97+
6698
@mock.patch('SoftLayer.FileStorageManager.list_file_volumes')
6799
def test_volume_count(self, list_mock):
68100
list_mock.return_value = [

0 commit comments

Comments
 (0)