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

fix delete dataset workflow #497

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
services:
wis2box:
wis2box-management:
volumes:
- ${WIS2BOX_HOST_DATADIR}:/data/wis2box:rw
- ./wis2box-management/wis2box/wis2box.cron:/etc/cron.d/wis2box:ro
- ./wis2box-management/wis2box:/usr/local/lib/python3.9/site-packages/wis2box-0.3.dev0-py3.9.egg/wis2box
- ./wis2box-management/wis2box:/app/wis2box
command: ["wis2box", "pubsub" , "subscribe", "--broker", "http://wis2box-minio:9000", "--topic", "wis2box-storage/#", "--verbosity", "INFO"]

# wis2box-api:
# volumes:
# - ../../wis2box-api/wis2box_api:/usr/local/lib/python3.8/dist-packages/wis2box_api-0.3.dev0-py3.8.egg/wis2box_api
wis2box-api:
volumes:
- ../wis2box-api/wis2box_api:/app/wis2box_api

elasticsearch:
ports:
Expand Down
8 changes: 6 additions & 2 deletions docs/source/reference/running/api-publishing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ discovery metadata MCF created as described in the :ref:`discovery-metadata` sec

wis2box data add-collection $WIS2BOX_DATADIR/data/config/foo/bar/baz/discovery-metadata.yml

To delete the colection from the API backend and configuration:

Deleting a dataset
------------------

To delete a dataset from the API backend and configuration:

.. code-block:: bash

wis2box api delete-collection foo.bar.baz
wis2box data delete-collection dataset-id


.. note::
Expand Down
30 changes: 18 additions & 12 deletions wis2box-management/wis2box/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,43 @@ def setup_collection(meta: dict = {}) -> bool:
return True


def remove_collection(name: str, backend: bool = True,
def remove_collection(collection_id: str, backend: bool = True,
config: bool = True) -> bool:
"""
Remove collection from api backend and configuration

:param name: `str` of collection name
:param collection_id: `str` of collection id

:returns: `bool` of API collection removal result
"""

api_backend = None
api_config = None

if backend:
api_backend = load_backend()
if api_backend.has_collection(name):
api_backend.delete_collection(name)
collection_data = load_config().get_collection_data(collection_id)

if config:
api_config = load_config()
if api_config.has_collection(name):
api_config.delete_collection(name)
if api_config.has_collection(collection_id):
api_config.delete_collection(collection_id)

if backend:
api_backend = load_backend()
if api_backend.has_collection(collection_data):
api_backend.delete_collection(collection_data)

if api_backend is not None and api_backend.has_collection(name):
LOGGER.error(f'Unable to remove collection for {name}')
if api_backend is not None and api_backend.has_collection(collection_data):
msg = f'Unable to remove collection backend for {collection_id}'
LOGGER.error(msg)
return False

if api_config is not None and api_backend.has_collection(name):
LOGGER.error(f'Unable to remove collection for {name}')
if api_config is not None and api_config.has_collection(collection_data):
LOGGER.error(f'Unable to remove collection for {collection_id}')
return False

if collection_id not in ['discovery-metadata', 'stations', 'messages']:
delete_collection_item('discovery-metadata', collection_id)

return True


Expand Down
22 changes: 22 additions & 0 deletions wis2box-management/wis2box/api/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ def __init__(self, defs: dict) -> None:
:param defs: `dict` of connection parameters
"""

def get_collection(self, name: str) -> dict:
"""
Get a collection

:param name: `str` of collection name

:returns: `dict` of collection configuration
"""

raise NotImplementedError()

def get_collection_data(self, name: str) -> dict:
"""
Get a collection's backend data configuration

:param name: `str` of collection name

:returns: `str` of collection backend data configuration
"""

raise NotImplementedError()

def add_collection(self, name: str, collection: dict) -> bool:
"""
Add a collection
Expand Down
31 changes: 31 additions & 0 deletions wis2box-management/wis2box/api/config/pygeoapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
###############################################################################

import logging
from urllib.parse import urlparse


from requests import Session
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
Expand Down Expand Up @@ -52,6 +55,34 @@ def __init__(self, defs: dict) -> None:
self.http.mount('https://', adapter)
self.http.mount('http://', adapter)

def get_collection(self, name: str) -> dict:
"""
Get a collection

:param name: `str` of collection name

:returns: `dict` of collection configuration
"""

r = self.http.get(f'{self.url}/{name}')
r.raise_for_status()

return r.json()

def get_collection_data(self, name: str) -> dict:
"""
Get a collection's backend data configuration

:param name: `str` of collection name

:returns: `str` of collection backend data configuration
"""

data = self.get_collection(name)['providers'][0]['data']

collection_data = urlparse(data).path.lstrip('/')
return collection_data

def add_collection(self, name: str, collection: dict) -> bool:
"""
Add a collection
Expand Down
15 changes: 5 additions & 10 deletions wis2box-management/wis2box/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from wis2box.metadata.discovery import DiscoveryMetadata
from wis2box.storage import put_data, move_data, list_content, delete_data
from wis2box.util import older_than, walk_path
from wis2box.topic_hierarchy import validate_and_load


LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -220,18 +219,14 @@ def add_collection(ctx, filepath, verbosity):

@click.command()
@click.pass_context
@cli_helpers.OPTION_TOPIC_HIERARCHY
@click.argument('collection')
@cli_helpers.OPTION_VERBOSITY
def delete_collection(ctx, topic_hierarchy, verbosity):
"""Delete collection from api backend"""

if topic_hierarchy is None:
raise click.ClickException('Missing -th/--topic-hierarchy')
def delete_collection(ctx, collection, verbosity):
"""Delete collection from API backend"""

click.echo(f'Deleting collection: {topic_hierarchy}')
click.echo(f'Deleting collection: {collection}')

th, _ = validate_and_load(topic_hierarchy)
remove_collection(th.dotpath)
remove_collection(collection)

click.echo('Done')

Expand Down
Loading