From 5b11f4ff05cf4bd3f042a4c531848b939e4ead8a Mon Sep 17 00:00:00 2001 From: "raviteja.karumuri" Date: Wed, 12 Jan 2022 18:48:44 +0000 Subject: [PMSH] Delete subscription API by Name Issue-ID: DCAEGEN2-2821 Signed-off-by: Raviteja, Karumuri Change-Id: I22eb00c74e40b5428c3c7bd2b0546175cd6f30ed --- .../pmsh_service/mod/api/controller.py | 41 +++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'components/pm-subscription-handler/pmsh_service/mod/api/controller.py') diff --git a/components/pm-subscription-handler/pmsh_service/mod/api/controller.py b/components/pm-subscription-handler/pmsh_service/mod/api/controller.py index aee6df0e..d887187a 100755 --- a/components/pm-subscription-handler/pmsh_service/mod/api/controller.py +++ b/components/pm-subscription-handler/pmsh_service/mod/api/controller.py @@ -1,5 +1,5 @@ # ============LICENSE_START=================================================== -# Copyright (C) 2019-2021 Nordix Foundation. +# Copyright (C) 2019-2022 Nordix Foundation. # ============================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -147,3 +147,42 @@ def get_meas_group_with_nfs(subscription_name, measurement_group_name): f'{exception}') return {'error': 'Request was not processed due to Exception : ' f'{exception}'}, HTTPStatus.INTERNAL_SERVER_ERROR.value + + +def delete_subscription_by_name(subscription_name): + """ Deletes the subscription by name + + Args: + subscription_name (String): Name of the subscription + + Returns: + NoneType, HTTPStatus: None, 204 + dict, HTTPStatus: subscription not defined, 404 + dict, HTTPStatus: Reason for not deleting subscription, 409 + dict, HTTPStatus: Exception details of failure, 500 + """ + logger.info(f'API call received to delete subscription by name: {subscription_name}') + try: + unlocked_locking_mgs = \ + subscription_service.query_unlocked_mg_by_sub_name(subscription_name) + if not unlocked_locking_mgs: + if subscription_service.query_to_delete_subscription_by_name(subscription_name) == 1: + return None, HTTPStatus.NO_CONTENT + else: + logger.error(f'Subscription is not defined with name {subscription_name}') + return {'error': f'Subscription is not defined with name {subscription_name}'}, \ + HTTPStatus.NOT_FOUND.value + else: + logger.error('Subscription is not deleted due to associated MGs were UNLOCKED ' + '(or) under update process to LOCKED') + return {'error': 'Subscription is not deleted due to the following MGs were UNLOCKED ' + '(or) under update process to LOCKED', 'measurementGroupNames': + [{'measurementGroupName': + mg.measurement_group_name}for mg in unlocked_locking_mgs]}, \ + HTTPStatus.CONFLICT.value + except Exception as exception: + logger.error(f'Try again, subscription with name {subscription_name}' + f'is not deleted due to following exception: {exception}') + return {'error': f'Try again, subscription with name {subscription_name}' + f'is not deleted due to following exception: {exception}'}, \ + HTTPStatus.INTERNAL_SERVER_ERROR.value -- cgit 1.2.3-korg