def main(reactor):
    args = getArgs()
    data = loadZoneData(args.filename)

    config = Config()
    config.createFromAPIKey(args.api_key)
    config['transport'] = 'twisted'

    nsoneObj = NSONE(config=config)

    if args.delete:
        return deleteZoneData(data, nsoneObj)

    else:
        return importZoneData(data, nsoneObj)
예제 #2
0
def config(monkeypatch, tmpdir):
    """
    Injects nsone.Config instance.

    :param pytest.monkeypatch
    :param pytest.tmpdir
    :return: nsone.Config instance in which os.path.expanduser is \
        patched with '/tmp' subdir that is unique per test.
    """
    def mockreturn(path):
        tmp_cfg_path = str(tmpdir.join('nsone_test'))
        return tmp_cfg_path

    monkeypatch.setattr(os.path, 'expanduser', mockreturn)

    cfg = Config()
    return cfg
예제 #3
0
nsone = NSONE()

# to specify an apikey here instead, use:
nsone = NSONE(apiKey='qACMD09OJXBxT7XOuRs8')

# to load an alternate configuration file:
nsone = NSONE(configFile='/etc/nsone/api.json')

# to load a specific keyID inside of your config file (see config format
# in docs), use this. this only makes sense for config file loads, not
# apiKey loads:
nsone = NSONE(keyID='all-access')

# if you have special needs, build your own Config object and pass it to
# NSONE:
config = Config()
config.createFromAPIKey('qACMD09OJXBxT7XOwv9v')
config['verbosity'] = 5
config['transport'] = 'twisted'
nsone = NSONE(config=config)

#  you can get the current config object NSONE is using via
config = nsone.config

# change config variables
config['verbosity'] = 5

# write out new config files
config.write('/tmp/newconfig.json')

# the config file format supports different apiKeys (see docs) using keyID
예제 #4
0
#
# Copyright (c) 2014 NSONE, Inc.
#
# License under The MIT License (MIT). See LICENSE in project root.
#

###########
# TWISTED #
###########

from nsone import NSONE, Config
from twisted.internet import defer, reactor

config = Config()
# load default config
config.loadFromFile(Config.DEFAULT_CONFIG_FILE)
# to load directly from apikey instead, use
# config.createFromAPIKey('qACMD09OJXBxT7XOuRs8')

# override default synchronous transport. note, this would normally go
# in config file.
config['transport'] = 'twisted'
nsone = NSONE(config=config)


@defer.inlineCallbacks
def getQPS():
    # when twisted transport is in use, all of the NSONE methods return
    # Deferred. yield them to gather the results, or add callbacks/errbacks
    # to be run when results are available
    zone = yield nsone.loadZone('test.com')
예제 #5
0
 def _build_nsone(self):
     config = Config()
     config[self._transport_key] = self._transport
     config.createFromAPIKey(self._api_key)
     self._nsone = NSONE(config=config)
     return self._nsone
예제 #6
0
#
# Copyright (c) 2014 NSONE, Inc.
#
# License under The MIT License (MIT). See LICENSE in project root.
#

###########
# TWISTED #
###########

from nsone import NSONE, Config
from twisted.internet import defer, reactor

config = Config()
# load default config
config.loadFromFile(Config.DEFAULT_CONFIG_FILE)
# to load directly from apikey instead, use
# config.createFromAPIKey('qACMD09OJXBxT7XOuRs8')

# override default synchronous transport. note, this would normally go
# in config file.
config['transport'] = 'twisted'
nsone = NSONE(config=config)


@defer.inlineCallbacks
def getQPS():
    # when twisted transport is in use, all of the NSONE methods return
    # Deferred. yield them to gather the results, or add callbacks/errbacks
    # to be run when results are available
    zone = yield nsone.loadZone('test.com')
예제 #7
0
#
# Copyright (c) 2014 NSONE, Inc.
#
# License under The MIT License (MIT). See LICENSE in project root.
#

import logging
from nsone import NSONE, Config

# to enable verbose logging, set 'verbosity' in the config and use
# the standard python logging system

config = Config()
config.createFromAPIKey('qACMD09OJXBxT7XOwv9v')
config['verbosity'] = 5
logging.basicConfig(level=logging.DEBUG)
print(config)
nsone = NSONE(config=config)

# now all requests will show up in the logging system

# exception handling:
# the follow exceptions may be thrown
# from nsone.rest.errors import ResourceException, \
#     RateLimitException, AuthException

# ResourceException is the base exception (Auth and RateLimit extend it)
# it (and therefore they) have the properties message, response, body

# AuthException is raised when apikey is incorrect or the key doesn't
# have permission to the requested resource
from argparse import ArgumentParser
from nsone import NSONE, Config
from file_parser import parseCSV
from twisted.internet import defer, reactor

# Get configuration settings from command line for added flexibility.
parser = ArgumentParser(description='Asychronously create DNS records from file.')
parser.add_argument('-f', dest='filename', required=True, type=str, nargs='?', help='relative path to file')
parser.add_argument('-k', dest='apikey', required=True, type=str, nargs='?', help='NS1 API key')
args = parser.parse_args()

# Set configuration with API key and asynchronous Twisted transport setting.
config = Config()
config.createFromAPIKey(args.apikey)
config['transport'] = 'twisted'
nsone = NSONE(config=config)

def getData(filename):
    # Return Deferred.
    # Parse file into dict containing zones with record data.
    # Currently only parses CSV files.
    # Could add other file type parsing and select based on filename.
    data = parseCSV(filename)
    return defer.succeed(data)

def handleData(data):
    # Return DeferredList.
    # Register success when record additions for all zones have completed.
    zones = []
    for domain, records in data.iteritems():
        zone = loadZone(domain)
예제 #9
0
nsone = NSONE()

# to specify an apikey here instead, use:
nsone = NSONE(apiKey="qACMD09OJXBxT7XOuRs8")

# to load an alternate configuration file:
nsone = NSONE(configFile="/etc/nsone/api.json")

# to load a specific keyID inside of your config file (see config format
# in docs), use this. this only makes sense for config file loads, not
# apiKey loads:
nsone = NSONE(keyID="all-access")

# if you have special needs, build your own Config object and pass it to
# NSONE:
config = Config()
config.createFromAPIKey("qACMD09OJXBxT7XOwv9v")
config["verbosity"] = 5
config["transport"] = "twisted"
nsone = NSONE(config=config)

#  you can get the current config object NSONE is using via
config = nsone.config

# change config variables
config["verbosity"] = 5

# write out new config files
config.write("/tmp/newconfig.json")

# the config file format supports different apiKeys (see docs) using keyID