def generate_template(yaml_data, default_data): """ Generate troposhere template from given yaml data :param yaml_data: User yaml data :param default_data: default yaml data :return: Troposphere generated cloud formation template """ yaml_return = Yaml(yaml_data, default_data) stack_input = yaml_return.united_data # Create stack and create stack template file template_trop = create_stack(stack_input) template_data = template_trop.template.to_json(indent=2, separators=(',', ': ')) return template_data
def main(): __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) with open(os.path.join(__location__, '../../amazonia/application.yaml'), 'r') as stack_yaml: user_stack_data = yaml.safe_load(stack_yaml) print('\nuser_stack_data=\n{0}\n'.format(user_stack_data)) with open(os.path.join(__location__, '../../amazonia/defaults.yaml'), 'r') as default_yaml: default_data = yaml.safe_load(default_yaml) print('\ndefault_data=\n{0}\n'.format(default_data)) yaml_return = Yaml(user_stack_data, default_data) stack_input = yaml_return.united_data print('\nstack_input=\n{0}\n'.format(stack_input))
def generate_template(yaml_data, default_data, env_variables, customise_script): """ Generate troposhere template from given yaml data :param yaml_data: User yaml data :param default_data: default yaml data :return: Troposphere generated cloud formation template """ yaml_return = Yaml(yaml_data, default_data) stack_input = yaml_return.united_data # Create stack and create stack template file stack = create_stack(stack_input) spec = importlib.util.spec_from_file_location("module.name", customise_script) script = importlib.util.module_from_spec(spec) spec.loader.exec_module(script) template = script.customise_stack_template(stack.template, env_variables) template_data = template.to_json(indent=2, separators=(',', ': ')) return template_data
def test_basic_values(): """ Test basic application.yaml, this tests that an elb is created with the default listener (among other properties) """ global default_data valid_stack_data = open_yaml_file('../../amazonia/application.yaml') amz_yaml = Yaml(valid_stack_data, default_data) stack_input = amz_yaml.united_data # Assert stack values are of type dict assert_equals(type(stack_input), dict) # Assert that there are no invalid stack keys stack_input_set = set(stack_input) expected_stack_set = set(YamlFields.stack_key_list) assert_equals(len(stack_input_set.difference(expected_stack_set)), 0) # Assert that there are no stack keys we missed assert_equals(len(expected_stack_set.difference(stack_input_set)), 0) # Assert correct values assert_equals(stack_input['keypair'], 'INSERT_YOUR_KEYPAIR_HERE') assert_list_equal( stack_input['availability_zones'], ['ap-southeast-2a', 'ap-southeast-2b', 'ap-southeast-2c']) assert_dict_equal(stack_input['vpc_cidr'], { 'name': 'VPC', 'cidr': '10.0.0.0/16' }) assert_dict_equal(stack_input['public_cidr'], { 'name': 'PublicIp', 'cidr': '0.0.0.0/0' }) assert_equals(stack_input['jump_image_id'], 'ami-dc361ebf') assert_equals(stack_input['jump_instance_type'], 't2.nano') assert_equals(stack_input['nat_image_id'], 'ami-53371f30') assert_equals(stack_input['nat_instance_type'], 't2.nano') assert_equals(stack_input['private_hosted_zone_name'], 'private.lan.') assert_equals(type(stack_input['home_cidrs']), list) assert_equals(len(stack_input['home_cidrs']), 1) assert_equals(type(stack_input['autoscaling_units']), list) assert_equals(len(stack_input['autoscaling_units']), 1) autoscaling_unit_input = stack_input['autoscaling_units'][0] # Assert autoscaling unit values are of type dict assert_equals(type(autoscaling_unit_input), dict) # Assert that there are no invalid autoscaling unit keys autoscaling_unit_input_set = set(autoscaling_unit_input.keys()) expected_autoscaling_unit_set = set(YamlFields.autoscaling_unit_key_list) assert_equals( len( autoscaling_unit_input_set.difference( expected_autoscaling_unit_set)), 0) # Assert that there are no autoscaling unit keys we missed assert_equals( len( expected_autoscaling_unit_set.difference( autoscaling_unit_input_set)), 0) assert_equals(autoscaling_unit_input['unit_title'], 'app1') assert_equals(autoscaling_unit_input['asg_config'].image_id, 'ami-dc361ebf') assert_equals(autoscaling_unit_input['asg_config'].instance_type, 't2.nano') assert_equals(autoscaling_unit_input['elb_config'].elb_health_check, 'HTTP:80/index.html') assert_equal( autoscaling_unit_input['elb_config'].elb_listeners_config[0]. instance_protocol, 'HTTP') assert_equal( autoscaling_unit_input['elb_config'].elb_listeners_config[0]. loadbalancer_protocol, 'HTTP') assert_equal( autoscaling_unit_input['elb_config'].elb_listeners_config[0]. loadbalancer_port, '80') assert_equal( autoscaling_unit_input['elb_config'].elb_listeners_config[0]. instance_port, '80') assert_equals(autoscaling_unit_input['asg_config'].minsize, '1') assert_equals(autoscaling_unit_input['asg_config'].maxsize, '2') assert_equals( autoscaling_unit_input['asg_config'].health_check_grace_period, '300') assert_equals(autoscaling_unit_input['asg_config'].health_check_type, 'ELB') assert_equals(autoscaling_unit_input['dependencies'], None)
def test_complete_valid_values(): """ Validate stack yaml value is a list of dictionaries Validate that stack value exists in expected list of stack values """ global default_data valid_stack_data = open_yaml_file('test_yaml_complete_valid.yaml') amz_yaml = Yaml(valid_stack_data, default_data) stack_input = amz_yaml.united_data # Assert stack values are of type dict assert_equals(type(stack_input), dict) # Assert that there are no invalid stack keys stack_input_set = set(stack_input) expected_stack_set = set(YamlFields.stack_key_list) assert_equals(len(stack_input_set.difference(expected_stack_set)), 0) # Assert that there are no stack keys we missed assert_equals(len(expected_stack_set.difference(stack_input_set)), 0) # Assert correct values assert_equals(stack_input['code_deploy_service_role'], 'arn:aws:iam::1234567890123:role/CodeDeployServiceRole') assert_equals(stack_input['keypair'], 'key') assert_list_equal( stack_input['availability_zones'], ['ap-southeast-2a', 'ap-southeast-2b', 'ap-southeast-2c']) assert_dict_equal(stack_input['vpc_cidr'], { 'name': 'VPC', 'cidr': '10.0.0.0/16' }) assert_dict_equal(stack_input['public_cidr'], { 'name': 'PublicIp', 'cidr': '0.0.0.0/0' }) assert_equals(stack_input['jump_image_id'], 'ami-dc361ebf') assert_equals(stack_input['jump_instance_type'], 't2.nano') assert_equals(stack_input['nat_image_id'], 'ami-53371f30') assert_equals(stack_input['nat_instance_type'], 't2.nano') assert_equals(stack_input['public_hosted_zone_name'], 'test.org.') assert_equals(stack_input['private_hosted_zone_name'], 'private.lan.') assert_equals(type(stack_input['home_cidrs']), list) assert_equals(len(stack_input['home_cidrs']), 1) assert_equals(type(stack_input['autoscaling_units']), list) assert_equals(len(stack_input['autoscaling_units']), 2) assert_equals(type(stack_input['database_units']), list) assert_equals(len(stack_input['database_units']), 1) autoscaling_unit_input = stack_input['autoscaling_units'][0] # Assert autoscaling unit values are of type dict assert_equals(type(autoscaling_unit_input), dict) # Assert that there are no invalid autoscaling unit keys autoscaling_unit_input_set = set(autoscaling_unit_input.keys()) expected_autoscaling_unit_set = set(YamlFields.autoscaling_unit_key_list) assert_equals( len( autoscaling_unit_input_set.difference( expected_autoscaling_unit_set)), 0) # Assert that there are no autoscaling unit keys we missed assert_equals( len( expected_autoscaling_unit_set.difference( autoscaling_unit_input_set)), 0) assert_equals(autoscaling_unit_input['unit_title'], 'app1') assert_equals(autoscaling_unit_input['asg_config'].image_id, 'ami-dc361ebf') assert_equals(autoscaling_unit_input['asg_config'].instance_type, 't2.nano') assert_equals(autoscaling_unit_input['elb_config'].elb_health_check, 'HTTP:80/index.html') assert_equal( autoscaling_unit_input['elb_config'].elb_listeners_config[0]. instance_protocol, 'HTTP') assert_equal( autoscaling_unit_input['elb_config'].elb_listeners_config[0]. loadbalancer_protocol, 'HTTP') assert_equal( autoscaling_unit_input['elb_config'].elb_listeners_config[0]. loadbalancer_port, '80') assert_equal( autoscaling_unit_input['elb_config'].elb_listeners_config[0]. instance_port, '80') assert_equals(autoscaling_unit_input['asg_config'].minsize, '1') assert_equals(autoscaling_unit_input['asg_config'].maxsize, '1') assert_equals( autoscaling_unit_input['asg_config'].health_check_grace_period, '300') assert_equals( autoscaling_unit_input['asg_config'].iam_instance_profile_arn, 'arn:aws:iam::1234567890124:role/InstanceProfile') assert_equals(autoscaling_unit_input['elb_config'].elb_log_bucket, 'elb_log_bucket') assert_equals(autoscaling_unit_input['asg_config'].health_check_type, 'ELB') assert_list_equal(autoscaling_unit_input['dependencies'], ['app2', 'db1']) database_unit_input = stack_input['database_units'][0] # Assert database unit values are of type dict assert_equals(type(database_unit_input), dict) # Assert that there are no invalid database unit keys database_unit_input_set = set(database_unit_input) expected_database_unit_set = set(YamlFields.database_unit_key_list) assert_equals( len(database_unit_input_set.difference(expected_database_unit_set)), 0) # Assert that there are no database unit keys we missed assert_equals( len(expected_database_unit_set.difference(database_unit_input_set)), 0) assert_equals(database_unit_input['unit_title'], 'db1') assert_equals(database_unit_input['database_config'].db_instance_type, 'db.t2.micro') assert_equals(database_unit_input['database_config'].db_engine, 'postgres') assert_equals(database_unit_input['database_config'].db_port, '5432') assert_equals(database_unit_input['database_config'].db_name, 'myDb')