def test_attach_detach(self): # setup ec2 ec2_client = boto3.client('ec2', 'us-east-1') vpc = ec2_client.create_vpc(CidrBlock='10.0.0.0/16')['Vpc'] subnet1 = ec2_client.create_subnet(VpcId=vpc['VpcId'], CidrBlock='10.1.0.0/24')['Subnet']['SubnetId'] self.ec2_particle_definition["aws_resource"]["SubnetId"] = subnet1 ec2_client.create_security_group( Description='test', GroupName='test_sg' ) # start quasiparticle for ebs attachment ebs_ec2_quasiparticle = Quasiparticle({ "pcf_name": "quasi", "flavor": "quasiparticle", "particles": [ self.ec2_particle_definition, self.particle_definition_attachment ] }) ebs_ec2_quasiparticle.set_desired_state(State.running) ebs_ec2_quasiparticle.apply() assert ebs_ec2_quasiparticle.get_state() == State.running assert len(ebs_ec2_quasiparticle.get_particle("ebs_volume","gg-pcf").current_state_definition.get("Attachments")) == 1
} } # example quasiparticle that contains all required infrastructure. jit_example_definition = { "pcf_name": "pcf-jit-example", # Required "flavor": "quasiparticle", # Required "particles": [ vpc_definition, security_group_definition, subnet_definition, iam_role_definition, ec2_definition ] } # create quasiparticle jit_quasiparticle = Quasiparticle(jit_example_definition) # start example jit_quasiparticle.set_desired_state(State.running) jit_quasiparticle.apply(sync=True) print(jit_quasiparticle.get_state()) # terminate example jit_quasiparticle.set_desired_state(State.terminated) jit_quasiparticle.apply(sync=True) print(jit_quasiparticle.get_state())
"flavor": "subnet", "parents": ["vpc:vpc_parent"], "aws_resource": { "custom_config": { "subnet_name": "test", "Tags": [{ "Key": "Name", "Value": "test" }] }, "CidrBlock": "10.0.0.0/24" } }] } subnet_quasiparticle = Quasiparticle(quasiparticle_definition) # example start subnet_quasiparticle.set_desired_state(State.running) subnet_quasiparticle.apply(sync=True) print(subnet_quasiparticle.get_state()) print( subnet_quasiparticle.get_particle( "subnet", "subnet_with_vpc_parent").current_state_definition) # example Terminate subnet_quasiparticle.set_desired_state(State.terminated) subnet_quasiparticle.apply(sync=True)
def test_passing_vars(): test_particle_definition_parent = { "pcf_name": "parent", "flavor": "particle_flavor_passing_vars", "aws_resource": { "resource_name": "some service" } } test_particle_definition_child = { "pcf_name": "child", "flavor": "particle_flavor_passing_vars", "parents": ["particle_flavor_passing_vars:parent"], "parent_var": "$inherit$particle_flavor_passing_vars:parent$item", "nested_parent_var": "$inherit$particle_flavor_passing_vars:parent$nested.nested_key", "parent_list_var": "$inherit$particle_flavor_passing_vars:parent$tags", "no_parent_exists": "$inherit$particle_flavor_passing_vars:does_not_exist$key", "no_flavor_exists": "$inherit$does_not_exist:parent$key", "no_key_exists": "$inherit$particle_flavor_passing_vars:parent$no_key", "aws_resource": { "resource_name": "some service" } } test_quasiparticle_base_config = { "pcf_name": "quasiparticle", "particles": [test_particle_definition_parent, test_particle_definition_child], "flavor": "particle_flavor_passing_vars", "aws_resource": { "resource_name": "some service" } } quasiparticle = Quasiparticle(test_quasiparticle_base_config) quasiparticle.set_desired_state(State.running) with raises(InvalidValueReplaceException): quasiparticle.apply() assert (quasiparticle.get_particle( "particle_flavor_passing_vars", "child").particle_definition["parent_var"] == "var_to_be_passed") assert (quasiparticle.get_particle( "particle_flavor_passing_vars", "child").particle_definition["nested_parent_var"] == "nested_var") assert (quasiparticle.get_particle( "particle_flavor_passing_vars", "child").particle_definition["no_parent_exists"] == "$inherit$particle_flavor_passing_vars:does_not_exist$key") assert (quasiparticle.get_particle( "particle_flavor_passing_vars", "child").particle_definition["no_flavor_exists"] == "$inherit$does_not_exist:parent$key") assert (quasiparticle.get_particle( "particle_flavor_passing_vars", "child").particle_definition["parent_list_var"] == [{ "tag1": "a" }, { "tag2": "b" }]) assert (quasiparticle.get_particle( "particle_flavor_passing_vars", "child").particle_definition["no_key_exists"] == "$inherit$particle_flavor_passing_vars:parent$no_key")
def test_apply_states(self): iam = boto3.resource('iam', region_name='us-east-1') conn = boto3.client('iam', region_name='us-east-1') my_managed_policy = { 'Version': '2012-10-17', 'Statement': [ { 'Effect': 'Allow', 'Action': 'logs:CreateLogGroup', 'Resource': '*' }, { 'Effect': 'Allow', 'Action': [ 'dynamodb:GetItem', ], 'Resource': '*' } ] } my_managed_policy2 = { 'Version': '2012-10-17', 'Statement': [ { 'Effect': 'Allow', 'Action': 'logs:CreateLogGroup', 'Resource': '*' }, ] } assume_role_policy_document = { "Version": "2012-10-17", "Statement": [ { "Action": "sts:AssumeRole", "Principal": { "Service": "ec2.amazonaws.com" }, "Effect": "Allow", } ] } quasiparticle_definition = { "pcf_name": "iam_role_with_iam_policy_parents", "flavor": "quasiparticle", "particles":[ { "flavor": "iam_policy", "pcf_name":"iam_policy_parent", "aws_resource": { "custom_config": {}, "PolicyName":"pcf-test", # Required "PolicyDocument": json.dumps(my_managed_policy) } }, { "flavor": "iam_policy", "pcf_name":"iam_policy_parent2", "aws_resource": { "custom_config": {}, "PolicyName":"pcf-test2", # Required "PolicyDocument": json.dumps(my_managed_policy2) } }, { "flavor": "iam_role", "parents":["iam_policy:iam_policy_parent", "iam_policy:iam_policy_parent2"], "aws_resource": { "custom_config": {}, "RoleName":"pcf-test", # Required "AssumeRolePolicyDocument": assume_role_policy_document, } } ] } test_role = conn.create_role(RoleName="pcf-test", AssumeRolePolicyDocument=json.dumps(assume_role_policy_document)) test_policy = conn.create_policy( PolicyName='pcf-test', PolicyDocument=json.dumps(my_managed_policy), Description='Test Policy' ) conn.attach_role_policy(RoleName='pcf-test', PolicyArn='arn:aws:iam::123456789012:policy/pcf-test') iam_quasiparticle = Quasiparticle(quasiparticle_definition) # test start iam_quasiparticle.set_desired_state(State.running) iam_quasiparticle.apply(sync=True) print(iam_quasiparticle.member_particles) assert iam_quasiparticle.get_state() == State.running #test update by removing one policy updated_quasiparticle_definition = { "pcf_name": "iam_role_with_iam_policy_parents", "flavor": "quasiparticle", "particles":[ { "flavor": "iam_policy", "pcf_name":"iam_policy_parent", "aws_resource": { "PolicyName":"pcf-test", # Required "PolicyDocument": json.dumps(my_managed_policy) } }, { "flavor": "iam_role", "parents":["iam_policy:iam_policy_parent"], "aws_resource": { "RoleName":"pcf-test", # Required "AssumeRolePolicyDocument": assume_role_policy_document, } } ] } iam_quasiparticle = Quasiparticle(updated_quasiparticle_definition) iam_quasiparticle.set_desired_state(State.running) iam_quasiparticle.apply(sync=True) assert iam_quasiparticle.get_state() == State.running
"Description": "pcf security group", "GroupName": "Hoos", "DryRun": False, "custom_config": { "Tags": [{ "Key": "Owner", "Value": "Hoos" }], "IpPermissionsEgress": [], "IpPermissions": [] } } }] } quasiparticle = Quasiparticle(vpc_parent_quasiparticle) # example start quasiparticle.set_desired_state(State.running) quasiparticle.apply(sync=True) print(quasiparticle.get_state()) print( quasiparticle.get_particle( "security_group", "sg_with_parent_vpc").get_current_state_definition()) # example Terminate quasiparticle.set_desired_state(State.terminated) quasiparticle.apply(sync=True)
"flavor": "iam_role", "parents": ["iam_policy:iam_policy_parent", "iam_policy:iam_policy_parent2"], "aws_resource": { "custom_config": { "IsInstanceProfile": True }, "RoleName": "pcf-test", # Required "AssumeRolePolicyDocument": assume_role_policy_document, } } ] } iam_quasiparticle = Quasiparticle(quasiparticle_definition) # example start iam_quasiparticle.set_desired_state(State.running) iam_quasiparticle.apply(sync=True) print(iam_quasiparticle.get_state()) # example update # example update by removing one policy updated_quasiparticle_definition = { "pcf_name": "iam_role_with_iam_policy_parents", "flavor":
}], } } # example quasiparticle that contains all required infrastructure. example_definition = { "pcf_name": "ec2-example", # Required "flavor": "quasiparticle", # Required "particles": [ vpc_definition, security_group_definition, subnet_definition, ec2_definition ] } # create ec2 server and required infrastructure quasiparticle = Quasiparticle(example_definition) # start example quasiparticle.set_desired_state("running") quasiparticle.apply(sync=True) print(quasiparticle.get_state()) ec2 = quasiparticle.get_particle("ec2_instance", "ec2-example") print(ec2.current_state_definition.get("PublicDnsName")) # terminate example (uncomment below to terminate) # quasiparticle.set_desired_state("terminated") # quasiparticle.apply(sync=True) # print(quasiparticle.get_state())
def test_vpc_parent(self): quasiparticle = Quasiparticle(self.vpc_parent_quasiparticle) # Test start quasiparticle.set_desired_state(State.running) quasiparticle.apply(sync=True) assert quasiparticle.get_state() == State.running assert quasiparticle.get_particle("subnet","subnet_with_parent").get_state() == State.running # Test Terminate quasiparticle.set_desired_state(State.terminated) quasiparticle.apply(sync=True) assert quasiparticle.get_state() == State.terminated
"parents": ["vpc_instance:example"], "aws_resource": { "custom_config": { "subnet_name": "example_subnet" } } }] } # create a vpc and subnet to be used for the example from pcf.core.quasiparticle import Quasiparticle from pcf.core import State # from pcf.particle.aws.vpc.vpc_instance import VPC # from pcf.particle.aws.vpc.subnet import Subnet subnet_vpc_quasiparticle = Quasiparticle(quasiparticle_definition) subnet_vpc_quasiparticle.set_desired_state(State.running) subnet_vpc_quasiparticle.apply() # get the full subnet definition and both print it and create pcf.json file with the definition from pcf.tools.pcf_generator.pcf_generator import GenerateParticle generated_subnet_particle = GenerateParticle(base_subnet_definition) print(generated_subnet_particle.generate_definition()) generated_subnet_particle.generate_json_file() # # # example of a quasiparticle using the generator from pcf.tools.pcf_generator.pcf_generator import GenerateQuasiparticle quasiparticle = GenerateQuasiparticle(base_quasiparticle_definition) print(quasiparticle.generate_definition()) # terminate the subnet created in this example