Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions package/cloudshell/cm/customscript/customscript_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,24 @@ def execute_script(self, command_context, script_conf_json, cancellation_context
:rtype str
"""
with LoggingSessionContext(command_context) as logger:
cancel_sampler = CancellationSampler(cancellation_context)
logger.debug('\'execute_script\' is called with the configuration json: \n' + script_conf_json)

with ErrorHandlingContext(logger):
logger.debug('\'execute_script\' is called with the configuration json: \n' + script_conf_json)
script_conf = ScriptConfigurationParser.json_to_object(script_conf_json)
with CloudShellSessionContext(command_context) as api:
cancel_sampler = CancellationSampler(cancellation_context)
script_conf = ScriptConfigurationParser(api).json_to_object(script_conf_json)

logger.info('Downloading file from \'%s\' ...' % script_conf.script_repo.url)
script_file = self._download_script(script_conf.script_repo, logger, cancel_sampler)
logger.info('Done (%s, %s chars).' % (script_file.name, len(script_file.text)))
logger.info('Downloading file from \'%s\' ...' % script_conf.script_repo.url)
script_file = self._download_script(script_conf.script_repo, logger, cancel_sampler)
logger.info('Done (%s, %s chars).' % (script_file.name, len(script_file.text)))

service = ScriptExecutorSelector.get(script_conf.host_conf, logger, cancel_sampler)
service = ScriptExecutorSelector.get(script_conf.host_conf, logger, cancel_sampler)

logger.info('Connectiong ...')
self._connect(service, cancel_sampler, script_conf.timeout_minutes)
logger.info('Done.')
logger.info('Connectiong ...')
self._connect(service, cancel_sampler, script_conf.timeout_minutes)
logger.info('Done.')

with CloudShellSessionContext(command_context) as session:
output_writer = ReservationOutputWriter(session, command_context)
output_writer = ReservationOutputWriter(api, command_context)
service.execute(script_file, script_conf.host_conf.parameters, output_writer)

def _download_script(self, script_repo, logger, cancel_sampler):
Expand Down
30 changes: 25 additions & 5 deletions package/cloudshell/cm/customscript/domain/script_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ def __init__(self):


class ScriptConfigurationParser(object):
@staticmethod
def json_to_object(json_str):

def __init__(self, api):
"""
:type api: CloudShellAPISession
"""
self.api = api

def json_to_object(self, json_str):
"""
Decodes a json string to an ScriptConfigurationParser instance.
:type json_str: str
Expand All @@ -54,16 +60,30 @@ def json_to_object(json_str):
host = json_obj['hostsDetails'][0]
script_conf.host_conf = HostConfiguration()
script_conf.host_conf.ip = host.get('ip')
script_conf.host_conf.connection_method = host['connectionMethod'].lower() if host.get('connectionMethod') else None
script_conf.host_conf.connection_method = host['connectionMethod'].lower()
script_conf.host_conf.connection_secured = bool_parse(host.get('connectionSecured'))
script_conf.host_conf.username = host.get('username')
script_conf.host_conf.password = host.get('password')
script_conf.host_conf.access_key = host.get('accessKey')
script_conf.host_conf.password = self._get_password(host)
script_conf.host_conf.access_key = self._get_access_key(host)
if host.get('parameters'):
script_conf.host_conf.parameters = dict((i['name'], i['value']) for i in host['parameters'])

return script_conf

def _get_password(self, json_host):
pw = json_host.get('password')
if pw:
return self.api.DecryptPassword(pw).Value
else:
return pw

def _get_access_key(self, json_host):
key = json_host.get('accessKey')
if key:
return self.api.DecryptPassword(key).Value
else:
return key

@staticmethod
def _validate(json_obj):
"""
Expand Down
44 changes: 28 additions & 16 deletions package/tests/test_script_configuration.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,93 @@
from unittest import TestCase

from cloudshell.cm.customscript.domain.script_configuration import ScriptConfigurationParser
from mock import Mock


class TestScriptConfiguration(TestCase):

def setUp(self):
self.api = Mock()
self.parser = ScriptConfigurationParser(self.api)

def test_cannot_parse_json_with_not_numeric_timeout(self):
json = '{"timeoutMinutes":"str"}'
with self.assertRaises(SyntaxError) as context:
ScriptConfigurationParser.json_to_object(json)
self.parser.json_to_object(json)
self.assertIn('Node "timeoutMinutes" must be numeric type.', context.exception.message)

def test_cannot_parse_json_with_negative_numeric_timeout(self):
json = '{"timeoutMinutes":-123}'
with self.assertRaises(SyntaxError) as context:
ScriptConfigurationParser.json_to_object(json)
self.parser.json_to_object(json)
self.assertIn('Node "timeoutMinutes" must be greater/equal to zero.', context.exception.message)

def test_cannot_parse_json_without_repository_details(self):
json = '{}'
with self.assertRaises(SyntaxError) as context:
ScriptConfigurationParser.json_to_object(json)
self.parser.json_to_object(json)
self.assertIn('Missing "repositoryDetails" node.', context.exception.message)

def test_cannot_parse_json_without_repository_url(self):
json = '{"repositoryDetails":{}}'
with self.assertRaises(SyntaxError) as context:
ScriptConfigurationParser.json_to_object(json)
self.parser.json_to_object(json)
self.assertIn('Missing/Empty "repositoryDetails.url" node.', context.exception.message)

def test_cannot_parse_json_with_an_empty_repository_url(self):
json = '{"repositoryDetails":{"url":""}}'
with self.assertRaises(SyntaxError) as context:
ScriptConfigurationParser.json_to_object(json)
self.parser.json_to_object(json)
self.assertIn('Missing/Empty "repositoryDetails.url" node.', context.exception.message)

def test_cannot_parse_json_without_hosts_detalis(self):
json = '{"repositoryDetails":{"url":"someurl"}}'
with self.assertRaises(SyntaxError) as context:
ScriptConfigurationParser.json_to_object(json)
self.parser.json_to_object(json)
self.assertIn('Missing/Empty "hostsDetails" node.', context.exception.message)

def test_cannot_parse_json_with_empty_host_detalis(self):
json = '{"repositoryDetails":{"url":"someurl"},"hostsDetails":[]}'
with self.assertRaises(SyntaxError) as context:
ScriptConfigurationParser.json_to_object(json)
self.parser.json_to_object(json)
self.assertIn('Missing/Empty "hostsDetails" node.', context.exception.message)

def test_cannot_parse_json_with_multiple_hosts_detalis(self):
json = '{"repositoryDetails":{"url":"someurl"},"hostsDetails":[{},{}]}'
with self.assertRaises(SyntaxError) as context:
ScriptConfigurationParser.json_to_object(json)
self.parser.json_to_object(json)
self.assertIn('Node "hostsDetails" must contain only one item.', context.exception.message)

def test_cannot_parse_json_with_host_without_an_ip(self):
json = '{"repositoryDetails":{"url":"someurl"},"hostsDetails":[{"someNode":""}]}'
with self.assertRaises(SyntaxError) as context:
ScriptConfigurationParser.json_to_object(json)
self.parser.json_to_object(json)
self.assertIn('Missing/Empty "hostsDetails[0].ip" node.', context.exception.message)

def test_cannot_parse_json_with_host_with_an_empty_ip(self):
json = '{"repositoryDetails":{"url":"someurl"},"hostsDetails":[{"ip":""}]}'
with self.assertRaises(SyntaxError) as context:
ScriptConfigurationParser.json_to_object(json)
self.parser.json_to_object(json)
self.assertIn('Missing/Empty "hostsDetails[0].ip" node.', context.exception.message)

def test_cannot_parse_json_with_host_without_an_connection_method(self):
json = '{"repositoryDetails":{"url":"someurl"},"hostsDetails":[{"ip":"x.x.x.x"}]}'
with self.assertRaises(SyntaxError) as context:
ScriptConfigurationParser.json_to_object(json)
self.parser.json_to_object(json)
self.assertIn('Missing/Empty "hostsDetails[0].connectionMethod" node.', context.exception.message)

def test_cannot_parse_json_with_host_with_an_empty_connection_method(self):
json = '{"repositoryDetails":{"url":"someurl"},"hostsDetails":[{"ip":"x.x.x.x", "connectionMethod":""}]}'
with self.assertRaises(SyntaxError) as context:
ScriptConfigurationParser.json_to_object(json)
self.parser.json_to_object(json)
self.assertIn('Missing/Empty "hostsDetails[0].connectionMethod" node.', context.exception.message)

def test_sanity(self):
def wrapIt(x):
m = Mock()
m.Value = 'decrypted-' + x
return m
self.api.DecryptPassword.side_effect = lambda x: wrapIt(x)
json = """
{
"timeoutMinutes": 12.3,
Expand All @@ -95,14 +105,16 @@ def test_sanity(self):
"parameters": [{"name":"K11","value":"K12"}, {"name":"K21","value":"K22"}]
}]
}"""
conf = ScriptConfigurationParser.json_to_object(json)
conf = self.parser.json_to_object(json)
self.assertEquals(12.3, conf.timeout_minutes)
self.assertEquals("B", conf.script_repo.url)
self.assertEquals("C", conf.script_repo.username)
self.assertEquals("D", conf.script_repo.password)
self.assertEquals("F", conf.host_conf.username)
self.assertEquals("G", conf.host_conf.password)
self.assertEquals("H", conf.host_conf.access_key)
self.assertEquals("decrypted-G", conf.host_conf.password)
self.assertEquals("decrypted-H", conf.host_conf.access_key)
self.assertEquals("iiiii", conf.host_conf.connection_method)
self.assertItemsEqual('K12', conf.host_conf.parameters['K11'])
self.assertItemsEqual('K22', conf.host_conf.parameters['K21'])
self.assertItemsEqual('K22', conf.host_conf.parameters['K21'])
self.api.DecryptPassword.assert_any_call('G')
self.api.DecryptPassword.assert_any_call('H')