Skip to content

Commit

Permalink
refactored app_ports_operation to move the get custom params method t…
Browse files Browse the repository at this point in the history
…o a domain service
  • Loading branch information
alexazarh committed Jun 15, 2016
1 parent 271416c commit 0cee4c6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
4 changes: 3 additions & 1 deletion package/cloudshell/cp/aws/aws_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from cloudshell.cp.aws.domain.services.session_providers.aws_session_provider import AWSSessionProvider
from cloudshell.cp.aws.domain.services.storage_services.ec2_storage_service import EC2StorageService
from cloudshell.cp.aws.domain.services.task_manager.instance_waiter import EC2InstanceWaiter
from cloudshell.cp.aws.domain.services.model_parser.custom_param_extractor import VmCustomParamsExtractor


class AWSShell(object):
Expand All @@ -23,6 +24,7 @@ def __init__(self):
self.model_parser = AWSModelsParser()
self.cloudshell_session_helper = CloudshellDriverHelper()
self.aws_session_manager = AWSSessionProvider()
self.vm_custom_params_extractor = VmCustomParamsExtractor()

self.security_group_service = AWSSecurityGroupService()

Expand All @@ -38,7 +40,7 @@ def __init__(self):
ec2_storage_service=self.ec2_storage_service,
security_group_service=self.security_group_service)

self.deployed_app_ports_operation = DeployedAppPortsOperation()
self.deployed_app_ports_operation = DeployedAppPortsOperation(self.vm_custom_params_extractor)

def deploy_ami(self, command_context, deployment_request):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
from cloudshell.cp.aws.domain.services.model_parser.port_group_attribute_parser import PortGroupAttributeParser
from cloudshell.cp.aws.models.port_data import PortData
from cloudshell.cp.aws.domain.services.model_parser.custom_param_extractor import VmCustomParamsExtractor


class DeployedAppPortsOperation(object):
def __init__(self):
pass
def __init__(self, vm_custom_params_extractor):
"""
:param VmCustomParamsExtractor vm_custom_params_extractor:
:return:
"""
self.vm_custom_params_extractor = vm_custom_params_extractor

def get_formated_deployed_app_ports(self, custom_params):
"""
:param custom_params:
:return:
"""
inbound_ports_value = self._get_custom_param_value(custom_params, "inbound_ports")
outbound_ports_value = self._get_custom_param_value(custom_params, "outbound_ports")
inbound_ports_value = self.vm_custom_params_extractor.get_custom_param_value(custom_params, "inbound_ports")
outbound_ports_value = self.vm_custom_params_extractor.get_custom_param_value(custom_params, "outbound_ports")

if not inbound_ports_value and not outbound_ports_value:
return ""
Expand All @@ -36,13 +41,6 @@ def get_formated_deployed_app_ports(self, custom_params):

return '\n'.join(result_str_list).strip()

def _get_custom_param_value(self, custom_params, name):
name = name.lower()
params = filter(lambda x: x.name.lower() == name, custom_params)
if len(params) == 1:
return params[0].value
return False

def _port_rule_to_string(self, port_rule):
"""
:param PortData port_rule:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class VmCustomParamsExtractor(object):
def __init__(self):
pass

def get_custom_param_value(self, custom_params, name):
"""
Returns the value of the requested custom param
:param custom_params: The VMCustomParams array that is created by the DeployDataHolder from the deployed app json
:param str name: the name of the custom param to extract
:return: the value of the custom param or None if custom param not found
:rtype str:
"""
name = name.lower()
params = filter(lambda x: x.name.lower() == name, custom_params)
if len(params) == 1:
return params[0].value
return None
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from cloudshell.cp.aws.common.deploy_data_holder import DeployDataHolder

from cloudshell.cp.aws.domain.deployed_app.operations.app_ports_operation import DeployedAppPortsOperation
from cloudshell.cp.aws.domain.services.model_parser.custom_param_extractor import VmCustomParamsExtractor


class TestDeployedAppPortsOperation(TestCase):
def setUp(self):
self.operation = DeployedAppPortsOperation()
self.operation = DeployedAppPortsOperation(VmCustomParamsExtractor())

def test_format_single_inbound(self):
json_str = '{"vmCustomParams":[{"name": "inbound_ports", "value": "80"}]}'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from unittest import TestCase

import jsonpickle
from cloudshell.cp.aws.common.deploy_data_holder import DeployDataHolder
from cloudshell.cp.aws.domain.services.model_parser.custom_param_extractor import VmCustomParamsExtractor


class TestVmCustomParamsExtractor(TestCase):
def setUp(self):
pass

def test_extracor(self):
json_str = '{"vmCustomParams":[{"name": "param", "value": "some_value"}]}'
dict = jsonpickle.decode(json_str)
vmdetails = DeployDataHolder(dict)

extracotr = VmCustomParamsExtractor()
param_value = extracotr.get_custom_param_value(vmdetails.vmCustomParams, "param")

self.assertEqual(param_value, "some_value")


0 comments on commit 0cee4c6

Please sign in to comment.