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

add WCMP2 unregister functionality #21

Merged
merged 4 commits into from
Sep 6, 2024
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ wis2-gdc register /path/to/dir/of/wcmp2-files
# loading metadata manually (from URL)
wis2-gdc register https://example.org/wcmp2-file.json

# deleting metadata by identifier
wis2-gdc unregister "urn:wmo:md:ca-eccc-msc:id123"

# loading metadata from a known harvest endpoint

# load from wis2box known deployments (https://demo.wis2box.wis.wmo.int)
Expand Down
3 changes: 2 additions & 1 deletion wis2-gdc-management/wis2_gdc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import click

from wis2_gdc.registrar import register, setup, teardown
from wis2_gdc.registrar import register, setup, teardown, unregister
from wis2_gdc.archive import archive
from wis2_gdc.sync import sync

Expand All @@ -38,6 +38,7 @@ def cli():

cli.add_command(setup)
cli.add_command(teardown)
cli.add_command(unregister)
cli.add_command(register)
cli.add_command(sync)
cli.add_command(archive)
14 changes: 13 additions & 1 deletion wis2-gdc-management/wis2_gdc/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def teardown(self) -> None:
raise NotImplementedError()

@abstractmethod
def save(self, record: dict) -> None:
def save_record(self, record: dict) -> None:
"""
Upsert a resource to a backend

Expand Down Expand Up @@ -83,5 +83,17 @@ def record_exists(self, identifier: str) -> bool:

raise NotImplementedError()

@abstractmethod
def delete_record(self, identifier: str) -> None:
"""
Delete a record from the backend

:param identifier: `str` of record identifier

:returns: `None`
"""

raise NotImplementedError()

def __repr__(self):
return '<BaseBackend>'
6 changes: 5 additions & 1 deletion wis2-gdc-management/wis2_gdc/backend/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,14 @@ def teardown(self) -> None:
LOGGER.debug(f'Deleting index {self.index_name}')
self.es.indices.delete(index=self.index_name)

def save(self, record: dict) -> None:
def save_record(self, record: dict) -> None:
LOGGER.debug(f"Indexing record {record['id']}")
self.es.index(index=self.index_name, id=record['id'], body=record)

def delete_record(self, identifier: str) -> None:
LOGGER.debug(f"Deleting record {identifier}")
self.es.delete(index=self.index_name, id=identifier)

def exists(self) -> bool:
LOGGER.debug('Checking whether backend exists')

Expand Down
2 changes: 1 addition & 1 deletion wis2-gdc-management/wis2_gdc/backend/ogcapi_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, defs):
self.conn = Records(env.API_URL)
self.collection = 'discovery-metadata'

def save(self):
def save_record(self):

ttype = 'create'

Expand Down
11 changes: 10 additions & 1 deletion wis2-gdc-management/wis2_gdc/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,19 @@

class DiscoveryMetadataHook(Hook):
def execute(self, topic: str, msg_dict: dict) -> None:
wcmp2_dict = None

LOGGER.debug('Discovery metadata hook execution begin')
r = Registrar()

wcmp2_dict = r.get_wcmp2(msg_dict, topic)
try:
wcmp2_dict = r.get_wcmp2(msg_dict, topic)
except (IndexError, KeyError):
is_deletion = list(filter(lambda d: d['rel'] == 'deletion',
msg_dict['links']))

if is_deletion:
r.delete_record(topic, msg_dict)

if wcmp2_dict is not None:
r.register(wcmp2_dict, topic)
Expand Down
61 changes: 59 additions & 2 deletions wis2-gdc-management/wis2_gdc/registrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,45 @@ def register(self, metadata: Union[dict, str], topic: str = None) -> None:

message = json.dumps(message).replace(API_URL_DOCKER, API_URL)

LOGGER.info('Publishing updated record to broker')
LOGGER.info('Publishing updated record to GDC broker')
publish_report_topic = f'origin/a/wis2/{CENTRE_ID}/metadata'
self.broker.pub(publish_report_topic, json.dumps(ets_results))

def delete_record(self, topic: str, wnm: dict) -> None:
"""
Delete a metadata document

:param topic: `str` of incoming topic (default is `None`)
:param wnm: `dict` of WNM

:returns: `None`
"""

centre_id = topic.split('/')[3]
publish_report_topic = f'monitor/a/wis2/{CENTRE_ID}/{centre_id}'

message = {
'id': str(uuid.uuid4()),
'href': None,
'report_by': CENTRE_ID,
'centre_id': centre_id
}

metadata_id = wnm['properties'].get('metadata_id')

if metadata_id is None:
message['message'] = 'No metadata id specified'
else:
try:
self.backend.delete_record(metadata_id)
message['message'] = f'metadata {metadata_id} deleted'
except Exception:
message['message'] = f'metadata {metadata_id} not found'

self.broker.pub(publish_report_topic, json.dumps(message))

return

def _process_record_metric(self, identifier: str, metric_name: str,
labels: list,
value: Union[str, int, float] = None) -> None:
Expand Down Expand Up @@ -317,7 +352,7 @@ def _publish(self):
"""

LOGGER.info(f'Saving to {BACKEND_TYPE} ({BACKEND_CONNECTION})')
self.backend.save(self.metadata)
self.backend.save_record(self.metadata)

def update_record_links(self) -> None:
"""
Expand Down Expand Up @@ -418,6 +453,8 @@ def teardown(ctx, bypass, verbosity='NOTSET'):
LOGGER.debug(f'Backend: {backend}')
backend.teardown()

click.echo('Done')


@click.command()
@click.pass_context
Expand Down Expand Up @@ -454,3 +491,23 @@ def register(ctx, path, verbosity='NOTSET'):
metadata = fh.read()

r.register(metadata)

click.echo('Done')


@click.command()
@click.pass_context
@click.argument('identifier')
@cli_options.OPTION_VERBOSITY
def unregister(ctx, identifier, verbosity='NOTSET'):
"""Unregister discovery metadata"""

click.echo(f'Unregistering {identifier}')
backend = BACKENDS[BACKEND_TYPE]({'connection': BACKEND_CONNECTION})
try:
LOGGER.debug(f'Backend: {backend}')
backend.delete_record(identifier)
except Exception:
click.echo('record not found')

click.echo('Done')
Loading