Commit bba9a46a authored by Yannick's avatar Yannick
Browse files

wdms_client.cli

parent 639e6651
Pipeline #71791 failed with stages
in 21 seconds
......@@ -28,7 +28,7 @@ integration_test_base_path = str(pathlib.Path(__file__).parent.absolute())
sys.path.append(integration_test_base_path) # set the current directory into the python path
# put after sys.path update
from .variables import Variables, CmdLineSpecialVar
from wdms_client.variables import Variables, CmdLineSpecialVar
from .tests.fixtures import WDMS_Variables
FILTER_IN_TAGS = set()
......
# Copyright 2021 Schlumberger
#
# 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 typing import Union, List
from .wdms import about
from .wdms import version
from .wdms import status
from .wdms import crud
from .wdms import error_cases
from .wdms import model_extensibility
from .wdms import recursive_delete
from .wdms import search_apis
def build_request(path: Union[str, List[str]], sep: str = ".") -> "RequestRunner":
path = path.split(sep) if isinstance(path, str) else path
n_path = ".".join([p.lower().replace(" ", "_") for p in path])
if n_path == "crud.well.get_well":
return crud.well.build_request_get_well()
if n_path == "crud.well.delete_well":
return crud.well.build_request_delete_well()
if n_path == "crud.well.get_well_specific_version":
return crud.well.build_request_get_well_specific_version()
if n_path == "crud.well.get_versions_of_well":
return crud.well.build_request_get_versions_of_well()
if n_path == "crud.well.create_well":
return crud.well.build_request_create_well()
if n_path == "crud.wellbore.delete_wellbore":
return crud.wellbore.build_request_delete_wellbore()
if n_path == "crud.wellbore.get_wellbore_specific_version":
return crud.wellbore.build_request_get_wellbore_specific_version()
if n_path == "crud.wellbore.get_wellbore":
return crud.wellbore.build_request_get_wellbore()
if n_path == "crud.wellbore.get_versions_of_wellbore":
return crud.wellbore.build_request_get_versions_of_wellbore()
if n_path == "crud.wellbore.create_wellbore":
return crud.wellbore.build_request_create_wellbore()
if n_path == "crud.osdu_wellbore.delete_osdu_wellbore":
return crud.osdu_wellbore.build_request_delete_osdu_wellbore()
if n_path == "crud.osdu_wellbore.get_osdu_wellbore_specific_version":
return crud.osdu_wellbore.build_request_get_osdu_wellbore_specific_version()
if n_path == "crud.osdu_wellbore.get_osdu_wellbore":
return crud.osdu_wellbore.build_request_get_osdu_wellbore()
if n_path == "crud.osdu_wellbore.get_versions_of_osdu_wellbore":
return crud.osdu_wellbore.build_request_get_versions_of_osdu_wellbore()
if n_path == "crud.osdu_wellbore.create_osdu_wellbore":
return crud.osdu_wellbore.build_request_create_osdu_wellbore()
if n_path == "crud.osdu_well.delete_osdu_well":
return crud.osdu_well.build_request_delete_osdu_well()
if n_path == "crud.osdu_well.get_osdu_well_specific_version":
return crud.osdu_well.build_request_get_osdu_well_specific_version()
if n_path == "crud.osdu_well.get_osdu_well":
return crud.osdu_well.build_request_get_osdu_well()
if n_path == "crud.osdu_well.get_versions_of_osdu_well":
return crud.osdu_well.build_request_get_versions_of_osdu_well()
if n_path == "crud.osdu_well.create_osdu_well":
return crud.osdu_well.build_request_create_osdu_well()
if n_path == "crud.osdu_welllog.delete_osdu_welllog":
return crud.osdu_welllog.build_request_delete_osdu_welllog()
if n_path == "crud.osdu_welllog.get_osdu_welllog_specific_version":
return crud.osdu_welllog.build_request_get_osdu_welllog_specific_version()
if n_path == "crud.osdu_welllog.get_osdu_welllog":
return crud.osdu_welllog.build_request_get_osdu_welllog()
if n_path == "crud.osdu_welllog.get_versions_of_osdu_welllog":
return crud.osdu_welllog.build_request_get_versions_of_osdu_welllog()
if n_path == "crud.osdu_welllog.create_osdu_welllog":
return crud.osdu_welllog.build_request_create_osdu_welllog()
if n_path == "crud.osdu_wellboretrajectory.delete_osdu_wellboretrajectory":
return crud.osdu_wellboretrajectory.build_request_delete_osdu_wellboretrajectory()
if n_path == "crud.osdu_wellboretrajectory.get_osdu_wellboretrajectory_specific_version":
return crud.osdu_wellboretrajectory.build_request_get_osdu_wellboretrajectory_specific_version()
if n_path == "crud.osdu_wellboretrajectory.get_osdu_wellboretrajectory":
return crud.osdu_wellboretrajectory.build_request_get_osdu_wellboretrajectory()
if n_path == "crud.osdu_wellboretrajectory.get_versions_of_osdu_wellboretrajectory":
return crud.osdu_wellboretrajectory.build_request_get_versions_of_osdu_wellboretrajectory()
if n_path == "crud.osdu_wellboretrajectory.create_osdu_wellboretrajectory":
return crud.osdu_wellboretrajectory.build_request_create_osdu_wellboretrajectory()
if n_path == "crud.osdu_wellboremarkerset.delete_osdu_wellboremarkerset":
return crud.osdu_wellboremarkerset.build_request_delete_osdu_wellboremarkerset()
if n_path == "crud.osdu_wellboremarkerset.get_osdu_wellboremarkerset_specific_version":
return crud.osdu_wellboremarkerset.build_request_get_osdu_wellboremarkerset_specific_version()
if n_path == "crud.osdu_wellboremarkerset.get_osdu_wellboremarkerset":
return crud.osdu_wellboremarkerset.build_request_get_osdu_wellboremarkerset()
if n_path == "crud.osdu_wellboremarkerset.get_versions_of_osdu_wellboremarkerset":
return crud.osdu_wellboremarkerset.build_request_get_versions_of_osdu_wellboremarkerset()
if n_path == "crud.osdu_wellboremarkerset.create_osdu_wellboremarkerset":
return crud.osdu_wellboremarkerset.build_request_create_osdu_wellboremarkerset()
if n_path == "crud.logset.get_versions_of_logset":
return crud.logset.build_request_get_versions_of_logset()
if n_path == "crud.logset.get_logset_specific_version":
return crud.logset.build_request_get_logset_specific_version()
if n_path == "crud.logset.get_logset":
return crud.logset.build_request_get_logset()
if n_path == "crud.logset.delete_logset":
return crud.logset.build_request_delete_logset()
if n_path == "crud.logset.create_logset":
return crud.logset.build_request_create_logset()
if n_path == "crud.marker.delete_marker":
return crud.marker.build_request_delete_marker()
if n_path == "crud.marker.get_versions_of_marker":
return crud.marker.build_request_get_versions_of_marker()
if n_path == "crud.marker.get_marker":
return crud.marker.build_request_get_marker()
if n_path == "crud.marker.get_marker_specific_version":
return crud.marker.build_request_get_marker_specific_version()
if n_path == "crud.marker.create_marker":
return crud.marker.build_request_create_marker()
if n_path == "crud.trajectory.get_versions_of_trajectory":
return crud.trajectory.build_request_get_versions_of_trajectory()
if n_path == "crud.trajectory.get_trajectory":
return crud.trajectory.build_request_get_trajectory()
if n_path == "crud.trajectory.get_trajectory_specific_version":
return crud.trajectory.build_request_get_trajectory_specific_version()
if n_path == "crud.trajectory.delete_trajectory":
return crud.trajectory.build_request_delete_trajectory()
if n_path == "crud.trajectory.create_trajectory":
return crud.trajectory.build_request_create_trajectory()
if n_path == "crud.log.delete_log":
return crud.log.build_request_delete_log()
if n_path == "crud.log.get_versions_of_log":
return crud.log.build_request_get_versions_of_log()
if n_path == "crud.log.get_log_bulk_data":
return crud.log.build_request_get_log_bulk_data()
if n_path == "crud.log.get_log":
return crud.log.build_request_get_log()
if n_path == "crud.log.get_log_specific_version":
return crud.log.build_request_get_log_specific_version()
if n_path == "crud.log.create_log":
return crud.log.build_request_create_log()
if n_path == "crud.log.add_log_bulk_data":
return crud.log.build_request_add_log_bulk_data()
if n_path == "crud.dips.get_dipset":
return crud.dips.build_request_get_dipset()
if n_path == "crud.dips.query_dips":
return crud.dips.build_request_query_dips()
if n_path == "crud.dips.delete_dip":
return crud.dips.build_request_delete_dip()
if n_path == "crud.dips.create_dips":
return crud.dips.build_request_create_dips()
if n_path == "crud.dips.create__dipset":
return crud.dips.build_request_create__dipset()
if n_path == "crud.dips.insert_dips":
return crud.dips.build_request_insert_dips()
if n_path == "crud.dips.get_dip_from_index":
return crud.dips.build_request_get_dip_from_index()
if n_path == "crud.dips.delete_dipset":
return crud.dips.build_request_delete_dipset()
if n_path == "crud.dips.patch_dip":
return crud.dips.build_request_patch_dip()
if n_path == "crud.dips.get_dips":
return crud.dips.build_request_get_dips()
if n_path == "error_cases.create_log_with_invalid_data_should_422":
return error_cases.build_request_create_log_with_invalid_data_should_422()
if n_path == "model_extensibility.get_log_check_for_extra_fields":
return model_extensibility.build_request_get_log_check_for_extra_fields()
if n_path == "model_extensibility.clean_up_delete_log":
return model_extensibility.build_request_clean_up_delete_log()
if n_path == "model_extensibility.create_log_with_extra_fields":
return model_extensibility.build_request_create_log_with_extra_fields()
if n_path == "recursive_delete.setup.recusive_del_setup_end":
return recursive_delete.setup.build_request_recursive_del_setup_end()
if n_path == "recursive_delete.setup.recusive_del_setup_create_well":
return recursive_delete.setup.build_request_recursive_del_setup_create_well()
if n_path == "recursive_delete.setup.recusive_del_setup_check_state_start":
return recursive_delete.setup.build_request_recursive_del_setup_check_state_start()
if n_path == "recursive_delete.setup.recusive_del_setup_create_logs":
return recursive_delete.setup.build_request_recursive_del_setup_create_logs()
if n_path == "recursive_delete.setup.recusive_del_setup_create_logset":
return recursive_delete.setup.build_request_recursive_del_setup_create_logset()
if n_path == "recursive_delete.setup.recusive_del_setup_create_wellbore":
return recursive_delete.setup.build_request_recursive_del_setup_create_wellbore()
if n_path == "recursive_delete.setup.recusive_del_setup_create_record_refs":
return recursive_delete.setup.build_request_recursive_del_setup_create_record_refs()
if n_path == "recursive_delete.delete_well.check_logset_is_deleted":
return recursive_delete.delete_well.build_request_check_logset_is_deleted()
if n_path == "recursive_delete.delete_well.recursive_delete_well":
return recursive_delete.delete_well.build_request_recursive_delete_well()
if n_path == "recursive_delete.delete_well.check_log_is_deleted":
return recursive_delete.delete_well.build_request_check_log_is_deleted()
if n_path == "recursive_delete.delete_well.check_wellbore_is_deleted":
return recursive_delete.delete_well.build_request_check_wellbore_is_deleted()
if n_path == "search_apis.setup.seach_tests_setup_end":
return search_apis.setup.build_request_seach_tests_setup_end()
if n_path == "search_apis.setup.seach_tests_setup_create_logsets":
return search_apis.setup.build_request_seach_tests_setup_create_logsets()
if n_path == "search_apis.setup.seach_tests_setup_create_record_refs":
return search_apis.setup.build_request_seach_tests_setup_create_record_refs()
if n_path == "search_apis.setup.seach_tests_setup_create_logs":
return search_apis.setup.build_request_seach_tests_setup_create_logs()
if n_path == "search_apis.setup.seach_tests_setup_create_wellbore":
return search_apis.setup.build_request_seach_tests_setup_create_wellbore()
if n_path == "search_apis.setup.seach_tests_setup_create_markers":
return search_apis.setup.build_request_seach_tests_setup_create_markers()
if n_path == "search_apis.setup.seach_tests_setup_start":
return search_apis.setup.build_request_seach_tests_setup_start()
if n_path == "search_apis.search.search_logs_by_logset_id":
return search_apis.search.build_request_search_logs_by_logset_id()
if n_path == "search_apis.search.search_markers_by_wellbore_id":
return search_apis.search.build_request_search_markers_by_wellbore_id()
if n_path == "search_apis.search.search_wellbores_by_geo_polygon":
return search_apis.search.build_request_search_wellbores_by_geo_polygon()
if n_path == "search_apis.search.search_logs_by_wellbore_id":
return search_apis.search.build_request_search_logs_by_wellbore_id()
if n_path == "search_apis.search.search_logset_by_wellbores_attribute":
return search_apis.search.build_request_search_logset_by_wellbores_attribute()
if n_path == "search_apis.search.search_logs_by_wellbores_attribute":
return search_apis.search.build_request_search_logs_by_wellbores_attribute()
if n_path == "search_apis.search.search_wellbores_by_bounding_box":
return search_apis.search.build_request_search_wellbores_by_bounding_box()
if n_path == "search_apis.search.search_wellbores_by_distance":
return search_apis.search.build_request_search_wellbores_by_distance()
if n_path == "search_apis.search.search_logs_by_logsets_attribute":
return search_apis.search.build_request_search_logs_by_logsets_attribute()
if n_path == "search_apis.search.search_logset_by_wellbore_id":
return search_apis.search.build_request_search_logset_by_wellbore_id()
if n_path == "about":
return about.build_request_about()
if n_path == "version":
return version.build_request_version()
if n_path == "status":
return status.build_request_status()
raise ValueError(f'No request matches the path {n_path}')
def get_cleaned_ref_and_res(kind: str, res_dict: dict) -> (dict, dict):
if kind =="osdu_wellbore":
return crud.osdu_wellbore.get_cleaned_ref_and_res(res_dict)
if kind =="osdu_well":
return crud.osdu_well.get_cleaned_ref_and_res(res_dict)
if kind =="osdu_welllog":
return crud.osdu_welllog.get_cleaned_ref_and_res(res_dict)
if kind =="osdu_wellboretrajectory":
return crud.osdu_wellboretrajectory.get_cleaned_ref_and_res(res_dict)
if kind =="osdu_wellboremarkerset":
return crud.osdu_wellboremarkerset.get_cleaned_ref_and_res(res_dict)
WDMS client CLI
# Usage example
`python -m wdms_client.cli --help` to list available commands.
## list callable apis
Callable apis can be listed using command `list`:
```
:$ python -m wdms_client.cli list
available apis:
- crud.well.get_well
- crud.well.delete_well
- crud.well.get_well_specific_version
- crud.well.get_versions_of_well
- crud.well.create_well
- crud.wellbore.delete_wellbore
- crud.wellbore.get_wellbore_specific_version
...
```
## Show environment
Describe environment variables using command `show-env`. Can pass an environment file:
```
:$ python -m wdms_client.cli show-env "..\..\my_environment.json"
environments variables given environment C:\SRC\opengroup\wellbore-domain-services\tests\integration\functional\my_environment.json
- base_url = http://127.0.0.1:8080/api/os-wellbore-ddms
- token = MY_TOKEN
- cloud_provider = local
- base_url_entity = logs
- entity_kind = osdu:wks:log:1.0.5
- dipsetKind = osdu:wks:dipSet:1.0.0
- wellKind = osdu:wks:well:1.0.2
...
```
## Describe api before call
Use command `descibe` with an `api` and optionally an environment file. It will print details of corresponding request:
```
python -m wdms_client.cli describe crud.well.create_well --environment="..\..\my_environment.json"
============ REQUEST ===============
URL: [POST] http://127.0.0.1:8080/api/os-wellbore-ddms/ddms/v2/wells)
headers:
- accept: application/json
- Content-Type: application/json
- data-partition-id: part
- Connection: close
- Authorization: Bearer 123******r 123
- correlation-id: wdms_e2e__30319cb0-fcc6-463b-9929-d204514d7701
body: -------------------------------------------
[
{
"acl": {
"owners": [
"data.default.owners@part.acl"
],
"viewers": [
"data.default.viewers@part.acl"
]
}, "legal": {
"legaltags": [
...
```
## Perform a call
Use command `descibe` with an `api` and optionally an environment file. It will run it and print the result:
```
python -m wdms_client.cli call about --environment="..\..\my_environment.json"
1 run(s) for about:
# run 1:
[200] - http://127.0.0.1:8080/api/os-wellbore-ddms/about
start: [2021-10-14 13:46:00.279199], end: 2021-10-14 13:46:00.291161, elapsed: 11 ms
============ REQUEST ===============
URL: [GET] http://127.0.0.1:8080/api/os-wellbore-ddms/about)
headers:
- User-Agent: python-requests/2.26.0
- Accept-Encoding: gzip, deflate
- accept: application/json
- Connection: close
- correlation-id: wdms_e2e__8b7c80ea-5bdd-45d7-9738-51431efdc820
============ RESPONSE ===============
headers:
- date: Thu, 14 Oct 2021 11:46:00 GMT
- server: uvicorn
- content-length: 97
- content-type: application/json
- Connection: close
body: -------------------------------------------
{"service":"Wellbore DDMS OSDU","version":"0.2","buildNumber":"local","cloudEnvironment":"local"}
```
## Generated basic environment file
use command `gen-env` to generate a basic environment file. By default, automatically launch editor on the created file.
```
python -m wdms_client.cli gen-env new_environment.json
basic environment file generated in $WORKING_DIR\new_environment.json
```
## Default environment file
If no environment file is provided, it will try to use `local_environment.json` from the working directory if exists.
import click
import os
import json
from .request_builders import request_path_dict
from .request_builders.wdms_variables import variables_dict
from .variables import Variables
from .request_runner import RequestRunner
def get_env_file(environment) -> str:
if environment:
if not os.path.exists(environment):
raise ValueError(f"{environment} does not exist")
print("using environment file", environment)
return environment
# try get local
try:
return get_env_file(os.path.join(os.getcwd(), "local_environment.json"))
except Exception:
pass
if not environment:
print("WARNING no valid environment file provided")
return environment
def get_env(environment) -> Variables:
env = Variables.from_dict(variables_dict)
environment = get_env_file(environment)
if environment:
local_env = Variables.load_env(environment, ignore_empty=True)
env.update_env(local_env)
return env
def get_runner(api) -> RequestRunner:
if api not in request_path_dict:
raise ValueError(f"ERROR:: api {api} is unknown")
return request_path_dict[api]()
@click.group()
def cli():
pass
@click.command(help='call an endpoint')
@click.option('--environment', default='', type=str, help='Specify a environment as a JSON file path (postman format)')
@click.argument('api', type=str) # help='api to call, use "list" command to list them'
def call(api, environment):
r = get_runner(api)
r.call(get_env(environment))
print(r)
@click.command(help='list available "api"')
def list():
print("available apis:")
for api in request_path_dict.keys():
print(' -', api)
@click.command(help='describe the given "api"')
@click.option('--environment', default='', type=str, help='Specify a environment as a JSON file path (postman format)')
@click.argument('api', type=str)
def describe(api, environment):
env = get_env(environment)
r = get_runner(api)
r.call(env, dry_run=True)
print(r.runs[0].request)
@click.command(help='show env')
@click.option('--environment', default='', type=str, help='Specify a environment as a JSON file path (postman format)')
def show_env(environment):
env = get_env(environment)
print(f"environments variables given environment {environment if environment else '[none]'}")
for k, v in env.enumerate():
print(f" - {k} = {v}")
@click.command(help='generate a basic environment file (targeting local server). Meant to be manually updated afterward')
@click.argument('output_file', type=str)
@click.option('--no-edit', '-n', default=False, is_flag=True, help="Do not edit file after creation.")
@click.option('--overwrite', '-o', default=False, is_flag=True, help="Allow overwrite if file exists.")
def gen_env(output_file: str, no_edit=False, overwrite=False):
base_env_dict = {
"description": "This is just a basic environment file target a local server.",
"name": "wdms integration environment file",
"values": [
{
"enabled": True,
"key": "token",
"value": "",
"description": "JWT content"
},
{
"enabled": True,
"key": "data_partition",
"value": ""
},
{
"enabled": True,
"key": "cloud_provider",
"value": "local",
"description": "any of ['local', 'az', 'gcp', 'aws', 'ibm']"
},
{
"enabled": True,
"key": "base_url",
"value": "http://127.0.0.1:8080/api/os-wellbore-ddms"
},
{
"enabled": True,
"key": "acl_domain",
"value": ""
},
{
"enabled": True,
"key": "legal_tag",
"value": ""
}
]
}
if not output_file.lower().endswith('.json'):
output_file = output_file + '.json'
if not overwrite and os.path.exists(output_file):
raise ValueError(f'"{output_file}" file already exists')
with open(output_file, 'w') as f:
json.dump(base_env_dict, f, indent=4)
if no_edit:
return
abs_output_file = os.path.abspath(output_file)
print("basic environment file generated in", os.path.abspath(abs_output_file))
click.edit(filename=abs_output_file)
cli.add_command(call)
cli.add_command(list)
cli.add_command(describe)
cli.add_command(show_env)
cli.add_command(gen_env)
if __name__ == "__main__":
cli()
# Copyright 2021 Schlumberger
#
# 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 typing import Union, List
from .wdms import about
from .wdms import version
from .wdms import status
from .wdms import crud
from .wdms import error_cases
from .wdms import model_extensibility
from .wdms import recursive_delete
from .wdms import search_apis
request_path_dict = {
"crud.well.get_well":
crud.well.build_request_get_well,
"crud.well.delete_well":
crud.well.build_request_delete_well,
"crud.well.get_well_specific_version":
crud.well.build_request_get_well_specific_version,
"crud.well.get_versions_of_well":
crud.well.build_request_get_versions_of_well,
"crud.well.create_well":
crud.well.build_request_create_well,
"crud.wellbore.delete_wellbore":
crud.wellbore.build_request_delete_wellbore,
"crud.wellbore.get_wellbore_specific_version":
crud.wellbore.build_request_get_wellbore_specific_version,
"crud.wellbore.get_wellbore":
crud.wellbore.build_request_get_wellbore,
"crud.wellbore.get_versions_of_wellbore":
crud.wellbore.build_request_get_versions_of_wellbore,
"crud.wellbore.create_wellbore":
crud.wellbore.build_request_create_wellbore,
"crud.osdu_wellbore.delete_osdu_wellbore":
crud.osdu_wellbore.build_request_delete_osdu_wellbore,
"crud.osdu_wellbore.get_osdu_wellbore_specific_version":
crud.osdu_wellbore.build_request_get_osdu_wellbore_specific_version,
"crud.osdu_wellbore.get_osdu_wellbore":
crud.osdu_wellbore.build_request_get_osdu_wellbore,
"crud.osdu_wellbore.get_versions_of_osdu_wellbore":
crud.osdu_wellbore.build_request_get_versions_of_osdu_wellbore,