ARI: Added new functionality to load a single module.
[asterisk/asterisk.git] / res / ari / resource_asterisk.c
index 6f2eb80..768f771 100644 (file)
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+ASTERISK_REGISTER_FILE()
 
 #include "asterisk/ast_version.h"
 #include "asterisk/buildinfo.h"
+#include "asterisk/module.h"
 #include "asterisk/paths.h"
 #include "asterisk/pbx.h"
 #include "resource_asterisk.h"
 
-void ast_ari_get_asterisk_info(struct ast_variable *headers,
-       struct ast_get_asterisk_info_args *args,
+void ast_ari_asterisk_get_info(struct ast_variable *headers,
+       struct ast_ari_asterisk_get_info_args *args,
        struct ast_ari_response *response)
 {
        RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
@@ -140,15 +141,179 @@ void ast_ari_get_asterisk_info(struct ast_variable *headers,
        ast_ari_response_ok(response, ast_json_ref(json));
 }
 
-void ast_ari_get_global_var(struct ast_variable *headers, struct ast_get_global_var_args *args, struct ast_ari_response *response)
+/*!
+ * \brief Process module information and append to a json array
+ * \param module Resource name
+ * \param description Resource description
+ * \param usecnt Resource use count
+ * \param status Resource running status
+ * \param like
+ * \param support_level Resource support level
+ * \param module_data_list Resource array
+ *
+ * \retval 0 if no resource exists
+ * \retval 1 if resource exists
+ */
+static int process_module_list(const char *module, const char *description, int usecnt,
+                               const char *status, const char *like,
+                               enum ast_module_support_level support_level, void *module_data_list)
+{
+       struct ast_json *module_info;
+
+       module_info = ast_json_pack("{s: s, s: s, s: i, s: s, s: s}",
+                              "name", module,
+                              "description", description,
+                              "use_count", usecnt,
+                              "status", status,
+                              "support_level", ast_module_support_level_to_string(support_level));
+       if (!module_info) {
+               return 0;
+       }
+       ast_json_array_append(module_data_list, module_info);
+       return 1;
+}
+
+void ast_ari_asterisk_list_modules(struct ast_variable *headers,
+       struct ast_ari_asterisk_list_modules_args *args,
+       struct ast_ari_response *response)
+{
+       struct ast_json *json;
+
+       json = ast_json_array_create();
+       ast_update_module_list_data(&process_module_list, NULL, json);
+
+       ast_ari_response_ok(response, json);
+}
+
+/*!
+ * \brief Identify module by name and process resource information
+ * \param module Resource name
+ * \param description Resource description
+ * \param usecnt Resource use count
+ * \param status Resource running status
+ * \param like
+ * \param support_level Resource support level
+ * \param data JSON body for resource
+ * \param condition Name to match resource to
+ *
+ * \retval 0 if no resource exists
+ * \retval 1 if resource exists
+ */
+static int identify_module(const char *module, const char *description, int usecnt,
+                           const char *status, const char *like,
+                           enum ast_module_support_level support_level, void *data,
+                           const char *condition)
+{
+       int json_obj_set = 0;
+
+       if (strcmp(condition, module) != 0) {
+               return 0;
+       }
+
+       json_obj_set += ast_json_object_set(data, "name", ast_json_string_create(module));
+       json_obj_set += ast_json_object_set(data, "description", ast_json_string_create(description));
+       json_obj_set += ast_json_object_set(data, "use_count", ast_json_integer_create(usecnt));
+       json_obj_set += ast_json_object_set(data, "status", ast_json_string_create(status));
+       json_obj_set += ast_json_object_set(data, "support_level", ast_json_string_create(
+                                           ast_module_support_level_to_string(support_level)));
+
+       if (json_obj_set != 0) {
+               return 0;
+       }
+
+       return 1;
+}
+
+void ast_ari_asterisk_get_module(struct ast_variable *headers,
+       struct ast_ari_asterisk_get_module_args *args,
+       struct ast_ari_response *response)
+{
+       struct ast_json *json;
+       int module_retrieved = 0;
+
+       ast_assert(response != NULL);
+
+       if (!ast_module_check(args->module_name)) {
+               ast_ari_response_error(
+                       response, 404, "Not Found",
+                       "Module could not be found in running modules");
+               return;
+       }
+
+       json = ast_json_object_create();
+       if (!json) {
+               ast_ari_response_alloc_failed(response);
+               return;
+       }
+
+       module_retrieved = ast_update_module_list_condition(&identify_module, NULL, json,
+                                                           args->module_name);
+       if (!module_retrieved) {
+               ast_ari_response_error(
+                       response, 409, "Conflict",
+                       "Module information could not be retrieved");
+               return;
+       }
+
+       ast_ari_response_ok(response, json);
+}
+
+void ast_ari_asterisk_load_module(struct ast_variable *headers,
+       struct ast_ari_asterisk_load_module_args *args,
+       struct ast_ari_response *response)
+{
+       enum ast_module_load_result load_result;
+
+       ast_assert(response != NULL);
+
+       if (ast_module_check(args->module_name)) {
+               ast_ari_response_error(
+                       response, 409, "Conflict",
+                       "Module is already loaded");
+               return;
+       }
+
+       load_result = ast_load_resource(args->module_name);
+
+       if (load_result == AST_MODULE_LOAD_DECLINE) {
+               ast_ari_response_error(
+                       response, 409, "Conflict",
+                       "Module load declined");
+               return;
+       } else if (load_result == AST_MODULE_LOAD_SKIP) {
+               ast_ari_response_error(
+                       response, 409, "Conflict",
+                       "Module was skipped");
+               return;
+       } else if (load_result == AST_MODULE_LOAD_FAILURE) {
+               ast_ari_response_error(
+                       response, 409, "Conflict",
+                       "Module could not be loaded properly");
+               return;
+       }
+
+       ast_ari_response_no_content(response);
+}
+
+void ast_ari_asterisk_get_global_var(struct ast_variable *headers,
+       struct ast_ari_asterisk_get_global_var_args *args,
+       struct ast_ari_response *response)
 {
        RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
-       RAII_VAR(struct ast_str *, tmp, ast_str_create(32), ast_free);
+       RAII_VAR(struct ast_str *, tmp, NULL, ast_free);
 
        const char *value;
 
        ast_assert(response != NULL);
 
+       if (ast_strlen_zero(args->variable)) {
+               ast_ari_response_error(
+                       response, 400, "Bad Request",
+                       "Variable name is required");
+               return;
+       }
+
+       tmp = ast_str_create(32);
        if (!tmp) {
                ast_ari_response_alloc_failed(response);
                return;
@@ -164,7 +329,9 @@ void ast_ari_get_global_var(struct ast_variable *headers, struct ast_get_global_
        ast_ari_response_ok(response, ast_json_ref(json));
 }
 
-void ast_ari_set_global_var(struct ast_variable *headers, struct ast_set_global_var_args *args, struct ast_ari_response *response)
+void ast_ari_asterisk_set_global_var(struct ast_variable *headers,
+       struct ast_ari_asterisk_set_global_var_args *args,
+       struct ast_ari_response *response)
 {
        ast_assert(response != NULL);