def main(): nsone = NSONE(apiKey='NSONE_API_KEY_GOES_HERE') zonenames = get_zones(nsone) for i in range(0, len(zonenames)): qps = get_qps(nsone, zonenames[i]) #print(zonenames[i] + ': ' + str(qps)) dd_send(zonenames[i], qps)
def load_rest_client(self): """Loads ns1 rest client config""" opts = self.rest_cfg_opts # Create default config without any key cfg = Config() cfg.createFromAPIKey('') if opts.get('path', None): cfg.loadFromFile(opts['path']) elif opts.get('api_key'): cfg.createFromAPIKey(opts['api_key']) else: path = os.path.join(self.home_dir, self.DEFAULT_CONFIG_FILE) if os.path.exists(path): cfg.loadFromFile(path) if opts.get('api_key_id'): cfg.useKeyId(opts['api_key_id']) if opts.get('endpoint'): cfg['endpoint'] = opts['endpoint'] if opts.get('transport'): cfg['transport'] = opts['transport'] if opts.get('ignore_ssl'): cfg['ignore-ssl-errors'] = opts['ignore_ssl'] if cfg['ignore-ssl-errors']: import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) # Store the cli cfg dict as an attr of the rest config instance. for k, v in self.cfg.items(): cfg['cli'][k] = v self.rest = NSONE(config=cfg)
def __init__(self, id, api_key, *args, **kwargs): self.log = getLogger('Ns1Provider[{}]'.format(id)) self.log.debug('__init__: id=%s, api_key=***', id) super(Ns1Provider, self).__init__(id, *args, **kwargs) self._client = NSONE(apiKey=api_key)
# # Copyright (c) 2014 NSONE, Inc. # # License under The MIT License (MIT). See LICENSE in project root. # from nsone import NSONE, Config # you can either build your own config, or let NSONE build a default one for # you. the latter is easier and works like this: # NSONE will use config in ~/.nsone by default 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'
# # Copyright (c) 2014 NSONE, Inc. # # License under The MIT License (MIT). See LICENSE in project root. # from nsone import NSONE # NSONE will use config in ~/.nsone by default 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') # import a zone from the included example zone definition zone = nsone.createZone('example2.com', zoneFile='./importzone.db') print(zone) # delete a whole zone, including all records, data feeds, etc. this is # immediate and irreversible, so be careful! zone.delete()
# 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') qps = yield zone.qps() defer.returnValue(qps) def gotQPS(result): print("current QPS for test.com: %s" % result['qps']) reactor.stop()
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
def main(): args = docopt(__doc__, version=BANNER, options_first=True) verbosity = args.get('-v', 0) if verbosity > 1: logging.basicConfig(level=logging.DEBUG) elif verbosity > 0: logging.basicConfig(level=logging.INFO) else: logging.basicConfig(level=logging.CRITICAL) # tweak requests logging if verbosity < 2: requests_log = logging.getLogger("requests") requests_log.setLevel(logging.WARNING) # if api key given, use a custom config config = None if args['--key']: config = Config() # this will save a .nsone with this key if one doesn't already exist config.createFromAPIKey(args['--key'], maybeWriteDefault=True) config['verbosity'] = verbosity try: nsone = NSONE(config=config) except ConfigException as e: print(e.message) sys.exit(1) except IOError as e: print('No config file was found. Either specify an API key (with -k) ' 'on the command line, or create %s' % Config.DEFAULT_CONFIG_FILE) sys.exit(1) # do config overrides in nsone based on cmd args if args['--format']: nsone.config['cli']['output_format'] = args['--format'] # do defaults if 'output_format' not in nsone.config.get('cli', {}): nsone.config['cli']['output_format'] = 'text' if args['--endpoint']: nsone.config['endpoint'] = args['--endpoint'] if args['--ignore-ssl-errors']: nsone.config['ignore-ssl-errors'] = args['--ignore-ssl-errors'] if verbosity < 2: logging.captureWarnings(True) if args['--transport']: nsone.config['transport'] = args['--transport'] BaseCommand.nsone = nsone cmd = args['<command>'] if not cmd: info = "\nType 'help' for help\n\nCurrent Key: %s\nEndpoint: %s" % \ (nsone.config.getCurrentKeyID(), nsone.config.getEndpoint()) repl = NS1Repl(cmdListDoc, cmdList) repl.interact(BANNER + info) sys.exit(0) cmdArgs = args['<args>'] subArgv = [cmd] + cmdArgs if cmd in cmdList.keys(): svc = cmdList[cmd] try: subArgs = docopt(svc.__doc__, argv=subArgv, options_first=True) except DocoptExit as e: if cmd == 'help': print(__doc__) else: print(e.usage) sys.exit(1) try: svc.run(subArgs) except ResourceException as e: print('REST API error: %s' % e.message) except CommandException as e: print(e.message) sys.exit(1) else: exit("%r is not a command. See 'ns1 help'." % cmd)