Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests #25

Merged
merged 16 commits into from
May 5, 2017
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ omit =
*/profiler/*
*/tests/*
*/remap_child_resources_constants.py
*/setup_script.py
*/teardown_script.py
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,10 @@ qpm.ini
SandboxOrchestration/environment_scripts/env_setup/data.json
SandboxOrchestrationPackage/Topology Scripts/Default Sandbox Setup.zip
SandboxOrchestrationPackage/Topology Scripts/Default Sandbox Teardown.zip
SandboxOrchestrationPackage/Topology Scripts/Default Sandbox Teardown.zip
SandboxOrchestrationPackage/Topology Scripts/SaveSnapshot.zip
sandbox_scripts/environment/.idea/.name
sandbox_scripts/environment/.idea/encodings.xml
sandbox_scripts/environment/.idea/misc.xml
sandbox_scripts/environment/.idea/workspace.xml
*.zip
sandbox_scripts/environment/.idea/environment.iml
SandboxOrchestrationPackage/Topology Scripts/Default Sandbox Setup.zip
sandbox_scripts/environment/.idea/modules.xml
SandboxOrchestrationPackage/Topology Scripts/Default Sandbox Teardown.zip
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from sandbox_scripts.environment.teardown.teardown_resources import EnvironmentTeardownResources
from sandbox_scripts.environment.teardown.teardown_VM import EnvironmentTeardownVM


def main():
EnvironmentTeardown().execute()
EnvironmentTeardownVM().execute()
Expand Down
62 changes: 34 additions & 28 deletions sandbox_scripts/QualiEnvironmentUtils/ConfigFileManager.py
Original file line number Diff line number Diff line change
@@ -1,86 +1,92 @@
# coding=utf-8
from sandbox_scripts.QualiEnvironmentUtils.Sandbox import *
from time import gmtime, strftime
import traceback
import re
from time import gmtime, strftime
from sandbox_scripts.QualiEnvironmentUtils.QualiUtils import QualiError


# ===================================
# ===================================
class ConfigFileManager:
def __init__(self, sandbox):
"""
:param SandboxBase sandbox: The sandbox the config file mgr will work with
"""
self.sandbox = sandbox
def __init__(self):
pass

# ----------------------------------
# ----------------------------------
def create_concrete_config_from_template(self, template_config_data, config_set_pool_data,
resource):
def create_concrete_config_from_template(self, template_config_data, config_set_pool_data, sandbox, resource):
"""
Replace parameters in the template file with concrete values
Parameters in the template file are marked with {}
:param str template_config_data: The data from the config template file
:param dict config_set_pool_data: A dictionary with the data from the config set pool
:param SandboxBase sandbox: The sandbox to get other resources values from
:param ResourceBase resource: The resource we want to create the config file for
"""
try:
concrete_config_data = template_config_data
# Replace {ConfigPool.PARAM} with PARAM's value from the pool
it = re.finditer(r"\{ConfigPool\:[^}]*\}", concrete_config_data, flags=re.IGNORECASE)
it = re.finditer(r'\{ConfigPool\:[^}]*\}', concrete_config_data, flags=re.IGNORECASE)
for match in it:
param = match.group()
concrete_config_data = concrete_config_data.replace(param, config_set_pool_data[param.lower()])
if param.lower() in config_set_pool_data:
concrete_config_data = concrete_config_data.replace(param, config_set_pool_data[param.lower()])
else:
raise Exception('Could not find attribute ' + param.lower() + ' in the config pool')

# Replace {QUALI-NOTATION} WITH A NOTE
it = re.finditer(r"\{QUALI NOTATION\}", concrete_config_data,flags=re.IGNORECASE)
it = re.finditer(r'\{QUALI NOTATION\}', concrete_config_data, flags=re.IGNORECASE)
for match in it:
param = match.group()
quali_note = "Built from template: " + strftime("%Y-%b-%d %H:%M:%S", gmtime())
quali_note = 'Built from template: ' + strftime('%Y-%b-%d %H:%M:%S', gmtime())
concrete_config_data = concrete_config_data.replace(param, quali_note)

# Replace {Device.Self.Name} with the resource's name
it = re.finditer(r"\{Device:Self:Name\}", concrete_config_data,flags=re.IGNORECASE)
it = re.finditer(r'\{Device:Self:Name\}', concrete_config_data, flags=re.IGNORECASE)
for match in it:
param = match.group()
concrete_config_data = concrete_config_data.replace(param, resource.name)

# Replace {Device.Self.Address} with the resource's management ip
it = re.finditer(r"\{Device:Self:Address\}", concrete_config_data,flags=re.IGNORECASE)
it = re.finditer(r'\{Device:Self:Address\}', concrete_config_data, flags=re.IGNORECASE)
for match in it:
param = match.group()
concrete_config_data = concrete_config_data.replace(param, resource.address)

# Replace {Device.Self.ATTRIBUTE_NAME} with the resource's attribute value
# Need to decode password attributes: Password, Enable Password, and SNMP Read Community
it = re.finditer(r"\{Device:Self\:[^}]*\}", concrete_config_data,flags=re.IGNORECASE)
# TODO: Need to decode password attributes: Password, Enable Password, and SNMP Read Community
it = re.finditer(r'\{Device:Self\:[^}]*\}', concrete_config_data, flags=re.IGNORECASE)
for match in it:
param = match.group()
idx = param.rfind(':')+1
att_name = param[idx:len(param)-1]
param_val = resource.get_attribute(att_name)
#param_val = resource.get_attribute(param)
# param_val = resource.get_attribute(param)
concrete_config_data = concrete_config_data.replace(param, param_val)

# Replacemant of params from types: {Device:ALIAS:Attribute_name}
root_resources = self.sandbox.get_root_networking_resources()
it = re.finditer(r"\{Device:[^}]*\}", concrete_config_data, flags=re.IGNORECASE)
# Replacement of params from types: {Device:ALIAS:Attribute_name}
it = re.finditer(r'\{Device:[^}]*\}', concrete_config_data, flags=re.IGNORECASE)
root_resources = None
for match in it:
param = match.group()
junk, sb_alias, alias_attribname = param.split(":")
alias_attribname = alias_attribname.replace("}","")
alias_attribname = alias_attribname.replace("}", "")
concrete_name = ''
if root_resources is None: # fetch once the resources
root_resources = sandbox.get_root_networking_resources()
for resource in root_resources:
if resource.alias == sb_alias:
concrete_name = resource.name
param_val = resource.get_attribute(alias_attribname)
if resource.attribute_exist(alias_attribname):
param_val = resource.get_attribute(alias_attribname)
else:
raise Exception("Could not find attribute '{0}' in resource '{1}'".format(alias_attribname,
resource.name))
concrete_config_data = concrete_config_data.replace(param, param_val)
break
if concrete_name <= ' ':
raise ('did not find concrete device with alias ' + sb_alias + '; likely missing from blueprint.')
raise Exception('Could not find a resource with alias ' + sb_alias + '; likely missing from blueprint.')

return concrete_config_data
except:
print str(Exception.message)
self.sandbox.report_error("Failed to create a concrete config file from the template\'s data. "
"Unexpected error: " + traceback.format_exc())
except Exception as ex:
raise QualiError('ConfigFileManager', "Failed to create a concrete config file from the template\'s data. "
"Unexpected error: " + ex.message)
57 changes: 11 additions & 46 deletions sandbox_scripts/QualiEnvironmentUtils/ConfigPoolManager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# coding=utf-8
from sandbox_scripts.QualiEnvironmentUtils.Sandbox import *
from sandbox_scripts.QualiEnvironmentUtils.Sandbox import SandboxBase
from sandbox_scripts.QualiEnvironmentUtils.Resource import ResourceBase
from sandbox_scripts.QualiEnvironmentUtils.QualiUtils import QualiError
import re


Expand All @@ -11,61 +13,24 @@ def __init__(self, sandbox, pool_resource):
:param ResourceBase pool_resource: The pool resource in the sandbox
"""
self.sandbox = sandbox
self.pool_resource = pool_resource

# -----------------------------------------
# Walk over the sub-resources of the pool resource,
# For each sub-resource in the pool, get the data from their attributes, and update it on the actual device
# The device's alias in the sandbox will appear as a pool sub-resource,
# optionally with a number concatenated at the end e.g. Gateway1
# -----------------------------------------
def push_data_from_pool_to_sandbox(self):
message = ''
sandbox_resources = self.sandbox.get_root_resources()
# a list to mark which resources in the sandbox were already updated
sandbox_resources_can_update = [True]*len(sandbox_resources)

for resource_from_pool in self.pool_resource.details.ChildResources:
split_name = resource_from_pool.Name.split('/')
name_of_resource_from_pool = split_name[len(split_name)-1]
found_resource_in_sandbox = False
idx = 0
# Find the resource_from_pool in the sandbox
for sandbox_resource in sandbox_resources:
# use a regular expression since the pool may contain resources with a running index at the
# end of the name, that should match a generic alias for multiple resources in the sandbox.
# This can be caused from an abstract with quantity>1
# e.g. the pool has CC1, CC2. The sandbox has a CC X2

pattern = sandbox_resource.alias + '\d*'
found_pattern = re.search(pattern, name_of_resource_from_pool, flags=0)
if found_pattern:
if sandbox_resources_can_update[idx]:
found_resource_in_sandbox = True
sandbox_resources_can_update[idx] = False
for attribute in resource_from_pool.ResourceAttributes:
sandbox_resource.set_attribute_value(attribute_name=attribute.Name,
attribute_value=attribute.Value)
break
idx += 1
if not found_resource_in_sandbox:
message += 'Resource ' + resource_from_pool.Name + ' is in the pool, but could not be found in the sandbox. Please check\n'
if message != '':
raise QualiError(name=self.sandbox.id, message=message)
if pool_resource:
self.pool_resource = pool_resource
else:
raise QualiError('ConfigPoolManager', 'Trying to use the ConfigPoolManager without a pool resource')
self.pool_data = self._pool_data_to_dict()

# -----------------------------------------
# Create a dictionary that will hold the data from the pool
# -----------------------------------------
def pool_data_to_dict(self):
def _pool_data_to_dict(self):
pool_data_dict = dict()
for attribute in self.pool_resource.attributes:
pool_data_dict[str('{ConfigPool:' + attribute.Name + '}').lower()] = attribute.Value
for resource_from_pool in self.pool_resource.details.ChildResources:
split_name = resource_from_pool.Name.split('/')
name_of_resource_from_pool = split_name[len(split_name)-1]
#resource_attributes_dict = dict()
for attribute in resource_from_pool.ResourceAttributes:
resource_dict_key = str('{ConfigPool:' + name_of_resource_from_pool + ':' + attribute.Name + '}').lower()
#resource_attributes_dict[resource_dict_key] = attribute.Value
resource_dict_key = str('{ConfigPool:' + name_of_resource_from_pool + ':' +
attribute.Name + '}').lower()
pool_data_dict[resource_dict_key] = attribute.Value
return pool_data_dict
Loading