From 23efe5ede0679829048bb8ce2d43a0a5018a09d1 Mon Sep 17 00:00:00 2001
From: Carles Garcia Cabot <carles.garcia.cabot@cern.ch>
Date: Tue, 10 Mar 2020 13:23:35 +0100
Subject: [PATCH] Migrate archive controller

---
 src/fts3rest/fts3rest/controllers/archive.py | 68 ++++++++++++++++++--
 src/fts3rest/fts3rest/controllers/jobs.py    |  4 +-
 2 files changed, 64 insertions(+), 8 deletions(-)

diff --git a/src/fts3rest/fts3rest/controllers/archive.py b/src/fts3rest/fts3rest/controllers/archive.py
index 8104f558..2d4f5a7f 100644
--- a/src/fts3rest/fts3rest/controllers/archive.py
+++ b/src/fts3rest/fts3rest/controllers/archive.py
@@ -1,13 +1,69 @@
-from werkzeug.exceptions import NotFound
+#   Copyright  Members of the EMI Collaboration, 2013.
+#   Copyright 2020 CERN
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+from werkzeug.exceptions import NotFound, Forbidden
+from flask import jsonify
+
+from fts3.model import ArchivedJob
+from fts3rest.model.meta import Session
+from fts3rest.lib.middleware.fts3auth.authorization import authorized
+from fts3rest.lib.middleware.fts3auth.constants import *
 
 
 def index():
-    raise NotFound
+    """
+    Just give the operations that can be performed
+    """
+    ret = {
+        "_links": {
+            "curies": [{"name": "fts", "href": "https://gitlab.cern.ch/fts/fts3"}],
+            "fts:archivedJob": {
+                "href": "/archive/{id}",
+                "title": "Archived job information",
+                "templated": True,
+            },
+        }
+    }
+    return jsonify(ret)
+
+
+def _get_job(job_id):
+    job = Session.query(ArchivedJob).get(job_id)
+    if job is None:
+        raise NotFound('No job with the id "%s" has been found in the archive' % job_id)
+    if not authorized(TRANSFER, resource_owner=job.user_dn, resource_vo=job.vo_name):
+        raise Forbidden('Not enough permissions to check the job "%s"' % job_id)
+    return job
 
 
-def get():
-    raise NotFound
+def get(job_id):
+    """
+    Get the job with the given ID
+    """
+    job = _get_job(job_id)
+    # Trigger the query, so it is serialized
+    files = job.files
+    return job
 
 
-def get_field():
-    raise NotFound
+def get_field(job_id, field):
+    """
+    Get a specific field from the job identified by id
+    """
+    job = _get_job(job_id)
+    if hasattr(job, field):
+        return getattr(job, field)
+    else:
+        raise NotFound("No such field")
diff --git a/src/fts3rest/fts3rest/controllers/jobs.py b/src/fts3rest/fts3rest/controllers/jobs.py
index 18c536c0..8ff94842 100644
--- a/src/fts3rest/fts3rest/controllers/jobs.py
+++ b/src/fts3rest/fts3rest/controllers/jobs.py
@@ -498,9 +498,9 @@ def modify(job_id_list):
     # Now, modify those that can be
     modification = get_input_as_dict(request)
     priority = None
+    # todo: verify this is correct for the migration
     try:
-        # todo: verify this is correct for the migration
-        priority = int(modification["priority"])
+        priority = int(modification["params"]["priority"])
     except KeyError:
         pass
     except ValueError:
-- 
GitLab