Commit 9040a9db authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Added endpoint for providing url and commands for upload. Removed per upload commands/urls.

parent 57392d52
...@@ -21,6 +21,7 @@ from flask import g, request ...@@ -21,6 +21,7 @@ from flask import g, request
from flask_restplus import Resource, fields, abort from flask_restplus import Resource, fields, abort
from datetime import datetime from datetime import datetime
from nomad import config
from nomad.processing import Upload from nomad.processing import Upload
from nomad.processing import NotAllowedDuringProcessing from nomad.processing import NotAllowedDuringProcessing
from nomad.utils import get_logger from nomad.utils import get_logger
...@@ -60,8 +61,6 @@ upload_model = api.inherit('UploadProcessing', proc_model, { ...@@ -60,8 +61,6 @@ upload_model = api.inherit('UploadProcessing', proc_model, {
'used within nomad to identify uploads.' 'used within nomad to identify uploads.'
), ),
'additional_metadata': fields.Arbitrary, 'additional_metadata': fields.Arbitrary,
'upload_url': fields.String,
'upload_command': fields.String,
'local_path': fields.String, 'local_path': fields.String,
'upload_time': fields.DateTime(dt_format='iso8601'), 'upload_time': fields.DateTime(dt_format='iso8601'),
}) })
...@@ -298,3 +297,26 @@ class UploadResource(Resource): ...@@ -298,3 +297,26 @@ class UploadResource(Resource):
return upload, 200 return upload, 200
abort(400, message='Unsuported operation %s.' % operation) abort(400, message='Unsuported operation %s.' % operation)
upload_command_model = api.model('UploadCommand', {
'upload_url': fields.Url,
'upload_command': fields.String
})
@ns.route('/command')
class UploadCommandResource(Resource):
@api.marshal_with(upload_command_model, code=200, description='Upload command send')
@login_really_required
def get(self):
""" Get url and example command for shell based uploads. """
upload_url = 'http://%s:%s%s/uploads/' % (
config.services.api_host,
config.services.api_port,
config.services.api_base_path)
upload_command = 'curl -H "X-Token: "%s" "%s" --upload-file <local_file>' % (
g.user.get_auth_token().decode('utf-8'), upload_url)
return dict(upload_url=upload_url, upload_command=upload_command), 200
...@@ -28,12 +28,11 @@ from typing import List, Any, ContextManager, Tuple, Generator ...@@ -28,12 +28,11 @@ from typing import List, Any, ContextManager, Tuple, Generator
from elasticsearch.exceptions import NotFoundError from elasticsearch.exceptions import NotFoundError
from mongoengine import StringField, BooleanField, DateTimeField, DictField, IntField from mongoengine import StringField, BooleanField, DateTimeField, DictField, IntField
import logging import logging
import base64
import time import time
from structlog import wrap_logger from structlog import wrap_logger
from contextlib import contextmanager from contextlib import contextmanager
from nomad import config, utils, coe_repo from nomad import utils, coe_repo
from nomad.files import UploadFile, ArchiveFile, ArchiveLogFile, File from nomad.files import UploadFile, ArchiveFile, ArchiveLogFile, File
from nomad.repo import RepoCalc from nomad.repo import RepoCalc
from nomad.processing.base import Proc, Chord, process, task, PENDING, SUCCESS, FAILURE, RUNNING from nomad.processing.base import Proc, Chord, process, task, PENDING, SUCCESS, FAILURE, RUNNING
...@@ -325,8 +324,6 @@ class Upload(Chord): ...@@ -325,8 +324,6 @@ class Upload(Chord):
upload_hash = StringField(default=None) upload_hash = StringField(default=None)
user_id = StringField(required=True) user_id = StringField(required=True)
upload_url = StringField(default=None)
upload_command = StringField(default=None)
coe_repo_upload_id = IntField(default=None) coe_repo_upload_id = IntField(default=None)
...@@ -386,13 +383,6 @@ class Upload(Chord): ...@@ -386,13 +383,6 @@ class Upload(Chord):
with lnr(logger, 'deleting upload'): with lnr(logger, 'deleting upload'):
super().delete() super().delete()
@classmethod
def _external_objects_url(cls, url):
""" Replaces the given internal object storage url with an URL that allows
external access.
"""
return 'http://%s:%s%s%s' % (config.services.api_host, config.services.api_port, config.services.api_base_path, url)
@classmethod @classmethod
def create(cls, **kwargs) -> 'Upload': def create(cls, **kwargs) -> 'Upload':
""" """
...@@ -410,12 +400,6 @@ class Upload(Chord): ...@@ -410,12 +400,6 @@ class Upload(Chord):
kwargs.update(user_id=str(user.user_id)) kwargs.update(user_id=str(user.user_id))
self = super().create(**kwargs) self = super().create(**kwargs)
basic_auth_token = base64.b64encode(b'%s:' % user.get_auth_token()).decode('utf-8')
self.upload_url = cls._external_objects_url('/uploads/%s/file' % self.upload_id)
self.upload_command = 'curl -H "Authorization: Basic %s" "%s" --upload-file local_file' % (
basic_auth_token, self.upload_url)
self._continue_with('uploading') self._continue_with('uploading')
return self return self
......
...@@ -115,8 +115,6 @@ class TestUploads: ...@@ -115,8 +115,6 @@ class TestUploads:
if id is not None: if id is not None:
assert id == data['upload_id'] assert id == data['upload_id']
assert 'create_time' in data assert 'create_time' in data
assert 'upload_url' in data
assert 'upload_command' in data
for key, value in kwargs.items(): for key, value in kwargs.items():
assert data.get(key, None) == value assert data.get(key, None) == value
...@@ -170,6 +168,13 @@ class TestUploads: ...@@ -170,6 +168,13 @@ class TestUploads:
self.assert_uploads(rv.data, count=0) self.assert_uploads(rv.data, count=0)
assert_coe_upload(upload['upload_hash'], proc_infra['repository_db'], empty=empty_upload) assert_coe_upload(upload['upload_hash'], proc_infra['repository_db'], empty=empty_upload)
def test_get_command(self, client, test_user_auth, no_warn):
rv = client.get('/uploads/command', headers=test_user_auth)
assert rv.status_code == 200
data = json.loads(rv.data)
assert 'upload_command' in data
assert 'upload_url' in data
def test_get_empty(self, client, test_user_auth, no_warn): def test_get_empty(self, client, test_user_auth, no_warn):
rv = client.get('/uploads/', headers=test_user_auth) rv = client.get('/uploads/', headers=test_user_auth)
......
Supports Markdown
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