Skip to content

Commit 5b448fe

Browse files
committed
Merge pull request softlayer#375 from Neetuj/newissue341
Resolves issue341 of parsing argument value
2 parents a1c9b91 + 476755b commit 5b448fe

4 files changed

Lines changed: 73 additions & 1 deletion

File tree

SoftLayer/CLI/helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,13 @@ def resolve_id(resolver, identifier, name='object'):
3030
(name, identifier, ', '.join([str(_id) for _id in ids])))
3131

3232
return ids[0]
33+
34+
35+
def sanitize_args(args):
36+
""" sanitize input (remove = sign from argument values)
37+
:returns args back
38+
"""
39+
for key, value in args.items():
40+
if isinstance(value, str) and value.startswith('='):
41+
args[key] = value[1:]
42+
return args

SoftLayer/CLI/modules/vs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ def _parse_create_args(self, args):
580580
581581
:param dict args: CLI arguments
582582
"""
583+
args = helpers.sanitize_args(args)
583584
data = {
584585
"hourly": args['--hourly'],
585586
"cpus": args['--cpu'],

SoftLayer/tests/CLI/helper_tests.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ class ResolveIdTests(testing.TestCase):
249249
def test_resolve_id_one(self):
250250
resolver = lambda r: [12345]
251251
id = helpers.resolve_id(resolver, 'test')
252-
253252
self.assertEqual(id, 12345)
254253

255254
def test_resolve_id_none(self):
@@ -423,3 +422,19 @@ def test_export_to_template(self):
423422
mock.call('datacenter=ams01\n'),
424423
mock.call('disk=disk1,disk2\n'),
425424
], any_order=True) # Order isn't really guaranteed
425+
426+
427+
class TestInputSanitization(testing.TestCase):
428+
429+
def test_equalto_removed(self):
430+
args = {
431+
'--os': None,
432+
'--datacenter': '=ams01',
433+
'--memory': '=1g',
434+
'--test': '=1344'}
435+
argnew = helpers.sanitize_args(args)
436+
self.assertEqual(argnew, {
437+
'--os': None,
438+
'--datacenter': 'ams01',
439+
'--memory': '1g',
440+
'--test': '1344'})

SoftLayer/tests/CLI/modules/vs_tests.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,49 @@ def test_create(self, confirm_mock):
134134
'id': 100,
135135
'created': '2013-08-01 15:23:45'}],
136136
formatting.format_output(output, 'python'))
137+
138+
@mock.patch('SoftLayer.CLI.formatting.confirm')
139+
def test_check_create_args(self, confirm_mock):
140+
confirm_mock.return_value = True
141+
command = vs.CreateVS(client=self.client)
142+
output = command.execute({'--cpu': '2',
143+
'--domain': 'example.com',
144+
'--hostname': 'host',
145+
'--image': None,
146+
'--os': 'UBUNTU_LATEST',
147+
'--memory': '=1024',
148+
'--nic': '100',
149+
'--hourly': True,
150+
'--monthly': False,
151+
'--like': None,
152+
'--datacenter': None,
153+
'--dedicated': False,
154+
'--san': False,
155+
'--test': False,
156+
'--export': None,
157+
'--userfile': None,
158+
'--postinstall': None,
159+
'--key': [],
160+
'--network': [],
161+
'--disk': [],
162+
'--private': False,
163+
'--template': None,
164+
'--userdata': None,
165+
'--vlan_public': None,
166+
'--vlan_private': None,
167+
'--wait': None,
168+
'--really': False})
169+
170+
self.assertEqual([{'guid': '1a2b3c-1701',
171+
'id': 100,
172+
'created': '2013-08-01 15:23:45'}],
173+
formatting.format_output(output, 'python'))
174+
service = self.client['Virtual_Guest']
175+
service.createObject.assert_called_with({
176+
'domain': 'example.com',
177+
'localDiskFlag': True,
178+
'startCpus': 2,
179+
'operatingSystemReferenceCode': 'UBUNTU_LATEST',
180+
'maxMemory': 1024,
181+
'hourlyBillingFlag': True,
182+
'hostname': 'host'})

0 commit comments

Comments
 (0)