def run(): """Parses command line and dispatches the commands""" args = docopt(__doc__) configure_logging(args["--debug"]) disco_route53 = DiscoRoute53() if args['list-zones']: for hosted_zone in disco_route53.list_zones(): is_private_zone = hosted_zone.config['PrivateZone'] print("{0:<20} {1:10} {2:5}".format(hosted_zone.name, hosted_zone.id, is_private_zone)) elif args['list-records']: for hosted_zone in disco_route53.list_zones(): # the Hosted Zone name is the domain name with a period appended to it # allow searching by either with or without the period if not args['--zone'] or hosted_zone.name in (args['--zone'], args['--zone'] + '.'): for record in disco_route53.list_records(hosted_zone.name): values = ','.join(record.resource_records) print("{0:<5} {1:20} {2:50}".format(record.type, record.name, values)) elif args['create-record']: disco_route53.create_record(args['<zone-name>'], args['<record-name>'], args['<type>'], args['<value>']) elif args['delete-record']: record_name = args['<record-name>'] # AWS appends a . to the end of the record name. # Add it here as a convenience if the argument is missing it if not record_name.endswith('.'): record_name += '.' disco_route53.delete_record(args['<zone-name>'], record_name, args['<type>'])
def test_list_records(self): """Test listing records for a hosted zone""" disco_route53 = DiscoRoute53() _create_mock_zone_and_records(disco_route53.route53) records = disco_route53.list_records(TEST_DOMAIN) self.assertEqual(len(records), 1) self.assertEqual(records[0].name, TEST_RECORD_NAME)
def test_delete_records_by_value(self): """Test deleting records by value""" disco_route53 = DiscoRoute53() _create_mock_zone_and_records(disco_route53.route53) disco_route53.delete_records_by_value(TEST_RECORD_TYPE, TEST_RECORD_VALUE) zone = disco_route53.route53.get_zones()[0] self.assertEqual(len(disco_route53.route53.get_all_rrsets(zone.id)), 0)
def test_list_zones(self): """Test listing the available hosted zones""" disco_route53 = DiscoRoute53() _create_mock_zone_and_records(disco_route53.route53) zones = disco_route53.list_zones() self.assertEqual(len(zones), 2) self.assertEqual(zones[0].name, TEST_DOMAIN) self.assertEqual(zones[1].name, TEST_DOMAIN2)
def test_get_records_by_value(self): """Test getting records by value""" disco_route53 = DiscoRoute53() _create_mock_zone_and_records(disco_route53.route53) actual = disco_route53.get_records_by_value(TEST_RECORD_TYPE, TEST_RECORD_VALUE) expected = [{ 'zone_name': TEST_DOMAIN, 'record_name': TEST_RECORD_NAME }, { 'zone_name': TEST_DOMAIN2, 'record_name': TEST_RECORD_NAME2 }] self.assertEqual(actual, expected)