Mentions légales du service

Skip to content
Snippets Groups Projects
Commit a3d09bb4 authored by Luke Bertot's avatar Luke Bertot
Browse files

New Object/Mangers around vlans nodes and users

parent b988314b
Branches
Tags
1 merge request!6New vlan
......@@ -26,9 +26,17 @@ class VlanNode(RESTObject):
pass
class VlanUser(RESTObject):
pass
class UserVlan(RESTObject):
pass
class Vlan(RESTObject):
_managers = (("nodes", "NodeInVlanManager"),)
_managers = (("nodes", "NodeInVlanManager"), ("users", "UserInVlanManager"))
@exc.on_http_error(exc.Grid5000GetError)
def submit(self, data, **kwargs):
......@@ -99,6 +107,7 @@ class Site(RESTObject):
("storage", "StorageHomeManager"),
("vlans", "VlanManager"),
("vlansnodes", "VlanNodeManager"),
("vlansusers", "VlanUserManager"),
("versions", "SiteVersionManager"),
)
......@@ -245,7 +254,7 @@ class VlanManager(RetrieveMixin, BracketMixin, RESTManager):
_from_parent_attrs = {"site": "uid"}
class VlanNodeManager(ListMixin, RESTManager):
class VlanNodeManager(RetrieveMixin, RESTManager):
_path = "/sites/%(site)s/vlans/nodes"
_obj_cls = VlanNode
_from_parent_attrs = {"site": "uid"}
......@@ -269,24 +278,81 @@ class VlanNodeManager(ListMixin, RESTManager):
class NodeInVlanManager(RESTManager):
"""
Nodes in this list do not have underlying local endpoints.
Instead lists refer to object under the NodeVlanManager's path.
Code functionally equivalent to BracketMixin, RetrieveMixin.
"""
_path = "/sites/%(site)s/vlans/%(vlan_id)s/nodes"
_obj_cls = VlanNode
_from_parent_attrs = {"site": "site", "vlan_id": "uid"}
@exc.on_http_error(exc.Grid5000GetError)
def get(self, id, **kwargs):
"""
Objects in this list do not have endpoint here
Instead one must refer to the items under the VlanNodeManager.
"""
if not isinstance(id, int):
id = id.replace("/", "%2F")
path = self._compute_path("/sites/%(site)s/vlans/nodes/") + id
server_data = self.grid5000.http_get(path, **kwargs)
return self._obj_cls(self, server_data)
def __getitem__(self, key):
return self.get(key)
@exc.on_http_error(exc.Grid5000ListError)
def list(self, **kwargs):
"""
We got this kind of answere (which isn't a list as one could expect)
{
'uid': 'nodes',
'nodes': ['paranoia-8-eth2.rennes.grid5000.fr'],
'links': [...]
}
Items listed under this manager do not contain a vlan field as it does
in other similar VlanNode listings. We add this field of convenience.
"""
# We get our vlan_id from our parent.
if self._parent is None or not hasattr(self, "_from_parent_attrs"):
vlan_id = None
else:
vlan_id = self._parent.get_id()
# Duplicate data to avoid messing with what the user sent us
data = kwargs.copy()
# We get the attributes that need some special transformation
types = getattr(self, "_types", {})
if types:
for attr_name, type_cls in types.items():
if attr_name in data.keys():
type_obj = type_cls(data[attr_name])
data[attr_name] = type_obj.get_for_api()
# Allow to overwrite the path, handy for custom listings
path = data.pop("path", self.path)
server_data = self.grid5000.http_list(path, **data)
return [self._obj_cls(self, {"uid": node}) for node in server_data["nodes"]]
l_obj = self.grid5000.http_list(path, **data)
if isinstance(l_obj, list):
# Add vlan to items
if vlan_id:
for item in l_obj:
item["vlan"] = vlan_id
return [self._obj_cls(self, item) for item in l_obj]
elif isinstance(l_obj, dict):
# Add vlan to object
if vlan_id:
l_obj["vlan"] = vlan_id
return self._obj_cls(self, l_obj)
else:
raise exc.Grid5000ListError("Returned value is neither a list nor a dict")
class VlanUserManager(BracketMixin, RetrieveMixin, RESTManager):
_path = "/sites/%(site)s/vlans/users"
_obj_cls = UserVlan
_from_parent_attrs = {"site": "uid"}
class UserInVlanManager(BracketMixin, RetrieveMixin, RESTManager):
_path = "/sites/%(site)s/vlans/%(vlan_id)s/users"
_obj_cls = VlanUser
_from_parent_attrs = {"site": "site", "vlan_id": "uid"}
class VersionManager(RESTManager, BracketMixin, RetrieveMixin):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment