Asterisk - The Open Source Telephony Project  18.5.0
Functions
resource_asterisk.c File Reference

Implementation for ARI stubs. More...

#include "asterisk.h"
#include "asterisk/ast_version.h"
#include "asterisk/buildinfo.h"
#include "asterisk/logger.h"
#include "asterisk/module.h"
#include "asterisk/paths.h"
#include "asterisk/pbx.h"
#include "asterisk/sorcery.h"
#include "resource_asterisk.h"
Include dependency graph for resource_asterisk.c:

Go to the source code of this file.

Functions

void ast_ari_asterisk_add_log (struct ast_variable *headers, struct ast_ari_asterisk_add_log_args *args, struct ast_ari_response *response)
 Adds a log channel. More...
 
void ast_ari_asterisk_delete_log (struct ast_variable *headers, struct ast_ari_asterisk_delete_log_args *args, struct ast_ari_response *response)
 Deletes a log channel. More...
 
void ast_ari_asterisk_delete_object (struct ast_variable *headers, struct ast_ari_asterisk_delete_object_args *args, struct ast_ari_response *response)
 Delete a dynamic configuration object. More...
 
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)
 Get the value of a global variable. More...
 
void ast_ari_asterisk_get_info (struct ast_variable *headers, struct ast_ari_asterisk_get_info_args *args, struct ast_ari_response *response)
 Gets Asterisk system information. More...
 
void ast_ari_asterisk_get_module (struct ast_variable *headers, struct ast_ari_asterisk_get_module_args *args, struct ast_ari_response *response)
 Get Asterisk module information. More...
 
void ast_ari_asterisk_get_object (struct ast_variable *headers, struct ast_ari_asterisk_get_object_args *args, struct ast_ari_response *response)
 Retrieve a dynamic configuration object. More...
 
void ast_ari_asterisk_list_log_channels (struct ast_variable *headers, struct ast_ari_asterisk_list_log_channels_args *args, struct ast_ari_response *response)
 Gets Asterisk log channel information. More...
 
void ast_ari_asterisk_list_modules (struct ast_variable *headers, struct ast_ari_asterisk_list_modules_args *args, struct ast_ari_response *response)
 List Asterisk modules. More...
 
void ast_ari_asterisk_load_module (struct ast_variable *headers, struct ast_ari_asterisk_load_module_args *args, struct ast_ari_response *response)
 Load an Asterisk module. More...
 
void ast_ari_asterisk_ping (struct ast_variable *headers, struct ast_ari_asterisk_ping_args *args, struct ast_ari_response *response)
 Response pong message. More...
 
void ast_ari_asterisk_reload_module (struct ast_variable *headers, struct ast_ari_asterisk_reload_module_args *args, struct ast_ari_response *response)
 Reload an Asterisk module. More...
 
void ast_ari_asterisk_rotate_log (struct ast_variable *headers, struct ast_ari_asterisk_rotate_log_args *args, struct ast_ari_response *response)
 Rotates a log channel. More...
 
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)
 Set the value of a global variable. More...
 
void ast_ari_asterisk_unload_module (struct ast_variable *headers, struct ast_ari_asterisk_unload_module_args *args, struct ast_ari_response *response)
 Unload an Asterisk module. More...
 
void ast_ari_asterisk_update_object (struct ast_variable *headers, struct ast_ari_asterisk_update_object_args *args, struct ast_ari_response *response)
 Create or update a dynamic configuration object. More...
 
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)
 Identify module by name and process resource information. More...
 
static int process_log_list (const char *channel, const char *type, const char *status, const char *configuration, void *log_data_list)
 Process logger information and append to a json array. More...
 
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)
 Process module information and append to a json array. More...
 
static void return_sorcery_object (struct ast_sorcery *sorcery, void *sorcery_obj, struct ast_ari_response *response)
 

Detailed Description

Implementation for ARI stubs.

Author
David M. Lee, II dlee@.nosp@m.digi.nosp@m.um.co.nosp@m.m

Definition in file resource_asterisk.c.

Function Documentation

◆ ast_ari_asterisk_add_log()

void ast_ari_asterisk_add_log ( struct ast_variable headers,
struct ast_ari_asterisk_add_log_args args,
struct ast_ari_response response 
)

Adds a log channel.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 705 of file resource_asterisk.c.

References ast_ari_response_error(), ast_ari_response_no_content(), ast_assert, AST_LOGGER_ALLOC_ERROR, ast_logger_create_channel(), AST_LOGGER_DECLINE, AST_LOGGER_FAILURE, ast_ari_asterisk_add_log_args::configuration, ast_ari_asterisk_add_log_args::log_channel_name, and NULL.

Referenced by ast_ari_asterisk_add_log_cb().

708 {
709  int res;
710 
711  ast_assert(response != NULL);
712 
714 
715  if (res == AST_LOGGER_DECLINE) {
716  ast_ari_response_error(response, 400, "Bad Request",
717  "Configuration levels are required");
718  return;
719  } else if (res == AST_LOGGER_FAILURE) {
720  ast_ari_response_error(response, 409, "Conflict",
721  "Log channel already exists");
722  return;
723  } else if (res == AST_LOGGER_ALLOC_ERROR) {
724  ast_ari_response_error(response, 500, "Internal Server Error",
725  "Allocation failed");
726  return;
727  }
728 
729  ast_ari_response_no_content(response);
730 }
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
int ast_logger_create_channel(const char *log_channel, const char *components)
Create a log channel.
Definition: logger.c:1419
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
void ast_ari_response_no_content(struct ast_ari_response *response)
Fill in a No Content (204) ast_ari_response.
Definition: res_ari.c:284

◆ ast_ari_asterisk_delete_log()

void ast_ari_asterisk_delete_log ( struct ast_variable headers,
struct ast_ari_asterisk_delete_log_args args,
struct ast_ari_response response 
)

Deletes a log channel.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 757 of file resource_asterisk.c.

References ast_ari_response_error(), ast_ari_response_no_content(), ast_assert, AST_LOGGER_ALLOC_ERROR, AST_LOGGER_FAILURE, ast_logger_remove_channel(), ast_ari_asterisk_delete_log_args::log_channel_name, and NULL.

Referenced by ast_ari_asterisk_delete_log_cb().

760 {
761  int res;
762 
763  ast_assert(response != NULL);
764 
766 
767  if (res == AST_LOGGER_FAILURE) {
768  ast_ari_response_error(response, 404, "Not Found",
769  "Log channel does not exist");
770  return;
771  } else if (res == AST_LOGGER_ALLOC_ERROR) {
772  ast_ari_response_error(response, 500, "Internal Server Error",
773  "Allocation failed");
774  return;
775  }
776 
777  ast_ari_response_no_content(response);
778 }
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
int ast_logger_remove_channel(const char *log_channel)
Delete the specified log channel.
Definition: logger.c:1485
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
void ast_ari_response_no_content(struct ast_ari_response *response)
Fill in a No Content (204) ast_ari_response.
Definition: res_ari.c:284

◆ ast_ari_asterisk_delete_object()

void ast_ari_asterisk_delete_object ( struct ast_variable headers,
struct ast_ari_asterisk_delete_object_args args,
struct ast_ari_response response 
)

Delete a dynamic configuration object.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 247 of file resource_asterisk.c.

References ao2_cleanup, ast_ari_response_error(), ast_ari_response_no_content(), ast_sorcery_delete(), ast_sorcery_get_object_type(), ast_sorcery_retrieve_by_id(), ast_sorcery_retrieve_by_module_name(), ast_sorcery_unref, ast_ari_asterisk_delete_object_args::config_class, ast_ari_asterisk_delete_object_args::id, NULL, ast_ari_asterisk_delete_object_args::object_type, RAII_VAR, and sorcery.

Referenced by ast_ari_asterisk_delete_object_cb().

250 {
252  RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
253  RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup);
254 
256  if (!sorcery) {
258  response, 404, "Not Found",
259  "configClass '%s' not found",
260  args->config_class);
261  return;
262  }
263 
264  object_type = ast_sorcery_get_object_type(sorcery, args->object_type);
265  if (!object_type) {
267  response, 404, "Not Found",
268  "objectType '%s' not found",
269  args->object_type);
270  return;
271  }
272 
273  sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args->object_type, args->id);
274  if (!sorcery_obj) {
276  response, 404, "Not Found",
277  "Object with id '%s' not found",
278  args->id);
279  return;
280  }
281 
282  if (ast_sorcery_delete(sorcery, sorcery_obj)) {
284  response, 403, "Forbidden",
285  "Could not delete object with id '%s'",
286  args->id);
287  return;
288  }
289 
290  ast_ari_response_no_content(response);
291 }
Full structure for sorcery.
Definition: sorcery.c:230
#define NULL
Definition: resample.c:96
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition: sorcery.h:1502
Structure for registered object type.
Definition: sorcery.c:148
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
struct ast_sorcery_object_type * ast_sorcery_get_object_type(const struct ast_sorcery *sorcery, const char *type)
Get the sorcery object type given a type name.
Definition: sorcery.c:2489
int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
Delete an object.
Definition: sorcery.c:2233
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
void ast_ari_response_no_content(struct ast_ari_response *response)
Fill in a No Content (204) ast_ari_response.
Definition: res_ari.c:284
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
struct ast_sorcery * ast_sorcery_retrieve_by_module_name(const char *module)
Retrieves an existing sorcery instance by module name.
Definition: sorcery.c:672

◆ ast_ari_asterisk_get_global_var()

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 
)

Get the value of a global variable.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 780 of file resource_asterisk.c.

References ast_ari_response_alloc_failed(), ast_ari_response_error(), ast_ari_response_ok(), ast_assert, ast_free, ast_json_pack(), ast_json_ref(), ast_json_unref(), ast_str_create, ast_str_retrieve_variable(), ast_strlen_zero, NULL, RAII_VAR, S_OR, tmp(), value, and ast_ari_asterisk_get_global_var_args::variable.

Referenced by ast_ari_asterisk_get_global_var_cb().

783 {
784  RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
785  RAII_VAR(struct ast_str *, tmp, NULL, ast_free);
786 
787  const char *value;
788 
789  ast_assert(response != NULL);
790 
791  if (ast_strlen_zero(args->variable)) {
793  response, 400, "Bad Request",
794  "Variable name is required");
795  return;
796  }
797 
798  tmp = ast_str_create(32);
799  if (!tmp) {
801  return;
802  }
803 
804  value = ast_str_retrieve_variable(&tmp, 0, NULL, NULL, args->variable);
805 
806  if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
808  return;
809  }
810 
811  ast_ari_response_ok(response, ast_json_ref(json));
812 }
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
static int tmp()
Definition: bt_open.c:389
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37
void ast_ari_response_alloc_failed(struct ast_ari_response *response)
Fill in response with a 500 message for allocation failures.
Definition: res_ari.c:298
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
void ast_ari_response_ok(struct ast_ari_response *response, struct ast_json *message)
Fill in an OK (200) ast_ari_response.
Definition: res_ari.c:276
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
#define ast_free(a)
Definition: astmm.h:182
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
const char * ast_str_retrieve_variable(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, struct varshead *headp, const char *var)
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
Abstract JSON element (object, array, string, int, ...).
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:620

◆ ast_ari_asterisk_get_info()

void ast_ari_asterisk_get_info ( struct ast_variable headers,
struct ast_ari_asterisk_get_info_args args,
struct ast_ari_response response 
)

Gets Asterisk system information.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 294 of file resource_asterisk.c.

References ast_ari_response_alloc_failed(), ast_ari_response_ok(), ast_build_date, ast_build_kernel, ast_build_machine, ast_build_os, ast_build_user, AST_BUILDOPTS, ast_config_AST_RUN_GROUP, ast_config_AST_RUN_USER, ast_config_AST_SYSTEM_NAME, ast_defaultlanguage, ast_eid_default, ast_eid_to_str(), ast_get_version(), ast_json_integer_create(), ast_json_object_create(), ast_json_object_set(), ast_json_pack(), ast_json_real_create(), ast_json_ref(), ast_json_timeval(), ast_json_unref(), ast_lastreloadtime, ast_log, ast_option_maxcalls, ast_option_maxfiles, ast_option_maxload, ast_startuptime, config, LOG_WARNING, NULL, ast_ari_asterisk_get_info_args::only, ast_ari_asterisk_get_info_args::only_count, and RAII_VAR.

Referenced by ast_ari_asterisk_get_info_cb().

297 {
298  RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
299  int show_all = args->only_count == 0;
300  int show_build = show_all;
301  int show_system = show_all;
302  int show_config = show_all;
303  int show_status = show_all;
304  size_t i;
305  int res = 0;
306 
307  for (i = 0; i < args->only_count; ++i) {
308  if (strcasecmp("build", args->only[i]) == 0) {
309  show_build = 1;
310  } else if (strcasecmp("system", args->only[i]) == 0) {
311  show_system = 1;
312  } else if (strcasecmp("config", args->only[i]) == 0) {
313  show_config = 1;
314  } else if (strcasecmp("status", args->only[i]) == 0) {
315  show_status = 1;
316  } else {
317  ast_log(LOG_WARNING, "Unrecognized info section '%s'\n",
318  args->only[i]);
319  }
320  }
321 
322  json = ast_json_object_create();
323 
324  if (show_build) {
325  res |= ast_json_object_set(json, "build",
327  "{ s: s, s: s, s: s,"
328  " s: s, s: s, s: s }",
329 
330  "os", ast_build_os,
331  "kernel", ast_build_kernel,
332  "machine", ast_build_machine,
333 
334  "options", AST_BUILDOPTS,
335  "date", ast_build_date,
336  "user", ast_build_user));
337  }
338 
339  if (show_system) {
340  char eid_str[128];
341 
342  ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
343 
344  res |= ast_json_object_set(json, "system",
345  ast_json_pack("{ s: s, s: s }",
346  "version", ast_get_version(),
347  "entity_id", eid_str));
348  }
349 
350  if (show_config) {
351  struct ast_json *config = ast_json_pack(
352  "{ s: s, s: s,"
353  " s: { s: s, s: s } }",
354 
356  "default_language", ast_defaultlanguage,
357 
358  "setid",
359  "user", ast_config_AST_RUN_USER,
360  "group", ast_config_AST_RUN_GROUP);
361 
362  res |= ast_json_object_set(json, "config", config);
363 
364  if (ast_option_maxcalls) {
365  res |= ast_json_object_set(config, "max_channels",
367  }
368 
369  if (ast_option_maxfiles) {
370  res |= ast_json_object_set(config, "max_open_files",
372  }
373 
374  if (ast_option_maxload) {
375  res |= ast_json_object_set(config, "max_load",
377  }
378  }
379 
380  if (show_status) {
381  res |= ast_json_object_set(json, "status",
382  ast_json_pack("{ s: o, s: o }",
383  "startup_time",
385  "last_reload_time",
387  }
388 
389  if (res != 0) {
391  return;
392  }
393 
394  ast_ari_response_ok(response, ast_json_ref(json));
395 }
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
int ast_option_maxfiles
Definition: options.c:81
const char * ast_build_user
Definition: buildinfo.c:34
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
char * config
Definition: conf2ael.c:66
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
Definition: main/utils.c:2587
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
const char * ast_get_version(void)
Retrieve the Asterisk version string.
Definition: version.c:16
#define LOG_WARNING
Definition: logger.h:274
const char * ast_build_date
Definition: buildinfo.c:33
const char * ast_build_os
Definition: buildinfo.c:32
double ast_option_maxload
Definition: options.c:77
const char * ast_config_AST_RUN_USER
Definition: options.c:168
#define NULL
Definition: resample.c:96
void ast_ari_response_alloc_failed(struct ast_ari_response *response)
Fill in response with a 500 message for allocation failures.
Definition: res_ari.c:298
int ast_option_maxcalls
Definition: options.c:79
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
Definition: json.c:404
const char * ast_build_kernel
Definition: buildinfo.c:30
const char * ast_config_AST_SYSTEM_NAME
Definition: options.c:170
const char * ast_config_AST_RUN_GROUP
Definition: options.c:169
const char * ast_build_machine
Definition: buildinfo.c:31
#define ast_log
Definition: astobj2.c:42
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
char ast_defaultlanguage[]
Definition: options.c:98
void ast_ari_response_ok(struct ast_ari_response *response, struct ast_json *message)
Fill in an OK (200) ast_ari_response.
Definition: res_ari.c:276
struct timeval ast_lastreloadtime
Definition: asterisk.c:337
struct ast_json * ast_json_timeval(const struct timeval tv, const char *zone)
Construct a timeval as JSON.
Definition: json.c:649
#define AST_BUILDOPTS
Definition: buildopts.h:11
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93
struct timeval ast_startuptime
Definition: asterisk.c:336
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
Definition: json.c:389
struct ast_json * ast_json_real_create(double value)
Create a JSON real number.
Definition: json.c:332
Abstract JSON element (object, array, string, int, ...).
struct ast_json * ast_json_integer_create(intmax_t value)
Create a JSON integer.
Definition: json.c:317

◆ ast_ari_asterisk_get_module()

void ast_ari_asterisk_get_module ( struct ast_variable headers,
struct ast_ari_asterisk_get_module_args args,
struct ast_ari_response response 
)

Get Asterisk module information.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 484 of file resource_asterisk.c.

References ast_ari_response_alloc_failed(), ast_ari_response_error(), ast_ari_response_ok(), ast_assert, ast_json_object_create(), ast_json_unref(), ast_module_check(), ast_update_module_list_condition(), identify_module(), ast_ari_asterisk_get_module_args::module_name, and NULL.

Referenced by ast_ari_asterisk_get_module_cb().

487 {
488  struct ast_json *json;
489  int module_retrieved = 0;
490 
491  ast_assert(response != NULL);
492 
493  if (!ast_module_check(args->module_name)) {
495  response, 404, "Not Found",
496  "Module could not be found in running modules");
497  return;
498  }
499 
500  json = ast_json_object_create();
501  if (!json) {
503  return;
504  }
505 
506  module_retrieved = ast_update_module_list_condition(&identify_module, NULL, json,
507  args->module_name);
508  if (!module_retrieved) {
510  response, 409, "Conflict",
511  "Module information could not be retrieved");
512  ast_json_unref(json);
513  return;
514  }
515 
516  ast_ari_response_ok(response, json);
517 }
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
int ast_update_module_list_condition(int(*modentry)(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), const char *like, void *data, const char *condition)
Ask for a list of modules, descriptions, use counts and status.
Definition: loader.c:2622
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
void ast_ari_response_alloc_failed(struct ast_ari_response *response)
Fill in response with a 500 message for allocation failures.
Definition: res_ari.c:298
int ast_module_check(const char *name)
Check if module with the name given is loaded.
Definition: loader.c:2653
void ast_ari_response_ok(struct ast_ari_response *response, struct ast_json *message)
Fill in an OK (200) ast_ari_response.
Definition: res_ari.c:276
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
Definition: json.c:389
Abstract JSON element (object, array, string, int, ...).
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)
Identify module by name and process resource information.

◆ ast_ari_asterisk_get_object()

void ast_ari_asterisk_get_object ( struct ast_variable headers,
struct ast_ari_asterisk_get_object_args args,
struct ast_ari_response response 
)

Retrieve a dynamic configuration object.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 89 of file resource_asterisk.c.

References ao2_cleanup, ast_ari_response_error(), ast_sorcery_get_object_type(), ast_sorcery_retrieve_by_id(), ast_sorcery_retrieve_by_module_name(), ast_sorcery_unref, ast_ari_asterisk_get_object_args::config_class, ast_ari_asterisk_get_object_args::id, NULL, ast_ari_asterisk_get_object_args::object_type, RAII_VAR, return_sorcery_object(), and sorcery.

Referenced by ast_ari_asterisk_get_object_cb().

92 {
94  RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
95  RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup);
96 
97 
99  if (!sorcery) {
101  response, 404, "Not Found",
102  "configClass '%s' not found",
103  args->config_class);
104  return;
105  }
106 
107  object_type = ast_sorcery_get_object_type(sorcery, args->object_type);
108  if (!object_type) {
110  response, 404, "Not Found",
111  "objectType '%s' not found",
112  args->object_type);
113  return;
114  }
115 
116  sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args->object_type, args->id);
117  if (!sorcery_obj) {
119  response, 404, "Not Found",
120  "Object with id '%s' not found",
121  args->id);
122  return;
123  }
124 
125  return_sorcery_object(sorcery, sorcery_obj, response);
126 }
Full structure for sorcery.
Definition: sorcery.c:230
#define NULL
Definition: resample.c:96
static void return_sorcery_object(struct ast_sorcery *sorcery, void *sorcery_obj, struct ast_ari_response *response)
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition: sorcery.h:1502
Structure for registered object type.
Definition: sorcery.c:148
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
struct ast_sorcery_object_type * ast_sorcery_get_object_type(const struct ast_sorcery *sorcery, const char *type)
Get the sorcery object type given a type name.
Definition: sorcery.c:2489
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
struct ast_sorcery * ast_sorcery_retrieve_by_module_name(const char *module)
Retrieves an existing sorcery instance by module name.
Definition: sorcery.c:672

◆ ast_ari_asterisk_list_log_channels()

void ast_ari_asterisk_list_log_channels ( struct ast_variable headers,
struct ast_ari_asterisk_list_log_channels_args args,
struct ast_ari_response response 
)

Gets Asterisk log channel information.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 680 of file resource_asterisk.c.

References ast_ari_response_error(), ast_ari_response_ok(), ast_json_array_create(), ast_json_unref(), AST_LOGGER_ALLOC_ERROR, AST_LOGGER_FAILURE, ast_logger_get_channels(), and process_log_list().

Referenced by ast_ari_asterisk_list_log_channels_cb().

683 {
684  struct ast_json *json;
685  int res;
686 
687  json = ast_json_array_create();
689 
690  if (res == AST_LOGGER_FAILURE) {
691  ast_ari_response_error(response, 500, "Internal Server Error",
692  "Response body is not valid");
693  ast_json_unref(json);
694  return;
695  } else if (res == AST_LOGGER_ALLOC_ERROR) {
696  ast_ari_response_error(response, 500, "Internal Server Error",
697  "Allocation Failed");
698  ast_json_unref(json);
699  return;
700  }
701 
702  ast_ari_response_ok(response, json);
703 }
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
int ast_logger_get_channels(int(*logentry)(const char *channel, const char *type, const char *status, const char *configuration, void *data), void *data)
Retrieve the existing log channels.
Definition: logger.c:1336
void ast_ari_response_ok(struct ast_ari_response *response, struct ast_json *message)
Fill in an OK (200) ast_ari_response.
Definition: res_ari.c:276
static int process_log_list(const char *channel, const char *type, const char *status, const char *configuration, void *log_data_list)
Process logger information and append to a json array.
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:352
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
Abstract JSON element (object, array, string, int, ...).

◆ ast_ari_asterisk_list_modules()

void ast_ari_asterisk_list_modules ( struct ast_variable headers,
struct ast_ari_asterisk_list_modules_args args,
struct ast_ari_response response 
)

List Asterisk modules.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 429 of file resource_asterisk.c.

References ast_ari_response_alloc_failed(), ast_ari_response_ok(), ast_json_array_create(), ast_update_module_list_data(), NULL, and process_module_list().

Referenced by ast_ari_asterisk_list_modules_cb().

432 {
433  struct ast_json *json;
434 
435  json = ast_json_array_create();
436  if (!json) {
438  return;
439  }
441 
442  ast_ari_response_ok(response, json);
443 }
int ast_update_module_list_data(int(*modentry)(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 *like, void *data)
Ask for a list of modules, descriptions, use counts and status.
Definition: loader.c:2594
#define NULL
Definition: resample.c:96
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)
Process module information and append to a json array.
void ast_ari_response_alloc_failed(struct ast_ari_response *response)
Fill in response with a 500 message for allocation failures.
Definition: res_ari.c:298
void ast_ari_response_ok(struct ast_ari_response *response, struct ast_json *message)
Fill in an OK (200) ast_ari_response.
Definition: res_ari.c:276
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:352
Abstract JSON element (object, array, string, int, ...).

◆ ast_ari_asterisk_load_module()

void ast_ari_asterisk_load_module ( struct ast_variable headers,
struct ast_ari_asterisk_load_module_args args,
struct ast_ari_response response 
)

Load an Asterisk module.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 519 of file resource_asterisk.c.

References ast_ari_response_error(), ast_ari_response_no_content(), ast_assert, ast_load_resource(), ast_module_check(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SKIP, ast_ari_asterisk_load_module_args::module_name, and NULL.

Referenced by ast_ari_asterisk_load_module_cb().

522 {
523  enum ast_module_load_result load_result;
524 
525  ast_assert(response != NULL);
526 
527  if (ast_module_check(args->module_name)) {
529  response, 409, "Conflict",
530  "Module is already loaded");
531  return;
532  }
533 
534  load_result = ast_load_resource(args->module_name);
535 
536  if (load_result == AST_MODULE_LOAD_DECLINE) {
538  response, 409, "Conflict",
539  "Module load declined");
540  return;
541  } else if (load_result == AST_MODULE_LOAD_SKIP) {
543  response, 409, "Conflict",
544  "Module was skipped");
545  return;
546  } else if (load_result == AST_MODULE_LOAD_FAILURE) {
548  response, 409, "Conflict",
549  "Module could not be loaded properly");
550  return;
551  }
552 
553  ast_ari_response_no_content(response);
554 }
ast_module_load_result
Definition: module.h:68
enum ast_module_load_result ast_load_resource(const char *resource_name)
Load a module.
Definition: loader.c:1819
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
int ast_module_check(const char *name)
Check if module with the name given is loaded.
Definition: loader.c:2653
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
Module could not be loaded properly.
Definition: module.h:102
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
void ast_ari_response_no_content(struct ast_ari_response *response)
Fill in a No Content (204) ast_ari_response.
Definition: res_ari.c:284

◆ ast_ari_asterisk_ping()

void ast_ari_asterisk_ping ( struct ast_variable headers,
struct ast_ari_asterisk_ping_args args,
struct ast_ari_response response 
)

Response pong message.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 634 of file resource_asterisk.c.

References ast_ari_response_ok(), ast_assert, ast_eid_default, ast_eid_to_str(), ast_json_pack(), ast_json_timeval(), ast_tvnow(), and NULL.

Referenced by ast_ari_asterisk_ping_cb().

637 {
638  struct ast_json *json;
639  char eid[20];
640 
641  ast_assert(response != NULL);
642 
643  json = ast_json_pack("{s: s, s: o, s: s}",
644  "ping", "pong",
645  "timestamp", ast_json_timeval(ast_tvnow(), NULL),
646  "asterisk_id", ast_eid_to_str(eid, sizeof(eid), &ast_eid_default)
647  );
648 
649  ast_ari_response_ok(response, json);
650 }
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
Definition: main/utils.c:2587
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
void ast_ari_response_ok(struct ast_ari_response *response, struct ast_json *message)
Fill in an OK (200) ast_ari_response.
Definition: res_ari.c:276
struct ast_json * ast_json_timeval(const struct timeval tv, const char *zone)
Construct a timeval as JSON.
Definition: json.c:649
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93
Abstract JSON element (object, array, string, int, ...).

◆ ast_ari_asterisk_reload_module()

void ast_ari_asterisk_reload_module ( struct ast_variable headers,
struct ast_ari_asterisk_reload_module_args args,
struct ast_ari_response response 
)

Reload an Asterisk module.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 584 of file resource_asterisk.c.

References ast_ari_response_accepted(), ast_ari_response_error(), ast_ari_response_no_content(), ast_assert, ast_module_check(), ast_module_reload(), AST_MODULE_RELOAD_ERROR, AST_MODULE_RELOAD_IN_PROGRESS, AST_MODULE_RELOAD_NOT_FOUND, AST_MODULE_RELOAD_NOT_IMPLEMENTED, AST_MODULE_RELOAD_QUEUED, AST_MODULE_RELOAD_UNINITIALIZED, ast_ari_asterisk_reload_module_args::module_name, and NULL.

Referenced by ast_ari_asterisk_reload_module_cb().

587 {
588  enum ast_module_reload_result reload_result;
589 
590  ast_assert(response != NULL);
591 
592  if (!ast_module_check(args->module_name)) {
594  response, 404, "Not Found",
595  "Module not found in running modules");
596  return;
597  }
598 
599  reload_result = ast_module_reload(args->module_name);
600 
601  if (reload_result == AST_MODULE_RELOAD_NOT_FOUND) {
603  response, 404, "Not Found",
604  "Module could not be found");
605  return;
606  } else if (reload_result == AST_MODULE_RELOAD_ERROR) {
608  response, 409, "Conflict",
609  "An unknown error occurred while reloading the module");
610  return;
611  } else if (reload_result == AST_MODULE_RELOAD_IN_PROGRESS) {
613  response, 409, "Conflict",
614  "Another reload is currently in progress");
615  return;
616  } else if (reload_result == AST_MODULE_RELOAD_UNINITIALIZED) {
618  response, 409, "Conflict",
619  "Module has not been initialized");
620  return;
621  } else if (reload_result == AST_MODULE_RELOAD_NOT_IMPLEMENTED) {
623  response, 409, "Conflict",
624  "Module does not support reloading");
625  return;
626  } else if (reload_result == AST_MODULE_RELOAD_QUEUED) {
627  ast_ari_response_accepted(response);
628  return;
629  }
630 
631  ast_ari_response_no_content(response);
632 }
enum ast_module_reload_result ast_module_reload(const char *name)
Reload asterisk modules.
Definition: loader.c:1562
ast_module_reload_result
Possible return types for ast_module_reload.
Definition: module.h:109
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
int ast_module_check(const char *name)
Check if module with the name given is loaded.
Definition: loader.c:2653
void ast_ari_response_accepted(struct ast_ari_response *response)
Fill in a Accepted (202) ast_ari_response.
Definition: res_ari.c:291
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
void ast_ari_response_no_content(struct ast_ari_response *response)
Fill in a No Content (204) ast_ari_response.
Definition: res_ari.c:284

◆ ast_ari_asterisk_rotate_log()

void ast_ari_asterisk_rotate_log ( struct ast_variable headers,
struct ast_ari_asterisk_rotate_log_args args,
struct ast_ari_response response 
)

Rotates a log channel.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 732 of file resource_asterisk.c.

References ast_ari_response_error(), ast_ari_response_no_content(), ast_assert, AST_LOGGER_ALLOC_ERROR, AST_LOGGER_FAILURE, ast_logger_rotate_channel(), ast_ari_asterisk_rotate_log_args::log_channel_name, and NULL.

Referenced by ast_ari_asterisk_rotate_log_cb().

735 {
736  int res;
737 
738  ast_assert(response != NULL);
739 
741 
742  if (res == AST_LOGGER_FAILURE) {
744  response, 404, "Not Found",
745  "Log channel does not exist");
746  return;
747  } else if (res == AST_LOGGER_ALLOC_ERROR) {
749  response, 500, "Internal Server Error",
750  "Allocation failed");
751  return;
752  }
753 
754  ast_ari_response_no_content(response);
755 }
int ast_logger_rotate_channel(const char *log_channel)
Rotate the specified log channel.
Definition: logger.c:1259
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
void ast_ari_response_no_content(struct ast_ari_response *response)
Fill in a No Content (204) ast_ari_response.
Definition: res_ari.c:284

◆ ast_ari_asterisk_set_global_var()

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 
)

Set the value of a global variable.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 814 of file resource_asterisk.c.

References ast_ari_response_error(), ast_ari_response_no_content(), ast_assert, ast_strlen_zero, NULL, pbx_builtin_setvar_helper(), ast_ari_asterisk_set_global_var_args::value, and ast_ari_asterisk_set_global_var_args::variable.

Referenced by ast_ari_asterisk_set_global_var_cb().

817 {
818  ast_assert(response != NULL);
819 
820  if (ast_strlen_zero(args->variable)) {
822  response, 400, "Bad Request",
823  "Variable name is required");
824  return;
825  }
826 
828 
829  ast_ari_response_no_content(response);
830 }
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
void ast_ari_response_no_content(struct ast_ari_response *response)
Fill in a No Content (204) ast_ari_response.
Definition: res_ari.c:284
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...

◆ ast_ari_asterisk_unload_module()

void ast_ari_asterisk_unload_module ( struct ast_variable headers,
struct ast_ari_asterisk_unload_module_args args,
struct ast_ari_response response 
)

Unload an Asterisk module.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 556 of file resource_asterisk.c.

References ast_ari_response_error(), ast_ari_response_no_content(), ast_assert, AST_FORCE_SOFT, ast_module_check(), ast_unload_resource(), ast_ari_asterisk_unload_module_args::module_name, and NULL.

Referenced by ast_ari_asterisk_unload_module_cb().

559 {
560  int unload_result;
561  enum ast_module_unload_mode unload_mode = AST_FORCE_SOFT;
562 
563  ast_assert(response != NULL);
564 
565  if (!ast_module_check(args->module_name)) {
567  response, 404, "Not Found",
568  "Module not found in running modules");
569  return;
570  }
571 
572  unload_result = ast_unload_resource(args->module_name, unload_mode);
573 
574  if (unload_result != 0) {
576  response, 409, "Conflict",
577  "Module could not be unloaded");
578  return;
579  }
580 
581  ast_ari_response_no_content(response);
582 }
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
int ast_module_check(const char *name)
Check if module with the name given is loaded.
Definition: loader.c:2653
ast_module_unload_mode
Definition: module.h:61
int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode)
Unload a module.
Definition: loader.c:1229
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
void ast_ari_response_no_content(struct ast_ari_response *response)
Fill in a No Content (204) ast_ari_response.
Definition: res_ari.c:284

◆ ast_ari_asterisk_update_object()

void ast_ari_asterisk_update_object ( struct ast_variable headers,
struct ast_ari_asterisk_update_object_args args,
struct ast_ari_response response 
)

Create or update a dynamic configuration object.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 128 of file resource_asterisk.c.

References ao2_cleanup, ao2_ref, ast_ari_response_alloc_failed(), ast_ari_response_error(), ast_debug, ast_json_array_get(), ast_json_array_size(), ast_json_object_get(), ast_json_string_get(), ast_sorcery_alloc(), ast_sorcery_copy(), ast_sorcery_create(), ast_sorcery_get_object_type(), ast_sorcery_objectset_apply(), ast_sorcery_retrieve_by_id(), ast_sorcery_retrieve_by_module_name(), ast_sorcery_unref, ast_sorcery_update(), ast_variable_list_append, ast_variable_new, ast_variables_destroy(), ast_ari_asterisk_update_object_args::config_class, copy(), ast_ari_asterisk_update_object_args::fields, ast_ari_asterisk_update_object_args::id, NULL, ast_ari_asterisk_update_object_args::object_type, RAII_VAR, return_sorcery_object(), and sorcery.

Referenced by ast_ari_asterisk_update_object_cb().

129 {
131  RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
132  RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup);
133  struct ast_json *fields;
134  struct ast_variable *update_set = NULL;
135  int created = 0;
136 
138  if (!sorcery) {
140  response, 404, "Not Found",
141  "configClass '%s' not found",
142  args->config_class);
143  return;
144  }
145 
146  object_type = ast_sorcery_get_object_type(sorcery, args->object_type);
147  if (!object_type) {
149  response, 404, "Not Found",
150  "objectType '%s' not found",
151  args->object_type);
152  return;
153  }
154 
155  sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args->object_type, args->id);
156  if (!sorcery_obj) {
157  ast_debug(5, "Sorcery object '%s' does not exist; creating it\n", args->id);
158  sorcery_obj = ast_sorcery_alloc(sorcery, args->object_type, args->id);
159  if (!sorcery_obj) {
161  return;
162  }
163 
164  created = 1;
165  } else {
166  void *copy;
167 
168  copy = ast_sorcery_copy(sorcery, sorcery_obj);
169  if (!copy) {
171  return;
172  }
173 
174  ao2_ref(sorcery_obj, -1);
175  sorcery_obj = copy;
176  }
177 
178  fields = ast_json_object_get(args->fields, "fields");
179  if (!fields && !created) {
180  /* Whoops. We need data. */
182  response, 400, "Bad request",
183  "Fields must be provided to update object '%s'",
184  args->id);
185  return;
186  } else if (fields) {
187  size_t i;
188 
189  for (i = 0; i < ast_json_array_size(fields); i++) {
190  struct ast_variable *new_var;
191  struct ast_json *json_value = ast_json_array_get(fields, i);
192 
193  if (!json_value) {
194  continue;
195  }
196 
197  new_var = ast_variable_new(
198  ast_json_string_get(ast_json_object_get(json_value, "attribute")),
199  ast_json_string_get(ast_json_object_get(json_value, "value")),
200  "");
201  if (!new_var) {
202  ast_variables_destroy(update_set);
204  return;
205  }
206  ast_variable_list_append(&update_set, new_var);
207  }
208  }
209 
210  /* APPLY! Note that a NULL update_set is fine (and necessary), as it
211  * will force validation on a newly created object.
212  */
213  if (ast_sorcery_objectset_apply(sorcery, sorcery_obj, update_set)) {
214  ast_variables_destroy(update_set);
216  response, 400, "Bad request",
217  "%s of object '%s' failed field value validation",
218  created ? "Creation" : "Update",
219  args->id);
220  return;
221  }
222 
223  ast_variables_destroy(update_set);
224 
225  if (created) {
226  if (ast_sorcery_create(sorcery, sorcery_obj)) {
228  response, 403, "Forbidden",
229  "Cannot create sorcery objects of type '%s'",
230  args->object_type);
231  return;
232  }
233  } else {
234  if (ast_sorcery_update(sorcery, sorcery_obj)) {
236  response, 403, "Forbidden",
237  "Cannot update sorcery objects of type '%s'",
238  args->object_type);
239  return;
240  }
241  }
242 
243  return_sorcery_object(sorcery, sorcery_obj, response);
244 }
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1263
Structure for variables, used for configurations and for channel variables.
Full structure for sorcery.
Definition: sorcery.c:230
int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
Apply an object set (KVP list) to an object.
Definition: sorcery.c:1632
static int copy(char *infile, char *outfile)
Utility function to copy a file.
#define NULL
Definition: resample.c:96
static void return_sorcery_object(struct ast_sorcery *sorcery, void *sorcery_obj, struct ast_ari_response *response)
void ast_ari_response_alloc_failed(struct ast_ari_response *response)
Fill in response with a 500 message for allocation failures.
Definition: res_ari.c:298
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition: sorcery.h:1502
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
Create and potentially persist an object using an available wizard.
Definition: sorcery.c:2057
Structure for registered object type.
Definition: sorcery.c:148
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ao2_ref(o, delta)
Definition: astobj2.h:464
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:273
#define ast_variable_new(name, value, filename)
struct ast_sorcery_object_type * ast_sorcery_get_object_type(const struct ast_sorcery *sorcery, const char *type)
Get the sorcery object type given a type name.
Definition: sorcery.c:2489
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
void * ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
Create a copy of an object.
Definition: sorcery.c:1778
size_t ast_json_array_size(const struct ast_json *array)
Get the size of a JSON array.
Definition: json.c:356
Abstract JSON element (object, array, string, int, ...).
struct ast_json * ast_json_array_get(const struct ast_json *array, size_t index)
Get an element from an array.
Definition: json.c:360
#define ast_variable_list_append(head, new_var)
struct ast_sorcery * ast_sorcery_retrieve_by_module_name(const char *module)
Retrieves an existing sorcery instance by module name.
Definition: sorcery.c:672
int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
Update an object.
Definition: sorcery.c:2145

◆ identify_module()

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 
)
static

Identify module by name and process resource information.

Parameters
moduleResource name
descriptionResource description
usecntResource use count
statusResource running status
like
support_levelResource support level
dataJSON body for resource
conditionName to match resource to
Return values
0if no resource exists
1if resource exists

Definition at line 459 of file resource_asterisk.c.

References ast_json_integer_create(), ast_json_object_set(), ast_json_string_create(), and ast_module_support_level_to_string().

Referenced by ast_ari_asterisk_get_module().

463 {
464  int json_obj_set = 0;
465 
466  if (strcmp(condition, module) != 0) {
467  return 0;
468  }
469 
470  json_obj_set += ast_json_object_set(data, "name", ast_json_string_create(module));
471  json_obj_set += ast_json_object_set(data, "description", ast_json_string_create(description));
472  json_obj_set += ast_json_object_set(data, "use_count", ast_json_integer_create(usecnt));
473  json_obj_set += ast_json_object_set(data, "status", ast_json_string_create(status));
474  json_obj_set += ast_json_object_set(data, "support_level", ast_json_string_create(
475  ast_module_support_level_to_string(support_level)));
476 
477  if (json_obj_set != 0) {
478  return 0;
479  }
480 
481  return 1;
482 }
const char * ast_module_support_level_to_string(enum ast_module_support_level support_level)
Definition: loader.c:2755
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
Definition: json.c:404
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:268
static int usecnt
Definition: chan_ooh323.c:332
jack_status_t status
Definition: app_jack.c:146
struct ast_json * ast_json_integer_create(intmax_t value)
Create a JSON integer.
Definition: json.c:317

◆ process_log_list()

static int process_log_list ( const char *  channel,
const char *  type,
const char *  status,
const char *  configuration,
void *  log_data_list 
)
static

Process logger information and append to a json array.

Parameters
channelResource logger channel name path
typeResource log type
statusResource log status
configurationResource logger levels
log_data_listResource array
Return values
-1if no resource exists
0if resource exists

Definition at line 663 of file resource_asterisk.c.

References ast_json_array_append(), ast_json_pack(), AST_LOGGER_FAILURE, and AST_LOGGER_SUCCESS.

Referenced by ast_ari_asterisk_list_log_channels().

665 {
666  struct ast_json *logger_info;
667 
668  logger_info = ast_json_pack("{s: s, s: s, s: s, s: s}",
669  "channel", channel, "type", type, "status", status, "configuration",
670  configuration);
671 
672  if (!logger_info) {
673  return AST_LOGGER_FAILURE;
674  }
675 
676  ast_json_array_append(log_data_list, logger_info);
677  return AST_LOGGER_SUCCESS;
678 }
static const char type[]
Definition: chan_ooh323.c:109
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
Definition: muted.c:95
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:368
Abstract JSON element (object, array, string, int, ...).
jack_status_t status
Definition: app_jack.c:146

◆ process_module_list()

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 
)
static

Process module information and append to a json array.

Parameters
moduleResource name
descriptionResource description
usecntResource use count
statusResource running status
like
support_levelResource support level
module_data_listResource array
Return values
0if no resource exists
1if resource exists

Definition at line 410 of file resource_asterisk.c.

References ast_json_array_append(), ast_json_pack(), and ast_module_support_level_to_string().

Referenced by ast_ari_asterisk_list_modules().

413 {
414  struct ast_json *module_info;
415 
416  module_info = ast_json_pack("{s: s, s: s, s: i, s: s, s: s}",
417  "name", module,
418  "description", description,
419  "use_count", usecnt,
420  "status", status,
421  "support_level", ast_module_support_level_to_string(support_level));
422  if (!module_info) {
423  return 0;
424  }
425  ast_json_array_append(module_data_list, module_info);
426  return 1;
427 }
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
const char * ast_module_support_level_to_string(enum ast_module_support_level support_level)
Definition: loader.c:2755
static int usecnt
Definition: chan_ooh323.c:332
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:368
Abstract JSON element (object, array, string, int, ...).
jack_status_t status
Definition: app_jack.c:146

◆ return_sorcery_object()

static void return_sorcery_object ( struct ast_sorcery sorcery,
void *  sorcery_obj,
struct ast_ari_response response 
)
static

Definition at line 41 of file resource_asterisk.c.

References ast_ari_response_alloc_failed(), ast_ari_response_ok(), ast_json_array_append(), ast_json_array_create(), ast_json_pack(), ast_json_ref(), ast_json_unref(), ast_sorcery_objectset_create, ast_variables_destroy(), ast_variable::name, ast_variable::next, NULL, RAII_VAR, and ast_variable::value.

Referenced by ast_ari_asterisk_get_object(), and ast_ari_asterisk_update_object().

43 {
44  RAII_VAR(struct ast_json *, return_set, NULL, ast_json_unref);
45  struct ast_variable *change_set;
46  struct ast_variable *it_change_set;
47 
48  return_set = ast_json_array_create();
49  if (!return_set) {
51  return;
52  }
53 
54  /* Note that we can't use the sorcery JSON change set directly,
55  * as it will hand us back an Object (with fields), and we need
56  * a more generic representation of whatever the API call asked
57  * for, i.e., a list of tuples.
58  */
59  change_set = ast_sorcery_objectset_create(sorcery, sorcery_obj);
60  if (!change_set) {
62  return;
63  }
64 
65  for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
66  struct ast_json *tuple;
67 
68  tuple = ast_json_pack("{s: s, s: s}",
69  "attribute", it_change_set->name,
70  "value", it_change_set->value);
71  if (!tuple) {
72  ast_variables_destroy(change_set);
74  return;
75  }
76 
77  if (ast_json_array_append(return_set, tuple)) {
78  ast_json_unref(tuple);
79  ast_variables_destroy(change_set);
81  return;
82  }
83  }
84  ast_variables_destroy(change_set);
85 
86  ast_ari_response_ok(response, ast_json_ref(return_set));
87 }
struct ast_variable * next
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1263
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
Structure for variables, used for configurations and for channel variables.
#define NULL
Definition: resample.c:96
void ast_ari_response_alloc_failed(struct ast_ari_response *response)
Fill in response with a 500 message for allocation failures.
Definition: res_ari.c:298
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
void ast_ari_response_ok(struct ast_ari_response *response, struct ast_json *message)
Fill in an OK (200) ast_ari_response.
Definition: res_ari.c:276
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:352
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:368
#define ast_sorcery_objectset_create(sorcery, object)
Create an object set (KVP list) for an object.
Definition: sorcery.h:1136
Abstract JSON element (object, array, string, int, ...).