class TestRoute53PrivateZone(unittest.TestCase): @classmethod def setUpClass(self): time_str = str(int(time.time())) self.route53 = Route53Connection() self.base_domain = 'boto-private-zone-test-%s.com' % time_str self.vpc = VPCConnection() self.test_vpc = self.vpc.create_vpc(cidr_block='10.11.0.0/16') # tag the vpc to make it easily identifiable if things go spang self.test_vpc.add_tag("Name", self.base_domain) self.zone = self.route53.get_zone(self.base_domain) if self.zone is not None: self.zone.delete() def test_create_private_zone(self): self.zone = self.route53.create_hosted_zone(self.base_domain, private_zone=True, vpc_id=self.test_vpc.id, vpc_region='us-east-1') @classmethod def tearDownClass(self): if self.zone is not None: self.zone.delete() self.test_vpc.delete()
def _configure_environment(self): """Create a new disco style environment VPC""" vpc_cidr = self.get_config("vpc_cidr") # Create VPC vpc_conn = VPCConnection() self.vpc = vpc_conn.create_vpc(self.get_config("vpc_cidr")) keep_trying(300, self.vpc.add_tag, "Name", self.environment_name) keep_trying(300, self.vpc.add_tag, "type", self.environment_type) logging.debug("vpc: %s", self.vpc) dhcp_options = self._configure_dhcp() self.vpc.connection.associate_dhcp_options(dhcp_options.id, self.vpc.id) # Enable DNS vpc_conn.modify_vpc_attribute(self.vpc.id, enable_dns_support=True) vpc_conn.modify_vpc_attribute(self.vpc.id, enable_dns_hostnames=True) # Create metanetworks (subnets, route_tables and security groups) for network in self.networks.itervalues(): network.create() # Configure security group rules for network in self.networks.values(): self._add_sg_rules(network) # Set up security group rules self._open_customer_ports() # Allow ICMP (ping, traceroute & etc) and DNS traffic for all subnets for network in self.networks.itervalues(): self.vpc.connection.authorize_security_group( group_id=network.security_group.id, ip_protocol="icmp", from_port=-1, to_port=-1, cidr_ip=vpc_cidr) self.vpc.connection.authorize_security_group( group_id=network.security_group.id, ip_protocol="udp", from_port=53, to_port=53, cidr_ip=vpc_cidr) # Setup internet gateway internet_gateway = self.vpc.connection.create_internet_gateway() self.vpc.connection.attach_internet_gateway(internet_gateway.id, self.vpc.id) logging.debug("internet_gateway: %s", internet_gateway) self._add_igw_routes(internet_gateway) self._attach_vgw() self.configure_notifications() DiscoVPC.create_peering_connections( DiscoVPC.parse_peerings_config(self.vpc.id)) self.rds.update_all_clusters_in_vpc()
def _configure_environment(self): """Create a new disco style environment VPC""" vpc_cidr = self.get_config("vpc_cidr") # Create VPC vpc_conn = VPCConnection() self.vpc = vpc_conn.create_vpc(self.get_config("vpc_cidr")) keep_trying(300, self.vpc.add_tag, "Name", self.environment_name) keep_trying(300, self.vpc.add_tag, "type", self.environment_type) logging.debug("vpc: %s", self.vpc) dhcp_options = self._configure_dhcp() self.vpc.connection.associate_dhcp_options(dhcp_options.id, self.vpc.id) # Enable DNS vpc_conn.modify_vpc_attribute(self.vpc.id, enable_dns_support=True) vpc_conn.modify_vpc_attribute(self.vpc.id, enable_dns_hostnames=True) # Create metanetworks (subnets, route_tables and security groups) for network in self.networks.itervalues(): network.create() # Configure security group rules for network in self.networks.values(): self._add_sg_rules(network) # Set up security group rules self._open_customer_ports() # Allow ICMP (ping, traceroute & etc) and DNS traffic for all subnets for network in self.networks.itervalues(): self.vpc.connection.authorize_security_group( group_id=network.security_group.id, ip_protocol="icmp", from_port=-1, to_port=-1, cidr_ip=vpc_cidr ) self.vpc.connection.authorize_security_group( group_id=network.security_group.id, ip_protocol="udp", from_port=53, to_port=53, cidr_ip=vpc_cidr ) # Setup internet gateway internet_gateway = self.vpc.connection.create_internet_gateway() self.vpc.connection.attach_internet_gateway(internet_gateway.id, self.vpc.id) logging.debug("internet_gateway: %s", internet_gateway) self._add_igw_routes(internet_gateway) self._attach_vgw() self.configure_notifications() DiscoVPC.create_peering_connections(DiscoVPC.parse_peerings_config(self.vpc.id)) self.rds.update_all_clusters_in_vpc()
def test_db_subnet_group(self): vpc_api = VPCConnection() rds_api = RDSConnection() vpc = vpc_api.create_vpc('10.0.0.0/16') az_list = vpc_api.get_all_zones(filters={'state': 'available'}) subnet = list() n = 0 for az in az_list: try: subnet.append( vpc_api.create_subnet(vpc.id, '10.0.' + str(n) + '.0/24', availability_zone=az.name)) n = n + 1 except: pass grp_name = 'db_subnet_group' + str(int(time.time())) subnet_group = rds_api.create_db_subnet_group( grp_name, grp_name, [subnet[0].id, subnet[1].id]) if not _is_ok(subnet_group, vpc.id, grp_name, [subnet[0].id, subnet[1].id]): raise Exception("create_db_subnet_group returned bad values") rds_api.modify_db_subnet_group(grp_name, description='new description') subnet_grps = rds_api.get_all_db_subnet_groups(name=grp_name) if not _is_ok(subnet_grps[0], vpc.id, 'new description', [subnet[0].id, subnet[1].id]): raise Exception( "modifying the subnet group desciption returned bad values") rds_api.modify_db_subnet_group(grp_name, subnet_ids=[subnet[1].id, subnet[2].id]) subnet_grps = rds_api.get_all_db_subnet_groups(name=grp_name) if not _is_ok(subnet_grps[0], vpc.id, 'new description', [subnet[1].id, subnet[2].id]): raise Exception( "modifying the subnet group subnets returned bad values") rds_api.delete_db_subnet_group(subnet_group.name) try: rds_api.get_all_db_subnet_groups(name=grp_name) raise Exception(subnet_group.name + " still accessible after delete_db_subnet_group") except: pass while n > 0: n = n - 1 vpc_api.delete_subnet(subnet[n].id) vpc_api.delete_vpc(vpc.id)
def test_db_subnet_group(self): vpc_api = VPCConnection() rds_api = RDSConnection() vpc = vpc_api.create_vpc('10.0.0.0/16') az_list = vpc_api.get_all_zones(filters={'state':'available'}) subnet = list() n = 0; for az in az_list: try: subnet.append(vpc_api.create_subnet(vpc.id, '10.0.'+str(n)+'.0/24',availability_zone=az.name)) n = n+1 except: pass grp_name = 'db_subnet_group'+str(int(time.time())) subnet_group = rds_api.create_db_subnet_group(grp_name, grp_name, [subnet[0].id,subnet[1].id]) if not _is_ok(subnet_group, vpc.id, grp_name, [subnet[0].id,subnet[1].id]): raise Exception("create_db_subnet_group returned bad values") rds_api.modify_db_subnet_group(grp_name, description='new description') subnet_grps = rds_api.get_all_db_subnet_groups(name=grp_name) if not _is_ok(subnet_grps[0], vpc.id, 'new description', [subnet[0].id,subnet[1].id]): raise Exception("modifying the subnet group desciption returned bad values") rds_api.modify_db_subnet_group(grp_name, subnet_ids=[subnet[1].id,subnet[2].id]) subnet_grps = rds_api.get_all_db_subnet_groups(name=grp_name) if not _is_ok(subnet_grps[0], vpc.id, 'new description', [subnet[1].id,subnet[2].id]): raise Exception("modifying the subnet group subnets returned bad values") rds_api.delete_db_subnet_group(subnet_group.name) try: rds_api.get_all_db_subnet_groups(name=grp_name) raise Exception(subnet_group.name+" still accessible after delete_db_subnet_group") except: pass while n > 0: n = n-1 vpc_api.delete_subnet(subnet[n].id) vpc_api.delete_vpc(vpc.id)
def create_vpc(self, vpc_cidr, subnet_cidr): c = VPCConnection() vpc = c.create_vpc(vpc_cidr) vpc.add_tag('organization', value=self.organization) subnet = c.create_subnet(vpc.id, subnet_cidr) while True: try: subnet.add_tag('organization', value=self.organization) break except: pass db_vpc = organization_vpc.OrganizationVPC() org = organizations.Organizations() org.load(name=self.organization) print org db_vpc.set('organization_id', org.get('id')) db_vpc.set('vpc_id', vpc.id) db_vpc.set('subnet_id', subnet.id) db_vpc.insert() return vpc.id, subnet.id, org.get('id')
Press 'Enter' when finished" VPC_CIDR = raw_input() print "Type the Name for VPC (to use as Tag), Press 'Enter' when finished" VPC_NAME = raw_input() ''' #boto.set_stream_logger("Stratus") print "Boto Version: ", boto.Version, "\n" # print boto.rds2.regions() exist_vpc = vpcc.get_all_vpcs(filters=[("cidrBlock", VPC_CIDR)]) if not len(exist_vpc): new_vpc = vpcc.create_vpc(cidr_block=VPC_CIDR, instance_tenancy=VPC_TENANCY, dry_run=BOOLEAN_DRYRUN) aws_vpc_id = new_vpc.id print "No existing VPC" print "New VPC created: ", aws_vpc_id, "\n" else: # Return 1st object from list aws_vpc_id = str(exist_vpc.pop(0))[4:] print "Identical CIDR already used. Skipped creation." print "Existing VPC ID: {}\n".format(aws_vpc_id) # Use existing VPC new_vpc = vpcc.get_all_vpcs(filters=[("cidrBlock", VPC_CIDR)]) ''' # Doesn't work due to VPC dependency print "Requested VPC already exists! Will attempt to delete vpc and recreate" del_status = vpcc.delete_vpc(aws_vpc_id)
def create_asg_with_tags(asg_name, tags, ami_id="ami-abcd1234", elbs=None): """ Create an ASG with the given name, tags and AMI. This is meant to be used in tests that are decorated with the @mock_autoscaling moto decorator. Arguments: asg_name(str): The name of the new auto-scaling group. tags(dict): A dict mapping tag names to tag values. ami_id(str): The ID of the AMI that should be deployed. Returns: boto.ec2.autoscale.group.AutoScalingGroup """ tag_list = [ Tag(key=k, value=v, resource_id=asg_name, propagate_at_launch=True) for k, v in six.iteritems(tags) ] if elbs is None: elbs = [] # Create asgs vpcconn = VPCConnection() conn = boto.ec2.autoscale.connect_to_region('us-east-1') config = LaunchConfiguration( name='{}_lc'.format(asg_name), image_id=ami_id, instance_type='t2.medium', ) conn.create_launch_configuration(config) vpc = vpcconn.create_vpc('10.0.0.0/24') subnetc = vpcconn.create_subnet(vpc.id, '10.0.0.0/28', 'us-east-1c') subnetb = vpcconn.create_subnet(vpc.id, '10.0.0.16/28', 'us-east-1b') group = AutoScalingGroup( name=asg_name, availability_zones=['us-east-1c', 'us-east-1b'], default_cooldown=60, desired_capacity=2, load_balancers=elbs, health_check_period=100, health_check_type="EC2", max_size=2, min_size=2, launch_config=config, placement_group="test_placement", vpc_zone_identifier="{subnetbid},{subnetcid}".format( subnetbid=subnetb.id, subnetcid=subnetc.id), termination_policies=["OldestInstance", "NewestInstance"], tags=tag_list, ) conn.create_auto_scaling_group(group) # Each ASG tag that has 'propagate_at_launch' set to True is *supposed* to be set on the instances. # However, it seems that moto (as of 0.4.30) does not properly set the tags on the instances created by the ASG. # So set the tags on the ASG instances manually instead. ec2_conn = boto.connect_ec2() for asg in conn.get_all_groups(): if asg.name == asg_name: asg_instance_ids = [ instance.instance_id for instance in asg.instances ] for instance_id in asg_instance_ids: ec2_conn.create_tags(instance_id, tags) return group
Press 'Enter' when finished" VPC_CIDR = raw_input() print "Type the Name for VPC (to use as Tag), Press 'Enter' when finished" VPC_NAME = raw_input() """ # boto.set_stream_logger("Stratus") print "Boto Version: ", boto.Version, "\n" # print boto.rds2.regions() exist_vpc = vpcc.get_all_vpcs(filters=[("cidrBlock", VPC_CIDR)]) if not len(exist_vpc): new_vpc = vpcc.create_vpc(cidr_block=VPC_CIDR, instance_tenancy=VPC_TENANCY, dry_run=BOOLEAN_DRYRUN) aws_vpc_id = new_vpc.id print "No existing VPC" print "New VPC created: ", aws_vpc_id, "\n" else: # Return 1st object from list aws_vpc_id = str(exist_vpc.pop(0))[4:] print "Identical CIDR already used. Skipped creation." print "Existing VPC ID: {}\n".format(aws_vpc_id) # Use existing VPC new_vpc = vpcc.get_all_vpcs(filters=[("cidrBlock", VPC_CIDR)]) """ # Doesn't work due to VPC dependency print "Requested VPC already exists! Will attempt to delete vpc and recreate" del_status = vpcc.delete_vpc(aws_vpc_id) print "Deletion Completed", del_status
import boto import boto.ec2 from boto.vpc import VPCConnection import time cloud_network = '10.1.0.0/16' blue_subnet = '10.1.0.0/24' green_subnet = '10.1.1.0/24' generic_linux_image = 'ami-a250ecca' nat_linux_image = 'ami-184dc970' connection = VPCConnection() private_cloud = connection.create_vpc(cloud_network) time.sleep(2) #fix race condition later private_cloud.add_tag('Name', 'virtual-private-cloud') print ("created vpc", private_cloud.id) private_subnet = connection.create_subnet(private_cloud.id, green_subnet) time.sleep(2) #fix race condition later private_subnet.add_tag('Name', 'green_subnet') public_subnet = connection.create_subnet(private_cloud.id, blue_subnet) time.sleep(2) public_subnet.add_tag('Name', 'blue_subnet') print "created public and private subnets" igw = connection.create_internet_gateway() connection.attach_internet_gateway(igw.id, private_cloud.id) print "created and attached internet gateway"
def create_vpc(region, vpc_cidr): ec2region = get_region(region) conn = VPCConnection(region=ec2region) return conn.create_vpc(vpc_cidr)
def create_security_group(ec2_conn,vpc_id,security_group_name): groups = ec2_conn.get_all_security_groups() for group in groups : if group.name == security_group_name : return group web = ec2_conn.create_security_group(security_group_name,vpc_id=vpc_id,description='ABC Talks Group') web.authorize('tcp',8080,8080,'0.0.0.0/0') return web def create_network_interface(ec2_conn,domain='vpc',public=False,subnet=None,groups=None,description=None): elastic_network_interface = ec2_conn.create_network_interface(subnet,groups=groups,description=description) if public: elastic_ip_address = ec2_conn.allocate_address(domain=domain) associated_address = ec2_conn.associate_address(allocation_id=elastic_ip_address.allocation_id,network_interface_id=elastic_network_interface.id) return elastic_netowrk_interface vpc_connection = VPCConnection(aws_access_key_id='',aws_secret_access_key='') vpc = vpc_connection.create_vpc('10.0.0.0/16') print vpc.id print vpc.state time.sleep(60) print vpc.state subnet = vpc_connection.create_subnet(vpc.id,'10.0.0.0/25') print subnet.state print subnet.available_ip_address_count ec2_conn = ec2.connect_to_region('us-east-1',aws_access_key_id='',aws_secret_access_key='') web = create_security_group(ec2_conn,vpc.id,'abctalks1') #address = address.Address(connection=ec2_conn,instance_id=ec2_instance.instances[0].id) print address network_interface = create_network_interface(ec2_conn,public=True,subnet=subnet,groups=[web.id],description='Storm Cluster') ec2_instance = create_ec2_instance(ec2_conn,'ami-d05e75b8','abctalks',get_user_data(),web.id,subnet.id,network_interface)
import boto import boto.ec2 from boto.vpc import VPCConnection import time cloud_network = '10.1.0.0/16' blue_subnet = '10.1.0.0/24' green_subnet = '10.1.1.0/24' generic_linux_image = 'ami-a250ecca' nat_linux_image = 'ami-184dc970' connection = VPCConnection() private_cloud = connection.create_vpc(cloud_network) time.sleep(2) #fix race condition later private_cloud.add_tag('Name', 'virtual-private-cloud') print("created vpc", private_cloud.id) private_subnet = connection.create_subnet(private_cloud.id, green_subnet) time.sleep(2) #fix race condition later private_subnet.add_tag('Name', 'green_subnet') public_subnet = connection.create_subnet(private_cloud.id, blue_subnet) time.sleep(2) public_subnet.add_tag('Name', 'blue_subnet') print "created public and private subnets" igw = connection.create_internet_gateway() connection.attach_internet_gateway(igw.id, private_cloud.id) print "created and attached internet gateway"