def update_vpc_tags(module, vpc, vpc_id, tags, name): tags.update({'Name': name}) try: vpc.create_tags(vpc_id, tags) updated_tags = dict( (t.name, t.value) for t in vpc.get_all_tags(filters={'resource-id': vpc_id})) except Exception, e: e_msg = boto_exception(e) module.fail_json(msg=e_msg)
def update_vpc_tags(vpc, module, vpc_obj, tags, name): if tags is None: tags = dict() tags.update({'Name': name}) try: current_tags = dict((t.name, t.value) for t in vpc.get_all_tags(filters={'resource-id': vpc_obj.id})) if cmp(tags, current_tags): vpc.create_tags(vpc_obj.id, tags) return True else: return False except Exception, e: e_msg=boto_exception(e) module.fail_json(msg=e_msg)
def createVPC(region, ig, cidr_vpc, subnet_name, vpcname, ec2, client): #ec2 = boto3.resource('ec2',region_name=region) vpc = ec2.create_vpc(CidrBlock=cidr_vpc, AmazonProvidedIpv6CidrBlock=True) vpc.create_tags(Tags=[ { 'Key': 'Name', 'Value': vpcname }, ]) vpc.wait_until_available() vpc.attach_internet_gateway(InternetGatewayId=ig.id) client.modify_vpc_attribute(VpcId=vpc.id, EnableDnsSupport={'Value': True}) client.modify_vpc_attribute(VpcId=vpc.id, EnableDnsHostnames={'Value': True}) return (vpc)
def update_vpc_tags(vpc, module, vpc_obj, tags, name): if tags is None: tags = dict() tags.update({'Name': name}) try: current_tags = dict((t.name, t.value) for t in vpc.get_all_tags(filters={'resource-id': vpc_obj.id})) if cmp(tags, current_tags): vpc.create_tags(vpc_obj.id, tags) return True else: return False except Exception as e: e_msg=boto_exception(e) module.fail_json(msg=e_msg)
if update_dhcp: dhcp_opts = update_dhcp_opts(module, vpc, vpc_id, dhcp_id) if update_tags: e_tags = update_vpc_tags(module, vpc, vpc_id, tags, name) module.exit_json(changed=changed, name=name, dhcp_options_id=dhcp_opts, tags=e_tags) if not already_exists: try: vpc_id = str( vpc.create_vpc(cidr_block, instance_tenancy=tenancy)).split(':')[1] vpc.create_tags(vpc_id, dict(Name=name)) except Exception, e: e_msg = boto_exception(e) module.fail_json(msg=e_msg) update_dhcp, update_tags = vpc_needs_update( module, vpc, vpc_id, dns_support, dns_hostnames, dhcp_id, tags) if update_dhcp: new_dhcp_opts = update_dhcp_opts(module, vpc, vpc_id, dhcp_id) if update_tags: new_tags = update_vpc_tags(module, vpc, vpc_id, tags, name) module.exit_json(changed=True, name=name, vpc_id=vpc_id, dhcp_options=new_dhcp_opts,