diff --git a/modules/attribute_formatter.py b/modules/attribute_formatter.py index 484d8c2e61234531de57c6d4b8c1d1919f0fe2fc..4c6f625f10a4539b88ced27d835cfe34e85e74cd 100644 --- a/modules/attribute_formatter.py +++ b/modules/attribute_formatter.py @@ -1,22 +1,26 @@ """ This module handles the creation of simple vagrant commands from yaml attributes. """ +def _format_variable_name(key, variable_type, mappings): + """ Formats the variable to the required vagrantfile definition. """ + return "device." + mappings[variable_type][key] + " = " + def _format_and_add(key, value, mappings, device_definition): """ Formats and adds the definition of a simple attribute. """ - if key in mappings['string']: + if key in mappings["string"]: device_definition.append( - 'device.' + mappings['string'][key] - + ' = \"' + str(value) + '\"') - elif key in mappings['integer']: + _format_variable_name(key, "string", mappings) + + '\"' + str(value) + '\"') + elif key in mappings["integer"]: device_definition.append( - 'device.' + mappings['integer'][key] - + ' = ' + str(value)) - elif key in mappings['boolean']: + _format_variable_name(key, "integer", mappings) + + str(value)) + elif key in mappings["boolean"]: device_definition.append( - 'device.' + mappings['boolean'][key] - + ' = ' + str(value).lower()) + _format_variable_name(key, "boolean", mappings) + + str(value).lower()) def add_simple_commands(device, mappings, device_definition): diff --git a/modules/device_creator.py b/modules/device_creator.py index ae5923889787033ee13f8be0ec52a61c2bcadfe8..ae7e9362df4d1bee460befc77530aa3b43ee9e10 100644 --- a/modules/device_creator.py +++ b/modules/device_creator.py @@ -7,35 +7,17 @@ from modules.attribute_formatter import add_simple_commands from modules.provider import add_prov_attributes, add_router_specification from modules.network_parser import add_networks, add_router_ip - -def _open_yml(yml_file_name): - """ Opens the input yaml file. """ - try: - ymlfile = open(str(yml_file_name)) - yml = yaml.safe_load(ymlfile) - return yml - except IOError: - print("Error: file does not exist.") - - -def _open_mappings(): - """ Opens the file with name mappings. """ - try: - mapping_file = open("name_mapping/mapping.yml") - mappings = yaml.safe_load(mapping_file) - return mappings - except IOError: - print("Error: cannot find mappings.") +MAPPING_FILE = "name_mapping/mapping.yml" +FLAVORS_FILE = "name_mapping/flavors.yml" -def _open_flavors(): - """ Opens the file with flavor definitions. """ +def _open_file(file_name): + """ Opens and returns a file from the argument. """ try: - flavors_file = open("name_mapping/flavors.yml") - flavors = yaml.safe_load(flavors_file) - return flavors + input_file = open(str(file_name)) + return yaml.safe_load(input_file) except IOError: - print("Error: cannot find the list of flavors.") + print("Error: Cannot find a required file.") def _create_hosts(yml, mappings, flavors): @@ -66,11 +48,11 @@ def _create_routers(yml): def create_devices(yml_file_name): - """ Returns a marged dictionary of host and router definitions. """ + """ Returns a merged dictionary of host and router definitions. """ - yml = _open_yml(yml_file_name) - mappings = _open_mappings() - flavors = _open_flavors() + yml = _open_file(yml_file_name) + mappings = _open_file(MAPPING_FILE) + flavors = _open_file(FLAVORS_FILE) return { **_create_hosts(yml, mappings, flavors), diff --git a/modules/network_parser.py b/modules/network_parser.py index 9a85a4bcba36c0224ea7e667c82c4941ba01d576..920313c425f86df8690a356abaf8110aaaaa2462 100644 --- a/modules/network_parser.py +++ b/modules/network_parser.py @@ -7,14 +7,9 @@ def _find_ip(hostname, mappings, device_type): """ Finds the ip address of a device. """ for mapping in mappings: - if device_type == 'host': - if mapping['host'] and mapping['host'] == hostname: + if device_type in ('host', 'router'): + if mapping[device_type] and mapping[device_type] == hostname: return mapping['ip'] - elif device_type == 'router': - if mapping['router'] and mapping['router'] == hostname: - return mapping['ip'] - - return None def _add_ip(hostname, mappings, device_type, definitions): diff --git a/modules/provider.py b/modules/provider.py index 343187c91fb7d4f95251d9725575bffe69ffb0f1..a8f180b1d86ef6aff016a47639096bfcfa4cff6f 100644 --- a/modules/provider.py +++ b/modules/provider.py @@ -51,12 +51,12 @@ def add_prov_attributes(host, flavors, provider_attributes, definitions): def add_router_specification(router, definitions): """ Adds the default specification for a router. """ - ROUTER_BOX = "generic/debian10" - ROUTER_MEMORY = 256 + router_box = "generic/debian10" + router_memory = 256 definitions[router['name']].append( - "device.vm.box = \"" + ROUTER_BOX + "\"") + "device.vm.box = \"" + router_box + "\"") definitions[router['name']].append( "device.vm.provider \"virtualbox\" do |vb|") - definitions[router['name']].append(" vb.memory = " + str(ROUTER_MEMORY)) + definitions[router['name']].append(" vb.memory = " + str(router_memory)) definitions[router['name']].append("end")