Пример #1
0
    def edit(self, id):
        """ Edit a schema
        """

        if 'schema' in request.params:
            c.schema = Schema.get(int(request.params['schema']))

        c.action = 'edit'
        c.edit_schema = Schema.get(int(id))

        # Did we have any action passed to us?
        if 'action' in request.params:

            if request.params['action'] == 'edit':
                c.edit_schema.name = request.params['name']
                c.edit_schema.description = request.params['description']
                if request.params['vrf'].strip() == '':
                    c.edit_schema.vrf = None
                else:
                    c.edit_schema.vrf = request.params['vrf']
                c.edit_schema.save()

                if hasattr(c, 'schema'):
                    r_url = url(controller='schema', action='list', schema=c.schema.id)
                else:
                    r_url = url(controller='schema', action='list')

                redirect(r_url)

        return render('/schema_edit.html')
Пример #2
0
    def list(self):
        """ List schemas.
        """

        if 'schema' in request.params:
            c.schema = Schema.get(int(request.params['schema']))

        c.schemas = Schema.list()
        return render('/schema_list.html')
Пример #3
0
    def add_prefix(self):
        """ Add prefix according to the specification.

            The following keys can be used:

            schema          Schema to which the prefix is to be added (mandatory)
            prefix          the prefix to add if already known
            family          address family (4 or 6)
            description     A short description
            comment         Longer comment
            node            Hostname of node
            type            Type of prefix; reservation, assignment, host
            pool            ID of pool
            country         Country where the prefix is used
            order_id        Order identifier
            vrf             VRF
            alarm_priority  Alarm priority of prefix
            monitor         If the prefix should be monitored or not

            from-prefix     A prefix the prefix is to be allocated from
            from-pool       A pool (ID) the prefix is to be allocated from
            prefix_length   Prefix length of allocated prefix
        """

        p = Prefix()

        # parameters which are "special cases"
        try:
            p.schema = Schema.get(int(request.params['schema']))
        except NipapError, e:
            return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
Пример #4
0
    def index(self):

        if 'schema' not in request.params:
            redirect(url(controller = 'schema', action = 'list'))
        schema = Schema.get(int(request.params['schema']))

        redirect(url(controller = 'pool', action = 'list', schema = schema.id))
Пример #5
0
    def add(self):
        """ Add a pool.
        """

        if 'schema' not in request.params:
            redirect(url(controller = 'schema', action = 'list'))
        c.schema = Schema.get(int(request.params['schema']))

        # Adding to NIPAP
        if request.method == 'POST':
            p = Pool()
            p.schema = c.schema
            p.name = request.params.get('name')
            p.description = request.params.get('description')
            p.default_type = request.params.get('default_type')

            if request.params['ipv4_default_prefix_length'].strip() != '':
                p.ipv4_default_prefix_length = request.params['ipv4_default_prefix_length']

            if request.params['ipv6_default_prefix_length'].strip() != '':
                p.ipv6_default_prefix_length = request.params['ipv6_default_prefix_length']

            p.save()
            redirect(url(controller = 'pool', action = 'list', schema = c.schema.id))

        return render("/pool_add.html")
Пример #6
0
    def edit(self, id):
        """ Edit a pool.
        """

        if 'schema' not in request.params:
            redirect(url(controller = 'schema', action = 'list'))
        c.schema = Schema.get(int(request.params['schema']))

        c.pool = Pool.get(c.schema, int(id))
        c.prefix_list = Prefix.list(c.schema, {'pool': c.pool.id})

        # save changes to NIPAP
        if request.method == 'POST':
            c.pool.name = request.params['name']
            c.pool.description = request.params['description']
            c.pool.default_type = request.params['default_type']
            if request.params['ipv4_default_prefix_length'].strip() == '':
                c.pool.ipv4_default_prefix_length = None
            else:
                c.pool.ipv4_default_prefix_length = request.params['ipv4_default_prefix_length']
            if request.params['ipv6_default_prefix_length'].strip() == '':
                c.pool.ipv6_default_prefix_length = None
            else:
                c.pool.ipv6_default_prefix_length = request.params['ipv6_default_prefix_length']
            c.pool.save()
            redirect(url(controller = 'pool', action = 'list', schema = c.schema.id))

        c.search_opt_parent = 'all'
        c.search_opt_child = 'none'

        return render("/pool_edit.html")
Пример #7
0
def get_schema(arg = None, opts = None):
    """ Returns schema to work in

        Returns a pynipap.Schema object representing the schema we are working
        in. If there is a schema set globally, return this. If not, fetch the
        schema named 'arg'. If 'arg' is None, fetch the default_schema
        attribute from the config file and return this schema.
    """

    # yep, global variables are evil
    global schema

    # if there is a schema set, return it
    if schema is not None:
        return schema

    if arg is None:
        # fetch default schema
        try:
            schema_name = cfg.get('global', 'default_schema')
        except ConfigParser.NoOptionError:
            print >> sys.stderr, "Please define the default schema in your .nipaprc"
            sys.exit(1)
    else:
        schema_name = arg

    try:
        schema = Schema.list({ 'name': schema_name })[0]
    except IndexError:
        schema = False

    return schema
Пример #8
0
    def smart_search_pool(self):
        """ Perform a smart pool search.

            The "smart" search function tries extract a query from
            a text string. This query is then passed to the search_pool
            function, which performs the search.
        """

        search_options = {}

        if 'query_id' in request.params:
            search_options['query_id'] = request.params['query_id']

        if 'max_result' in request.params:
            search_options['max_result'] = request.params['max_result']
        if 'offset' in request.params:
            search_options['offset'] = request.params['offset']

        log.debug("params: %s" % str(request.params))

        log.debug("Smart search query: schema=%d q=%s search_options=%s" %
            (int(request.params['schema']),
            request.params['query_string'],
            str(search_options)
        ))

        try:
            schema = Schema.get(int(request.params['schema']))
            result = Pool.smart_search(schema,
                request.params['query_string'],
                search_options
                )
        except NipapError, e:
            return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
Пример #9
0
    def list_schema(self):
        """ List schemas and return JSON encoded result.
        """

        try:
            schemas = Schema.list()
        except NipapError, e:
            return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
Пример #10
0
    def remove(self, id):
        """ Removes a schema.
        """

        s = Schema.get(int(id))
        s.remove()

        redirect(url(controller='schema', action='list'))
Пример #11
0
    def edit_schema(self, id):
        """ Edit a schema.
        """

        try:
            s = Schema.get(int(id))
        except NipapError, e:
            return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
Пример #12
0
    def edit_pool(self, id):
        """ Edit a pool.
        """

        try:
            c.schema = Schema.get(int(request.params.get('schema')))
        except NipapError, e:
            return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
Пример #13
0
    def edit(self, id):
        """ Edit a prefix.
        """

        # make sure we have a schema
        try:
            c.schema = Schema.get(int(request.params['schema']))
        except (KeyError, NipapNonExistentError), e:
            redirect(url(controller = 'schema', action = 'list'))
Пример #14
0
    def list_pool(self):
        """ List pools and return JSON encoded result.
        """

        try:
            schema = Schema.get(int(request.params['schema']))
            pools = Pool.list(schema)
        except NipapError, e:
            return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
Пример #15
0
    def list(self):
        """ Prefix list.
        """

        # Handle schema in session.
        if 'schema' in request.params:
            try:
                c.schema = Schema.get(int(request.params['schema']))
            except NipapNonExistentError, e:
                redirect(url(controller = 'schema', action = 'list'))
Пример #16
0
    def add(self):
        """ Add a prefix.
        """

        # make sure we have a schema
        try:
            c.schema = Schema.get(int(request.params['schema']))
            c.pools = Pool.list(c.schema)
        except (KeyError, NipapNonExistentError), e:
            redirect(url(controller = 'schema', action = 'list'))
Пример #17
0
    def list(self):
        """ Displays a list of pools.
        """

        if 'schema' not in request.params:
            redirect(url(controller = 'schema', action = 'list'))
        c.schema = Schema.get(int(request.params['schema']))

        c.pools = Pool.list(c.schema)

        return render('/pool_list.html')
Пример #18
0
    def remove(self, id):
        """ Remove pool.
        """

        if 'schema' not in request.params:
            redirect(url(controller = 'schema', action = 'list'))
        schema = Schema.get(int(request.params['schema']))

        p = Pool.get(schema, int(id))
        p.remove()
        redirect(url(controller = 'pool', action = 'list', schema = schema.id))
Пример #19
0
    def remove_prefix(self):
        """ Remove a prefix.
        """

        try:
            schema = Schema.get(int(request.params['schema']))
            p = Prefix.get(schema, int(request.params['id']))
            p.remove()

        except NipapError, e:
            return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
Пример #20
0
    def __init__(self, url, schema_name):
        self._logger = logging.getLogger(self.__class__.__name__)

        import pynipap
        pynipap.xmlrpc_uri = url

        # import nap class and stuff
        sl = Schema.list({ 'name': schema_name })
        try:
            sl = Schema.list({ 'name': schema_name })
        except:
            # TODO: handle this?
            pass

        if len(sl) == 0:
            raise Exception("Non-existant schema specified: " + schema_name)

        s = sl[0]

        self.schema = Schema.get(int(s.id))
Пример #21
0
    def __init__(self, url, schema_name):
        self._logger = logging.getLogger(self.__class__.__name__)

        import pynipap
        pynipap.xmlrpc_uri = url

        # import nap class and stuff
        sl = Schema.list({ 'name': schema_name })
        try:
            sl = Schema.list({ 'name': schema_name })
        except:
            # TODO: handle this?
            pass

        if len(sl) == 0:
            raise Exception("Non-existant schema specified: " + schema_name)

        s = sl[0]

        self.schema = Schema.get(int(s.id))
Пример #22
0
    def list_prefix(self):
        """ List prefixes and return JSON encoded result.
        """

        # fetch attributes from request.params
        attr = XhrController.extract_prefix_attr(request.params)

        try:
            schema = Schema.get(int(request.params['schema']))
            prefixes = Prefix.list(schema, attr)
        except NipapError, e:
            return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
Пример #23
0
    def remove_prefix(self, id):
        """ Remove a prefix from pool 'id'.
        """

        if 'schema' not in request.params:
            redirect(url(controller = 'schema', action = 'list'))
        schema = Schema.get(int(request.params['schema']))

        if 'prefix' not in request.params:
            abort(400, 'Missing prefix.')
        prefix = Prefix.get(schema, int(request.params['prefix']))
        prefix.pool = None
        prefix.save()

        redirect(url(controller = 'pool', action = 'edit', id = id, schema = schema.id))
Пример #24
0
    def smart_search_prefix(self):
        """ Perform a smart search.

            The smart search function tries extract a query from
            a text string. This query is then passed to the search_prefix
            function, which performs the search.
        """

        search_options = {}

        if 'query_id' in request.params:
            search_options['query_id'] = request.params['query_id']

        if 'include_all_parents' in request.params:
            if request.params['include_all_parents'] == 'true':
                search_options['include_all_parents'] = True
            else:
                search_options['include_all_parents'] = False
                
        if 'include_all_children' in request.params:
            if request.params['include_all_children'] == 'true':
                search_options['include_all_children'] = True
            else:
                search_options['include_all_children'] = False

        if 'parents_depth' in request.params:
            search_options['parents_depth'] = request.params['parents_depth']
        if 'children_depth' in request.params:
            search_options['children_depth'] = request.params['children_depth']
        if 'max_result' in request.params:
            search_options['max_result'] = request.params['max_result']
        if 'offset' in request.params:
            search_options['offset'] = request.params['offset']

        log.debug("Smart search query: schema=%s q=%s search_options=%s" %
            (str(request.params.get('schema')),
            request.params.get('query_string'),
            str(search_options)
        ))

        try:
            schema = Schema.get(int(request.params['schema']))
            result = Prefix.smart_search(schema,
                request.params['query_string'],
                search_options
                )
        except NipapError, e:
            return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
Пример #25
0
def view_schema(arg, opts):
    """ View a single schema
    """

    res = Schema.list({ 'name': arg })
    if len(res) < 1:
        print >> sys.stderr, "No schema with name %s found." % arg
        sys.exit(1)

    s = res[0]

    print "-- Schema"
    print "  %-12s : %s" % ("Name", s.name)
    print "  %-12s : %d" % ("ID", s.id)
    print "  %-12s : %s" % ("Description", s.description)
    print "  %-12s : %s" % ("VRF", s.vrf)
Пример #26
0
def list_schema(arg, opts):
    """ List schemas matching a search criteria
    """

    query = _expand_list_query(opts)
    res = Schema.search(query)
    if len(res['result']) > 0:
        print "%-17s %-45s %-16s" % ("Name", "Description", "VRF")
        print "--------------------------------------------------------------------------------"
    else:
        print "No matching schemas found."

    for s in res['result']:
        if len(s.description) > 45:
            desc = s.description[0:42] + "..."
        else:
            desc = s.description
        print "%-17s %-45s %-16s" % (s.name, desc, s.vrf)
Пример #27
0
    def add_prefix(self, id):
        """ Add a prefix to pool 'id'
        """

        if 'schema' not in request.params:
            redirect(url(controller = 'schema', action = 'list'))
        schema = Schema.get(int(request.params['schema']))

        if 'prefix' not in request.params:
            abort(400, 'Missing prefix.')

        pool = Pool.get(schema, int(id))

        prefix = Prefix.get(schema, int(request.params['prefix']))
        prefix.pool = pool
        prefix.save()

        redirect(url(controller = 'pool', action = 'edit', id = id, schema = schema.id))
Пример #28
0
def complete_schema_name(arg):
    """ Returns list of matching schema names
    """

    search_string = ''
    if arg is not None:
        search_string = '^%s' % arg

    res = Schema.search({
        'operator': 'regex_match',
        'val1': 'name',
        'val2':  search_string
        })

    ret = []
    for schema in res['result']:
        ret.append(schema.name)

    return ret
Пример #29
0
def add_schema(arg, opts):
    """ Add schema to NIPAP
    """

    s = Schema()
    s.name = opts.get('name')
    s.description = opts.get('description')
    s.vrf = opts.get('vrf')

    try:
        s.save()
    except pynipap.NipapError, e:
        print >> sys.stderr, "Could not add schema to NIPAP: %s" % e.message
        sys.exit(1)
Пример #30
0
def remove_schema(arg, opts):
    """ Remove schema
    """

    res = Schema.list({ 'name': arg })
    if len(res) < 1:
        print >> sys.stderr, "No schema with name %s found." % arg
        sys.exit(1)

    s = res[0]

    print "Name: %s\nDescription: %s\nVRF: %s" % (s.name, s.description, s.vrf)
    print "\nWARNING: THIS WILL REMOVE THE SCHEMA INCLUDING ALL IT'S ADDRESSES"
    res = raw_input("Do you really want to remove the schema %s? [y/n]: " % s.name)

    if res == 'y':
        s.remove()
        print "Schema %s removed." % s.name
    else:
        print "Operation canceled."
Пример #31
0
    def add_schema(self):
        """ Add a new schema to NIPAP and returns its data.
        """

        s = Schema()
        if 'name' in request.params:
            s.name = request.params['name']
        if 'description' in request.params:
            s.description = request.params['description']
        if 'vrf' in request.params:
            if request.params['vrf'].strip() == '':
                s.vrf = None
            else:
                s.vrf = request.params['vrf']

        try:
            s.save()
        except NipapError, e:
            return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})