コード例 #1
0
def import_zone_data(filename, api_key, **kwargs):
    nsone_obj = NSONE(apiKey=api_key)
    extension = os.path.splitext(filename)[1]

    with open(filename, 'rb') as f:
        if extension == '.csv':
            reader = csv.DictReader(f)
            data = transform_csv(reader)
        elif extension == '.json':
            data = transform_json(json.loads(f))

    if kwargs.get('delete'):
        delete_zone_data(nsone_obj, data)
        return

    for k, v in data.iteritems():

        try:
            zone = nsone_obj.createZone(k)
            print 'Added Zone {}'.format(zone)
        except AuthException as e:
            # Invalid api key passed in
            print e.message
            return
        except ResourceException as e:
            # zone already exists
            print '{} {}'.format(k, e.message)
            zone = nsone_obj.loadZone(k)

        for rec in v:

            answers = rec['Data'].split()
            try:
                # determine which record type to add using types provided in the file
                method_name = 'add_{}'.format(rec['Type'])
                add_method = getattr(zone, method_name)
                # data corresponds to the answer and it might have priority values
                record = add_method(k, [answers], ttl=rec['TTL'])
                print 'Successfully Added record {}'.format(record)
            except ResourceException as e:
                # record already exists, so add answers to it
                # Or Invalid add method
                print '{} {}'.format(rec, e.message)
                try:
                    record = nsone_obj.loadRecord(k, rec['Type'], k)
                    print 'Successfully loaded record {}'.format(k, rec['Type'])
                    recordAnswers = {answer['answer'][0] for answer in record.data['answers']}
                    if recordAnswers.intersection(answers[0]):
                        record.addAnswers(answers)
                        print 'Added answer: {}'.format(answers[0])
                    record.addAnswers([answers])
                    print 'Successfully Processed Answers {}'.format(answers)
                except ResourceException as e:
                    # Invalid format for answer, so ignore this answer, or invalid record type
                    print e.message
                    continue
コード例 #2
0
ファイル: zones.py プロジェクト: amirwollman/nsone-python
                 }
             }],
           use_csubnet=True,
           filters=[{'geotarget_country': {}},
                    {'select_first_n': {'N': 1}}])

# zone usage
print(zone.qps())

###########################
# LOAD and UPDATE RECORDS #
###########################

# if you don't have a Record object yet, you can load it in one of two ways:
# 1) directly from the top level NSONE object by specifying name and type
rec = nsone.loadRecord('honey.example.com', 'A')
print(rec)
# 2) if you have a Zone object already, you can load it from that
rec = zone.loadRecord('honey', 'A')
print(rec)

# you can access all the record information via the data property
print(rec.data['answers'])

# add answer(s) to existing answer list
rec.addAnswers('4.4.4.4')
rec.addAnswers(['3.4.5.6', '4.5.6.8'])
print(rec.data['answers'])

# update the full answer list
rec.update(answers=['6.6.6.6', '7.7.7.7'])
コード例 #3
0
ファイル: zones.py プロジェクト: tomprince/nsone-python
           }, {
               'select_first_n': {
                   'N': 1
               }
           }])

# zone usage
print(zone.qps())

###########################
# LOAD and UPDATE RECORDS #
###########################

# if you don't have a Record object yet, you can load it in one of two ways:
# 1) directly from the top level NSONE object by specifying name and type
rec = nsone.loadRecord('honey.example.com', 'A')
print(rec)
# 2) if you have a Zone object already, you can load it from that
rec = zone.loadRecord('honey', 'A')
print(rec)

# you can access all the record information via the data property
print(rec.data['answers'])

# add answer(s) to existing answer list
rec.addAnswers('4.4.4.4')
rec.addAnswers(['3.4.5.6', '4.5.6.8'])
print(rec.data['answers'])

# update the full answer list
rec.update(answers=['6.6.6.6', '7.7.7.7'])