def add(self, method, route, obj, name=None): if re.search('\s', route): raise ValueError('Route may not include whitespace.') fields = re.findall('{([^}]*)}', route) for field in fields: is_identifier = re.match('[A-Za-z_][A-Za-z0-9_]+$', field) if not is_identifier or field in keyword.kwlist: raise ValueError('Field names must be valid identifiers.') route = route.strip('/') if self._match(method, route) is None: r = [] r.append(method) r.append(route) r.append(obj) r.append(name) self.routes.append(r) else: raise nfw.Error('Adding duplicate API route %s' % (route))
def createDevicePost(req, resp): api = RestClient(req.context['restapi']) values = req.post subnet = values.get('device_ip') data = {'snmp_comm': values.get('snmp_community')} issubnet = re.search('/', subnet) if not issubnet: subnet += "/32" try: subnet = IPNetwork(subnet) except e: raise (nfw.Error(e)) results = [] for ip in subnet: ver = ip._version ip = ip.ip_network data['id'] = ip2dec(ip, 4) response_headers, result = api.execute( nfw.HTTP_POST, "/infrastructure/network/devices", obj=data) results.append(result) return results
def __init__(self, config_file=None): self.config = {} if config_file is None: config_file = nfw_config if config_file is not None: if os.path.isfile(config_file): if config_file not in self.configs: config = ConfigParser.ConfigParser() config.read(config_file) sections = config.sections() for s in sections: self.config[s] = Section() options = config.options(s) for o in options: self.config[s][o] = config.get(s, o) self.configs[config_file] = self.config else: self.config = self.configs[config_file] else: raise nfw.Error("Configuration file not found: %s" % (config_file,))
# nfw.config.nfw_config = os.environ['NEUTRINO_CONFIG'] # else: # raise nfw.Error("Configuration file not found: %s" # % (os.environ['NEUTRINO_CONFIG'],)) # else: # raise nfw.Error("Configuration file not found in os.environment") celeryConfPath = '/etc/netrino-celery.cfg' config = nfw.Config(celeryConfPath) try: celery = config.get('celery') app = Celery(celery.get('app'), broker=celery.get('broker'), backend=celery.get('backend'), include=celery.get('include').split(',')) app.conf.update(result_expires=celery.get('result_expires')) except: raise nfw.Error("No Celery settings found in %s" % celeryConfPath) try: mysql = config.get('mysql') db = nfw.Mysql(host=mysql.get('host'), database=mysql.get('database'), username=mysql.get('username'), password=mysql.get('password')) except: raise nfw.Error("No Celery settings found in %s" % celeryConfPath)
def __init__(self, data=None, validate=True, readonly=False, **kwargs): super(nfw.ModelDict, self).__init__(**kwargs) if validate is True: load = False else: load = True self.readonly = readonly if isinstance(data, nfw.Request): values = {} for v in data.post: values[v] = data.post[v].value elif isinstance(data, dict): values = data else: raise nfw.Error('forms only accept request or dict object') try: if values is not None: for v in values: if hasattr(self, v): t = v.replace('_confirm', '') field = getattr(self, t) if field.label is not None: name = field.label else: name = v if '_confirm' in v: t = v.replace('_confirm', '') field = getattr(self, t) if field.label is not None: name = field.label else: name = v if values[t] is None or values[t] == '': if values[v] is not None and values[v] != '': raise nfw.HTTPBadRequest( title="Field Invalid", description="%s mis-match" % (name, )) if hasattr(self, v): if ((values[v] is not None and values[v] != '') or field.required is True): if field.readonly is False: if isinstance(field, nfw.ModelDict.Bool): self._set({v: True}, load) elif isinstance(field, nfw.ModelDict.Password): confirm = "%s_confirm" % (v) if confirm in values: if load is False: c = values[confirm] if c != values[v]: raise nfw.HTTPBadRequest( title="Field Invalid", description="%s mis-match" % (name, )) self._set({v: values[v]}, load) elif isinstance(field, nfw.ModelDict.Integer): try: self._set({v: long(values[v])}, load) except: self._set({v: values[v].value}, load) elif isinstance(field, nfw.ModelDict.Number): try: self._set({v: float(values[v])}, load) except: self._set({v: values[v]}, load) else: self._set({v: values[v]}, load) except nfw.FieldError as e: raise nfw.HTTPBadRequest(title="Field Invalid", description=e)
def execute(self, method, url, data=None, headers=[]): import pycurl host = self.get_host_port_from_url(url) if host in self.curl_session: curl = self.curl_session[host] else: self.curl_session[host] = pycurl.Curl() curl = self.curl_session[host] url = url.replace(" ", "%20") method = method.upper() self.server_headers = dict() buffer = BytesIO() curl.setopt(curl.URL, nfw.utils.if_unicode_to_utf8(url)) try: curl.setopt(curl.WRITEDATA, buffer) except TypeError: curl.setopt(curl.WRITEFUNCTION, buffer.write) curl.setopt(curl.HEADERFUNCTION, self.header_function) curl.setopt(curl.FOLLOWLOCATION, True) curl.setopt(curl.SSL_VERIFYPEER, self.ssl_verify_peer) curl.setopt(curl.SSL_VERIFYHOST, self.ssl_verify_host) curl.setopt(curl.CONNECTTIMEOUT, self.connect_timeout) curl.setopt(curl.TIMEOUT, self.timeout) curl.setopt(curl.DEBUGFUNCTION, _debug) curl.setopt(curl.VERBOSE, 1) if data is not None: curl.setopt(curl.POSTFIELDS, nfw.utils.if_unicode_to_utf8(data)) else: curl.setopt(curl.POSTFIELDS, nfw.utils.if_unicode_to_utf8('')) send_headers = list() for header in headers: send_header = nfw.utils.if_unicode_to_utf8( "%s: %s" % (header, headers[header])) send_headers.append(send_header) curl.setopt(pycurl.HTTPHEADER, send_headers) if method == nfw.HTTP_GET: curl.setopt(curl.CUSTOMREQUEST, nfw.utils.if_unicode_to_utf8('GET')) elif method == nfw.HTTP_PUT: curl.setopt(curl.CUSTOMREQUEST, nfw.utils.if_unicode_to_utf8('PUT')) elif method == nfw.HTTP_POST: curl.setopt(curl.CUSTOMREQUEST, nfw.utils.if_unicode_to_utf8('POST')) elif method == nfw.HTTP_PATCH: curl.setopt(curl.CUSTOMREQUEST, nfw.utils.if_unicode_to_utf8('PATCH')) elif method == nfw.HTTP_DELETE: curl.setopt(curl.CUSTOMREQUEST, nfw.utils.if_unicode_to_utf8('DELETE')) elif method == nfw.HTTP_OPTIONS: curl.setopt(curl.CUSTOMREQUEST, nfw.utils.if_unicode_to_utf8('OPTIONS')) elif method == nfw.HTTP_HEAD: curl.setopt(curl.CUSTOMREQUEST, nfw.utils.if_unicode_to_utf8('HEAD')) elif method == nfw.HTTP_TRACE: curl.setopt(curl.CUSTOMREQUEST, nfw.utils.if_unicode_to_utf8('TRACE')) elif method == nfw.HTTP_CONNECT: curl.setopt(curl.CUSTOMREQUEST, nfw.utils.if_unicode_to_utf8('CONNECT')) else: raise nfw.Error("Invalid request type %s" % (method, )) try: curl.perform() status = curl.getinfo(pycurl.HTTP_CODE) except pycurl.error as e: del self.curl_session[host] if e[0] == 28: raise nfw.RestClientError("Connection timeout %s" % (host, )) else: raise pycurl.error(e) # Figure out what encoding was sent with the response, if any. # Check against lowercased header name. encoding = None if 'content-type' in self.server_headers: content_type = self.server_headers['content-type'].lower() match = re.search('charset=(\S+)', content_type) if match: encoding = match.group(1) if encoding is None: # Default encoding for JSON is UTF-8. # Other content types may have different default encoding, # or in case of binary data, may have no encoding at all. encoding = 'utf_8' body = buffer.getvalue() # Decode using the encoding we figured out. body = body.decode(encoding) resp_header = nfw.Headers() for h in self.server_headers: resp_header[h] = self.server_headers[h] return (status, resp_header, body)