Skip to content

Commit

Permalink
feat(healtcheck-ecommerce): PPABV-60 New api to support healthcheck e…
Browse files Browse the repository at this point in the history
…commerce (#2656)

* feat: new api to support healthcheck ecommerce

* docs: update readme

* chore: add ignore-for-error for send-request

* fix(healthcheck); minor fix
  • Loading branch information
infantesimone authored Dec 16, 2024
1 parent 152c466 commit 90f5337
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 5 deletions.
48 changes: 48 additions & 0 deletions src/domains/ecommerce-app/04_apim_ecommerce_healthcheck.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#################################################
## API ecommerce healthcheck service ##
#################################################
locals {
apim_ecommerce_healthcheck_service_api = {
display_name = "eCommerce pagoPA - healthcheck API"
description = "API to support ecommerce healthcheck"
path = "ecommerce-healthcheck"
subscription_required = true
service_url = null
}
}

# Healthcheck service APIs
resource "azurerm_api_management_api_version_set" "ecommerce_healthcheck_api" {
name = "${local.project}-healtcheck-api"
resource_group_name = local.pagopa_apim_rg
api_management_name = local.pagopa_apim_name
display_name = local.apim_ecommerce_healthcheck_service_api.display_name
versioning_scheme = "Segment"
}

module "apim_ecommerce_healthcheck_api_v1" {
source = "./.terraform/modules/__v3__/api_management_api"

name = "${local.project}-healtcheck-api"
api_management_name = local.pagopa_apim_name
resource_group_name = local.pagopa_apim_rg
product_ids = ["statuspage_nodo_pagamenti"]
subscription_required = local.apim_ecommerce_healthcheck_service_api.subscription_required
version_set_id = azurerm_api_management_api_version_set.ecommerce_healthcheck_api.id
api_version = "v1"

description = local.apim_ecommerce_healthcheck_service_api.description
display_name = local.apim_ecommerce_healthcheck_service_api.display_name
path = local.apim_ecommerce_healthcheck_service_api.path
protocols = ["https"]
service_url = local.apim_ecommerce_healthcheck_service_api.service_url

content_format = "openapi"
content_value = templatefile("./api/ecommerce-healthcheck/v1/_openapi.json.tpl", {
hostname = local.apim_hostname
})

xml_content = templatefile("./api/ecommerce-healthcheck/v1/_base_policy.xml.tpl", {
hostname = local.ecommerce_hostname
})
}
2 changes: 2 additions & 0 deletions src/domains/ecommerce-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
| <a name="module_apim_ecommerce_checkout_api_v1"></a> [apim\_ecommerce\_checkout\_api\_v1](#module\_apim\_ecommerce\_checkout\_api\_v1) | ./.terraform/modules/__v3__/api_management_api | n/a |
| <a name="module_apim_ecommerce_checkout_api_v2"></a> [apim\_ecommerce\_checkout\_api\_v2](#module\_apim\_ecommerce\_checkout\_api\_v2) | ./.terraform/modules/__v3__/api_management_api | n/a |
| <a name="module_apim_ecommerce_checkout_product"></a> [apim\_ecommerce\_checkout\_product](#module\_apim\_ecommerce\_checkout\_product) | ./.terraform/modules/__v3__/api_management_product | n/a |
| <a name="module_apim_ecommerce_healthcheck_api_v1"></a> [apim\_ecommerce\_healthcheck\_api\_v1](#module\_apim\_ecommerce\_healthcheck\_api\_v1) | ./.terraform/modules/__v3__/api_management_api | n/a |
| <a name="module_apim_ecommerce_helpdesk_commands_product"></a> [apim\_ecommerce\_helpdesk\_commands\_product](#module\_apim\_ecommerce\_helpdesk\_commands\_product) | ./.terraform/modules/__v3__/api_management_product | n/a |
| <a name="module_apim_ecommerce_helpdesk_product"></a> [apim\_ecommerce\_helpdesk\_product](#module\_apim\_ecommerce\_helpdesk\_product) | ./.terraform/modules/__v3__/api_management_product | n/a |
| <a name="module_apim_ecommerce_io_api_v2"></a> [apim\_ecommerce\_io\_api\_v2](#module\_apim\_ecommerce\_io\_api\_v2) | ./.terraform/modules/__v3__/api_management_api | n/a |
Expand Down Expand Up @@ -106,6 +107,7 @@
| [azurerm_api_management_api_version_set.apim_ecommerce_redirect_outcome_api_v1](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management_api_version_set) | resource |
| [azurerm_api_management_api_version_set.apim_ecommerce_webview_api](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management_api_version_set) | resource |
| [azurerm_api_management_api_version_set.ecommerce_checkout_api_v1](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management_api_version_set) | resource |
| [azurerm_api_management_api_version_set.ecommerce_healthcheck_api](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management_api_version_set) | resource |
| [azurerm_api_management_api_version_set.ecommerce_io_api_v1](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management_api_version_set) | resource |
| [azurerm_api_management_api_version_set.ecommerce_io_outcomes_api](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management_api_version_set) | resource |
| [azurerm_api_management_api_version_set.ecommerce_io_webview_pay_v1](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management_api_version_set) | resource |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<policies>
<inbound>
<base />

<rate-limit-by-key calls="10" renewal-period="10" counter-key="@(context.Request.Headers.GetValueOrDefault("X-Forwarded-For"))" />

<send-request ignore-error="true" mode="new" response-variable-name="transactionServiceLiveness" timeout="10">
<set-url>
https://${hostname}/pagopa-ecommerce-transactions-service/actuator/health/liveness
</set-url>
<set-method>GET</set-method>
</send-request>

<send-request ignore-error="true" mode="new" response-variable-name="paymentMethodServiceLiveness" timeout="10">
<set-url>
https://${hostname}/pagopa-ecommerce-payment-methods-service/actuator/health/liveness
</set-url>
<set-method>GET</set-method>
</send-request>

<send-request ignore-error="true" mode="new" response-variable-name="paymentRequestServiceLiveness" timeout="10">
<set-url>
https://${hostname}/pagopa-ecommerce-payment-requests-service/actuator/health/liveness
</set-url>
<set-method>GET</set-method>
</send-request>

<send-request ignore-error="true" mode="new" response-variable-name="userStatServiceLiveness" timeout="10">
<set-url>
https://${hostname}/pagopa-ecommerce-user-stats-service/actuator/health/liveness
</set-url>
<set-method>GET</set-method>
</send-request>

<send-request ignore-error="true" mode="new" response-variable-name="eventDispatcherServiceLiveness" timeout="10">
<set-url>
https://${hostname}/pagopa-ecommerce-event-dispatcher/actuator/health/liveness
</set-url>
<set-method>GET</set-method>
</send-request>

<send-request ignore-error="true" mode="new" response-variable-name="transactionSchedulerServiceLiveness" timeout="10">
<set-url>
https://${hostname}/pagopa-ecommerce-transactions-scheduler-service/actuator/health/liveness
</set-url>
<set-method>GET</set-method>
</send-request>

</inbound>
<backend>
</backend>
<outbound>
<base />
<set-body>
@{
var services = new[] {
"transactionServiceLiveness",
"paymentMethodServiceLiveness",
"paymentRequestServiceLiveness",
"userStatServiceLiveness",
"eventDispatcherServiceLiveness",
"transactionSchedulerServiceLiveness"
};

var combinedResults = new JObject();

bool allUp = true;

foreach (var service in services) {
var parsedResponse = ((IResponse)context.Variables[service]).Body.As<JObject>();
combinedResults[service] = parsedResponse;
if ((string)parsedResponse["status"] != "UP") {
allUp = false;
}
}

var response = new JObject();

response["status"] = allUp ? "UP" : "DOWN";
response["details"] = combinedResults;

return response.ToString();
}
</set-body>
</outbound>
<on-error>
<base />
</on-error>
</policies>
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"openapi": "3.0.3",
"info": {
"version": "0.0.1,",
"title": "eCommerce pagoPA - healthcheck",
"description": "The Healthcheck API is used to monitor the operational status of a REST API by providing endpoints for liveness and readiness checks.\n- Liveness Check: This endpoint verifies that the API is running and able to process requests.\n- Readiness Check: This endpoint confirms that the API is fully initialized and ready to handle traffic. It ensures that all necessary resources, such as databases or external services, are available before the API starts accepting requests.\n",
"contact": {
"name": "pagoPA - Touchpoints team"
}
},
"tags": [
{
"name": "eCommerceHealthcheck",
"description": "Api's to handle eCommerce healtcheck"
}
],
"servers": [
{
"url": "https://${hostname}"
}
],
"paths": {
"/liveness": {
"get": {
"tags": [
"eCommerceHealthcheck"
],
"summary": "API to support Liveness Check",
"operationId": "eCommerceHealthcheckLiveness",
"responses": {
"200": {
"description": "Healthcheck Liveness info",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/EcommerceHealthcheckLivenessResponse"
}
}
}
},
"401": {
"description": "Unauthorized",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemJson"
},
"example": {
"type": "https://example.com/problem/",
"title": "string",
"status": 401,
"detail": "Unauthorized"
}
}
}
},
"500": {
"description": "Internal server error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemJson"
},
"example": {
"type": "https://example.com/problem/",
"title": "string",
"status": 500,
"detail": "Internal server error"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"EcommerceHealthcheckLivenessResponse": {
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": [
"UP",
"DOWN"
],
"description": "The overall status of all services."
},
"details": {
"type": "object",
"description": "Detailed status of each ecommerce service (additionalProperties).",
"additionalProperties": {
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": [
"UP",
"DOWN"
],
"description": "The status of the individual service."
}
}
}
}
},
"required": [
"status"
]
},
"ProblemJson": {
"description": "Body definition for error responses containing failure details",
"type": "object",
"properties": {
"type": {
"type": "string",
"format": "uri",
"description": "An absolute URI that identifies the problem type. When dereferenced,\nit SHOULD provide human-readable documentation for the problem type\n(e.g., using HTML).",
"default": "about:blank",
"example": "https://example.com/problem/constraint-violation"
},
"title": {
"type": "string",
"description": "A short, summary of the problem type. Written in english and readable\nfor engineers (usually not suited for non technical stakeholders and\nnot localized); example: Service Unavailable"
},
"status": {
"$ref": "#/components/schemas/HttpStatusCode"
},
"detail": {
"type": "string",
"description": "A human readable explanation specific to this occurrence of the\nproblem.",
"example": "There was an error processing the request"
},
"instance": {
"type": "string",
"format": "uri",
"description": "An absolute URI that identifies the specific occurrence of the problem.\nIt may or may not yield further information if dereferenced."
}
}
},
"HttpStatusCode": {
"type": "integer",
"format": "int32",
"description": "The HTTP status code generated by the origin server for this occurrence\nof the problem.",
"minimum": 100,
"maximum": 600,
"exclusiveMaximum": true,
"example": 502
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@
return response.ToString();
}
</set-body>
<choose>
<when condition="@(context.Response.Body.As<JObject>()["status"].ToString() == "DOWN")">
<set-status code="502" reason="Bad Gateway" />
</when>
</choose>
</outbound>
<on-error>
<base />
Expand Down

0 comments on commit 90f5337

Please sign in to comment.