Commit 279ee188 authored by Mark Hewitt's avatar Mark Hewitt
Browse files

Merge branch 'feature/fileservice' into 'main'

file download and info commands

See merge request !9
parents 3e7d39be a5d64cd6
Pipeline #77649 passed with stages
in 4 minutes and 30 seconds
......@@ -28,6 +28,11 @@ For more information, specify the `-h` flag:
Change Log
==========
0.0.27
------
- file download and info commands
0.0.26
------
......
......@@ -12,4 +12,4 @@
""" OSDU command line environment"""
__VERSION__ = "0.0.26"
__VERSION__ = "0.0.27"
# 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.
"""File service commands"""
# 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.
"""CRS constants"""
FILE_DOCUMENTATION = None
FILE_SERVICE_NAME = "File service"
FILE_STATUS_PATH = "readiness_check"
FILE_SWAGGER_PATH = "../swagger-ui.html"
# 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.
"""file download command"""
import click
import requests
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
from osducli.config import CONFIG_FILE_URL
# click entry point
@click.command(cls=CustomClickCommand)
@click.option("-id", "--id", "_id", help="id to download", required=True)
@click.option(
"-p",
"--path",
help="Path to download the file to.",
type=click.Path(file_okay=True, dir_okay=False, writable=True, resolve_path=True),
required=True,
)
@handle_cli_exceptions
@command_with_output(
# "crses[].{Name:aliasNames[0],Authority:baseCRSAuthorityCode.auth,Code:baseCRSAuthorityCode.code,Type:crstype,Source:source}" # noqa: E501 pylint:disable=line-too-long
)
def _click_command(state: State, _id: str, path: str):
"""Download a file"""
return file_download(state, _id, path)
def file_download(state: State, _id: str, path: str):
"""List coordinate referense systems
Args:
state (State): Global state
"""
connection = CliOsduClient(state.config)
json = connection.cli_get_returning_json(CONFIG_FILE_URL, f"files/{_id}/downloadURL")
signed_url = json["SignedUrl"]
with requests.get(signed_url, stream=True) as response:
response.raise_for_status()
with open(path, "wb") as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
return json
# 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.
"""CRS Catalog info command"""
import click
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import handle_cli_exceptions
from osducli.commands.file._const import (
FILE_DOCUMENTATION,
FILE_SERVICE_NAME,
FILE_STATUS_PATH,
FILE_SWAGGER_PATH,
)
from osducli.config import CONFIG_FILE_URL
from osducli.util.service_info import info
# click entry point
@click.command(cls=CustomClickCommand)
@handle_cli_exceptions
@command_with_output(None)
def _click_command(state: State) -> dict:
"""Information about the service"""
return info(
state,
FILE_SERVICE_NAME,
CONFIG_FILE_URL,
FILE_STATUS_PATH,
FILE_SWAGGER_PATH,
FILE_DOCUMENTATION,
)
......@@ -17,16 +17,25 @@ from requests.exceptions import RequestException
from osducli.click_cli import CustomClickCommand, State, command_with_output
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
from osducli.commands.crs._const import (
CRS_CATALOG_SERVICE_NAME,
CRS_CATALOG_STATUS_PATH,
CRS_CONVERTER_SERVICE_NAME,
CRS_CONVERTER_STATUS_PATH,
)
from osducli.commands.entitlements._const import (
ENTITLEMENTS_SERVICE_NAME,
ENTITLEMENTS_STATUS_PATH,
)
from osducli.commands.file._const import FILE_SERVICE_NAME, FILE_STATUS_PATH
from osducli.commands.legal._const import LEGAL_SERVICE_NAME, LEGAL_STATUS_PATH
from osducli.commands.schema._const import SCHEMA_SERVICE_NAME, SCHEMA_STATUS_PATH
from osducli.commands.search._const import SEARCH_SERVICE_NAME, SEARCH_STATUS_PATH
from osducli.commands.unit._const import UNIT_SERVICE_NAME, UNIT_STATUS_PATH
from osducli.commands.workflow._const import WORKFLOW_SERVICE_NAME, WORKFLOW_STATUS_PATH
from osducli.config import (
CONFIG_CRS_CATALOG_URL,
CONFIG_CRS_CONVERTER_URL,
CONFIG_ENTITLEMENTS_URL,
CONFIG_FILE_URL,
CONFIG_LEGAL_URL,
......@@ -60,7 +69,9 @@ def status(state: State): # pylint: disable=unused-argument
connection = CliOsduClient(state.config)
results = []
services = [
("File service", CONFIG_FILE_URL, "readiness_check"),
(CRS_CATALOG_SERVICE_NAME, CONFIG_CRS_CATALOG_URL, CRS_CATALOG_STATUS_PATH),
(CRS_CONVERTER_SERVICE_NAME, CONFIG_CRS_CONVERTER_URL, CRS_CONVERTER_STATUS_PATH),
(FILE_SERVICE_NAME, CONFIG_FILE_URL, FILE_STATUS_PATH),
(ENTITLEMENTS_SERVICE_NAME, CONFIG_ENTITLEMENTS_URL, ENTITLEMENTS_STATUS_PATH),
(LEGAL_SERVICE_NAME, CONFIG_LEGAL_URL, LEGAL_STATUS_PATH),
(SCHEMA_SERVICE_NAME, CONFIG_SCHEMA_URL, SCHEMA_STATUS_PATH),
......
# 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.
"""Tests for OSDU CLI"""
# 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.
"""Test cases for file download"""
import unittest
import mock
from click.testing import CliRunner
from mock import patch
from osducli.__main__ import cli
from osducli.click_cli import State
from osducli.cliclient import CliOsduClient
from osducli.commands.file.download import file_download
from tests.helpers import MockConfig
class StatusTests(unittest.TestCase):
"""Test cases for file download comand"""
# region CLI interface
def test_checkrefs_default_params(self):
"""test cli with required parameters"""
with mock.patch(
"osducli.commands.file.download.file_download", return_value=None
) as mock_file_download:
runner = CliRunner()
print(__file__)
result = runner.invoke(cli, ["file", "download", "-id", "someid", "-p", __file__])
mock_file_download.assert_called_once_with(mock.ANY, mock.ANY, mock.ANY)
self.assertEqual(result.exit_code, 0)
# endregion CLI interface
@patch.object(CliOsduClient, "cli_get_returning_json", side_effect=SystemExit(1))
def test_exit_on_error(self, mock_cli_get_returning_json): # pylint: disable=W0613
"""Test any exit error is propogated"""
state = State()
state.config = MockConfig
with self.assertRaises(SystemExit) as sysexit:
_ = file_download(state, "id", "path")
self.assertEqual(sysexit.exception.code, 1)
if __name__ == "__main__":
import nose2
nose2.main()
# 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.
"""Test cases for file info"""
import unittest
import mock
from click.testing import CliRunner
from osducli.__main__ import cli
from osducli.commands.file._const import (
FILE_DOCUMENTATION,
FILE_SERVICE_NAME,
FILE_STATUS_PATH,
FILE_SWAGGER_PATH,
)
from osducli.config import CONFIG_FILE_URL
class ServiceInfoTests(unittest.TestCase):
"""Test cases for file info"""
def test_info(self):
"""Test json output"""
with mock.patch("osducli.commands.file.info.info", return_value=None) as mock_info:
runner = CliRunner()
result = runner.invoke(cli, ["file", "info"])
mock_info.assert_called_once_with(
mock.ANY,
FILE_SERVICE_NAME,
CONFIG_FILE_URL,
FILE_STATUS_PATH,
FILE_SWAGGER_PATH,
FILE_DOCUMENTATION,
)
self.assertEqual(result.exit_code, 0)
if __name__ == "__main__":
import nose2
nose2.main()
......@@ -41,7 +41,7 @@ class StatusTests(unittest.TestCase):
results = status(state)
self.assertEqual(len(results["results"]), 8) # Return json with 8 entries
self.assertEqual(len(results["results"]), 10) # Return json with 10 entries
for result in results["results"]:
self.assertIsNotNone(result["name"])
self.assertEqual(result["status"], 200)
......@@ -74,7 +74,7 @@ class StatusTests(unittest.TestCase):
status(state)
self.assertEqual(cli_get_patch.call_count, 8) # Backend called 8 times
self.assertEqual(cli_get_patch.call_count, 10) # Backend called 10 times
@params(
(404, ""),
......@@ -95,8 +95,8 @@ class StatusTests(unittest.TestCase):
with LogCapture(level=logging.DEBUG) as log_capture:
results = status(state)
self.assertEqual(len(results["results"]), 8) # Return json with 8 entries
self.assertEqual(len(log_capture.records), 8) # 8 debug log records
self.assertEqual(len(results["results"]), 10) # Return json with 10 entries
self.assertEqual(len(log_capture.records), 10) # 10 debug log records
for result in results["results"]:
self.assertIsNotNone(result["name"])
self.assertEqual(result["status"], status_code)
......@@ -117,8 +117,8 @@ class StatusTests(unittest.TestCase):
results = status(state)
print(log_capture)
self.assertEqual(len(results["results"]), 8) # Return json with 8 entries
self.assertEqual(len(log_capture.records), 8) # 8 debug log records
self.assertEqual(len(results["results"]), 10) # Return json with 10 entries
self.assertEqual(len(log_capture.records), 10) # 10 debug log records
for result in results["results"]:
self.assertIsNotNone(result["name"])
self.assertEqual(result["status"], -1)
......
......@@ -382,6 +382,7 @@ class HelpTextTests(unittest.TestCase):
"crs",
"dataload",
"entitlements",
"file",
"legal",
"list",
"schema",
......@@ -434,6 +435,14 @@ class HelpTextTests(unittest.TestCase):
),
)
self.validate_output(
"osdu file",
commands=(
"download",
"info",
),
)
self.validate_output(
"osdu entitlements groups",
commands=("add", "delete", "members"),
......
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