def test_rrset(): conn = boto.connect_route53('the_key', 'the_secret') conn.get_all_rrsets.when.called_with("abcd", type="A").\ should.throw(boto.route53.exception.DNSServerError, "404 Not Found") zone = conn.create_hosted_zone("testdns.aws.com") zoneid = zone["CreateHostedZoneResponse"]["HostedZone"]["Id"] changes = ResourceRecordSets(conn, zoneid) change = changes.add_change("CREATE", "foo.bar.testdns.aws.com", "A") change.add_value("1.2.3.4") changes.commit() rrsets = conn.get_all_rrsets(zoneid, type="A") rrsets.should.have.length_of(1) rrsets[0].resource_records[0].should.equal('1.2.3.4') rrsets = conn.get_all_rrsets(zoneid, type="CNAME") rrsets.should.have.length_of(0) changes = ResourceRecordSets(conn, zoneid) changes.add_change("DELETE", "foo.bar.testdns.aws.com", "A") changes.commit() rrsets = conn.get_all_rrsets(zoneid) rrsets.should.have.length_of(0)
def test_route53_ec2_instance_with_public_ip(): route53_conn = boto.connect_route53() ec2_conn = boto.ec2.connect_to_region("us-west-1") template_json = json.dumps(route53_ec2_instance_with_public_ip.template) conn = boto.cloudformation.connect_to_region("us-west-1") conn.create_stack( "test_stack", template_body=template_json, ) instance_id = ec2_conn.get_all_reservations()[0].instances[0].id zones = route53_conn.get_all_hosted_zones()['ListHostedZonesResponse']['HostedZones'] list(zones).should.have.length_of(1) zone_id = zones[0]['Id'] rrsets = route53_conn.get_all_rrsets(zone_id) rrsets.should.have.length_of(1) record_set1 = rrsets[0] record_set1.name.should.equal('{0}.us-west-1.my_zone.'.format(instance_id)) record_set1.identifier.should.equal(None) record_set1.type.should.equal('A') record_set1.ttl.should.equal('900') record_set1.weight.should.equal(None) record_set1.resource_records[0].should.equal("10.0.0.25")
def test_create_health_check(): conn = boto.connect_route53('the_key', 'the_secret') check = HealthCheck( ip_addr="10.0.0.25", port=80, hc_type="HTTP", resource_path="/", fqdn="example.com", string_match="a good response", request_interval=10, failure_threshold=2, ) conn.create_health_check(check) checks = conn.get_list_health_checks()['ListHealthChecksResponse'][ 'HealthChecks'] list(checks).should.have.length_of(1) check = checks[0] config = check['HealthCheckConfig'] config['IPAddress'].should.equal("10.0.0.25") config['Port'].should.equal("80") config['Type'].should.equal("HTTP") config['ResourcePath'].should.equal("/") config['FullyQualifiedDomainName'].should.equal("example.com") config['SearchString'].should.equal("a good response") config['RequestInterval'].should.equal("10") config['FailureThreshold'].should.equal("2")
def test_deleting_latency_route(): conn = boto.connect_route53() conn.create_hosted_zone("testdns.aws.com.") zone = conn.get_zone("testdns.aws.com.") zone.add_cname("cname.testdns.aws.com", "example.com", identifier=('success-test-foo', 'us-west-2')) zone.add_cname("cname.testdns.aws.com", "example.com", identifier=('success-test-bar', 'us-west-1')) cnames = zone.get_cname('cname.testdns.aws.com.', all=True) cnames.should.have.length_of(2) foo_cname = [ cname for cname in cnames if cname.identifier == 'success-test-foo' ][0] foo_cname.region.should.equal('us-west-2') zone.delete_record(foo_cname) cname = zone.get_cname('cname.testdns.aws.com.', all=True) # When get_cname only had one result, it returns just that result instead # of a list. cname.identifier.should.equal('success-test-bar') cname.region.should.equal('us-west-1')
def test_route53_associate_health_check(): route53_conn = boto.connect_route53() template_json = json.dumps(route53_health_check.template) conn = boto.cloudformation.connect_to_region("us-west-1") conn.create_stack( "test_stack", template_body=template_json, ) checks = route53_conn.get_list_health_checks()['ListHealthChecksResponse']['HealthChecks'] list(checks).should.have.length_of(1) check = checks[0] health_check_id = check['Id'] config = check['HealthCheckConfig'] config["FailureThreshold"].should.equal("3") config["IPAddress"].should.equal("10.0.0.4") config["Port"].should.equal("80") config["RequestInterval"].should.equal("10") config["ResourcePath"].should.equal("/") config["Type"].should.equal("HTTP") zones = route53_conn.get_all_hosted_zones()['ListHostedZonesResponse']['HostedZones'] list(zones).should.have.length_of(1) zone_id = zones[0]['Id'] rrsets = route53_conn.get_all_rrsets(zone_id) rrsets.should.have.length_of(1) record_set = rrsets[0] record_set.health_check.should.equal(health_check_id)
def create(cls, name, conn=None): """ Create new hosted zone.""" conn = conn or connect_route53() name = name if name.endswith('.') else name+'.' zone = conn.create_hosted_zone(name) zone = zone['CreateHostedZoneResponse']['HostedZone'] return cls(zone, conn=conn)
def set_public_record(): parser = argparse.ArgumentParser(description="Updates DNS records") parser.add_argument( "--address", default=urllib2.urlopen( "http://169.254.169.254/latest/meta-data/public-ipv4", timeout=120 ).read() ) parser.add_argument( "hostname", help="The hostname to establish the DNS record for" ) args = parser.parse_args() if not args.hostname.endswith("."): parser.error("Expected record to end with '.'") zone_name = ".".join(list(filter(bool, args.hostname.split(".")))[-2:]) route53 = boto.connect_route53() zone = route53.get_zone(zone_name) record = zone.get_a(args.hostname) if record is None: logger.info("Creating A %s %s", args.hostname, args.address) zone.add_a(args.hostname, args.address, ttl=60) else: logger.info("Updating A %s %s", args.hostname, args.address) zone.update_record(record, args.address, new_ttl=60)
def addUpdateDNS(domain, host, ip): dns = boto.connect_route53(AWS_CONFIG['AWS_KEY'], AWS_CONFIG['AWS_SECRET']) if not dns: print "Failed to connect to Route53" return False zone = dns.get_hosted_zone_by_name(domain) if not zone: print "Domain %s is not known to Route53 service" % domain return False records = dns.get_all_rrsets(zone['GetHostedZoneResponse']['HostedZone']['Id'].replace('/hostedzone/','')) # now check if we already have this host in the DNS # route53 cannot 'update', we need make delete / create # route53 also returns everything with dot at the end, which we generally don't hostname = '%s.%s.' % (host,domain) hostRecords = filter(lambda h : h.name == hostname, records) if len(hostRecords) > 1: print "Received multiple records for host %s, check DNS in web console" % hostname return False elif len(hostRecords) == 1: # double'check that we're not deleting something not of DNS 'A' type if hostRecords[0].type != 'A': print "Host %s has record of type %s, aborting" % (hostname, hostRecords[0].type) return False records.add_change('DELETE', hostRecords[0].name, 'A').resource_records = hostRecords[0].resource_records # normally add 'A' record records.add_change('CREATE', hostname, 'A').resource_records = [ip] records.commit() return True
def _get_route53_conn(env): ir_params = env['ir.config_parameter'] aws_access_key_id = ir_params.sudo().get_param('saas_route53.saas_route53_aws_accessid') aws_secret_access_key = ir_params.sudo().get_param('saas_route53.saas_route53_aws_accesskey') if not aws_access_key_id or not aws_secret_access_key: raise Warning('Please specify both your AWS Access key and ID') return boto.connect_route53(aws_access_key_id, aws_secret_access_key)
def create_dns_record(hostname, hostip): from boto.route53.record import ResourceRecordSets conn = boto.connect_route53() edits = ResourceRecordSets(conn, ZONE_ID) edit = edits.add_change("CREATE", hostname.strip() + HOSTNAME_POSTFIX, "CNAME") edit.add_value(hostip) status = edits.commit()
def commit(self): """Commit this change""" if not self.connection: import boto self.connection = boto.connect_route53() return self.connection.change_rrsets(self.hosted_zone_id, self.to_xml())
def test_use_health_check_in_resource_record_set(): conn = boto.connect_route53('the_key', 'the_secret') check = HealthCheck( ip_addr="10.0.0.25", port=80, hc_type="HTTP", resource_path="/", ) check = conn.create_health_check( check)['CreateHealthCheckResponse']['HealthCheck'] check_id = check['Id'] zone = conn.create_hosted_zone("testdns.aws.com") zone_id = zone["CreateHostedZoneResponse"][ "HostedZone"]["Id"].split("/")[-1] changes = ResourceRecordSets(conn, zone_id) change = changes.add_change( "CREATE", "foo.bar.testdns.aws.com", "A", health_check=check_id) change.add_value("1.2.3.4") changes.commit() record_sets = conn.get_all_rrsets(zone_id) record_sets[0].health_check.should.equal(check_id)
def test_create_health_check(): conn = boto.connect_route53("the_key", "the_secret") check = HealthCheck( ip_addr="10.0.0.25", port=80, hc_type="HTTP", resource_path="/", fqdn="example.com", string_match="a good response", request_interval=10, failure_threshold=2, ) conn.create_health_check(check) checks = conn.get_list_health_checks( )["ListHealthChecksResponse"]["HealthChecks"] list(checks).should.have.length_of(1) check = checks[0] config = check["HealthCheckConfig"] config["IPAddress"].should.equal("10.0.0.25") config["Port"].should.equal("80") config["Type"].should.equal("HTTP") config["ResourcePath"].should.equal("/") config["FullyQualifiedDomainName"].should.equal("example.com") config["SearchString"].should.equal("a good response") config["RequestInterval"].should.equal("10") config["FailureThreshold"].should.equal("2")
def test_use_health_check_in_resource_record_set(): conn = boto.connect_route53("the_key", "the_secret") check = HealthCheck(ip_addr="10.0.0.25", port=80, hc_type="HTTP", resource_path="/") check = conn.create_health_check( check)["CreateHealthCheckResponse"]["HealthCheck"] check_id = check["Id"] zone = conn.create_hosted_zone("testdns.aws.com") zone_id = zone["CreateHostedZoneResponse"]["HostedZone"]["Id"].split( "/")[-1] changes = ResourceRecordSets(conn, zone_id) change = changes.add_change("CREATE", "foo.bar.testdns.aws.com", "A", health_check=check_id) change.add_value("1.2.3.4") changes.commit() record_sets = conn.get_all_rrsets(zone_id) record_sets[0].health_check.should.equal(check_id)
def remove_domain(): conn = boto.connect_route53() changes = ResourceRecordSets(conn, env.hosted_zone_id) for url in env.urls: change = changes.add_change("DELETE", url, "CNAME") change.add_value(env.public_host) changes.commit()
def test_alias_rrset(): conn = boto.connect_route53("the_key", "the_secret") zone = conn.create_hosted_zone("testdns.aws.com") zoneid = zone["CreateHostedZoneResponse"]["HostedZone"]["Id"].split( "/")[-1] changes = ResourceRecordSets(conn, zoneid) changes.add_change( "CREATE", "foo.alias.testdns.aws.com", "A", alias_hosted_zone_id="Z3DG6IL3SJCGPX", alias_dns_name="foo.testdns.aws.com", ) changes.add_change( "CREATE", "bar.alias.testdns.aws.com", "CNAME", alias_hosted_zone_id="Z3DG6IL3SJCGPX", alias_dns_name="bar.testdns.aws.com", ) changes.commit() rrsets = conn.get_all_rrsets(zoneid, type="A") alias_targets = [rr_set.alias_dns_name for rr_set in rrsets] alias_targets.should.have.length_of(2) alias_targets.should.contain("foo.testdns.aws.com") alias_targets.should.contain("bar.testdns.aws.com") rrsets[0].alias_dns_name.should.equal("foo.testdns.aws.com") rrsets[0].resource_records.should.have.length_of(0) rrsets = conn.get_all_rrsets(zoneid, type="CNAME") rrsets.should.have.length_of(1) rrsets[0].alias_dns_name.should.equal("bar.testdns.aws.com") rrsets[0].resource_records.should.have.length_of(0)
def test_create_health_check(): conn = boto.connect_route53('the_key', 'the_secret') check = HealthCheck( ip_addr="10.0.0.25", port=80, hc_type="HTTP", resource_path="/", fqdn="example.com", string_match="a good response", request_interval=10, failure_threshold=2, ) conn.create_health_check(check) checks = conn.get_list_health_checks()['ListHealthChecksResponse']['HealthChecks'] list(checks).should.have.length_of(1) check = checks[0] config = check['HealthCheckConfig'] config['IPAddress'].should.equal("10.0.0.25") config['Port'].should.equal("80") config['Type'].should.equal("HTTP") config['ResourcePath'].should.equal("/") config['FullyQualifiedDomainName'].should.equal("example.com") config['SearchString'].should.equal("a good response") config['RequestInterval'].should.equal("10") config['FailureThreshold'].should.equal("2")
def update_elb_dns(stack, elb_attr, hosted_zone_id, dns_name): ttl = 60 elb_dns = cf.get_stack_output_value(stack, elb_attr) dns_record_name = dns_name + "." conn = boto.connect_route53() existing_entries = conn.get_all_rrsets(hosted_zone_id, "CNAME", dns_name) changes = ResourceRecordSets(conn, hosted_zone_id) for item in existing_entries: if dns_record_name != item.name: logging.info('Nothing to change for %s', dns_name) continue for record in item.resource_records: logging.warning('Deleting CNAME entry {0} -> {1}'.format(dns_name, record)) change = changes.add_change("DELETE", dns_name, "CNAME", ttl=ttl) change.add_value(record) logging.warning("Adding CNAME entry %s -> %s", dns_name, elb_dns) change = changes.add_change("CREATE", dns_name, "CNAME", ttl=ttl) change.add_value(elb_dns) try: changes.commit() print '*** DNS record updated' print '{0} IN CNAME {1}'.format(dns_name, elb_dns) except Exception, e: logging.error(e)
def Route53(zone_id=None): con = boto.connect_route53() if zone_id != None: ''' http://localhost/r53/<zone_id> get all rrset specified zone id ''' rl = con.get_all_rrsets(zone_id) rr = [{ 'type': r.type, 'name': r.name, 'ttl': r.ttl, 'content': r.to_print() } for r in rl] return render_template('r53/ZoneView.html', zone_id=zone_id, rr=rr) else: ''' http://localhost/r53/ get all hosted zone ''' hl = con.get_all_hosted_zones() hzl = [{ 'id': hz['Id'].split("/")[-1], 'name': hz['Name'], 'count': hz['ResourceRecordSetCount'] } for hz in hl['ListHostedZonesResponse']['HostedZones']] return render_template('r53/ZoneIndex.html', hzl=hzl)
def remove_domain(): conn = boto.connect_route53() changes = ResourceRecordSets(conn, env.hosted_zone_id) for url in env.urls: change = changes.add_change("DELETE", url,"CNAME") change.add_value(env.public_host) changes.commit()
def test_get_zone_id(): conn = boto.connect_route53() conn.create_hosted_zone('devel.huit.harvard.edu.') r53 = core.Route53() zone_id = r53.get_zone_id('devel.huit.harvard.edu') tools.assert_true(zone_id)
def get_route53_records(): """ Get and store the map of resource records to domain names that point to them. """ r53 = boto.connect_route53() route53_zones = [zone for zone in r53.get_zones()] route53_records = {} for zone in route53_zones: rrsets = r53_conn.get_all_rrsets(zone.id) for record_set in rrsets: record_name = record_set.name if record_name.endswith('.'): record_name = record_name[:-1] for resource in record_set.resource_records: route53_records.setdefault(resource, set()) route53_records[resource].add(record_name) return route53_records
def delete_route53_record(name): ''' Deletes a record! ''' # NOTICE: Our registered DNS aliases have a "pub-" prefix. # Our instance names do not. route53_ip = get_route53_ip(name) logging.info("%s, IP=%s" % (name, route53_ip)) conn = connect_route53() changes = ResourceRecordSets(conn, zone_id) logging.info("DELETE %s, IP=%s, TTL=%s" % (name, route53_ip, ttl)) if options.dryrun is True: return # Delete old record if it exists if route53_ip is not None: # NOTE: TTL is 300. But it must match in the record or this fails. change1 = changes.add_change("DELETE", name, "A", ttl=ttl) change1.add_value(route53_ip) result = changes.commit() # Note: If delete&create this commit does both in one transaction. print "Delete %s, IP=%s" % (name, route53_ip) else: print "No record match found." return
def main(): argument_spec = ec2_argument_spec() argument_spec.update(dict(zone=dict(required=True))) module = AnsibleModule(argument_spec=argument_spec) zone = module.params["zone"] region = module.params["region"] conn = boto.connect_route53() records = [r for r in conn.get_all_rrsets(zone) if r.type == "CNAME"] hosts = {"workers": []} changed = False for record in records: # Strip off trailing '.' in the DNS record name = re.sub("\.$", "", record.name) if name.startswith("manager"): hosts["manager"] = name elif name.startswith("master"): hosts["master"] = name elif name.startswith("primary"): hosts["primary"] = name elif name.startswith("secondary"): hosts["secondary"] = name elif name.startswith("worker"): hosts["workers"].append(name) module.exit_json(changed=changed, hosts=hosts)
def test_get_zone(): conn = boto.connect_route53() conn.create_hosted_zone('devel.huit.harvard.edu.') r53 = core.Route53() zone = r53.get_zone('devel.huit.harvard.edu') assert zone.name == 'devel.huit.harvard.edu.'
def update_dns(self, host_name, ip_address): ttl = 10 host_name = ".".join([host_name, self.domain]) conn = boto.connect_route53() response = conn.get_all_rrsets(self.hosted_zone_id, 'A', host_name, maxitems=1) if len(response): response = response[0] comment = "Starcluster route53 plugin deleted record for %s"%(host_name) changes = ResourceRecordSets(conn, self.hosted_zone_id, comment) change1 = changes.add_change("DELETE", host_name, 'A', response.ttl) for old_value in response.resource_records: change1.add_value(old_value) try: changes.commit() log.info(comment) except Exception as e: log.warning(e) comment = "Starcluster route53 plugin updated record for %s to %s"%(host_name, ip_address) changes = ResourceRecordSets(conn, self.hosted_zone_id, comment) change2 = changes.add_change("CREATE", host_name, 'A', ttl) change2.add_value(ip_address) try: changes.commit() log.info(comment) except Exception as e: log.warning(e)
def test_does_zone_exist(): conn = boto.connect_route53() conn.create_hosted_zone('devel.huit.harvard.edu.') r53 = core.Route53() result = r53.does_zone_exist('devel.huit.harvard.edu') print result tools.assert_true(result)
def test_list_zone(): conn = boto.connect_route53() conn.create_hosted_zone('devel1.huit.harvard.edu.') conn.create_hosted_zone('devel2.huit.harvard.edu.') r53 = core.Route53() zones = r53.list_zones() len(zones).should.equal(2)
def __init__(self, **kwargs): super(Route53Service, self).__init__(**kwargs) self.conn = boto.connect_route53() try: self.hostname = os.environ['EC2_PUBLIC_HOSTNAME'] except KeyError: app.logger.warn("We cannot register a domain on non ec2 instances")
def run(self, *args): args = self.arg_parser.parse_args(args) if not args.tenant or not args.ip: self.arg_parser.print_help() return False if args.record_type: # verify if a valid record type was entered if not args.record_type in boto.route53.record.RECORD_TYPES: print "Record type entered is incorrect. Must be one of %s" % RECORD_TYPES return False creds = self.read_credentials(args.credentials) try: if not creds or not creds.aws_access_key_id or not creds.aws_secret_access_key: print "AWS security credentials currently unavailable...terminating process." return False else: print "Extracted AWS credentials..." # use boto to make request to alter Route 53 entry. self.conn = boto.connect_route53(aws_access_key_id=creds.aws_access_key_id, aws_secret_access_key=creds.aws_secret_access_key) if not self.conn: print "Connection cannot be established to AWS with the provided credentials" else: print "Connection established with AWS..." # get hosted zone id for the cluster provided hosted_zones = self.conn.get_all_hosted_zones() if hosted_zones: print "Recieved Hosted Zone information..." else: print "No hosted zones available" zone_id = self.find_host_zone_id(zones=hosted_zones, cluster="%s." % args.cluster) if zone_id: print "Found host zone id %s for cluster %s" % (zone_id, args.cluster) dns_entry = "%s.%s." % (args.tenant, args.cluster) # check if there is a record already available record_set = self.get_resource_record_set(host_zone_id=zone_id) record = self.get_resource_record(rr_set=record_set, dns_entry=dns_entry) values = [args.ip] if not record: self.add_record(rr_set=record_set, dns_entry=dns_entry[:-1], record_type=args.record_type, values=values) print "No existing record set found. Added record set ..." else: self.modify_record(rr_set=record_set, record=record, new_record_type=args.record_type, new_values=values) print "Modified existing DNS entry for %s" % dns_entry return True else: print "Host zone with name %s not found..." % args.cluster return False except Exception as ex: print "Error occurred in setting DNS entry" print "" print "Exception: %s" % ex print "" return False
def test_rrset(): conn = boto.connect_route53('the_key', 'the_secret') conn.get_all_rrsets.when.called_with("abcd", type="A").should.throw( boto.route53.exception.DNSServerError, "404 Not Found") zone = conn.create_hosted_zone("testdns.aws.com") zoneid = zone["CreateHostedZoneResponse"]["HostedZone"]["Id"].split("/")[-1] changes = ResourceRecordSets(conn, zoneid) change = changes.add_change("CREATE", "foo.bar.testdns.aws.com", "A") change.add_value("1.2.3.4") changes.commit() rrsets = conn.get_all_rrsets(zoneid, type="A") rrsets.should.have.length_of(1) rrsets[0].resource_records[0].should.equal('1.2.3.4') rrsets = conn.get_all_rrsets(zoneid, type="CNAME") rrsets.should.have.length_of(0) changes = ResourceRecordSets(conn, zoneid) changes.add_change("DELETE", "foo.bar.testdns.aws.com", "A") change = changes.add_change("CREATE", "foo.bar.testdns.aws.com", "A") change.add_value("5.6.7.8") changes.commit() rrsets = conn.get_all_rrsets(zoneid, type="A") rrsets.should.have.length_of(1) rrsets[0].resource_records[0].should.equal('5.6.7.8') changes = ResourceRecordSets(conn, zoneid) changes.add_change("DELETE", "foo.bar.testdns.aws.com", "A") changes.commit() rrsets = conn.get_all_rrsets(zoneid) rrsets.should.have.length_of(0) changes = ResourceRecordSets(conn, zoneid) change = changes.add_change("CREATE", "foo.bar.testdns.aws.com", "A") change.add_value("1.2.3.4") change = changes.add_change("CREATE", "bar.foo.testdns.aws.com", "A") change.add_value("5.6.7.8") changes.commit() rrsets = conn.get_all_rrsets(zoneid, type="A") rrsets.should.have.length_of(2) rrsets = conn.get_all_rrsets(zoneid, name="foo.bar.testdns.aws.com", type="A") rrsets.should.have.length_of(1) rrsets[0].resource_records[0].should.equal('1.2.3.4') rrsets = conn.get_all_rrsets(zoneid, name="bar.foo.testdns.aws.com", type="A") rrsets.should.have.length_of(1) rrsets[0].resource_records[0].should.equal('5.6.7.8') rrsets = conn.get_all_rrsets(zoneid, name="foo.foo.testdns.aws.com", type="A") rrsets.should.have.length_of(0)
def delete_cname(zone_id, src, dest): conn = boto.connect_route53(app.config['aws']['key'], app.config['aws']['secret']) changes = ResourceRecordSets(conn, zone_id, '') change = changes.add_change('DELETE', dest, 'CNAME', 300) change.set_alias(None, None) change.add_value(src) changes.commit()
def add_cname(zone_id, src, dest, _type='CNAME'): app.logger.info("Add cname: {}, {}, {}, {}".format(zone_id, src, dest, _type)) conn = boto.connect_route53(app.config['aws']['key'], app.config['aws']['secret']) changes = ResourceRecordSets(conn, zone_id, '') change = changes.add_change("CREATE", dest, _type, 300) change.add_value(src) changes.commit()
def connRoute53(): my_logger.info("In ttAWS.connBotoRoute53....") try: connR53=boto.connect_route53() my_logger.info("Boto Route53 connection success!") except: my_logger.error("Error Encountered. Boto Route53 connection failed! Exiting....") sys.exit(-1) return connR53
def _get_route53_conn(env): ir_params = env['ir.config_parameter'] aws_access_key_id = ir_params.get_param( 'saas_route53.saas_route53_aws_accessid') aws_secret_access_key = ir_params.get_param( 'saas_route53.saas_route53_aws_accesskey') if not aws_access_key_id or not aws_secret_access_key: raise Warning('Please specify both your AWS Access key and ID') return boto.connect_route53(aws_access_key_id, aws_secret_access_key)
def __init__(self, change_resp, conn=None, key='ChangeResourceRecordSetsResponse'): self.conn = conn or connect_route53() _dict = change_resp[key]['ChangeInfo'] for key, value in _dict.items(): setattr(self, key.lower(), value) self.id = (self.id or '').replace('/change/','') self.status = (self.status or '').lower()
def route53_connection(self): """ :return: boto route53 connection object based on __init__ credentials """ if self._route53_connection is None: self._route53_connection = boto.connect_route53( aws_access_key_id=self.acl['aws_access_key_id'], aws_secret_access_key=self.acl['aws_secret_access_key']) return self._route53_connection
def __init__(self, zone_dict, conn=None): self.conn = conn or connect_route53() self.id = zone_dict.pop('Id','').replace('/hostedzone/','') for key, value in zone_dict.items(): setattr(self, key.lower(), value) if not self.name.endswith('.'): self.name += '.' # make sure the name is fully qualified
def delete_record(name, type, zone): """Delete DNS entries in route53""" conn = boto.connect_route53() zones = {z.id: z for z in conn.get_zones()} if type == "CNAME": status = zones[zone].delete_cname(name) elif type == "A": status = zones[zone].delete_a(name) else: exit("Unsupported record type")
def _r53_connection(): """returns a *boto2* route53 connection. route53 for boto3 is *very* poor and much too low-level with no 'resource' construct (yet?). It should be avoided. http://boto.cloudhackers.com/en/latest/ref/route53.html lsh@2021-08: boto3 still hasn't got it's higher level 'resource' interface yet, but it's 'client' interface looks more fleshed out now than it did when boto3 was first introduced. Consider upgrading.""" return boto.connect_route53() # no region necessary
def create_route53(args,instance,zonename): fqdn = args.name + "." + zonename zone_dict = {} conn = boto.connect_route53() zone_id = conn.get_zone(zonename) zone_dict['id'] = zone_id.id zone = boto.route53.zone.Zone(conn, zone_dict) result = zone.add_a(fqdn,instance.private_ip_address) print "Adding %s to Route53: %s" % (fqdn,result)
def __init__(self, heet, name, value, **kwargs): self.heet = heet self.name = name self.value = value self.zone_id = self.heet.get_value('zone_id', required=True) self.domain = self.heet.get_value('domain', required=True) self.ttl = self.heet.get_value('ttl', kwargs, default=300) self.conn = boto.connect_route53( aws_access_key_id=heet.access_key_id, aws_secret_access_key=heet.secret_access_key) # get_zone does not like leading periods self.zone = self.conn.get_zone(self.domain.lstrip('.')) heet.add_resource(self)
def test_delete_health_check(): conn = boto.connect_route53("the_key", "the_secret") check = HealthCheck(ip_addr="10.0.0.25", port=80, hc_type="HTTP", resource_path="/") conn.create_health_check(check) checks = conn.get_list_health_checks()["ListHealthChecksResponse"]["HealthChecks"] list(checks).should.have.length_of(1) health_check_id = checks[0]["Id"] conn.delete_health_check(health_check_id) checks = conn.get_list_health_checks()["ListHealthChecksResponse"]["HealthChecks"] list(checks).should.have.length_of(0)
def r53_change_record(name, values, aws_access_key_id, aws_secret_access_key, proxy=None, proxy_port=None, type="A", ttl="600", comment=""): # note: if the network is unreachable this function will retry, # blocking up to one minute before the last retry (not configurable?) conn = boto.connect_route53(aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, proxy=proxy, proxy_port=proxy_port) res = conn.get_all_hosted_zones() domain_name = re.sub('^[^\.]*\.', '', name) if name[0] == '.': name = name[1:] hosted_zone_id = None for zoneinfo in res['ListHostedZonesResponse']['HostedZones']: zonename = zoneinfo['Name'] _zone_id = zoneinfo['Id'] _zone_id = re.sub('/hostedzone/', '', _zone_id) if zonename[-1] == '.': zonename = zonename[:-1] #print domain_name, zonename if domain_name == zonename: hosted_zone_id = _zone_id break if not hosted_zone_id: raise RuntimeError, 'domain_name ' + repr( domain_name) + ' not found in hosted zones' changes = ResourceRecordSets(conn, hosted_zone_id, comment) response = conn.get_all_rrsets(hosted_zone_id, type, name, maxitems=1) if response: rrset = response[0] change1 = changes.add_change("DELETE", name, type, rrset.ttl) for old_value in rrset.resource_records: change1.add_value(old_value) change2 = changes.add_change("CREATE", name, type, ttl) for new_value in values.split(','): change2.add_value(new_value) return changes.commit()
def remove_route53(args, zonename): fqdn = args.name + "." + zonename zone_dict = {} conn = boto.connect_route53() zone_id = conn.get_zone(zonename) zone_dict['id'] = zone_id.id zone = boto.route53.zone.Zone(conn, zone_dict) try: result = zone.delete_a(fqdn) print "Removed %s from Route53: %s" % (fqdn, result) except AttributeError: print "Route53: Hostname %s not found." % fqdn
def test_rrset_with_multiple_values(): conn = boto.connect_route53("the_key", "the_secret") zone = conn.create_hosted_zone("testdns.aws.com") zoneid = zone["CreateHostedZoneResponse"]["HostedZone"]["Id"].split("/")[-1] changes = ResourceRecordSets(conn, zoneid) change = changes.add_change("CREATE", "foo.bar.testdns.aws.com", "A") change.add_value("1.2.3.4") change.add_value("5.6.7.8") changes.commit() rrsets = conn.get_all_rrsets(zoneid) rrsets.should.have.length_of(1) set(rrsets[0].resource_records).should.equal(set(["1.2.3.4", "5.6.7.8"]))
def test_hosted_zone_comment_preserved(): conn = boto.connect_route53('the_key', 'the_secret') firstzone = conn.create_hosted_zone("testdns.aws.com.", comment="test comment") zone_id = firstzone["CreateHostedZoneResponse"]["HostedZone"]["Id"].split("/")[-1] hosted_zone = conn.get_hosted_zone(zone_id) hosted_zone["GetHostedZoneResponse"]["HostedZone"]["Config"]["Comment"].should.equal("test comment") hosted_zones = conn.get_all_hosted_zones() hosted_zones["ListHostedZonesResponse"]["HostedZones"][0]["Config"]["Comment"].should.equal("test comment") zone = conn.get_zone("testdns.aws.com.") zone.config["Comment"].should.equal("test comment")
def test_hosted_zone_private_zone_preserved(): conn = boto.connect_route53('the_key', 'the_secret') firstzone = conn.create_hosted_zone("testdns.aws.com.", private_zone=True, vpc_id='vpc-fake', vpc_region='us-east-1') zone_id = firstzone["CreateHostedZoneResponse"]["HostedZone"]["Id"].split("/")[-1] hosted_zone = conn.get_hosted_zone(zone_id) # in (original) boto, these bools returned as strings. hosted_zone["GetHostedZoneResponse"]["HostedZone"]["Config"]["PrivateZone"].should.equal('True') hosted_zones = conn.get_all_hosted_zones() hosted_zones["ListHostedZonesResponse"]["HostedZones"][0]["Config"]["PrivateZone"].should.equal('True') zone = conn.get_zone("testdns.aws.com.") zone.config["PrivateZone"].should.equal('True')
def run_daemon(args): logger.warn("Dynamic route53 updater started for: %s" % get_dyn_domain(args.subdomain, args.domain)) while True: time.sleep(300) try: route53 = boto.connect_route53() except Exception, e: logging.warn('Unable to connect to route53: %s', e) continue try: update(args) except Exception, e: logger.warn("Unable to upaate zone: %s", e)
def r53_change_record(name, values, aws_access_key_id, aws_secret_access_key, proxy=None, proxy_port=None, type="A", ttl="600", comment=""): conn = boto.connect_route53(aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, proxy=proxy, proxy_port=proxy_port) res = conn.get_all_hosted_zones() domain_name = re.sub('^[^\.]*\.', '', name) if name[0] == '.': name = name[1:] hosted_zone_id = None for zoneinfo in res['ListHostedZonesResponse']['HostedZones']: zonename = zoneinfo['Name'] _zone_id = zoneinfo['Id'] _zone_id = re.sub('/hostedzone/', '', _zone_id) if zonename[-1] == '.': zonename = zonename[:-1] logging.debug("%s %s" % (domain_name, zonename)) if domain_name == zonename: hosted_zone_id = _zone_id break if not hosted_zone_id: raise NotInHostedZonesError(name) changes = ResourceRecordSets(conn, hosted_zone_id, comment) response = conn.get_all_rrsets(hosted_zone_id, type, name, maxitems=1) if response: rrset = response[0] change1 = changes.add_change("DELETE", name, type, rrset.ttl) for old_value in rrset.resource_records: change1.add_value(old_value) change2 = changes.add_change("CREATE", name, type, ttl) for new_value in values.split(','): change2.add_value(new_value) return changes.commit()
def test_create_stack_hosted_zone_by_id(): conn = boto.connect_cloudformation() dummy_template = { "AWSTemplateFormatVersion": "2010-09-09", "Description": "Stack 1", "Parameters": {}, "Resources": { "Bar": { "Type": "AWS::Route53::HostedZone", "Properties": { "Name": "foo.bar.baz" }, } }, } dummy_template2 = { "AWSTemplateFormatVersion": "2010-09-09", "Description": "Stack 2", "Parameters": { "ZoneId": { "Type": "String" } }, "Resources": { "Foo": { "Properties": { "HostedZoneId": { "Ref": "ZoneId" }, "RecordSets": [] }, "Type": "AWS::Route53::RecordSetGroup", } }, } conn.create_stack("test_stack1", template_body=json.dumps(dummy_template), parameters={}.items()) r53_conn = boto.connect_route53() zone_id = r53_conn.get_zones()[0].id conn.create_stack( "test_stack2", template_body=json.dumps(dummy_template2), parameters={"ZoneId": zone_id}.items(), ) stack = conn.describe_stacks()[0] assert stack.list_resources()
def add_route53_record(emr_internal_ips, cr): """ Given a list of IP addressed, create A records in a given domain """ conn = connect_route53( aws_access_key_id=cr.get_config("aws_access_key"), aws_secret_access_key=cr.get_config("aws_secret_key")) zone = conn.get_zone("alpinenow.local") print "Adding DNS Records for: {0}".format(emr_internal_ips) for ip in emr_internal_ips: internal_dns = "ip-" + ip.replace(".", "-") + ".alpinenow.local" response = zone.add_a(internal_dns, ip) # TODO: Do something with response
def test_slurp(self): conn = boto.connect_route53('the_key', 'the_secret') zone = conn.create_hosted_zone("testdns.aws.com") zone_id = zone["CreateHostedZoneResponse"][ "HostedZone"]["Id"].split("/")[-1] changes = boto.route53.record.ResourceRecordSets(conn, zone_id) change = changes.add_change("CREATE", "testdns.aws.com", "A") change.add_value("10.1.1.1") changes.commit() watcher = Route53(accounts=[self.account.name]) item_list, exception_map = watcher.slurp() self.assertIs( expr1=len(item_list), expr2=1, msg="Watcher should have 1 item but has {}".format(len(item_list)))
def test_delete_health_check(): conn = boto.connect_route53('the_key', 'the_secret') check = HealthCheck( ip_addr="10.0.0.25", port=80, hc_type="HTTP", resource_path="/", ) conn.create_health_check(check) checks = conn.get_list_health_checks()['ListHealthChecksResponse']['HealthChecks'] list(checks).should.have.length_of(1) health_check_id = checks[0]['Id'] conn.delete_health_check(health_check_id) checks = conn.get_list_health_checks()['ListHealthChecksResponse']['HealthChecks'] list(checks).should.have.length_of(0)