<commit_before>import jsonpickle
from cloudshell.cp.core.utils import first_or_default

from cloudshell.cp.azure.common.parsers.security_group_parser import SecurityGroupParser
from cloudshell.cp.azure.domain.services.parsers.network_actions import NetworkActionsParser
from cloudshell.cp.azure.models.app_security_groups_model import AppSecurityGroupModel, DeployedApp, VmDetails
from cloudshell.cp.azure.models.azure_cloud_provider_resource_model import AzureCloudProviderResourceModel
from cloudshell.cp.azure.models.deploy_azure_vm_resource_models import DeployAzureVMResourceModel, \
    RouteTableRequestResourceModel, RouteResourceModel
from cloudshell.cp.azure.models.deploy_azure_vm_resource_models import DeployAzureVMFromCustomImageResourceModel
from cloudshell.cp.azure.common.deploy_data_holder import DeployDataHolder
from cloudshell.cp.azure.models.reservation_model import ReservationModel
from cloudshell.cp.azure.domain.services.parsers.connection_params import convert_to_bool


class AzureModelsParser(object):
    @staticmethod
    def convert_app_resource_to_deployed_app(resource):
        json_str = jsonpickle.decode(resource.app_context.deployed_app_json)
        data_holder = DeployDataHolder(json_str)
        return data_holder

    @staticmethod
    def convert_app_resource_to_request(resource):
        json_str = jsonpickle.decode(resource.app_context.app_request_json)
        data_holder = DeployDataHolder(json_str)
        return data_holder

    @staticmethod
    def _set_base_deploy_azure_vm_model_params(deployment_resource_model, deploy_action, network_actions, cloudshell_session, logger):
        """
        Set base parameters to the Azure Deploy model

        :param deployment_resource_model: deploy_azure_vm_resource_models.BaseDeployAzureVMResourceModel subclass
        :param data_holder: DeployDataHolder instance
        :param cloudshell.api.cloudshell_api.CloudShellAPISession cloudshell_session: instance
        :param logging.Logger logger:
        """

        data_attributes = deploy_action.actionParams.deployment.attributes
        deployment_resource_model.add_public_ip = AzureModelsParser.convert_to_boolean(data_attributes['Add Public IP'])
        deployment_resource_model.autoload = AzureModelsParser.convert_to_boolean(data_attributes['Autoload'])
        deployment_resource_model.inbound_ports = data_attributes['Inbound Ports']
        deployment_resource_model.vm_size = data_attributes['VM Size']
        deployment_resource_model.disk_size = data_attributes['Disk Size']
        deployment_resource_model.public_ip_type = data_attributes['Public IP Type']
        deployment_resource_model.extension_script_file = data_attributes['Extension Script file']
        deployment_resource_model.extension_script_configurations = data_attributes['Extension Script Configurations']
        deployment_resource_model.extension_script_timeout = (int(data_attributes['Extension Script Timeout']))
        deployment_resource_model.disk_type = data_attributes['Disk Type']
        deployment_resource_model.allow_all_sandbox_traffic = data_attributes['Allow all Sandbox Traffic']
        deployment_resource_model.app_name = deploy_action.actionParams.appName
        logical_resource = deploy_action.actionParams.appResource.attributes  # its not a dictionary!!?@@#@!?#

        keys = logical_resource.keys()

        username_key = 'User'
        full_username_key = first_or_default(keys, AzureModelsParser._gen2_attributes_lambda(username_key))
        deployment_resource_model.username = logical_resource[full_username_key] if full_username_key else None

        password_key = 'Password'
        full_password_key = first_or_default(keys, AzureModelsParser._gen2_attributes_lambda(password_key))
        deployment_resource_model.password = logical_resource[full_password_key] if full_password_key else None

        if deployment_resource_model.password:
            logger.info('Decrypting Azure VM password...')
            decrypted_pass = cloudshell_session.DecryptPassword(deployment_resource_model.password)
            deployment_resource_model.password = decrypted_pass.Value

        deployment_resource_model.network_configurations = \
            AzureModelsParser.parse_deploy_networking_configurations(network_actions,logger)

        return deployment_resource_model

    @staticmethod
    def _gen2_attributes_lambda(attribute_key):
        return lambda x: x == attribute_key or x.endswith("." + attribute_key)

    @staticmethod
    def convert_to_boolean( value):
        return value.lower() in ['1', 'true']

    @staticmethod
    def _convert_list_attribute(attribute):
        """Convert string attribute "param1, param2, param3" into list ["param1", "param2", "param3"]

        :param str attribute: data model attribute
        :return: attribute list of params
        :rtype: list
        """
        if attribute:
            list_attr = [param.strip() for param in attribute.split(",")]
        else:
            list_attr = []

        return list_attr


    @staticmethod
    def convert_to_route_table_model(route_table_request):
        """
        Convert deployment request JSON to the DeployAzureVMResourceModel model

        :param str deployment_request: JSON string
        :return: deploy_azure_vm_resource_models.DeployAzureVMResourceModel instance
        :rtype: list[RouteTableRequestResourceModel]
        """
        data = jsonpickle.decode(route_table_request)
        route_table_models = []
        for route_table in data['route_tables']:
            route_table_model = RouteTableRequestResourceModel()
            route_table_model.name = route_table['name']
            route_table_model.subnets = []
            if route_table['subnets']:
                route_table_model.subnets = route_table['subnets']
            routes = []
            for route in route_table['routes']:
                route_model = RouteResourceModel()
                route_model.name = route['name']
                route_model.route_address_prefix = route['address_prefix']
                route_model.next_hop_type = route['next_hop_type']
                route_model.next_hope_address = route['next_hop_address']
                routes.append(route_model)
            route_table_models.routes = routes

            route_table.append(route_table_model)

        return route_table_models

    @staticmethod
    def convert_to_deploy_azure_vm_resource_model(deploy_action, network_actions, cloudshell_session, logger):
        """
        Convert deployment request JSON to the DeployAzureVMResourceModel model

        :param package.cloudshell.cp.core.models.DeployApp deploy_action: describes the desired deployment
        :param cloudshell.api.cloudshell_api.CloudShellAPISession cloudshell_session: instance
        :param logging.Logger logger:
        :return: deploy_azure_vm_resource_models.DeployAzureVMResourceModel instance
        :rtype: DeployAzureVMResourceModel
        """
        deployment_resource_model = DeployAzureVMResourceModel()
        data_attributes = deploy_action.actionParams.deployment.attributes
        deployment_resource_model.image_offer = data_attributes['Image Offer']<commit_msg>should have appended routes to route_table_model, not route_table_models :/<commit_after>import jsonpickle
from cloudshell.cp.core.utils import first_or_default

from cloudshell.cp.azure.common.parsers.security_group_parser import SecurityGroupParser
from cloudshell.cp.azure.domain.services.parsers.network_actions import NetworkActionsParser
from cloudshell.cp.azure.models.app_security_groups_model import AppSecurityGroupModel, DeployedApp, VmDetails
from cloudshell.cp.azure.models.azure_cloud_provider_resource_model import AzureCloudProviderResourceModel
from cloudshell.cp.azure.models.deploy_azure_vm_resource_models import DeployAzureVMResourceModel, \
    RouteTableRequestResourceModel, RouteResourceModel
from cloudshell.cp.azure.models.deploy_azure_vm_resource_models import DeployAzureVMFromCustomImageResourceModel
from cloudshell.cp.azure.common.deploy_data_holder import DeployDataHolder
from cloudshell.cp.azure.models.reservation_model import ReservationModel
from cloudshell.cp.azure.domain.services.parsers.connection_params import convert_to_bool


class AzureModelsParser(object):
    @staticmethod
    def convert_app_resource_to_deployed_app(resource):
        json_str = jsonpickle.decode(resource.app_context.deployed_app_json)
        data_holder = DeployDataHolder(json_str)
        return data_holder

    @staticmethod
    def convert_app_resource_to_request(resource):
        json_str = jsonpickle.decode(resource.app_context.app_request_json)
        data_holder = DeployDataHolder(json_str)
        return data_holder

    @staticmethod
    def _set_base_deploy_azure_vm_model_params(deployment_resource_model, deploy_action, network_actions, cloudshell_session, logger):
        """
        Set base parameters to the Azure Deploy model

        :param deployment_resource_model: deploy_azure_vm_resource_models.BaseDeployAzureVMResourceModel subclass
        :param data_holder: DeployDataHolder instance
        :param cloudshell.api.cloudshell_api.CloudShellAPISession cloudshell_session: instance
        :param logging.Logger logger:
        """

        data_attributes = deploy_action.actionParams.deployment.attributes
        deployment_resource_model.add_public_ip = AzureModelsParser.convert_to_boolean(data_attributes['Add Public IP'])
        deployment_resource_model.autoload = AzureModelsParser.convert_to_boolean(data_attributes['Autoload'])
        deployment_resource_model.inbound_ports = data_attributes['Inbound Ports']
        deployment_resource_model.vm_size = data_attributes['VM Size']
        deployment_resource_model.disk_size = data_attributes['Disk Size']
        deployment_resource_model.public_ip_type = data_attributes['Public IP Type']
        deployment_resource_model.extension_script_file = data_attributes['Extension Script file']
        deployment_resource_model.extension_script_configurations = data_attributes['Extension Script Configurations']
        deployment_resource_model.extension_script_timeout = (int(data_attributes['Extension Script Timeout']))
        deployment_resource_model.disk_type = data_attributes['Disk Type']
        deployment_resource_model.allow_all_sandbox_traffic = data_attributes['Allow all Sandbox Traffic']
        deployment_resource_model.app_name = deploy_action.actionParams.appName
        logical_resource = deploy_action.actionParams.appResource.attributes  # its not a dictionary!!?@@#@!?#

        keys = logical_resource.keys()

        username_key = 'User'
        full_username_key = first_or_default(keys, AzureModelsParser._gen2_attributes_lambda(username_key))
        deployment_resource_model.username = logical_resource[full_username_key] if full_username_key else None

        password_key = 'Password'
        full_password_key = first_or_default(keys, AzureModelsParser._gen2_attributes_lambda(password_key))
        deployment_resource_model.password = logical_resource[full_password_key] if full_password_key else None

        if deployment_resource_model.password:
            logger.info('Decrypting Azure VM password...')
            decrypted_pass = cloudshell_session.DecryptPassword(deployment_resource_model.password)
            deployment_resource_model.password = decrypted_pass.Value

        deployment_resource_model.network_configurations = \
            AzureModelsParser.parse_deploy_networking_configurations(network_actions,logger)

        return deployment_resource_model

    @staticmethod
    def _gen2_attributes_lambda(attribute_key):
        return lambda x: x == attribute_key or x.endswith("." + attribute_key)

    @staticmethod
    def convert_to_boolean( value):
        return value.lower() in ['1', 'true']

    @staticmethod
    def _convert_list_attribute(attribute):
        """Convert string attribute "param1, param2, param3" into list ["param1", "param2", "param3"]

        :param str attribute: data model attribute
        :return: attribute list of params
        :rtype: list
        """
        if attribute:
            list_attr = [param.strip() for param in attribute.split(",")]
        else:
            list_attr = []

        return list_attr


    @staticmethod
    def convert_to_route_table_model(route_table_request):
        """
        Convert deployment request JSON to the DeployAzureVMResourceModel model

        :param str deployment_request: JSON string
        :return: deploy_azure_vm_resource_models.DeployAzureVMResourceModel instance
        :rtype: list[RouteTableRequestResourceModel]
        """
        data = jsonpickle.decode(route_table_request)
        route_table_models = []
        for route_table in data['route_tables']:
            route_table_model = RouteTableRequestResourceModel()
            route_table_model.name = route_table['name']
            route_table_model.subnets = []
            if route_table['subnets']:
                route_table_model.subnets = route_table['subnets']
            routes = []
            for route in route_table['routes']:
                route_model = RouteResourceModel()
                route_model.name = route['name']
                route_model.route_address_prefix = route['address_prefix']
                route_model.next_hop_type = route['next_hop_type']
                route_model.next_hope_address = route['next_hop_address']
                routes.append(route_model)
            route_table_model.routes = routes

            route_table.append(route_table_model)

        return route_table_models

    @staticmethod
    def convert_to_deploy_azure_vm_resource_model(deploy_action, network_actions, cloudshell_session, logger):
        """
        Convert deployment request JSON to the DeployAzureVMResourceModel model

        :param package.cloudshell.cp.core.models.DeployApp deploy_action: describes the desired deployment
        :param cloudshell.api.cloudshell_api.CloudShellAPISession cloudshell_session: instance
        :param logging.Logger logger:
        :return: deploy_azure_vm_resource_models.DeployAzureVMResourceModel instance
        :rtype: DeployAzureVMResourceModel
        """
        deployment_resource_model = DeployAzureVMResourceModel()
        data_attributes = deploy_action.actionParams.deployment.attributes
        deployment_resource_model.image_offer = data_attributes['Image Offer']
{'fraction_non_alphanumeric': 0.06666666666666667, 'fraction_numerical': 0.0013759889920880633, 'mean_word_length': 4.012413793103448, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '828224', 'n_tokens_mistral': 3856, 'n_tokens_neox': 3814, 'n_words': 687}