def api_connect(username, password, token, use_http, domain):
    """
    Authenticate to UltraDNS api portal using username and
    password plus token
    """
    password = '******'.format(password, token)
    api = ultra_rest_client.RestApiClient(username, password, use_http, domain)
    return api
示例#2
0
def main():
    """
    Handle command line and do API requests
    """
    # parse command line args
    parser = argparse.ArgumentParser()
    parser.add_argument('-z', '--zone', default=None, help="Specify zone to use in query")
    parser.add_argument('-a', '--a-record', default=None, 
            help="Specify A record to use in query.  Can be relative (ex 'foo') or absolute (ex 'foo.example.com')")
    parser.add_argument('-p', '--primary-ns', default=None,
            help="primary NS to receive zone xfer from")
    parser.add_argument('command',
            type=str,
            choices=[ 
                'list_primary_zone',
                'list_secondary_zone',
                'add_secondary_zone',
                'delete_zone',
                'promote_zone',
                'delete_a',
                ],
            help="command: list/add/delete/promote zone, delete A record")
    parser_required = parser.add_argument_group('required arguments')
    parser_required.add_argument('-c', '--creds-file',
            help="API credentials yaml file: contains {} and {}".format(USER_NAME, PASSWORD))

    args = parser.parse_args()

    # validate args
    if getattr(args, 'creds_file', None) == None:
        errordie("Please specify API credentials file")

    if (args.command == 'add_secondary_zone' or
            args.command == 'delete_zone' or
            args.command == 'promote_zone' or
            args.command == 'delete_a'):
        if args.zone == None:
            errordie("Please specify zone to run query against")

    if args.command == 'add_secondary_zone' and getattr(args, 'primary_ns', None) == None:
        errordie("Please specify primary NS to receive zone xfer from")

    if args.command =='delete_a' and getattr(args, 'a_record', None) == None:
        errordie("Please specify an A record to delete")

    # validate creds yaml file
    try:
        creds_file = open(args.creds_file, "r")
        creds = yaml.load(creds_file)
        creds_file.close()

    except Exception as e:
        errordie("Could not load API credentials yaml file: {}".format(e))

    if creds is None:
        errordie("API credentials file does not contain valid YAML")
    if USER_NAME not in creds:
        errordie("API credentials file does not specify '{}'".format(USER_NAME))
    if PASSWORD not in creds:
        errordie("API credentials file does not specify '{}'".format(PASSWORD))

    # create authenticated session
    try:
        client = ultra_rest_client.RestApiClient(creds[USER_NAME], creds[PASSWORD], False,
                API_DOMAIN)
    except Exception as e:
        errordie("could not authenticate: {}".format(e))

    # do query
    if args.command == 'list_primary_zone':
        list_zone(client, args.zone, {'zone_type':'PRIMARY'})
    elif args.command == 'list_secondary_zone':
        list_zone(client, args.zone, {'zone_type':'SECONDARY'})
    elif args.command == 'add_secondary_zone':
        add_secondary_zone(client, args.zone, args.primary_ns)
    elif args.command == 'delete_zone':
        delete_zone(client, args.zone)
    elif args.command == 'promote_zone':
        promote_zone(client, args.zone)
    elif args.command == 'delete_a':
        delete_a_record(client, args.zone, args.a_record)
示例#3
0
password = sys.argv[2]
path = sys.argv[3]
role_arn = sys.argv[4]
s3_bucket_name = sys.argv[5]
region_name = "us-east-1"
use_http = 'False'
domain = 'restapi.ultradns.com'

# clean up zone files from previous run and recreate the folder
create_zone_file_directory(path)

# change into the zone file directory
os.chdir(path)

# connect to the UltraDNS REST API service
c = ultra_rest_client.RestApiClient(username, password, 'True' == use_http,
                                    domain)
account_details = c.get_account_details()
account_name = account_details[u'accounts'][0][u'accountName']

all_zones = c.get_zones_of_account(account_name,
                                   offset=0,
                                   limit=5,
                                   reverse=False)  # 47

num_arrays = len(all_zones)
num_zones = len(all_zones[u'zones'])

zones = all_zones[u'zones']

# iterate through all the zones returned
for index in range(0, len(zones)):
import ultra_rest_client as ultradns

requests.packages.urllib3.disable_warnings()

# slurp credentials
with open('config/config.json', 'r') as f:
    config = json.load(f)
f.close()

f5_host = config['f5_host']
f5_user = config['f5_user']
f5_password = config['f5_password']
udns_username = config['udns_username']
udns_password = config['udns_password']

udns = ultradns.RestApiClient(udns_username, udns_password)

# Logging
logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)


def dump(obj):
    for attr in dir(obj):
        print "obj.%s = %s" % (attr, getattr(obj, attr))


def _has_dns_propagated(name, token):
    successes = 0
    dns_servers = []
    target_file.write('\tttl = {}\n'.format(rrset['ttl']))
    target_file.write('}\n')
    tfstate.write('"ultradns_record.{}": {{\n'.format(record_name))
    tfstate.write('"depends_on": [],\n')
    tfstate.write('"primary": {\n')
    tfstate.write('"id": "{}",\n'.format(record_name))
    tfstate.write('"attributes": {{\n "zone": "{}",\n'.format(zone_name))
    tfstate.write('"id": "{}",\n'.format(record_name))
    tfstate.write('"ttl": "{}",\n'.format(rrset['ttl']))
    tfstate.write('"type": "{}",\n'.format(rrtype))
    tfstate.write('"name": "{}"\n'.format(rrset['ownerName']))
    tfstate.write('}\n}\n}')


ultraclient = ultra_rest_client.RestApiClient(args.rest_api_user,
                                              args.rest_api_password,
                                              host=args.rest_api_url)
account_details = ultraclient.get_account_details()
account_name = account_details[u'accounts'][0][u'accountName']
all_zones = ultraclient.get_zones_of_account(account_name,
                                             offset=0,
                                             reverse=True)

credentials = open('credentials.tf', 'w')
credentials.write('variable "ultradns_username" {\n')
credentials.write('\tdefault = "{}"\n'.format(args.rest_api_user))
credentials.write('}\n')
credentials.write('variable "ultradns_password" {\n')
credentials.write('\tdefault = "{}"\n'.format(args.rest_api_password))
credentials.write('}\n')
credentials.close()