def decorated(self, *args, **kwargs): """ """ # Fetch auth options from args auth_options = {} nipap_args = {} # validate function arguments if len(args) == 1: nipap_args = args[0] else: #logger.info("Malformed request: got %d parameters" % len(args)) raise Fault( 1000, ("NIPAP API functions take exactly 1 argument (%d given)") % len(args)) if type(nipap_args) != dict: raise Fault(1000, ( "Function argument must be XML-RPC struct/Python dict (Python %s given)." % type(nipap_args).__name__)) # fetch auth options try: auth_options = nipap_args['auth'] if type(auth_options) is not dict: raise ValueError() except (KeyError, ValueError): raise Fault(1000, ("Missing/invalid authentication options in request.")) # fetch authoritative source try: auth_source = auth_options['authoritative_source'] except KeyError: raise Fault(1000, ("Missing authoritative source in auth options.")) if not request.authorization: return authenticate() # init AuthFacory() af = AuthFactory() auth = af.get_auth(request.authorization.username, request.authorization.password, auth_source, auth_options or {}) # authenticated? if not auth.authenticate(): raise Fault(1510, ("Incorrect username or password.")) # Replace auth options in API call arguments with auth object new_args = dict(args[0]) new_args['auth'] = auth return f(self, *(new_args, ), **kwargs)
def login(username, password): if not username or not password: raise Fault("unknown_data", "Username and password are required!") user = auth.authenticate(username, password) if not user: raise Fault("invalid_user", "Invalid username/password, please try again.") else: auth.login_user(user) return session["user_pk"]
def search_prefix(self, args): """ Search for prefixes. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `query` [struct] A struct specifying the search query. * `search_options` [struct] Options for the search query, such as limiting the number of results returned. Returns a struct containing the search result together with the search options used. Certain values are casted from numbers to strings because XML-RPC simply cannot handle anything bigger than an integer. """ try: res = self.nip.search_prefix(args.get('auth'), args.get('query'), args.get('search_options') or {}) # fugly cast from large numbers to string to deal with XML-RPC for pref in res['result']: pref['total_addresses'] = str(pref['total_addresses']) pref['used_addresses'] = str(pref['used_addresses']) pref['free_addresses'] = str(pref['free_addresses']) return res except (AuthError, NipapError), e: raise Fault(e.error_code, str(e))
def getProductInfo(*a, **kw): try: return products.getProductInfo(*a, **kw) except Exception: utils.log_remote_call_error('XMLRPC call getProductInfo() failed', *a, **kw) raise Fault(1, 'An unexpected error has occurred.')
def pub_ListResources (self, credentials, options): try: CredVerifier.checkValid(credentials, []) compressed = options.get("geni_compressed", False) urn = options.get("geni_slice_urn", None) if urn: CredVerifier.checkValid(credentials, "getsliceresources", urn) self.recordAction("listresources", credentials, urn) sliver_urn = GeniDB.getSliverURN(urn) if sliver_urn is None: raise Fault("ListResources", "Sliver for slice URN (%s) does not exist" % (urn)) rspec = GeniDB.getManifest(sliver_urn) else: self.recordAction("listresources", credentials) rspec = foam.geni.lib.getAdvertisement() if compressed: zrspec = zlib.compress(rspec) rspec = base64.b64encode(zrspec) return rspec except ExpatError, e: self._log.error("Error parsing credential strings") e._foam_logged = True raise e
def priv_DeleteSliver(self, slice_urn, credentials, options=None): try: #if CredVerifier.checkValid(credentials, "deletesliver", slice_urn): if True: self.recordAction("deletesliver", credentials, slice_urn) if GeniDB.getSliverURN(slice_urn) is None: raise Fault( "DeleteSliver", "Sliver for slice URN (%s) does not exist" % (slice_urn)) return self.errorResult(12, "") #not sure if this is needed sliver_urn = GeniDB.getSliverURN(slice_urn) data = GeniDB.getSliverData(sliver_urn, True) foam.geni.lib.deleteSliver(sliver_urn=sliver_urn) foam.task.emailGAPIDeleteSliver(data) return self.successResult(True) return self.successResult(False) except UnknownSlice, x: self._log.info( "Attempt to delete unknown sliver for slice URN %s" % (slice_urn)) x._foam_logged = True raise x
def edit_pool(self, args): """ Edit pool. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `pool` [struct] Specifies pool attributes to match. * `attr` [struct] Pool attributes to set. """ try: res = self.nip.edit_pool(args.get('auth'), args.get('pool'), args.get('attr')) # fugly cast from large numbers to string to deal with XML-RPC for pool in res: for val in ('member_prefixes_v4', 'member_prefixes_v6', 'used_prefixes_v4', 'used_prefixes_v6', 'free_prefixes_v4', 'free_prefixes_v6', 'total_prefixes_v4', 'total_prefixes_v6', 'total_addresses_v4', 'total_addresses_v6', 'used_addresses_v4', 'used_addresses_v6', 'free_addresses_v4', 'free_addresses_v6'): if pool[val] is not None: pool[val] = unicode(pool[val]) return res except (AuthError, NipapError) as exc: self.logger.debug(unicode(exc)) raise Fault(exc.error_code, unicode(exc))
def add_vrf(self, args): """ Add a new VRF. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `attr` [struct] VRF attributes. Returns the internal database ID for the VRF. """ try: res = self.nip.add_vrf(args.get('auth'), args.get('attr')) # fugly cast from large numbers to string to deal with XML-RPC for val in ('num_prefixes_v4', 'num_prefixes_v6', 'total_addresses_v4', 'total_addresses_v6', 'used_addresses_v4', 'used_addresses_v6', 'free_addresses_v4', 'free_addresses_v6'): res[val] = unicode(res[val]) return res except (AuthError, NipapError) as exc: self.logger.debug(unicode(exc)) raise Fault(exc.error_code, unicode(exc))
def smart_search_vrf(self, args): """ Perform a smart search. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `query_string` [string] The search string. * `search_options` [struct] Options for the search query, such as limiting the number of results returned. Returns a struct containing search result, interpretation of the search string and the search options used. """ try: res = self.nip.smart_search_vrf(args.get('auth'), args.get('query_string'), args.get('search_options', {}), args.get('extra_query')) # fugly cast from large numbers to string to deal with XML-RPC for vrf in res['result']: for val in ('num_prefixes_v4', 'num_prefixes_v6', 'total_addresses_v4', 'total_addresses_v6', 'used_addresses_v4', 'used_addresses_v6', 'free_addresses_v4', 'free_addresses_v6'): vrf[val] = unicode(vrf[val]) return res except (AuthError, NipapError) as exc: self.logger.debug(unicode(exc)) raise Fault(exc.error_code, unicode(exc))
def add_pool(self, args): """ Add a pool. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `attr` [struct] Attributes which will be set on the new pool. Returns ID of created pool. """ try: res = self.nip.add_pool(args.get('auth'), args.get('attr')) # fugly cast from large numbers to string to deal with XML-RPC for val in ('member_prefixes_v4', 'member_prefixes_v6', 'used_prefixes_v4', 'used_prefixes_v6', 'free_prefixes_v4', 'free_prefixes_v6', 'total_prefixes_v4', 'total_prefixes_v6', 'total_addresses_v4', 'total_addresses_v6', 'used_addresses_v4', 'used_addresses_v6', 'free_addresses_v4', 'free_addresses_v6'): if res[val] is not None: res[val] = unicode(res[val]) return res except (AuthError, NipapError) as exc: self.logger.debug(unicode(exc)) raise Fault(exc.error_code, unicode(exc))
def list_vrf(self, args): """ List VRFs. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `vrf` [struct] Specifies VRF attributes to match (optional). Returns a list of structs matching the VRF spec. """ try: res = self.nip.list_vrf(args.get('auth'), args.get('vrf')) # fugly cast from large numbers to string to deal with XML-RPC for vrf in res: for val in ('num_prefixes_v4', 'num_prefixes_v6', 'total_addresses_v4', 'total_addresses_v6', 'used_addresses_v4', 'used_addresses_v6', 'free_addresses_v4', 'free_addresses_v6'): vrf[val] = unicode(vrf[val]) return res except (AuthError, NipapError) as exc: self.logger.debug(unicode(exc)) raise Fault(exc.error_code, unicode(exc))
def edit_vrf(self, args): """ Edit a VRF. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `vrf` [struct] A VRF spec specifying which VRF(s) to edit. * `attr` [struct] VRF attributes. """ try: res = self.nip.edit_vrf(args.get('auth'), args.get('vrf'), args.get('attr')) # fugly cast from large numbers to string to deal with XML-RPC for vrf in res: for val in ('num_prefixes_v4', 'num_prefixes_v6', 'total_addresses_v4', 'total_addresses_v6', 'used_addresses_v4', 'used_addresses_v6', 'free_addresses_v4', 'free_addresses_v6'): vrf[val] = unicode(vrf[val]) return res except (AuthError, NipapError) as exc: self.logger.debug(unicode(exc)) raise Fault(exc.error_code, unicode(exc))
def search_prefix(self, args): """ Search for prefixes. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `query` [struct] A struct specifying the search query. * `search_options` [struct] Options for the search query, such as limiting the number of results returned. Returns a struct containing the search result together with the search options used. Certain values are casted from numbers to strings because XML-RPC simply cannot handle anything bigger than an integer. """ try: res = self.nip.search_prefix(args.get('auth'), args.get('query'), args.get('search_options') or {}) # mangle result for prefix in res['result']: prefix = _mangle_prefix(prefix) return res except (AuthError, NipapError) as exc: self.logger.debug(unicode(exc)) raise Fault(exc.error_code, unicode(exc))
def search_vrf(self, args): """ Search for VRFs. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `query` [struct] A struct specifying the search query. * `search_options` [struct] Options for the search query, such as limiting the number of results returned. Returns a struct containing search result and the search options used. """ try: res = self.nip.search_vrf(args.get('auth'), args.get('query'), args.get('search_options') or {}) # fugly cast from large numbers to string to deal with XML-RPC for vrf in res['result']: for val in ('num_prefixes_v4', 'num_prefixes_v6', 'total_addresses_v4', 'total_addresses_v6', 'used_addresses_v4', 'used_addresses_v6', 'free_addresses_v4', 'free_addresses_v6'): vrf[val] = str(vrf[val]) return res except (AuthError, NipapError), e: raise Fault(e.error_code, str(e))
def list_prefix(self, args): """ List prefixes. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `prefix` [struct] Prefix attributes to match. Returns a list of structs describing the matching prefixes. Certain values are casted from numbers to strings because XML-RPC simply cannot handle anything bigger than an integer. """ try: res = self.nip.list_prefix(args.get('auth'), args.get('prefix') or {}) # mangle result for prefix in res: prefix = _mangle_prefix(prefix) return res except (AuthError, NipapError) as exc: self.logger.debug(unicode(exc)) raise Fault(exc.error_code, unicode(exc))
def smart_search_prefix(self, args): """ Perform a smart search. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `query_string` [string] The search string. * `search_options` [struct] Options for the search query, such as limiting the number of results returned. * `extra_query` [struct] Extra search terms, will be AND:ed together with what is extracted from the query string. Returns a struct containing search result, interpretation of the query string and the search options used. Certain values are casted from numbers to strings because XML-RPC simply cannot handle anything bigger than an integer. """ try: res = self.nip.smart_search_prefix( args.get('auth'), args.get('query_string'), args.get('search_options') or {}, args.get('extra_query')) # mangle result for prefix in res['result']: prefix = _mangle_prefix(prefix) return res except (AuthError, NipapError) as exc: self.logger.debug(unicode(exc)) raise Fault(exc.error_code, unicode(exc))
def list_prefix(self, args): """ List prefixes. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `prefix` [struct] Prefix attributes to match. Returns a list of structs describing the matching prefixes. Certain values are casted from numbers to strings because XML-RPC simply cannot handle anything bigger than an integer. """ try: res = self.nip.list_prefix(args.get('auth'), args.get('prefix') or {}) # fugly cast from large numbers to string to deal with XML-RPC for pref in res: pref['total_addresses'] = str(pref['total_addresses']) pref['used_addresses'] = str(pref['used_addresses']) pref['free_addresses'] = str(pref['free_addresses']) return res except (AuthError, NipapError), e: raise Fault(e.error_code, str(e))
def smart_search_asn(self, args): """ Perform a smart search among ASNs. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `query_string` [string] The search string. * `search_options` [struct] Options for the search query, such as limiting the number of results returned. Returns a struct containing search result, interpretation of the search string and the search options used. """ try: return self.nip.smart_search_asn(args.get('auth'), args.get('query_string'), args.get('search_options') or {}, args.get('extra_query')) except (AuthError, NipapError) as exc: self.logger.debug(unicode(exc)) raise Fault(exc.error_code, unicode(exc))
def add_prefix(self, args): """ Add a prefix. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `attr` [struct] Attributes to set on the new prefix. * `args` [srgs] Arguments for addition of prefix, such as what pool or prefix it should be allocated from. Returns ID of created prefix. """ try: res = self.nip.add_prefix(args.get('auth'), args.get('attr'), args.get('args')) # fugly cast from large numbers to string to deal with XML-RPC res['total_addresses'] = str(res['total_addresses']) res['used_addresses'] = str(res['used_addresses']) res['free_addresses'] = str(res['free_addresses']) return res except (AuthError, NipapError), e: raise Fault(e.error_code, str(e))
def smart_search_pool(self, args): """ Perform a smart search. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `query` [string] The search string. * `search_options` [struct] Options for the search query, such as limiting the number of results returned. Returns a struct containing search result, interpretation of the query string and the search options used. """ try: res = self.nip.smart_search_pool(args.get('auth'), args.get('query_string'), args.get('search_options') or {}, args.get('extra_query', {})) # fugly cast from large numbers to string to deal with XML-RPC for pool in res['result']: for val in ('member_prefixes_v4', 'member_prefixes_v6', 'used_prefixes_v4', 'used_prefixes_v6', 'total_addresses_v4', 'total_addresses_v6', 'used_addresses_v4', 'used_addresses_v6', 'free_addresses_v4', 'free_addresses_v6'): pool[val] = str(pool[val]) return res except (AuthError, NipapError), e: raise Fault(e.error_code, str(e))
def list_pool(self, args): """ List pools. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `pool` [struct] Specifies pool attributes which will be matched. Returns a list of structs describing the matching pools. """ try: res = self.nip.list_pool(args.get('auth'), args.get('pool')) # fugly cast from large numbers to string to deal with XML-RPC for pool in res: for val in ('member_prefixes_v4', 'member_prefixes_v6', 'used_prefixes_v4', 'used_prefixes_v6', 'total_addresses_v4', 'total_addresses_v6', 'used_addresses_v4', 'used_addresses_v6', 'free_addresses_v4', 'free_addresses_v6'): pool[val] = str(pool[val]) return res except (AuthError, NipapError), e: raise Fault(e.error_code, str(e))
def get_data(host_name): dc = DataCollector.query.filter( DataCollector.host_name == host_name).first() if not dc: raise Fault("HostNameError", "Host name %s does not exists in database!" % host_name) return "%s data in %s!" % (len(dc.data.all()), dc.host_name)
def write(user_id, password, model, odoo_id, vals): _check_user(user_id, password) if model not in ["customer", "product"]: raise Fault("unknown_model", "Reference model does not exist!") if model == "customer": try: customer = Customer.get(Customer.odoo_id == odoo_id) except Customer.DoesNotExist: raise Fault("unknown_registry", "Customer not found!") q = Customer.update(**vals).where(Customer.odoo_id == odoo_id) else: try: product = Product.get(Product.odoo_id == odoo_id) except Product.DoesNotExist: raise Fault("unknown_registry", "Product not found!") q = Product.update(**vals).where(Product.odoo_id == odoo_id) q.execute() return True
def create(user_id, password, model, vals): _check_user(user_id, password) odoo_id = vals["odoo_id"] if model not in MODELS_CLASS.keys(): raise Fault("unknown_model", "Reference model does not exist!") mod_class = MODELS_CLASS[model] parse_many2one_vals(mod_class, vals) mod_class.create(**vals) return True
def write(user_id, password, model, odoo_id, vals): _check_user(user_id, password) if model not in MODELS_CLASS.keys(): raise Fault("unknown_model", "Reference model does not exist!") mod_class = MODELS_CLASS[model] try: reg = mod_class.get(mod_class.odoo_id == odoo_id) except mod_class.DoesNotExist: raise Fault("unknown_registry", "%s not found!" % model) parse_many2one_vals(mod_class, vals) for field_name in vals.keys(): value = vals[field_name] if isinstance(value, basestring): value = value.replace('"', '\\"') value = '"%s"' % value exec('reg.%s = %s' % (field_name, value)) reg.save(is_update=True) return True
def remove_vrf(self, args): """ Removes a VRF. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `vrf` [struct] A VRF spec. """ try: self.nip.remove_vrf(args.get('auth'), args.get('vrf')) except (AuthError, NipapError), e: raise Fault(e.error_code, str(e))
def remove_pool(self, args): """ Remove a pool. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `pool` [struct] Specifies what pool(s) to remove. """ try: self.nip.remove_pool(args.get('auth'), args.get('pool')) except (AuthError, NipapError), e: raise Fault(e.error_code, str(e))
def remove_asn(self, args): """ Removes an ASN. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `asn` [integer] An ASN. """ try: self.nip.remove_asn(args.get('auth'), args.get('asn')) except (AuthError, NipapError), e: raise Fault(e.error_code, str(e))
def remove_prefix(self, args): """ Remove a prefix. Valid keys in the `args`-struct: * `auth` [struct] Authentication options passed to the :class:`AuthFactory`. * `prefix` [struct] Attributes used to select what prefix to remove. """ try: return self.nip.remove_prefix(args.get('auth'), args.get('prefix'), args.get('recursive')) except (AuthError, NipapError), e: raise Fault(e.error_code, str(e))
def unlink(user_id, password, model, odoo_id): _check_user(user_id, password) if model not in MODELS_CLASS.keys(): raise Fault("unknown_model", "Reference model does not exist!") mod_class = MODELS_CLASS[model] try: rec = mod_class.get(mod_class.odoo_id == odoo_id) except mod_class.DoesNotExist: pass else: if model == "customer": for invoice in Invoice.select().where( Invoice.partner_id == rec.id): invoice.delete_instance() for rma in Rma.select().where(Rma.partner_id == rec.id): for rma_product in RmaProduct.select().where( RmaProduct.id_rma == rma.id): rma_product.delete_instance() rma.delete_instance() elif model == 'product': for rma_product in RmaProduct.select().where( RmaProduct.product_id == rec.id): rma_product.delete_instance() elif model == 'rma': for rma_product in RmaProduct.select().where( RmaProduct.id_rma == rec.id): rma_product.delete_instance() elif model == 'rmastatus': for rma_product in RmaProduct.select().where( RmaProduct.status_id == rec.id): rma_product.delete_instance() elif model == 'rmastage': for rma in Rma.select().where(Rma.stage_id == rec.id): rma.delete_instance() elif model == 'customertagcustomerrel': for customertagcustomerrel in CustomerTagCustomerRel.select( ).where(CustomerTagCustomerRel.odoo_id == rec.odoo_id): customertagcustomerrel.delete_instance() elif model == 'producttagproductrel': for producttagproductrel in ProductTagProductRel.select().where( ProductTagProductRel.odoo_id == rec.odoo_id): producttagproductrel.delete_instance() elif model == 'order': for order in Order.select().where(Order.partner_id == rec.id): order.delete_instance() rec.delete_instance() return True