diff options
author | 2021-09-08 14:46:32 +0100 | |
---|---|---|
committer | 2021-10-18 15:01:20 +0100 | |
commit | 70de6a27b7722e3ed02d8e8a8c7933e053eacabb (patch) | |
tree | fb2c635116b705fdf64e3400233ad351ea0259f7 /components/pm-subscription-handler/pmsh_service/mod/api/controller.py | |
parent | 2f208f94eddd090c1983f849f61742288c21af6f (diff) |
[DCAEGEN2] PMSH Create Subscription public API
Issue-ID: DCAEGEN2-2819
Change-Id: I80636be25dc4f7b1c5ce7470c7a38c010cb339a1
Signed-off-by: SagarS <sagar.shetty@est.tech>
Diffstat (limited to 'components/pm-subscription-handler/pmsh_service/mod/api/controller.py')
-rwxr-xr-x | components/pm-subscription-handler/pmsh_service/mod/api/controller.py | 38 |
1 files changed, 37 insertions, 1 deletions
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 21d29caf..8af6c777 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-2020 Nordix Foundation. +# Copyright (C) 2019-2021 Nordix Foundation. # ============================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,6 +17,11 @@ # ============LICENSE_END===================================================== from mod.subscription import Subscription +from http import HTTPStatus +from mod import logger +from mod.api.services import subscription_service +from connexion import NoContent +from mod.api.custom_exception import InvalidDataException, DuplicateDataException def status(): @@ -40,3 +45,34 @@ def get_all_sub_to_nf_relations(): """ subs_dict = [s.serialize() for s in Subscription.get_all()] return subs_dict + + +def post_subscription(body): + """ + Creates a subscription + + Args: + body (dict): subscription request body to save. + + Returns: + Success : NoContent, 201 + Invalid Data : Invalid message, 400 + Duplicate Data : Duplicate field detail, 409 + + Raises: + Error: If anything fails in the server. + """ + response = NoContent, HTTPStatus.CREATED.value + try: + subscription_service.create_subscription(body['subscription']) + except DuplicateDataException as e: + logger.error(f'Failed to create subscription for ' + f'{body["subscription"]["subscriptionName"]} due to duplicate data: {e}', + exc_info=True) + response = e.duplicate_field_info, HTTPStatus.CONFLICT.value + except InvalidDataException as e: + logger.error(f'Failed to create subscription for ' + f'{body["subscription"]["subscriptionName"]} due to invalid data: {e}', + exc_info=True) + response = e.invalid_message, HTTPStatus.BAD_REQUEST.value + return response |