本文整理汇总了Python中boto.vpc.VPCConnection类的典型用法代码示例。如果您正苦于以下问题:Python VPCConnection类的具体用法?Python VPCConnection怎么用?Python VPCConnection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了VPCConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: resolve_security_groups
def resolve_security_groups(self):
filters = {}
self.log.info("Resolving security groups")
# If the server is being spun up in a vpc, search only that vpc
exists = lambda s: s in [group.name for group in
self.ec2.get_all_security_groups()
if self.vpc_id == group.vpc_id]
for index, group in enumerate(self.security_groups):
if not exists(group):
self.log.info('Security Group {group} does not exist'.format(
group=group))
if self.subnet_id is None:
self.ec2.create_security_group(group, group)
else:
vpc_conn = VPCConnection()
vpc_conn.create_security_group(
group, group, vpc_id=self.vpc_id)
self.log.info('Created security group {group}'.format(
group=group))
else:
self.log.info('Security Group {group} already exists'.format(
group=group))
示例2: run
def run(self, terms, variables=None, **kwargs):
ret = []
# Below i will implement
valid_lookups = {
'id' : None
'dhcp_options_id' : None
'state' : None
'cidr_block' : None
'is_default' : None
'instance_tenancy' : None
'classic_link_enabled' : None
}
conn = VPCConnection()
for term in terms:
params = term.split(' ')
key_to_lookup = params[0]
try:
assert( key_to_lookup in valid_lookups )
except (AssertionError) as e:
raise AnsibleError(e)
vpc_filter = {}
for param in params[1:]:
tag, value = param.split('=')
vpc_filter.update({'tag:'+ tag : value})
vpcs = conn.get_all_vpcs(None, vpc_filter)
if len(vpcs) > 1:
ret = [ x.get( key_to_lookup ) for x in vpcs ]
return ret
示例3: get_subnet2
def get_subnet2(name, region):
vpc = VPCConnection(aws_access_key_id=access_key, aws_secret_access_key=secret_key)
subnet = vpc.get_all_subnets(filters={'tag-value': name})
subnetid = str(subnet)
subnetid = (subnetid.split(":"))[1]
subnetid = subnetid.replace("]", '')
return subnetid
示例4: DiscoEIP
class DiscoEIP(object):
"""
A simple class to manage EIP's
"""
def __init__(self):
self.vpc_conn = VPCConnection()
def list(self):
"""Returns all of our currently allocated EIPs"""
return self.vpc_conn.get_all_addresses()
def allocate(self):
"""Allocates a new VPC EIP"""
return self.vpc_conn.allocate_address(domain='vpc')
def release(self, eip_address, force=False):
"""
Releases an EIP.
If it is currently associated with a machine we do not release it unless
the force param is set.
"""
eip = self.vpc_conn.get_all_addresses([eip_address])[0]
if eip.association_id:
if force:
eip.disassociate()
else:
return False
return eip.release()
示例5: delete_peerings
def delete_peerings(vpc_id=None):
"""Delete peerings. If vpc_id is specified, delete all peerings of the VPCs only"""
vpc_conn = VPCConnection()
for peering in DiscoVPC.list_peerings(vpc_id):
try:
logging.info('deleting peering connection %s', peering.id)
vpc_conn.delete_vpc_peering_connection(peering.id)
except EC2ResponseError:
raise RuntimeError('Failed to delete VPC Peering connection {}'.format(peering.id))
示例6: create_nacl
def create_nacl (vpc_id, region, network_aclname):
vpc = VPCConnection(aws_access_key_id=access_key, aws_secret_access_key=secret_key)
network = vpc.create_network_acl(vpc_id)
time.sleep(1)
ec2_conn = boto.ec2.connect_to_region(region,
aws_access_key_id=access_key,
aws_secret_access_key=secret_key)
ec2_conn.create_tags(network.id, {"Name": network_aclname , "Project": 'LiveLiveProd'})
return network.id
示例7: create_subnet
def create_subnet(vpc_id, cidr_block, availability_zone, subnet_name, region):
vpc = VPCConnection(aws_access_key_id=access_key, aws_secret_access_key=secret_key)
datacenters = vpc.create_subnet(vpc_id=vpc_id, cidr_block=cidr_block, availability_zone=availability_zone)
ec2_conn = boto.ec2.connect_to_region(region,
aws_access_key_id=access_key,
aws_secret_access_key=secret_key)
time.sleep(1)
ec2_conn.create_tags(datacenters.id, {"Name": subnet_name, "Project": 'LiveLiveProd'})
return datacenters.id
示例8: find_vpc_id_by_name
def find_vpc_id_by_name(vpc_name):
"""Find VPC by name"""
vpc_conn = VPCConnection()
vpc_ids = vpc_conn.get_all_vpcs(filters={'tag:Name': vpc_name})
if len(vpc_ids) == 1:
return vpc_ids[0].id
elif len(vpc_ids) == 0:
raise VPCNameNotFound("No VPC is named as {}".format(vpc_name))
else:
raise MultipleVPCsForVPCNameError("More than 1 VPC is named as {}".format(vpc_name))
示例9: TestVPCConnection
class TestVPCConnection(unittest.TestCase):
"""
Test class for `boto.vpc.VPCConnection`
"""
def setUp(self):
"""
Setup method to initialize vpc_connection objectq
"""
super(TestVPCConnection, self).setUp()
self.vpc_connection = VPCConnection(
aws_access_key_id="aws_access_key_id", aws_secret_access_key="aws_secret_access_key"
)
def test_detach_internet_gateway(self):
"""
Tests detach_internet_gateway with all valid parameters
"""
internet_gateway_id = "mock_gateway_id"
vpc_id = "mock_vpc_id"
def get_status(status, params):
if (
status == "DetachInternetGateway"
and params["InternetGatewayId"] == internet_gateway_id
and params["VpcId"] == vpc_id
):
return True
else:
return False
self.vpc_connection.get_status = get_status
status = self.vpc_connection.detach_internet_gateway(internet_gateway_id, vpc_id)
self.assertEquals(True, status)
def test_replace_route_table_association(self):
"""
Tests replace_route_table_assocation with all valid parameters
"""
association_id = "mock_association_id"
route_table_id = "mock_route_table_id"
def get_status(status, params):
if (
status == "ReplaceRouteTableAssociation"
and params["AssociationId"] == association_id
and params["RouteTableId"] == route_table_id
):
return True
else:
return False
self.vpc_connection.get_status = get_status
status = self.vpc_connection.replace_route_table_assocation(association_id, route_table_id)
self.assertEquals(True, status)
示例10: get_subnet_vpc_id
def get_subnet_vpc_id(self, subnet_id):
vpc_conn = VPCConnection()
subnets = vpc_conn.get_all_subnets(
filters={'subnet_id': subnet_id})
if len(subnets) == 1:
vpc_id = subnets[0].vpc_id
return vpc_id
elif len(subnets) == 0:
raise NoSubnetReturned("No subnets returned")
else:
raise Exception("More than 1 subnet returned")
示例11: DiscoEIP
class DiscoEIP(object):
"""
A simple class to manage EIP's
"""
def __init__(self):
self.vpc_conn = VPCConnection()
self.ec2_conn = boto3.client('ec2')
def list(self):
"""Returns all of our currently allocated EIPs"""
return self.vpc_conn.get_all_addresses()
def allocate(self):
"""Allocates a new VPC EIP"""
return self.vpc_conn.allocate_address(domain='vpc')
def tag_dynamic(self, eip_allocation_id):
"""
Tag an EIP as dynamic
"Tags": [
{
"Key": "dynamic",
"Value": "true"
}
]
"""
return throttled_call(self.ec2_conn.create_tags, Resources=[eip_allocation_id],
Tags=[{'Key': 'dynamic', 'Value': 'true'}])
def release(self, eip_address, force=False):
"""
Releases an EIP.
If it is currently associated with a machine we do not release it unless
the force param is set.
"""
eip = self.vpc_conn.get_all_addresses([eip_address])[0]
if eip.association_id:
if force:
eip.disassociate()
else:
return False
return eip.release()
def find_eip_address(self, eip):
""" Finds the EIP Address for the public eip specified. """
address_filter = {'public-ip': eip}
try:
return self.vpc_conn.get_all_addresses(filters=address_filter)[0]
except IndexError:
return None
示例12: TestVPCConnection
class TestVPCConnection(unittest.TestCase):
"""
Test class for `boto.vpc.VPCConnection`
"""
def setUp(self):
"""
Setup method to initialize vpc_connection objectq
"""
super(TestVPCConnection, self).setUp()
self.vpc_connection = VPCConnection(
aws_access_key_id='aws_access_key_id',
aws_secret_access_key='aws_secret_access_key')
def test_detach_internet_gateway(self):
"""
Tests detach_internet_gateway with all valid parameters
"""
internet_gateway_id = 'mock_gateway_id'
vpc_id = 'mock_vpc_id'
def get_status(status, params):
if status == "DetachInternetGateway" and \
params["InternetGatewayId"] == internet_gateway_id and \
params["VpcId"] == vpc_id:
return True
else:
return False
self.vpc_connection.get_status = get_status
status = self.vpc_connection.detach_internet_gateway(
internet_gateway_id, vpc_id)
self.assertEquals(True, status)
示例13: get_subnet_availability_zone
def get_subnet_availability_zone(self, subnet_id):
self.log.info(
"getting zone for subnet {subnet_id}".format(subnet_id=subnet_id))
vpc_conn = VPCConnection()
filters = {'subnet-id': subnet_id}
subnets = vpc_conn.get_all_subnets(filters=filters)
if len(subnets) == 1:
availability_zone = subnets[0].availability_zone
log_message = 'Subnet {subnet_id} is in ' \
'availability zone {availability_zone}'
self.log.info(log_message.format(
subnet_id=subnet_id,
availability_zone=availability_zone))
return availability_zone
示例14: TestRoute53PrivateZone
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()
示例15: discover_vpc
def discover_vpc(self,vpc_id):
try:
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY=self.find_aws_creds()
except:
logger.info("Error: {0}: Cannot retrieve boto credentials".format(__name__))
try:
conn_vpc = VPCConnection(aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
a = conn_vpc.get_all_vpcs()
for b in a:
if b.tags['Name'].lower() == vpc_id:
vpc_id=b.id
else:
vpc_id=''
except:
logger.info("Error: {0}: conn_vpc cannot VPCConnection()".format(__name__))
return vpc_id