diff --git a/grid5000/objects.py b/grid5000/objects.py index d6891ef4847687a9aa160bd440c51f893c063068..e22d1c06d952a39b0ce8fa25d31d90ff0da66cd2 100644 --- a/grid5000/objects.py +++ b/grid5000/objects.py @@ -2,6 +2,16 @@ from .base import * # noqa from .mixins import * # noqa +class StorageHomeUser(RESTObject): + pass + + +class StorageHome(RESTObject): + _managers = ( + ('access', 'StorageHomeUserManager'), + ) + + class Node(RESTObject): pass @@ -22,6 +32,7 @@ class Site(RESTObject): _managers = ( ('jobs', 'JobManager'), ('clusters', 'ClusterManager'), + ('storage', 'StorageHomeManager'), ) @@ -57,3 +68,98 @@ class NodeManager(RetrieveMixin, BracketMixin, RESTManager): _path = '/sites/%(site)s/clusters/%(cluster)s/nodes' _obj_cls = Node _from_parent_attrs = {'site': 'site', 'cluster': 'uid'} + + +class StorageHomeManager(RESTManager): + _path = '/sites/%(site)s/storage/home' + _obj_cls = StorageHome + _from_parent_attrs = {'site': 'uid'} + + # NOTE(msimonin): grr ... need to fix the return values because it's not + # consistent with the rest of API + # So we fake a return value + @exc.on_http_error(exc.Grid5000GetError) + def __getitem__(self, key): + return self._obj_cls(self, {"uid": key}) + + +class StorageHomeUserManager(RESTManager): + _path = '/sites/%(site)s/storage/home/%(user)s/access' + _obj_cls = StorageHomeUser + _from_parent_attrs = {'site': 'site', 'user': 'uid'} + + # NOTE(msimonin): grr ... need to fix the return values because it's not + # consistent with the rest of API + @exc.on_http_error(exc.Grid5000GetError) + def list(self, **kwargs): + """Retrieve a list of objects. + + The return value of the GET method is a dict: + { + "G5k-home_jpicard_j_1666466-nancy_1": { + "ipv4": [ + "172.16.64.97" + ], + "termination": { + "job": 1666466, + "site": "nancy" + }, + "nfs_address": "srv-data.nancy.grid5000.fr:/export/home/jpicard" + }, + "G5k-home_jpicard_u_1535456240_1": { + "ipv4": [ + "172.16.64.16" + ], + "termination": { + "until": 1535456240, + }, + "nfs_address": "srv-data.nancy.grid5000.fr:/export/home/jpicard" + } + } + We'd prefer having a list, so we inject an uid in the responses: + [ + { + "uid": "G5k-home_jpicard_j_1666466-nancy_1" + "ipv4": [ + "172.16.64.97" + ], + "termination": { + "job": 1666466, + "site": "nancy" + }, + "nfs_address": "srv-data.nancy.grid5000.fr:/export/home/jpicard" + }, + ] + """ + l_objs = self.grid5000.http_get(self.path) + _objs = [] + for uid, access in l_objs.items(): + _obj = access + _obj.update(uid=uid) + _objs.append(_obj) + return [self._obj_cls(self, _obj) for _obj in _objs] + + @exc.on_http_error(exc.Grid5000CreateError) + def create(self, data, **kwargs): + """Create a new object. + + Args: + data (dict): parameters to send to the server to create the + resource + **kwargs: Extra options to send to the server + + Returns: + RESTObject: a new instance of the managed object class built with + the data sent by the server + + Raises: + Grid5000AuthenticationError: If authentication is not correct + Grid5000CreateError: If the server cannot perform the request + """ + + # self._check_missing_create_attrs(data) + + # Handle specific URL for creation + server_data = self.grid5000.http_post(self.path, post_data=data, + **kwargs) + return self._obj_cls(self, server_data)