Skip to content

Commit

Permalink
fix dataset delection (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Aug 28, 2023
1 parent 12c638a commit d53791f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 21 deletions.
28 changes: 16 additions & 12 deletions wis2box-management/wis2box/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,41 @@ 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):
LOGGER.error(f'Unable to remove collection backend for {collection_id}')
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

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
16 changes: 7 additions & 9 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,17 @@ def add_collection(ctx, filepath, verbosity):

@click.command()
@click.pass_context
@cli_helpers.OPTION_TOPIC_HIERARCHY
@click.option('--identifier', '-i', help='Collection identifier')
@cli_helpers.OPTION_VERBOSITY
def delete_collection(ctx, topic_hierarchy, verbosity):
"""Delete collection from api backend"""
def delete_collection(ctx, identifier, verbosity):
"""Delete collection from API backend"""

if topic_hierarchy is None:
raise click.ClickException('Missing -th/--topic-hierarchy')
if identifier is None:
raise click.ClickException('Missing -i/--identifier')

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

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

click.echo('Done')

Expand Down

0 comments on commit d53791f

Please sign in to comment.