From 3a446ec394f0d38b33c611cdcd2773fd931afd39 Mon Sep 17 00:00:00 2001
From: msimonin <matthieu.simonin@inria.fr>
Date: Wed, 6 Oct 2021 16:30:33 +0200
Subject: [PATCH] Add programmable firewall interface

---
 grid5000/mixins.py  |  2 +-
 grid5000/objects.py | 25 +++++++++++++++++++++++--
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/grid5000/mixins.py b/grid5000/mixins.py
index e56438a..adbb6b5 100644
--- a/grid5000/mixins.py
+++ b/grid5000/mixins.py
@@ -156,7 +156,7 @@ class CreateMixin(object):
 
 class DeleteMixin(object):
     @exc.on_http_error(exc.Grid5000DeleteError)
-    def delete(self, id, **kwargs):
+    def delete(self, id=None, **kwargs):
         """Delete an object on the server.
 
         Args:
diff --git a/grid5000/objects.py b/grid5000/objects.py
index 225ca06..54a566c 100644
--- a/grid5000/objects.py
+++ b/grid5000/objects.py
@@ -2,6 +2,9 @@ from .base import *  # noqa
 from .mixins import *  # noqa
 
 
+class FirewallPort(RESTObject):
+    _id_attr = None
+
 class NetworkEquipment(RESTObject):
     pass
 
@@ -72,6 +75,11 @@ class Cluster(RESTObject):
 class Job(RESTObject, RefreshMixin, ObjectDeleteMixin):
     _create_attrs = (("command"),)
 
+    _managers=(
+        ("firewall", "SiteJobFirewallManager"),
+    )
+
+
     def __repr__(self):
         keys = ["uid", "site", "state", "user"]
         try:
@@ -134,8 +142,6 @@ class JobManager(NoUpdateMixin, BracketMixin, RESTManager):
     _path = "/sites/%(site)s/jobs"
     _obj_cls = Job
     _from_parent_attrs = {"site": "uid"}
-
-
 class ClusterManager(RetrieveMixin, BracketMixin, RESTManager):
     _path = "/sites/%(site)s/clusters"
     _obj_cls = Cluster
@@ -376,3 +382,18 @@ class SiteNetworkEquipmentManager(BracketMixin, RetrieveMixin, RESTManager):
     _path = "/sites/%(site)s/network_equipments"
     _obj_cls = NetworkEquipment
     _from_parent_attrs = {"site": "uid"}
+
+
+class SiteJobFirewallManager(ListMixin, DeleteMixin, CreateMixin, RESTManager):
+    _path = "/sites/%(site)s/firewall/%(jobid)s"
+    _obj_cls = FirewallPort
+    _from_parent_attrs = {"site": "site", "jobid": "uid"}
+
+    @exc.on_http_error(exc.Grid5000CreateError)
+    def create(self, data, **kwargs):
+        # The api answer with a list of firewall rules
+        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, s) for s in server_data]
\ No newline at end of file
-- 
GitLab