Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal CLI for uploading functionality #55

Merged
merged 77 commits into from
Jan 10, 2024
Merged

Conversation

CasperWA
Copy link
Collaborator

@CasperWA CasperWA commented Nov 23, 2023

Closes #12

This is a replacement for #17, which only implements the upload and config CLI commands.

To do:

  • Test upload
  • Test config
  • Test global_settings.py
  • Negative tests

The CLI is based on Typer, which is made by the same author of the popular FastAPI package for building REST APIs. FastAPI is what is used for OTEAPI Services and many other of our REST API repositories.

The way this CLI is built is similar to FastAPI and therefore makes is easy to build, maintain and improve upon.

The CLI is made up of the following commands:

  • upload
  • config set
  • config unset
  • config unset-all
  • config show

With some global options to for example show the configuration (for the DLite Entities Service) in a specific format (for example JSON or YAML). As well as a choice between two configuration file locations for the two different modules that now exist in the repository: the REST API service and the CLI.

All commands are tested with unit tests and there's complete code coverage for all new changes.


Edit: I've had a thought about the implementation of this (especially with the SINTEF firewall and internal port blocking in mind). Instead of connecting to the database directly in the CLI we should go through a service endpoint to upload a (verified) data model, and the service will then handle all communication with the database.
This ensures:

  1. No changes required for the CLI if the database changes, since all communication to the database will happen in the service.
  2. There's only one pathway of communication, which is between the live service (onto-ns.com) and the database.

For now, I'd keep this PR as is, though, and then implement this update in a separate PR.


Edit: Another issue that should be raised and fixed in another PR is the duplication of the SOFT5/7 pydantic models. Either a base model should be created or the SOFT7 model should inherit from SOFT5 implenting it's own "updates".

CasperWA and others added 30 commits April 25, 2023 11:43
Map out the initial commands and (almost) finish upload and config.
Add testing dependencies (pytest).
Add a special pylint run for tests and update workflows accordingly.
Start the first test file for the `entities-service upload` CLI command.

Also, fix filesuffix error for a workflow.
Prefer CLI-specific .env before a service-specific .env, and otherwise
fallback to CONFIG defaults.
These are currently only used in 'config show' to return the output in
the given format, but are made as global options to be available
whenever an output of that kind is desired.
Change --use-service-dotenv/--use-cli-dotenv to a simple --dotenv-path
instead, to point to a .env file to use.
Validate via the pydantic SOFT5 and SOFT7 models instead of trying to
instantiate entities in DLite.

This greatly simplifies things and automatically implements Python
3.12 support for the CLI.

Add helpful READMEs in the static folders for valid and invalid entities
to describe them.
This was done especially to showcase the validation errors pydantic
throws for the invalid entity Person.json and to announce the Cat.json
valid entity as a SOFT5 entity (the others are SOFT7 entities).
The new logic for the upload CLI command is:

Check if the incoming entity's URI already exists in the backend.
If no, upload.
Otherwise, check if the incoming entity is equal in every way to the
existing entity in the backend.
If yes, skip and continue.
Otherwise, have the user confirm if they want to upload the incoming
entity with a new version.
If no, skip and continue.
If yes, ask the user to enter the new version, while providing a
reasonable default value.
If user aborts, skip and continue.
Otherwise, update the incoming entity with the new version and upload
it.

We also add a new entity with a "ref" property type, since we need to
make sure special keys can be uploaded to the backend (Mongo does not
support keys starting with $).
Handle the $ character in the upload logic instead, before uploading to
the backend.
Add extra final summary information about skipped entities, if any.
Extend tests - this was added due to testing.
Especially add multiple invalid entities to ensure the SOFT5 and SOFT7
validations are caught.
Copy link
Contributor

@ajeklund ajeklund left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice! Added a few questions/suggestions.

dlite_entities_service/cli/main.py Outdated Show resolved Hide resolved
dlite_entities_service/cli/main.py Outdated Show resolved Hide resolved
dlite_entities_service/cli/_utils/global_settings.py Outdated Show resolved Hide resolved
dlite_entities_service/cli/main.py Show resolved Hide resolved
dlite_entities_service/cli/main.py Show resolved Hide resolved
dlite_entities_service/cli/main.py Show resolved Hide resolved
dlite_entities_service/cli/main.py Outdated Show resolved Hide resolved
dlite_entities_service/cli/main.py Outdated Show resolved Hide resolved
CasperWA and others added 4 commits January 9, 2024 21:21
CLI message clarifications for users

Co-authored-by: Anders Eklund <[email protected]>
Same as other code review commit

Co-authored-by: Anders Eklund <[email protected]>
@CasperWA CasperWA enabled auto-merge (squash) January 9, 2024 20:48
@CasperWA CasperWA requested a review from ajeklund January 9, 2024 20:49
Copy link
Contributor

@ajeklund ajeklund left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the nice work!

@CasperWA CasperWA merged commit ea0d778 into main Jan 10, 2024
11 checks passed
@CasperWA CasperWA deleted the cwa/close-12-minimal-cli branch January 10, 2024 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Help script for uploading entities
2 participants