def remove_subscription(subscription_id): """Removes a specific Subscription Args: subscription_id: The Subscription ID. """ lock = _get_file_lock() lock.acquire() try: sc = util.get_all_subscriptions()[subscription_id] event_types = sc.get_event_types() # delete subscription from all_subscription file _delete_subscription_from_file(subscription_id) for event in event_types: del util.get_subscriptions_by_type()[event][subscription_id] del util.get_all_subscriptions()[subscription_id] return Response(status=status.HTTP_200_OK, mimetype="application/json") except KeyError as e: logging.exception("Subscription not found: " + str(e)) abort(status.HTTP_404_NOT_FOUND) except Exception as e: logging.exception("Error while deleting subscription: " + str(e)) abort(status.HTTP_500_INTERNAL_SERVER_ERROR) finally: lock.release()
def add_subscription_from_file(): """Add the Redfish Subscription From File. EventTypes: - ResourceUpdated - ResourceAdded - ResourceRemoved - Alert Returns: True: If the read from file is successful """ data = get_file_content() if data is None: return False subscription_data = data["members"] if not subscription_data: return False for single_subscription in subscription_data: subscription_id = single_subscription["Id"] destination = single_subscription["Destination"] event_types = single_subscription["EventTypes"] context = single_subscription["Context"] sc = Subscription(subscription_id, destination, event_types, context) for event_type in sc.get_event_types(): util.get_subscriptions_by_type()[event_type][subscription_id] = sc util.get_all_subscriptions()[subscription_id] = sc return True
def remove_subscription(subscription_id): """Removes a specific Subscription Args: subscription_id: The Subscription ID. """ try: sc = util.get_all_subscriptions()[subscription_id] event_types = sc.get_event_types() for event in event_types: del util.get_subscriptions_by_type()[event][subscription_id] del util.get_all_subscriptions()[subscription_id] return Response( status=status.HTTP_200_OK, mimetype="application/json") except KeyError as e: logging.exception("Subscription not found: " + str(e)) abort(status.HTTP_404_NOT_FOUND)
def get_subscription_collection(): """Get the Redfish Subscription Collection. Get method to return SubscriptionCollection JSON when /redfish/v1/EventService/EventSubscriptions is requested. Returns: JSON: JSON with EventSubscriptions. """ # Build Subscription Collection object and validates it sc = SubscriptionCollection(util.get_all_subscriptions()) return ResponseBuilder.success(sc)
def get_subscription(subscription_id): """Gets a specific Subscription Args: subscription_id: The Subscription ID. Returns: Subscription JSON. """ try: sc = util.get_all_subscriptions()[subscription_id] json_str = sc.serialize() return Response(response=json_str, status=status.HTTP_200_OK, mimetype="application/json") except KeyError as e: logging.exception("Subscription not found: " + str(e)) abort(status.HTTP_404_NOT_FOUND)
def add_subscription(): """Add the Redfish Subscription. Add a new subscription when this POST operation is requested. The body of the request must have the Destination, an array of EventTypes. Context is optional. EventTypes: - ResourceUpdated - ResourceAdded - ResourceRemoved - Alert Returns: JSON: JSON with Subscription information. Exception: KeyError: When occur a key mapping error. return abort(400) """ # get Destination, EventTypes and Context from post # generate subscription uuid # add subscription in subscriptions_by_type # add subscription in all_subscriptions try: body = request.get_json() destination = body["Destination"] if not validators.url(destination): abort(status.HTTP_400_BAD_REQUEST, "Destination must be an URI.") # read all_subscription file and check if destination already present is_duplicate = _is_duplicate_subscription(destination) if is_duplicate: abort(status.HTTP_400_BAD_REQUEST, "Destination is duplicate") event_types = body["EventTypes"] if not event_types: abort(status.HTTP_400_BAD_REQUEST, "EventTypes cannot be empty.") context = body.get("Context") except KeyError: error_message = "Invalid JSON key. The JSON request body " \ "must have the keys Destination and EventTypes. " \ "The Context is optional." abort(status.HTTP_400_BAD_REQUEST, error_message) subscription_id = str(uuid.uuid1()) try: # Build Subscription object and validates it sc = Subscription(subscription_id, destination, event_types, context) except ValidationError: error_message = "Invalid EventType. The EventTypes are " \ "StatusChange, ResourceUpdated, ResourceAdded, " \ "ResourceRemoved and Alert." abort(status.HTTP_400_BAD_REQUEST, error_message) lock = _get_file_lock() lock.acquire() try: # checking for duplicate subscription again to avoid concurrency issue is_duplicate = _is_duplicate_subscription(destination) if not is_duplicate: # write subscription to the all_subscription.json file _add_subscription_to_file(sc.redfish) for event_type in sc.get_event_types(): util.get_subscriptions_by_type( )[event_type][subscription_id] = sc util.get_all_subscriptions()[subscription_id] = sc except Exception as e: logging.exception("Error while adding subscription: " + str(e)) abort(status.HTTP_500_INTERNAL_SERVER_ERROR, "Error while adding subscription to the file") finally: lock.release() # Build redfish json json_str = sc.serialize() # Build response and returns response = Response(response=json_str, status=status.HTTP_201_CREATED, mimetype="application/json") response.headers.add( "Location", "/redfish/v1/EventService/EventSubscriptions/" "{}".format(subscription_id)) return response
def add_subscription(): """Add the Redfish Subscription. Add a new subscription when this POST operation is requested. The body of the request must have the Destination, an array of EventTypes. Context is optional. EventTypes: - ResourceUpdated - ResourceAdded - ResourceRemoved - Alert Returns: JSON: JSON with Subscription information. Exception: KeyError: When occur a key mapping error. return abort(400) """ # get Destination, EventTypes and Context from post # generate subscription uuid # add subscription in subscriptions_by_type # add subscription in all_subscriptions try: body = request.get_json() destination = body["Destination"] if not validators.url(destination): abort(status.HTTP_400_BAD_REQUEST, "Destination must be an URI.") event_types = body["EventTypes"] if not event_types: abort(status.HTTP_400_BAD_REQUEST, "EventTypes cannot be empty.") context = body.get("Context") except KeyError: error_message = "Invalid JSON key. The JSON request body " \ "must have the keys Destination and EventTypes. " \ "The Context is optional." abort(status.HTTP_400_BAD_REQUEST, error_message) subscription_id = str(uuid.uuid1()) try: # Build Subscription object and validates it sc = Subscription(subscription_id, destination, event_types, context) except ValidationError: error_message = "Invalid EventType. The EventTypes are " \ "StatusChange, ResourceUpdated, ResourceAdded, " \ "ResourceRemoved and Alert." abort(status.HTTP_400_BAD_REQUEST, error_message) for event_type in sc.get_event_types(): util.get_subscriptions_by_type()[event_type][subscription_id] = sc util.get_all_subscriptions()[subscription_id] = sc # Build redfish json json_str = sc.serialize() # Build response and returns response = Response( response=json_str, status=status.HTTP_201_CREATED, mimetype="application/json") response.headers.add( "Location", "/redfish/v1/EventService/EventSubscriptions/" "{}".format(subscription_id)) return response