def test_flip_with_bad_data(self): """ Test that flip fails with an error message when passed bad data """ with self.assertRaisesRegexp(Exception, self.fail_message): cfn_flip.flip(self.bad_data)
def test_flip_with_bad_data(fail_message, bad_data): """ Test that flip fails with an error message when passed bad data """ with pytest.raises(Exception, message=fail_message): cfn_flip.flip(bad_data)
def test_flip_with_bad_data(fail_message, bad_data): """ Test that flip fails with an error message when passed bad data """ with pytest.raises(JSONDecodeError, match=fail_message): cfn_flip.flip(bad_data)
def test_explicit_json_rejects_yaml(input_yaml): """ Given an output format of YAML The input format should be assumed to be JSON and YAML input should be rejected """ with pytest.raises(Exception, message="Invalid JSON"): cfn_flip.flip(input_yaml, out_format="yaml")
def test_explicit_json_rejects_yaml(fail_message, input_yaml): """ Given an output format of YAML The input format should be assumed to be JSON and YAML input should be rejected """ with pytest.raises(JSONDecodeError, match=fail_message): cfn_flip.flip(input_yaml, out_format="yaml")
def test_explicit_yaml_rejects_bad_yaml(self): """ Given an output format of YAML The input format should be assumed to be JSON and YAML input should be rejected """ with self.assertRaisesRegexp(Exception, "Invalid YAML"): cfn_flip.flip(self.bad_data, out_format="json")
def test_explicit_yaml_rejects_bad_yaml(bad_data): """ Given an output format of YAML The input format should be assumed to be JSON and YAML input should be rejected """ with pytest.raises(Exception, message="Invalid YAML"): cfn_flip.flip(bad_data, out_format="json")
def test_explicit_yaml_rejects_bad_yaml(bad_data): """ Given an output format of YAML The input format should be assumed to be JSON and YAML input should be rejected """ with pytest.raises( yaml.scanner.ScannerError, match="while scanning for the next token\nfound character \'\\\\t\' that cannot start any token", ): cfn_flip.flip(bad_data, out_format="json")
def generate_sample_cfn_module(self, module_dir=None): """Generate skeleton CloudFormation sample module.""" if module_dir is None: module_dir = os.path.join(self.env_root, 'sampleapp.cfn') self.generate_sample_module(module_dir) for i in ['stacks.yaml', 'dev-us-east-1.env']: shutil.copyfile( os.path.join(os.path.dirname(os.path.dirname(__file__)), 'templates', 'cfn', i), os.path.join(module_dir, i) ) os.mkdir(os.path.join(module_dir, 'templates')) with open(os.path.join(module_dir, 'templates', 'tf_state.yml'), 'w') as stream: stream.write( cfn_flip.flip( check_output( [sys.executable, os.path.join(os.path.dirname(os.path.dirname(__file__)), # noqa 'templates', 'stacker', 'tfstate_blueprints', 'tf_state.py')] ) ) ) LOGGER.info("Sample CloudFormation module created at %s", module_dir)
def lambda_handler(event, context): data = urlparse.parse_qsl(event["body"]) for k, v in data: if k == 'code': plaintext = v else: plaintext = "" stdout_ = sys.stdout stream = cStringIO.StringIO() sys.stdout = stream hndlr = sys.stdout hndlr.write(flip( plaintext )) sys.stdout = stdout_ data = stream.getvalue() return { 'statusCode': 200, 'body': json.loads(json.dumps(data)), 'headers': { 'Content-Type': 'plain/text', 'Access-Control-Allow-Origin': '*', 'Cache-control': 'private, max-age=0, no-cache' }, }
def test_no_flip_with_json(input_json, parsed_json): """ We should be able to submit JSON and get JSON back """ actual = cfn_flip.flip(input_json, no_flip=True) assert load_json(actual) == parsed_json
def test_flip_to_json(input_yaml, input_json, parsed_json): """ Test that flip performs correctly transforming from yaml to json """ actual = cfn_flip.flip(input_yaml) assert load_json(actual) == parsed_json
def test_flip_with_json_output(input_yaml, parsed_json): """ We should be able to specify that the output is JSON """ actual = cfn_flip.flip(input_yaml, out_format="json") assert load_json(actual) == parsed_json
def test_flip_to_clean_json(input_yaml, clean_json, parsed_clean_json): """ Test that flip performs correctly transforming from yaml to json and the `clean_up` flag is active """ actual = cfn_flip.flip(input_yaml, clean_up=True) assert load_json(actual) == parsed_clean_json
def test_no_flip_with_explicit_json(input_json, parsed_json): """ We should be able to submit JSON and get JSON back and specify the output format explicity """ actual = cfn_flip.flip(input_json, out_format="json", no_flip=True) assert load_json(actual) == parsed_json
def identify_resources(stackName, region): #this function will build out the list of resources for analysis client = boto3.client('cloudformation', region) response = client.list_stack_resources(StackName=stackName, ) return_obj = {} ec2_resources = {} rds_resources = {} asg_resources = {} #Generate list for existing resources resources = response['StackResourceSummaries'] for resource in resources: if resource['ResourceType'] == 'AWS::EC2::Instance': ec2_resources[ resource['LogicalResourceId']] = resource['PhysicalResourceId'] elif resource['ResourceType'] == 'AWS::RDS::DBInstance': rds_resources[ resource['LogicalResourceId']] = resource['PhysicalResourceId'] elif resource['ResourceType'] == 'AWS::AutoScaling::AutoScalingGroup': asg_resources[ resource['LogicalResourceId']] = resource['PhysicalResourceId'] response = client.get_template(StackName=stackName) #Identify resources that don't currently exist if is_json(response['TemplateBody']): resources_fullList = json.loads(json.dumps( response['TemplateBody']))['Resources'] else: resources_fullList = json.loads(flip( response['TemplateBody']))['Resources'] print(resources_fullList) for resource in resources_fullList: if resources_fullList[resource]['Type'] == 'AWS::EC2::Instance': if resource not in ec2_resources: ec2_resources[resource] = "DoesNotExist" if resources_fullList[resource]['Type'] == 'AWS::RDS::DBInstance': if resource not in rds_resources: rds_resources[resource] = "DoesNotExist" if resources_fullList[resource][ 'Type'] == 'AWS::AutoScaling::AutoScalingGroup': if resource not in asg_resources: asg_resources[resource] = "DoesNotExist" #Build and return final object return_obj['ec2'] = ec2_resources return_obj['rds'] = rds_resources return_obj['asg'] = asg_resources print("Identified Resources: EC2[" + str(len(ec2_resources)) + "] RDS[" + str(len(rds_resources)) + "] ASG[" + str(len(asg_resources)) + "] --> " + str(return_obj)) return return_obj
def test_flip_to_json(self): """ Test that flip performs correctly transforming from yaml to json """ actual = cfn_flip.flip(self.input_yaml) parsed_actual = json.loads(actual) self.assertDictEqual(parsed_actual, self.parsed_json)
def test_no_flip_with_json(self): """ We should be able to submit JSON and get JSON back """ actual = cfn_flip.flip(self.input_json, no_flip=True) parsed_actual = json.loads(actual) self.assertDictEqual(parsed_actual, self.parsed_json)
def test_no_flip_with_yaml(self): """ We should be able to submit YAML and get YAML back """ actual = cfn_flip.flip(self.input_yaml, no_flip=True) parsed_actual = yaml.load(actual, Loader=CustomLoader) self.assertDictEqual(parsed_actual, self.parsed_yaml)
def test_flip_with_yaml_output(input_json, parsed_yaml): """ We should be able to specify that the output is YAML """ actual = cfn_flip.flip(input_json, out_format="yaml") parsed_actual = load_yaml(actual) assert parsed_actual == parsed_yaml
def test_flip_with_json_output(self): """ We should be able to specify that the output is JSON """ actual = cfn_flip.flip(self.input_yaml, out_format="json") parsed_actual = json.loads(actual) self.assertDictEqual(parsed_actual, self.parsed_json)
def test_flip_with_yaml_output(self): """ We should be able to specify that the output is YAML """ actual = cfn_flip.flip(self.input_json, out_format="yaml") parsed_actual = yaml.load(actual, Loader=CustomLoader) self.assertDictEqual(parsed_actual, self.parsed_yaml)
def test_no_flip_with_yaml(input_yaml, parsed_yaml): """ We should be able to submit YAML and get YAML back """ actual = cfn_flip.flip(input_yaml, no_flip=True) parsed_actual = load_yaml(actual) assert parsed_actual == parsed_yaml
def test_flip_to_clean_json(self): """ Test that flip performs correctly transforming from yaml to json and the `clean_up` flag is active """ actual = cfn_flip.flip(self.input_yaml, clean_up=True) parsed_actual = json.loads(actual) self.assertDictEqual(parsed_actual, self.parsed_clean_json)
def test_no_flip_with_explicit_yaml(input_yaml, parsed_yaml): """ We should be able to submit YAML and get YAML back and specify the output format explicity """ actual = cfn_flip.flip(input_yaml, out_format="yaml", no_flip=True) parsed_actual = load_yaml(actual) assert parsed_actual == parsed_yaml
def test_no_flip_with_explicit_json(self): """ We should be able to submit JSON and get JSON back and specify the output format explicity """ actual = cfn_flip.flip(self.input_json, out_format="json", no_flip=True) parsed_actual = json.loads(actual) self.assertDictEqual(parsed_actual, self.parsed_json)
def test_no_flip_with_explicit_yaml(self): """ We should be able to submit YAML and get YAML back and specify the output format explicity """ actual = cfn_flip.flip(self.input_yaml, out_format="yaml", no_flip=True) parsed_actual = yaml.load(actual, Loader=CustomLoader) self.assertDictEqual(parsed_actual, self.parsed_yaml)
def test_flip_to_yaml(self): """ Test that flip performs correctly transforming from json to yaml """ actual = cfn_flip.flip(self.input_json) # The result should not parse as json with self.assertRaises(ValueError): json.loads(actual) parsed_actual = yaml.load(actual) self.assertDictEqual(parsed_actual, self.parsed_yaml)
def test_flip_to_yaml(input_json, input_yaml, parsed_yaml): """ Test that flip performs correctly transforming from json to yaml """ actual = cfn_flip.flip(input_json) assert actual == input_yaml + "\n" # The result should not parse as json with pytest.raises(ValueError): load_json(actual) parsed_actual = load_yaml(actual) assert parsed_actual == parsed_yaml
def test_flip_to_yaml(self): """ Test that flip performs correctly transforming from json to yaml """ actual = cfn_flip.flip(self.input_json) # The result should not parse as json with self.assertRaises(ValueError): json.loads(actual) parsed_actual = custom_yaml.load(actual) self.assertDictEqual(parsed_actual, self.parsed_yaml)
def test_flip_to_yaml_with_longhand_functions(input_json, parsed_json): """ When converting to yaml, sometimes we'll want to keep the long form """ actual1 = cfn_flip.flip(input_json, long_form=True) actual2 = cfn_flip.to_yaml(input_json, long_form=True) # No custom loader as there should be no custom tags parsed_actual1 = yaml.load(actual1) parsed_actual2 = yaml.load(actual2) # We use the parsed JSON as it contains long form function calls assert parsed_actual1 == parsed_json assert parsed_actual2 == parsed_json