This simple project illustrates a specific usage of allocation with an important characteristic: if one of the two allocations fail for a single service, the service will be able to deallocate the resource that successfully got allocated.
In order to handle the de-allocation, we need to save the allocated value as soon as an error raises during another allocation. To do that, we used the AllocatorWrapper
class from the lsm module. This allocator will provide a failsafe mechanism for all the allocators it is feeded in its arguments.
This project has been highly inspired from the example about external allocation. It is in a sense, a simplified version of it. It contains a single module: test_module
. This module contains:
- One service,
VlanService
which needs allocation to populate two of its attributes,north_vlan_id
andsouth_vlan_id
.Each of those attributes also have anentity VlanService extends lsm::ServiceEntity: string name int? north_vlan_id = null int? south_vlan_id = null end
allocation_key
attribute related to it, which will contain the previously mentioned allocation key. - One resource,
VlanDeallocation
which we will only instantiate whenever we are deleting a service, during thedeallocating
state of the service. This entity will always we instantiated with itspurged
attribute set to true, meaning that we will only need to implement theread
anddelete
operation of its handler.We only care about one attribute in this resource:entity VlanDeallocation extends std::PurgeableResource: int vlan_id end
vlan_id
. This is where we pass the vlan id we wish to clean up. - A mock of an external vlan inventory,
VlanPool
(python class), which will allow us to reserve and free vlans easily. This is used by the allocator and the resource handler. - A vlan allocator,
ExternalVlanAllocator
(python class), which will handle the allocation of vlans. It takes two parameters in its constructor: the name of the attribute where to store the vlan, and the name of the attribute where to store the allocation key. - A resource handler,
VlanDeallocation
(python class), which will handles the deallocation.
VLAN_POOL_FILE
. This file must already exist, and should preferably contain a range, conforming to the following format:
{
"range": {
"from": 100,
"to": 200
}
}
If no range is supplied, the range defaults to [0: 0[
. No vlan can then be allocated.