[Asterisk-code-review] Added ARI resource /ari/asterisk/ping (asterisk[master])

sungtae kim asteriskteam at digium.com
Mon Jan 28 17:22:48 CST 2019


sungtae kim has uploaded this change for review. ( https://gerrit.asterisk.org/10932


Change subject: Added ARI resource /ari/asterisk/ping
......................................................................

Added ARI resource /ari/asterisk/ping

Added ARI resource.
GET /ari/asterisk/ping : It returns "pong" message with timestamp
and asterisk id. It would be useful for simple heath check.

Change-Id: I8d24e1dcc96f60f73437c68d9463ed746f688b29
---
M res/ari/ari_model_validators.c
M res/ari/ari_model_validators.h
M res/ari/resource_asterisk.c
M res/ari/resource_asterisk.h
M res/res_ari_asterisk.c
5 files changed, 158 insertions(+), 2 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/32/10932/1

diff --git a/res/ari/ari_model_validators.c b/res/ari/ari_model_validators.c
index ea5a885..e90ffb8 100644
--- a/res/ari/ari_model_validators.c
+++ b/res/ari/ari_model_validators.c
@@ -548,6 +548,69 @@
 	return ast_ari_validate_module;
 }
 
+int ast_ari_validate_ping(struct ast_json *json)
+{
+	int res = 1;
+	struct ast_json_iter *iter;
+	int has_ping = 0;
+	int has_timestamp = 0;
+	int has_asterisk_id = 0;
+
+	for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
+		if (strcmp("ping", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_ping = 1;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI Ping field ping failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("timestamp", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_timestamp = 1;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI Ping field timestamp failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("asterisk_id", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_asterisk_id = 1;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI Ping field asterisk_id failed validation\n");
+				res = 0;
+			}
+		} else
+		{
+			ast_log(LOG_ERROR,
+				"ARI Ping has undocumented field %s\n",
+				ast_json_object_iter_key(iter));
+			res = 0;
+		}
+	}
+
+	if (!has_ping) {
+		ast_log(LOG_ERROR, "ARI Ping missing required field ping\n");
+		res = 0;
+	}
+	if (!has_timestamp) {
+		ast_log(LOG_ERROR, "ARI Ping missing required field timestamp\n");
+		res = 0;
+	}
+	if (!has_asterisk_id) {
+		ast_log(LOG_ERROR, "ARI Ping missing required field asterisk_id\n");
+		res = 0;
+	}
+
+	return res;
+}
+
 int ast_ari_validate_set_id(struct ast_json *json)
 {
 	int res = 1;
diff --git a/res/ari/ari_model_validators.h b/res/ari/ari_model_validators.h
index 3c501cf..4c0b925 100644
--- a/res/ari/ari_model_validators.h
+++ b/res/ari/ari_model_validators.h
@@ -260,6 +260,15 @@
 ari_validator ast_ari_validate_module_fn(void);
 
 /*!
+ * \brief Validator for Ping.
+ *
+ * \param json JSON object to validate.
+ * \returns True (non-zero) if valid.
+ * \returns False (zero) if invalid.
+ */
+int ast_ari_validate_ping(struct ast_json *json);
+
+/*!
  * \brief Validator for SetId.
  *
  * Effective user/group id
diff --git a/res/ari/resource_asterisk.c b/res/ari/resource_asterisk.c
index 5c6a35a..520de8e 100644
--- a/res/ari/resource_asterisk.c
+++ b/res/ari/resource_asterisk.c
@@ -631,6 +631,23 @@
 	ast_ari_response_no_content(response);
 }
 
+void ast_ari_asterisk_get_ping(struct ast_variable *headers,
+	struct ast_ari_response *response)
+{
+	struct ast_json *json;
+	char eid[20];
+
+	ast_assert(response != NULL);
+
+	json = ast_json_pack("{s: s, s: o, s: s}",
+			"ping",	"pong",
+			"timestamp", ast_json_timeval(ast_tvnow(), NULL),
+			"asterisk_id", ast_eid_to_str(eid, sizeof(eid), &ast_eid_default)
+			);
+
+	ast_ari_response_ok(response, json);
+}
+
 /*!
  * \brief Process logger information and append to a json array
  * \param channel Resource logger channel name path
diff --git a/res/ari/resource_asterisk.h b/res/ari/resource_asterisk.h
index a4a7da0..b4f40f4 100644
--- a/res/ari/resource_asterisk.h
+++ b/res/ari/resource_asterisk.h
@@ -198,6 +198,14 @@
 struct ast_ari_asterisk_list_log_channels_args {
 };
 /*!
+ * \brief Response pong message
+ *
+ * \param headers HTTP headers
+ * \param args Swagger parameters
+ * \param[out] response HTTP response
+ */
+void ast_ari_asterisk_get_ping(struct ast_variable *headers, struct ast_ari_response *response);
+/*!
  * \brief Gets Asterisk log channel information.
  *
  * \param headers HTTP headers
diff --git a/res/res_ari_asterisk.c b/res/res_ari_asterisk.c
index e143a7f..c34c5db 100644
--- a/res/res_ari_asterisk.c
+++ b/res/res_ari_asterisk.c
@@ -681,6 +681,56 @@
 	return;
 }
 /*!
+ * \brief Parameter parsing callback for /asterisk/ping.
+ * \param get_params GET parameters in the HTTP request.
+ * \param path_vars Path variables extracted from the request.
+ * \param headers HTTP headers.
+ * \param[out] response Response to the HTTP request.
+ */
+static void ast_ari_asterisk_get_ping_cb(
+	struct ast_tcptls_session_instance *ser,
+	struct ast_variable *get_params, struct ast_variable *path_vars,
+	struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
+{
+#if defined(AST_DEVMODE)
+	int is_valid;
+	int code;
+#endif /* AST_DEVMODE */
+	ast_ari_asterisk_get_ping(headers, response);
+#if defined(AST_DEVMODE)
+	code = response->response_code;
+
+	switch (code) {
+	case 0: /* Implementation is still a stub, or the code wasn't set */
+		is_valid = response->message == NULL;
+		break;
+	case 500: /* Internal Server Error */
+	case 501: /* Not Implemented */
+	case 404: /* Module could not be found in running modules. */
+	case 409: /* Module information could not be retrieved. */
+		is_valid = 1;
+		break;
+	default:
+		if (200 <= code && code <= 299) {
+			is_valid = ast_ari_validate_ping(
+				response->message);
+		} else {
+			ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/ping\n", code);
+			is_valid = 0;
+		}
+	}
+
+	if (!is_valid) {
+		ast_log(LOG_ERROR, "Response validation failed for /asterisk/ping\n");
+		ast_ari_response_error(response, 500,
+			"Internal Server Error", "Response validation failed");
+	}
+#endif /* AST_DEVMODE */
+
+fin: __attribute__((unused))
+	return;
+}
+/*!
  * \brief Parameter parsing callback for /asterisk/logging.
  * \param get_params GET parameters in the HTTP request.
  * \param path_vars Path variables extracted from the request.
@@ -1164,6 +1214,15 @@
 	.children = { &asterisk_modules_moduleName, }
 };
 /*! \brief REST handler for /api-docs/asterisk.json */
+static struct stasis_rest_handlers asterisk_ping = {
+	.path_segment = "ping",
+	.callbacks = {
+		[AST_HTTP_GET] = ast_ari_asterisk_get_ping_cb,
+	},
+	.num_children = 0,
+	.children = {  }
+};
+/*! \brief REST handler for /api-docs/asterisk.json */
 static struct stasis_rest_handlers asterisk_logging_logChannelName_rotate = {
 	.path_segment = "rotate",
 	.callbacks = {
@@ -1207,8 +1266,8 @@
 	.path_segment = "asterisk",
 	.callbacks = {
 	},
-	.num_children = 5,
-	.children = { &asterisk_config,&asterisk_info,&asterisk_modules,&asterisk_logging,&asterisk_variable, }
+	.num_children = 6,
+	.children = { &asterisk_config,&asterisk_info,&asterisk_modules,&asterisk_logging,&asterisk_variable,&asterisk_ping, }
 };
 
 static int unload_module(void)

-- 
To view, visit https://gerrit.asterisk.org/10932
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I8d24e1dcc96f60f73437c68d9463ed746f688b29
Gerrit-Change-Number: 10932
Gerrit-PatchSet: 1
Gerrit-Owner: sungtae kim <pchero21 at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190128/892683ad/attachment-0001.html>


More information about the asterisk-code-review mailing list