def job_run(self, request): params = request.query_params() or {} payload = request.payload_params() or {} if not isinstance(params, dict): raise HttpReqErrJson(400, "invalid arguments type") if not xys.validate(params, self.RUN_QSCHEMA): raise HttpReqErrJson(415, "invalid arguments for command") if not isinstance(payload, dict): raise HttpReqErrJson(400, "invalid arguments type") if not xys.validate(payload, self.RUN_PSCHEMA): raise HttpReqErrJson(415, "invalid arguments for command") if not self.LOCK.acquire_write(self.lock_timeout): raise HttpReqErrJson( 503, "unable to take LOCK for writing after %s seconds" % self.lock_timeout) try: obj = self._push_epts_sync(params['endpoint'], params['id'], 'run', copy.copy(request)) return self._build_result(obj) except HttpReqErrJson: raise except Exception as e: LOG.exception(e) raise HttpReqErrJson(503, repr(e)) finally: self.LOCK.release()
def api_endpoint_patch(self, request): params = request.query_params() args = request.payload_params() self._check_api_key(request) if not isinstance(params, dict): raise HttpReqErrJson( 400, "invalid arguments type for query parameters") if not xys.validate(params, self.ENDPOINT_PATCH_QSCHEMA): raise HttpReqErrJson( 415, "invalid arguments for command for query parameters") if not isinstance(args, dict): raise HttpReqErrJson( 400, "invalid arguments type for payload parameters") if not xys.validate(args, self.ENDPOINT_PATCH_PSCHEMA): raise HttpReqErrJson( 415, "invalid arguments for command payload parameters") if not self.LOCK.acquire_write(self.lock_timeout): raise HttpReqErrJson( 503, "unable to take LOCK for writing after %s seconds" % self.lock_timeout) try: if params.get('endpoint') != 'zones': raise HttpReqErrJson(400, "invalid request") if not args.get('rrsets'): return self._do_response(request, None, args) zone = self._fetch_zone(request, params) uids = self._push_apis_sync('change_rrsets', params, args, zone) if not uids: raise HttpReqErrJson(500, "unable to retrieve uids") res = self._get_results(uids) if res['failed']: raise HttpReqErrJson( 409, "failed to synchronize on dns provider. (errors: %r)" % res['failed']) return self._do_response(request, None, args) except HttpReqErrJson as e: raise except Exception as e: LOG.exception("%r", e) finally: self.LOCK.release()
def job_status(self, request): params = request.query_params() if not isinstance(params, dict): raise HttpReqErrJson(400, "invalid arguments type") if not xys.validate(params, self.STATUS_QSCHEMA): raise HttpReqErrJson(415, "invalid arguments for command") obj = self._get_obj(params['endpoint'], params['id']) if not self.LOCK.acquire_read(self.lock_timeout): raise HttpReqErrJson( 503, "unable to take LOCK for reading after %s seconds" % self.lock_timeout) try: return self._build_result(obj) except HttpReqErrJson: raise except Exception as e: LOG.exception(e) raise HttpReqErrJson(503, repr(e)) finally: if obj.get_status() == STATUS_COMPLETE: self._clear_obj(params['endpoint'], params['id']) self.LOCK.release()
def probes(self, request): params = request.query_params() if not isinstance(params, dict): raise HttpReqError(400, "invalid arguments type") if not xys.validate(params, self.PROBES_QSCHEMA): raise HttpReqError(415, "invalid arguments for command") if not self.LOCK.acquire_read(self.lock_timeout): raise HttpReqError( 503, "unable to take LOCK for reading after %s seconds" % self.lock_timeout) try: uid = self._push_epts_sync(params['endpoint'], 'probes', params) res = self._get_result(uid) if res['error']: raise HttpReqError( 500, "failed to get results. (errors: %r)" % res['error']) return HttpResponse(data=res['result']) except HttpReqError: raise except Exception as e: LOG.exception(e) finally: gc.collect() self.LOCK.release()
def api_endpoint_validate(self, request): params = request.query_params() self._check_api_key(request) if not isinstance(params, dict): raise HttpReqErrJson( 400, "invalid arguments type for query parameters") if not xys.validate(params, self.ENDPOINT_VALIDATE_QSCHEMA): raise HttpReqErrJson( 415, "invalid arguments for command for query parameters") if not self.LOCK.acquire_read(self.lock_timeout): raise HttpReqErrJson( 503, "unable to take LOCK for reading after %s seconds" % self.lock_timeout) try: domain = params.pop('id').lower() r = self._do_response(request, method='GET') if not r: raise HttpReqErrJson(500, "unable to fetch domains list") data = r.get_data() if not data: raise HttpReqErrJson(500, "unable to fetch domains list") data = json.loads(data) if not isinstance(data, list): raise HttpReqErrJson(500, "unable to fetch domains list") if not data: return True for x in data: if x['name'] == domain: raise HttpReqErrJson(409, "domain %r already exists" % domain) return True except HttpReqErrJson as e: raise except Exception as e: LOG.exception("%r", e) finally: self.LOCK.release()
def api_endpoint_put(self, request): params = request.query_params() self._check_api_key(request) if not isinstance(params, dict): raise HttpReqErrJson(400, "invalid arguments type") if not xys.validate(params, self.ENDPOINT_PUT_QSCHEMA): raise HttpReqErrJson(415, "invalid arguments for command") if not self.LOCK.acquire_read(self.lock_timeout): raise HttpReqErrJson( 503, "unable to take LOCK for reading after %s seconds" % self.lock_timeout) try: return self._do_response(request, params) except HttpReqErrJson as e: raise except Exception as e: LOG.exception("%r", e) finally: self.LOCK.release()
def api_endpoint_post(self, request): params = request.query_params() args = request.payload_params() self._check_api_key(request) if not isinstance(params, dict): raise HttpReqErrJson( 400, "invalid arguments type for query parameters") if not xys.validate(params, self.ENDPOINT_POST_QSCHEMA): raise HttpReqErrJson( 415, "invalid arguments for command for query parameters") if not isinstance(args, dict): raise HttpReqErrJson( 400, "invalid arguments type for payload parameters") if not xys.validate(args, self.ENDPOINT_POST_PSCHEMA): raise HttpReqErrJson( 415, "invalid arguments for command payload parameters") if not self.LOCK.acquire_write(self.lock_timeout): raise HttpReqErrJson( 503, "unable to take LOCK for writing after %s seconds" % self.lock_timeout) try: if params.get('endpoint') != 'zones': raise HttpReqErrJson(400, "invalid request") has_rrsets = False domain_name = args['name'].rstrip('.') if self.config['dns'].get('domains'): for domain in (domain_name, 'default'): if not self.config['dns']['domains'].get(domain): continue rrsets = deepcopy( self.config['dns']['domains'][domain]['rrsets']) has_rrsets = self._append_rrsets(args, domain_name, rrsets) uids = self._push_apis_sync('create_hosted_zone', params, args) if not uids: raise HttpReqErrJson(500, "unable to retrieve uids") res = self._get_results(uids) if res['failed']: raise HttpReqErrJson( 409, "failed to synchronize on dns provider. (errors: %r)" % res['failed']) for ret in res['successful']: res = ret.get_result() if not res or 'nameservers' not in res: continue if 'nameservers' not in args: args['nameservers'] = [] for nameserver in res['nameservers']: args['nameservers'].append("%s." % nameserver.rstrip('.')) soa_content = self._helpers.build_soa_content( domain_name, args.get('nameservers')) if soa_content: has_rrsets = True self._add_rrset(args, domain_name, 'SOA', soa_content) r = self._do_response(request, None, args) if not r \ or (not args.get('nameservers') \ and not has_rrsets): return r data = r.get_data() if data: data = json.loads(data) if data and 'id' in data: self._refresh_apis(data) return r except HttpReqErrJson as e: raise except Exception as e: LOG.exception("%r", e) finally: self.LOCK.release()