black + suppress ssl warnings

parent 45044dc6
Pipeline #93733 passed with stage
in 3 minutes and 56 seconds
This diff is collapsed.
......@@ -34,10 +34,12 @@ class RESTManager(object):
self._parent_attrs = {}
if path is None:
path = self._path
if self._parent is None or not hasattr(self, '_from_parent_attrs'):
if self._parent is None or not hasattr(self, "_from_parent_attrs"):
return path
data = {self_attr: getattr(self._parent, parent_attr, None)
for self_attr, parent_attr in self._from_parent_attrs.items()}
data = {
self_attr: getattr(self._parent, parent_attr, None)
for self_attr, parent_attr in self._from_parent_attrs.items()
}
self._parent_attrs = data
return path % data
......@@ -56,36 +58,39 @@ class RESTObject(object):
must be used as uniq ID. ``None`` means that the object can be updated
without ID in the url.
"""
_id_attr = 'uid'
_id_attr = "uid"
def __init__(self, manager, attrs):
self.__dict__.update({
'manager': manager,
'_attrs': attrs,
'_updated_attrs': {},
'_module': importlib.import_module(self.__module__)
})
self.__dict__['_parent_attrs'] = self.manager.parent_attrs
self.__dict__.update(
{
"manager": manager,
"_attrs": attrs,
"_updated_attrs": {},
"_module": importlib.import_module(self.__module__),
}
)
self.__dict__["_parent_attrs"] = self.manager.parent_attrs
self._create_managers()
def __getstate__(self):
state = self.__dict__.copy()
# module can't be pickled
module = state.pop('_module')
state['_module_name'] = module.__name__
module = state.pop("_module")
state["_module_name"] = module.__name__
return state
def __setstate__(self, state):
module_name = state.pop('_module_name')
module_name = state.pop("_module_name")
self.__dict__.update(state)
self.__dict__.update(_module=importlib.import_module(module_name))
def __getattr__(self, name):
try:
return self.__dict__['_updated_attrs'][name]
return self.__dict__["_updated_attrs"][name]
except KeyError:
try:
value = self.__dict__['_attrs'][name]
value = self.__dict__["_attrs"][name]
# If the value is a list, we copy it in the _updated_attrs dict
# because we are not able to detect changes made on the object
......@@ -98,35 +103,37 @@ class RESTObject(object):
# note: _parent_attrs will only store simple values (int) so we
# don't make this check in the next except block.
if isinstance(value, list):
self.__dict__['_updated_attrs'][name] = value[:]
return self.__dict__['_updated_attrs'][name]
self.__dict__["_updated_attrs"][name] = value[:]
return self.__dict__["_updated_attrs"][name]
return value
except KeyError:
try:
return self.__dict__['_parent_attrs'][name]
return self.__dict__["_parent_attrs"][name]
except KeyError:
raise AttributeError(name)
def __setattr__(self, name, value):
self.__dict__['_updated_attrs'][name] = value
self.__dict__["_updated_attrs"][name] = value
def __str__(self):
data = self._attrs.copy()
data.update(self._updated_attrs)
return '%s => \n %s' % (type(self), json.dumps(data, indent=4))
return "%s => \n %s" % (type(self), json.dumps(data, indent=4))
def __repr__(self):
if self._id_attr:
return '<%s %s:%s>' % (self.__class__.__name__,
self._id_attr,
self.get_id())
return "<%s %s:%s>" % (
self.__class__.__name__,
self._id_attr,
self.get_id(),
)
else:
return '<%s>' % self.__class__.__name__
return "<%s>" % self.__class__.__name__
def _create_managers(self):
managers = getattr(self, '_managers', None)
managers = getattr(self, "_managers", None)
if managers is None:
return
......@@ -136,8 +143,8 @@ class RESTObject(object):
self.__dict__[attr] = manager
def _update_attrs(self, new_attrs):
self.__dict__['_updated_attrs'] = {}
self.__dict__['_attrs'].update(new_attrs)
self.__dict__["_updated_attrs"] = {}
self.__dict__["_attrs"].update(new_attrs)
def get_id(self):
"""Returns the id of the resource."""
......@@ -147,7 +154,7 @@ class RESTObject(object):
@property
def attributes(self):
d = self.__dict__['_updated_attrs'].copy()
d.update(self.__dict__['_attrs'])
d.update(self.__dict__['_parent_attrs'])
d = self.__dict__["_updated_attrs"].copy()
d.update(self.__dict__["_attrs"])
d.update(self.__dict__["_parent_attrs"])
return d
......@@ -7,7 +7,8 @@ import IPython
CONF_PATH = os.path.join(os.environ.get("HOME"), ".python-grid5000.yaml")
motd=r"""
motd = (
r"""
__ __
____ __ __/ /_/ /_ ____ ____
/ __ \/ / / / __/ __ \/ __ \/ __ \
......@@ -25,7 +26,9 @@ motd=r"""
# Example: Get all available sites
$) gk.sites.list()
""" % CONF_PATH
"""
% CONF_PATH
)
def main():
......
......@@ -2,8 +2,7 @@ import functools
class Grid5000Error(Exception):
def __init__(self, error_message="", response_code=None,
response_body=None):
def __init__(self, error_message="", response_code=None, response_body=None):
Exception.__init__(self, error_message)
# Http status code
......@@ -75,6 +74,7 @@ def on_http_error(error):
error(Exception): The exception type to raise -- must inherit from
Grid5000Error
"""
def wrap(f):
@functools.wraps(f)
def wrapped_f(*args, **kwargs):
......@@ -82,5 +82,7 @@ def on_http_error(error):
return f(*args, **kwargs)
except Grid5000HttpError as e:
raise error(e.error_message, e.response_code, e.response_body)
return wrapped_f
return wrap
......@@ -18,8 +18,8 @@ class GetMixin(object):
Grid5000GetError: If the server cannot perform the request
"""
if not isinstance(id, int):
id = id.replace('/', '%2F')
path = '%s/%s' % (self.path, id)
id = id.replace("/", "%2F")
path = "%s/%s" % (self.path, id)
server_data = self.grid5000.http_get(path, **kwargs)
return self._obj_cls(self, server_data)
......@@ -60,7 +60,7 @@ class RefreshMixin(object):
Grid5000GetError: If the server cannot perform the request
"""
if self._id_attr:
path = '%s/%s' % (self.manager.path, self.get_id())
path = "%s/%s" % (self.manager.path, self.get_id())
else:
path = self.manager.path
server_data = self.manager.grid5000.http_get(path, **kwargs)
......@@ -91,7 +91,7 @@ class ListMixin(object):
# 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', {})
types = getattr(self, "_types", {})
if types:
for attr_name, type_cls in types.items():
if attr_name in data.keys():
......@@ -99,7 +99,7 @@ class ListMixin(object):
data[attr_name] = type_obj.get_for_api()
# Allow to overwrite the path, handy for custom listings
path = data.pop('path', self.path)
path = data.pop("path", self.path)
l_obj = self.grid5000.http_list(path, **data)
if isinstance(l_obj, list):
......@@ -107,8 +107,7 @@ class ListMixin(object):
elif isinstance(l_obj, dict):
return self._obj_cls(self, l_obj)
else:
raise exc.Grid5000ListError(
"Returned value is neither a list nor a dict")
raise exc.Grid5000ListError("Returned value is neither a list nor a dict")
class CreateMixin(object):
......@@ -129,7 +128,7 @@ class CreateMixin(object):
tuple: 2 items: list of required arguments and list of optional
arguments for creation (in that order)
"""
return getattr(self, '_create_attrs', (tuple(), tuple()))
return getattr(self, "_create_attrs", (tuple(), tuple()))
@exc.on_http_error(exc.Grid5000CreateError)
def create(self, data, **kwargs):
......@@ -151,8 +150,7 @@ class CreateMixin(object):
self._check_missing_create_attrs(data)
# Handle specific URL for creation
server_data = self.grid5000.http_post(self.path, post_data=data,
**kwargs)
server_data = self.grid5000.http_post(self.path, post_data=data, **kwargs)
return self._obj_cls(self, server_data)
......@@ -173,8 +171,8 @@ class DeleteMixin(object):
path = self.path
else:
if not isinstance(id, int):
id = id.replace('/', '%2F')
path = '%s/%s' % (self.path, id)
id = id.replace("/", "%2F")
path = "%s/%s" % (self.path, id)
self.grid5000.http_delete(path, **kwargs)
......@@ -182,13 +180,14 @@ class BracketMixin(object):
@exc.on_http_error(exc.Grid5000GetError)
def __getitem__(self, key):
"""Because that reminds me about RESTfully."""
path = '%s/%s' % (self.path, key)
path = "%s/%s" % (self.path, key)
server_data = self.grid5000.http_get(path)
return self._obj_cls(self, server_data)
class ObjectDeleteMixin(object):
"""Mixin for RESTObject's that can be deleted."""
def delete(self, **kwargs):
"""Delete the object from the server.
......
from .base import * # noqa
from .mixins import * # noqa
from .base import * # noqa
from .mixins import * # noqa
class Server(RESTObject):
......@@ -20,9 +20,7 @@ class VlanNode(RESTObject):
class Vlan(RESTObject):
_managers = (
('nodes', 'NodeInVlanManager'),
)
_managers = (("nodes", "NodeInVlanManager"),)
@exc.on_http_error(exc.Grid5000GetError)
def submit(self, data, **kwargs):
......@@ -32,8 +30,8 @@ class Vlan(RESTObject):
class Deployment(RESTObject, RefreshMixin):
_create_attrs = (
('nodes', 'environment'),
('ssh_authorized_keys', 'version', 'client', 'custom_operations')
("nodes", "environment"),
("ssh_authorized_keys", "version", "client", "custom_operations"),
)
......@@ -42,92 +40,83 @@ class StorageHomeUser(RESTObject):
class StorageHome(RESTObject):
_managers = (
('access', 'StorageHomeUserManager'),
)
_managers = (("access", "StorageHomeUserManager"),)
class Node(RESTObject):
_managers = (
('versions', 'NodeVersionManager'),
)
_managers = (("versions", "NodeVersionManager"),)
class Cluster(RESTObject):
_managers = (
('nodes', 'NodeManager'),
('status', 'ClusterStatusManager'),
('versions', 'ClusterVersionManager')
("nodes", "NodeManager"),
("status", "ClusterStatusManager"),
("versions", "ClusterVersionManager"),
)
class Job(RESTObject, RefreshMixin, ObjectDeleteMixin):
_create_attrs = (
("command"),
)
_create_attrs = (("command"),)
def __repr__(self):
keys = ['uid', 'site', 'state', 'user']
keys = ["uid", "site", "state", "user"]
try:
_repr = ["%s:%s" % (k, getattr(self, k)) for k in keys]
return '<%s %s>' % (self.__class__.__name__, ' '.join(_repr))
return "<%s %s>" % (self.__class__.__name__, " ".join(_repr))
except:
return super().__repr__()
return super().__repr__()
class Site(RESTObject):
_managers = (
('clusters', 'ClusterManager'),
('deployments', 'DeploymentManager'),
('jobs', 'JobManager'),
('servers', 'ServerManager'),
('status', 'SiteStatusManager'),
('storage', 'StorageHomeManager'),
('vlans', 'VlanManager'),
('vlansnodes', 'VlanNodeManager'),
('versions', 'SiteVersionManager')
("clusters", "ClusterManager"),
("deployments", "DeploymentManager"),
("jobs", "JobManager"),
("servers", "ServerManager"),
("status", "SiteStatusManager"),
("storage", "StorageHomeManager"),
("vlans", "VlanManager"),
("vlansnodes", "VlanNodeManager"),
("versions", "SiteVersionManager"),
)
class Root(RESTObject):
_managers = (
('sites', 'SiteManager'),
('versions', 'VersionManager')
)
_managers = (("sites", "SiteManager"), ("versions", "VersionManager"))
class RootManager(GetWithoutIdMixin, RESTManager):
_path = '/'
_path = "/"
_obj_cls = Root
class SiteManager(RetrieveMixin, BracketMixin, RESTManager):
_path = '/sites'
_path = "/sites"
_obj_cls = Site
class JobManager(NoUpdateMixin, BracketMixin, RESTManager):
_path = '/sites/%(site)s/jobs'
_path = "/sites/%(site)s/jobs"
_obj_cls = Job
_from_parent_attrs = {'site': 'uid'}
_from_parent_attrs = {"site": "uid"}
class ClusterManager(RetrieveMixin, BracketMixin, RESTManager):
_path = '/sites/%(site)s/clusters'
_path = "/sites/%(site)s/clusters"
_obj_cls = Cluster
_from_parent_attrs = {'site': 'uid'}
_from_parent_attrs = {"site": "uid"}
class NodeManager(RetrieveMixin, BracketMixin, RESTManager):
_path = '/sites/%(site)s/clusters/%(cluster)s/nodes'
_path = "/sites/%(site)s/clusters/%(cluster)s/nodes"
_obj_cls = Node
_from_parent_attrs = {'site': 'site', 'cluster': 'uid'}
_from_parent_attrs = {"site": "site", "cluster": "uid"}
class StorageHomeManager(RESTManager):
_path = '/sites/%(site)s/storage/home'
_path = "/sites/%(site)s/storage/home"
_obj_cls = StorageHome
_from_parent_attrs = {'site': 'uid'}
_from_parent_attrs = {"site": "uid"}
# NOTE(msimonin): grr ... need to fix the return values because it's not
# consistent with the rest of API
......@@ -138,9 +127,9 @@ class StorageHomeManager(RESTManager):
class StorageHomeUserManager(RESTManager):
_path = '/sites/%(site)s/storage/home/%(user)s/access'
_path = "/sites/%(site)s/storage/home/%(user)s/access"
_obj_cls = StorageHomeUser
_from_parent_attrs = {'site': 'site', 'user': 'uid'}
_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
......@@ -214,51 +203,49 @@ class StorageHomeUserManager(RESTManager):
# self._check_missing_create_attrs(data)
# Handle specific URL for creation
server_data = self.grid5000.http_post(self.path, post_data=data,
**kwargs)
server_data = self.grid5000.http_post(self.path, post_data=data, **kwargs)
return self._obj_cls(self, server_data)
class DeploymentManager(NoUpdateMixin, BracketMixin, RESTManager):
_path = '/sites/%(site)s/deployments'
_path = "/sites/%(site)s/deployments"
_obj_cls = Deployment
_from_parent_attrs = {'site': 'uid'}
_from_parent_attrs = {"site": "uid"}
class VlanManager(RetrieveMixin, BracketMixin, RESTManager):
_path = '/sites/%(site)s/vlans'
_path = "/sites/%(site)s/vlans"
_obj_cls = Vlan
_from_parent_attrs = {'site': 'uid'}
_from_parent_attrs = {"site": "uid"}
class VlanNodeManager(ListMixin, RESTManager):
_path = '/sites/%(site)s/vlans/nodes'
_path = "/sites/%(site)s/vlans/nodes"
_obj_cls = VlanNode
_from_parent_attrs = {'site': 'uid'}
_from_parent_attrs = {"site": "uid"}
@exc.on_http_error(exc.Grid5000GetError)
def submit(self, data, **kwargs):
def _to_kavlan(node, vlan):
n = node.split('.')
n[0] = '%s-kavlan-%s' % (n[0], vlan)
return '.'.join(n)
n = node.split(".")
n[0] = "%s-kavlan-%s" % (n[0], vlan)
return ".".join(n)
server_data = self.grid5000.http_post(self.path,
post_data=data,
**kwargs)
server_data = self.grid5000.http_post(self.path, post_data=data, **kwargs)
# this returns a dict k=node, v=vlanid
# we generate the kavlan fqdn as uid
return [self._obj_cls(self, {'uid': _to_kavlan(node, vlan),
'node': node,
'vlan': vlan})
for node, vlan in server_data.items()]
return [
self._obj_cls(
self, {"uid": _to_kavlan(node, vlan), "node": node, "vlan": vlan}
)
for node, vlan in server_data.items()
]
class NodeInVlanManager(RESTManager):
_path = '/sites/%(site)s/vlans/%(vlan_id)s/nodes'
_path = "/sites/%(site)s/vlans/%(vlan_id)s/nodes"
_obj_cls = VlanNode
_from_parent_attrs = {'site': 'site', 'vlan_id': 'uid'}
_from_parent_attrs = {"site": "site", "vlan_id": "uid"}
@exc.on_http_error(exc.Grid5000ListError)
def list(self, **kwargs):
......@@ -271,48 +258,47 @@ class NodeInVlanManager(RESTManager):
}
"""
data = kwargs.copy()
path = data.pop('path', self.path)
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']]
return [self._obj_cls(self, {"uid": node}) for node in server_data["nodes"]]
class VersionManager(RESTManager, BracketMixin, RetrieveMixin):
_path = '/versions'
_path = "/versions"
_obj_cls = Version
class SiteVersionManager(RESTManager, BracketMixin, RetrieveMixin):
_path = '/sites/%(site)s/versions'
_path = "/sites/%(site)s/versions"
_obj_cls = Version
_from_parent_attrs = {'site': 'uid'}
_from_parent_attrs = {"site": "uid"}
class ClusterVersionManager(RESTManager, BracketMixin, RetrieveMixin):
_path = '/sites/%(site)s/clusters/%(cluster)s/versions'
_path = "/sites/%(site)s/clusters/%(cluster)s/versions"
_obj_cls = Version
_from_parent_attrs = {'site': 'site', 'cluster': 'uid'}
_from_parent_attrs = {"site": "site", "cluster": "uid"}
class NodeVersionManager(RESTManager, BracketMixin, RetrieveMixin):
_path = '/sites/%(site)s/clusters/%(cluster)s/nodes/%(node)s/versions'
_path = "/sites/%(site)s/clusters/%(cluster)s/nodes/%(node)s/versions"
_obj_cls = Version
_from_parent_attrs = {'site': 'site', 'cluster': 'cluster', 'node': 'uid'}
_from_parent_attrs = {"site": "site", "cluster": "cluster", "node": "uid"}
class SiteStatusManager(RESTManager, RetrieveMixin):
_path = '/sites/%(site)s/status'
_path = "/sites/%(site)s/status"
_obj_cls = Status
_from_parent_attrs = {'site': 'uid'}
_from_parent_attrs = {"site": "uid"}
class ClusterStatusManager(RESTManager, RetrieveMixin):
_path = '/sites/%(site)s/clusters/%(cluster)s/status'
_path = "/sites/%(site)s/clusters/%(cluster)s/status"
_obj_cls = Status
_from_parent_attrs = {'site': 'site', 'cluster': 'uid'}
_from_parent_attrs = {"site": "site", "cluster": "uid"}
class ServerManager(RESTManager, BracketMixin, RetrieveMixin):
_path = '/sites/%(site)s/servers'
_path = "/sites/%(site)s/servers"
_obj_cls = Server
_from_parent_attrs = {'site': 'uid'}
_from_parent_attrs = {"site": "uid"}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment