本文整理汇总了Python中pynipap.Prefix类的典型用法代码示例。如果您正苦于以下问题:Python Prefix类的具体用法?Python Prefix怎么用?Python Prefix使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Prefix类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_stats3
def test_stats3(self):
""" Check stats are correct when shrinking prefix
"""
th = TestHelper()
# add a top level prefix
p1 = th.add_prefix('1.0.0.0/24', 'assignment', 'test')
p2 = th.add_prefix('1.0.7.0/24', 'assignment', 'test')
# add a covering supernet around p1 and p2
p3 = th.add_prefix('1.0.0.0/21', 'reservation', 'bar')
# check that p3 looks good
res = Prefix.smart_search('1.0.0.0/21', {})
self.assertEqual(2048, res['result'][0].total_addresses)
self.assertEqual(512, res['result'][0].used_addresses)
self.assertEqual(1536, res['result'][0].free_addresses)
# now move our supernet, so we see that the update thingy works
p3.prefix = '1.0.0.0/22'
p3.save()
# check that p3 only covers p1
res = Prefix.smart_search('1.0.0.0/22', {})
self.assertEqual(1024, res['result'][0].total_addresses)
self.assertEqual(256, res['result'][0].used_addresses)
self.assertEqual(768, res['result'][0].free_addresses)
示例2: test_add_prefix
def test_add_prefix(self):
""" We should NOT be able to execute add_prefix as read-only user
"""
p = Prefix()
p.prefix = '1.3.3.7'
with self.assertRaises(NipapAuthorizationError):
p.save()
示例3: test_children8
def test_children8(self):
""" Remove prefix and check old parent is correctly updated
"""
th = TestHelper()
# p1 children are p2 (which covers p3 and p4) and p5
p1 = th.add_prefix('1.0.0.0/20', 'reservation', 'test')
p2 = th.add_prefix('1.0.0.0/22', 'reservation', 'test')
p3 = th.add_prefix('1.0.0.0/24', 'reservation', 'test')
p4 = th.add_prefix('1.0.1.0/24', 'reservation', 'test')
p5 = th.add_prefix('1.0.7.0/24', 'reservation', 'test')
# moving p2 means that p1 get p3, p4 and p5 as children
p2.prefix = '2.0.0.0/22'
p2.save()
# check stats for p1
res = Prefix.smart_search('1.0.0.0/20', {})
self.assertEqual(3, res['result'][0].children)
# moving back p2 which means that p1 get p2 and p5 as children
p2.prefix = '1.0.0.0/22'
p2.save()
# check stats for p1
res = Prefix.smart_search('1.0.0.0/20', {})
self.assertEqual(2, res['result'][0].children)
示例4: test_remove_prefix
def test_remove_prefix(self):
""" We should NOT be able to execute remove_prefix as read-only user
"""
p = Prefix()
p.id = 0
with self.assertRaises(NipapAuthorizationError):
p.remove()
示例5: test_children9
def test_children9(self):
""" Move prefix several indent steps and check children is correct
"""
th = TestHelper()
# tree of prefixes
p1 = th.add_prefix('1.0.0.0/20', 'reservation', 'test')
p2 = th.add_prefix('1.0.0.0/21', 'reservation', 'test')
p3 = th.add_prefix('1.0.0.0/22', 'reservation', 'test')
p4 = th.add_prefix('1.0.0.0/23', 'reservation', 'test')
p5 = th.add_prefix('1.0.0.0/24', 'reservation', 'test')
p6 = th.add_prefix('1.0.2.0/24', 'reservation', 'test')
p7 = th.add_prefix('1.0.4.0/22', 'reservation', 'test')
# check stats for p2
res = Prefix.smart_search('1.0.0.0/21', {})
self.assertEqual(2, res['result'][0].children)
# move p3 outside of the tree
p3.prefix = '2.0.0.0/22'
p3.save()
# check stats for p2
res = Prefix.smart_search('1.0.0.0/21', {})
self.assertEqual(3, res['result'][0].children)
# move p3 into the tree again
p3.prefix = '1.0.0.0/22'
p3.save()
# check stats for p2
res = Prefix.smart_search('1.0.0.0/21', {})
self.assertEqual(2, res['result'][0].children)
示例6: test_tags1
def test_tags1(self):
""" Verify tags are correctly inherited
"""
th = TestHelper()
# add to "top level" prefix, each with a unique tag
p1 = th.add_prefix('1.0.0.0/8', 'reservation', 'test', tags=['a'])
p2 = th.add_prefix('1.0.0.0/9', 'reservation', 'test')
p3 = th.add_prefix('1.0.0.0/10', 'reservation', 'test')
# p3 should have inherited_tags = ['a'] from p1
res = Prefix.smart_search('1.0.0.0/10', {})
self.assertEqual(['a'], res['result'][0].inherited_tags.keys())
p4 = th.add_prefix('1.0.0.0/24', 'reservation', 'test')
p5 = th.add_prefix('1.0.0.0/23', 'reservation', 'test')
p6 = th.add_prefix('1.0.0.0/22', 'reservation', 'test')
# p4 should have inherited_tags = ['a'] from p1
res = Prefix.smart_search('1.0.0.0/24', {})
self.assertEqual(['a'], res['result'][0].inherited_tags.keys())
# change tags on top level prefix
p1.tags = ['b']
p1.save()
# p4 should have inherited_tags = ['a'] from p1
res = Prefix.smart_search('1.0.0.0/8', {})
self.assertEqual([], res['result'][0].inherited_tags.keys())
self.assertEqual(['b'], res['result'][1].inherited_tags.keys())
self.assertEqual(['b'], res['result'][2].inherited_tags.keys())
self.assertEqual(['b'], res['result'][3].inherited_tags.keys())
self.assertEqual(['b'], res['result'][4].inherited_tags.keys())
self.assertEqual(['b'], res['result'][5].inherited_tags.keys())
示例7: test_edit_prefix
def test_edit_prefix(self):
""" We should NOT be able to execute edit_prefix as read-only user
"""
p = Prefix()
p.id = 123
with self.assertRaises(NipapAuthorizationError):
p.save()
示例8: test_stats7
def test_stats7(self):
""" Enlarge / shrink prefix over several indent levels
"""
th = TestHelper()
# p1 children are p2 (which covers p3 and p4) and p5
p1 = th.add_prefix('1.0.0.0/16', 'reservation', 'test')
p2 = th.add_prefix('1.0.0.0/22', 'reservation', 'test')
p3 = th.add_prefix('1.0.0.0/23', 'reservation', 'FOO')
p4 = th.add_prefix('1.0.0.0/24', 'reservation', 'test')
p5 = th.add_prefix('1.0.1.0/24', 'reservation', 'test')
p6 = th.add_prefix('1.0.2.0/24', 'reservation', 'test')
p7 = th.add_prefix('1.0.3.0/24', 'reservation', 'test')
# enlarge p3 so that it covers p2, ie moved up several indent levels
p3.prefix = '1.0.0.0/21'
p3.save()
# check stats for p3
res = Prefix.smart_search('1.0.0.0/21', {})
self.assertEqual(2048, res['result'][0].total_addresses)
self.assertEqual(1024, res['result'][0].used_addresses)
self.assertEqual(1024, res['result'][0].free_addresses)
# move back p3
p3.prefix = '1.0.0.0/23'
p3.save()
# check stats for p3
res = Prefix.smart_search('1.0.0.0/23', {})
self.assertEqual(512, res['result'][0].total_addresses)
self.assertEqual(512, res['result'][0].used_addresses)
self.assertEqual(0, res['result'][0].free_addresses)
示例9: test_stats5
def test_stats5(self):
""" Add prefixes within other prefix and verify parent prefix has correct statistics
"""
th = TestHelper()
# add a top level prefix
p1 = th.add_prefix('1.0.0.0/24', 'assignment', 'test')
# check stats for p1
res = Prefix.smart_search('1.0.0.0/24', {})
self.assertEqual(256, res['result'][0].total_addresses)
self.assertEqual(0, res['result'][0].used_addresses)
self.assertEqual(256, res['result'][0].free_addresses)
# add a host in our top prefix
p2 = th.add_prefix('1.0.0.1/32', 'host', 'bar')
# check stats for p1, our top level prefix
res = Prefix.smart_search('1.0.0.0/24', {})
self.assertEqual(256, res['result'][0].total_addresses)
self.assertEqual(1, res['result'][0].used_addresses)
self.assertEqual(255, res['result'][0].free_addresses)
# check stats for p2, our new host prefix
res = Prefix.smart_search('1.0.0.1/32', {})
self.assertEqual(1, res['result'][0].total_addresses)
self.assertEqual(1, res['result'][0].used_addresses)
self.assertEqual(0, res['result'][0].free_addresses)
示例10: add_prefix
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__})
示例11: add_prefix_from_pool
def add_prefix_from_pool(self, pool, family, description):
p = Prefix()
args = {}
args['from-pool'] = pool
args['family'] = family
p.type = pool.default_type
p.save(args)
return p
示例12: edit
def edit(self, id):
""" Edit a pool.
"""
c.pool = Pool.get(int(id))
c.prefix_list = Prefix.list({ 'pool_id': c.pool.id })
c.prefix = ''
# 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'))
c.search_opt_parent = 'all'
c.search_opt_child = 'none'
return render("/pool_edit.html")
示例13: test_prefix_edit
def test_prefix_edit(self):
""" Verify indent is correct after prefix edit
"""
th = TestHelper()
# add a few prefixes
p1 = th.add_prefix('192.168.0.0/16', 'reservation', 'test')
p2 = th.add_prefix('192.168.0.0/24', 'reservation', 'test')
p3 = th.add_prefix('192.168.1.0/24', 'reservation', 'test')
# now edit the "middle prefix" so that it now covers 192.168.1.0/24
p3.prefix = '192.168.0.0/20'
p3.save()
expected = []
# expected result is a list of list, each row is a prefix, first value is prefix, next is indent level
# notice how p2 and p3 switch places efter the edit
expected.append([p1.prefix, 0])
expected.append([p3.prefix, 1])
expected.append([p2.prefix, 2])
res = Prefix.smart_search('0.0.0.0/0', {})
result = []
for prefix in res['result']:
result.append([prefix.prefix, prefix.indent])
self.assertEqual(expected, result)
示例14: search_prefix
def search_prefix(self):
""" Search prefixes. Does not yet incorporate all the functions of the
search_prefix API function due to difficulties with transferring
a complete 'dict-to-sql' encoded data structure.
Instead, a list of prefix attributes can be given which will be
matched with the 'equals' operator if notheing else is specified. If
multiple attributes are given, they will be combined with the 'and'
operator. Currently, it is not possible to specify different
operators for different attributes.
"""
# extract operator
if 'operator' in request.json:
operator = request.json['operator']
else:
operator = 'equals'
# fetch attributes from request.json
attr = XhrController.extract_prefix_attr(request.json)
# build query dict
n = 0
q = {}
for key, val in attr.items():
if n == 0:
q = {
'operator': operator,
'val1': key,
'val2': val
}
else:
q = {
'operator': 'and',
'val1': {
'operator': operator,
'val1': key,
'val2': val
},
'val2': q
}
n += 1
# extract search options
search_opts = {}
if 'children_depth' in request.json:
search_opts['children_depth'] = request.json['children_depth']
if 'parents_depth' in request.json:
search_opts['parents_depth'] = request.json['parents_depth']
if 'include_neighbors' in request.json:
search_opts['include_neighbors'] = request.json['include_neighbors']
if 'max_result' in request.json:
search_opts['max_result'] = request.json['max_result']
if 'offset' in request.json:
search_opts['offset'] = request.json['offset']
try:
result = Prefix.search(q, search_opts)
except NipapError, e:
return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
示例15: view_prefix
def view_prefix(arg, opts):
""" View a single prefix.
"""
s = get_schema()
res = Prefix.search(s, { 'operator': 'equals', 'val1': 'prefix', 'val2': arg }, {})
if len(res['result']) == 0:
print "Address %s not found." % arg
return
p = res['result'][0]
print "-- Address "
print " %-15s : %s" % ("Prefix", p.prefix)
print " %-15s : %s" % ("Display prefix", p.display_prefix)
print " %-15s : %s" % ("Type", p.type)
print " %-15s : IPv%s" % ("Family", p.family)
print " %-15s : %s" % ("Description", p.description)
print " %-15s : %s" % ("Node", p.node)
print " %-15s : %s" % ("Order", p.order_id)
print " %-15s : %s" % ("VRF", p.vrf)
print " %-15s : %s" % ("Alarm priority", p.alarm_priority)
print " %-15s : %s" % ("Monitor", p.monitor)
print "-- Comment"
print p.comment