def address_create(): context = get_context() payload = bottle.request.json payload.pop('idnr', None) address = extract_payload_fields(payload, 'address')[0] vlan_tag = payload.pop('vlan', 0) if vlan_tag: try: vlan = context.service.vlan.lookup(vlan_tag) except exc.ServiceLookupError: vlan = model.VLAN(vlan_tag) context.service.vlan.create(vlan) iface = vlan.iface else: iface = None try: entity = model.IpAddress(address, iface=iface, **payload) except TypeError as e: return bottle.HTTPError(400, str(e)) context.service.address.create(entity) bottle.response.status = 201 return address_response(entity)
def _allocate_stack(self, stack): if not stack: return try: vlan = self.lookup(stack) except exc.ServiceLookupError: parent = self._allocate_stack(stack[:-1]) vlan = model.VLAN(stack, parent=parent) vlan = self.create(vlan) return vlan
def test_create_partial_stack(self): ipdb = self.service.get_ipdb() first = model.VLAN(2) self.service.create(first) self.assertEqual(1, ipdb.create.call_count) first_ip_iface = self.interfaces[first.iface.name] second = self.service.allocate_stack((2, 3)) self.assertEqual(2, ipdb.create.call_count) # first interface must not be recreated self.assertIs(first_ip_iface, self.interfaces[first.iface.name]) self.verify_vlan_create( second, 3, 'vlan.2.3', parent_iface_name='vlan.2')
def test_create(self): # create VLAN tag 1 iface target_vlan = model.VLAN(1) self.service.create(target_vlan) self.verify_vlan_create(target_vlan, 1, 'vlan.1')