diff --git a/tools/crm_resource.c b/tools/crm_resource.c index 38ea71f..72579c1 100644 --- a/tools/crm_resource.c +++ b/tools/crm_resource.c @@ -1378,7 +1378,7 @@ show_metadata(pcmk__output_t *out, const char *agent_spec, crm_exit_t *exit_code rc = pcmk_legacy2rc(rc); if (metadata) { - printf("%s\n", metadata); + out->output_xml(out, "metadata", metadata); } else { *exit_code = crm_errno2exit(rc); g_set_error(&error, PCMK__EXITC_ERROR, *exit_code, @@ -1904,17 +1904,47 @@ main(int argc, char **argv) break; case cmd_get_property: - rc = cli_resource_print_property(out, rsc, options.prop_name, data_set); + rc = out->message(out, "property", rsc, options.prop_name); + if (rc == pcmk_rc_no_output) { + rc = ENXIO; + } + break; case cmd_set_property: rc = set_property(); break; - case cmd_get_param: - rc = cli_resource_print_attribute(out, rsc, options.prop_name, - options.attr_set_type, data_set); + case cmd_get_param: { + unsigned int count = 0; + GHashTable *params = NULL; + pe_node_t *current = pe__find_active_on(rsc, &count, NULL); + + if (count > 1) { + out->err(out, "%s is active on more than one node," + " returning the default value for %s", rsc->id, crm_str(options.prop_name)); + current = NULL; + } + + params = crm_str_table_new(); + + if (pcmk__str_eq(options.attr_set_type, XML_TAG_ATTR_SETS, pcmk__str_casei)) { + get_rsc_attributes(params, rsc, current, data_set); + + } else if (pcmk__str_eq(options.attr_set_type, XML_TAG_META_SETS, pcmk__str_casei)) { + /* No need to redirect to the parent */ + get_meta_attributes(params, rsc, current, data_set); + + } else { + pe__unpack_dataset_nvpairs(rsc->xml, XML_TAG_UTILIZATION, NULL, params, + NULL, FALSE, data_set); + } + + crm_debug("Looking up %s in %s", options.prop_name, rsc->id); + rc = out->message(out, "attribute", rsc, options.prop_name, params); + g_hash_table_destroy(params); break; + } case cmd_set_param: if (pcmk__str_empty(options.prop_value)) { diff --git a/tools/crm_resource.h b/tools/crm_resource.h index 6b6dab2..4fc7c71 100644 --- a/tools/crm_resource.h +++ b/tools/crm_resource.h @@ -46,11 +46,6 @@ void cli_resource_print_colocation(pcmk__output_t *out, pe_resource_t * rsc, int cli_resource_print(pcmk__output_t *out, pe_resource_t *rsc, pe_working_set_t *data_set, bool expanded); -int cli_resource_print_attribute(pcmk__output_t *out, pe_resource_t *rsc, - const char *attr, const char *attr_set_type, - pe_working_set_t *data_set); -int cli_resource_print_property(pcmk__output_t *out, pe_resource_t *rsc, const char *attr, - pe_working_set_t *data_set); int cli_resource_print_operations(pcmk__output_t *out, const char *rsc_id, const char *host_uname, bool active, pe_working_set_t * data_set); diff --git a/tools/crm_resource_print.c b/tools/crm_resource_print.c index f7356fb..093eb75 100644 --- a/tools/crm_resource_print.c +++ b/tools/crm_resource_print.c @@ -235,63 +235,74 @@ cli_resource_print(pcmk__output_t *out, pe_resource_t *rsc, return pcmk_rc_ok; } -// \return Standard Pacemaker return code -int -cli_resource_print_attribute(pcmk__output_t *out, pe_resource_t *rsc, const char *attr, - const char *attr_set_type, pe_working_set_t * data_set) -{ - int rc = ENXIO; - unsigned int count = 0; - GHashTable *params = NULL; - const char *value = NULL; - pe_node_t *current = pe__find_active_on(rsc, &count, NULL); - - if (count > 1) { - CMD_ERR("%s is active on more than one node," - " returning the default value for %s", rsc->id, crm_str(attr)); - current = NULL; +PCMK__OUTPUT_ARGS("attribute", "pe_resource_t *", "char *", "GHashTable *") +static int +attribute_default(pcmk__output_t *out, va_list args) { + pe_resource_t *rsc = va_arg(args, pe_resource_t *); + char *attr = va_arg(args, char *); + GHashTable *params = va_arg(args, GHashTable *); + + const char *value = g_hash_table_lookup(params, attr); + + if (value != NULL) { + out->begin_list(out, NULL, NULL, "Attributes"); + out->list_item(out, attr, "%s", value); + out->end_list(out); + } else { + out->err(out, "Attribute '%s' not found for '%s'", attr, rsc->id); } - params = crm_str_table_new(); + return pcmk_rc_ok; +} - if (pcmk__str_eq(attr_set_type, XML_TAG_ATTR_SETS, pcmk__str_casei)) { - get_rsc_attributes(params, rsc, current, data_set); +PCMK__OUTPUT_ARGS("attribute", "pe_resource_t *", "char *", "GHashTable *") +static int +attribute_text(pcmk__output_t *out, va_list args) { + pe_resource_t *rsc = va_arg(args, pe_resource_t *); + char *attr = va_arg(args, char *); + GHashTable *params = va_arg(args, GHashTable *); - } else if (pcmk__str_eq(attr_set_type, XML_TAG_META_SETS, pcmk__str_casei)) { - /* No need to redirect to the parent */ - get_meta_attributes(params, rsc, current, data_set); + const char *value = g_hash_table_lookup(params, attr); + if (value != NULL) { + out->info(out, "%s", value); } else { - pe__unpack_dataset_nvpairs(rsc->xml, XML_TAG_UTILIZATION, NULL, params, - NULL, FALSE, data_set); + out->err(out, "Attribute '%s' not found for '%s'", attr, rsc->id); } - crm_debug("Looking up %s in %s", attr, rsc->id); - value = g_hash_table_lookup(params, attr); - if (value != NULL) { - fprintf(stdout, "%s\n", value); - rc = pcmk_rc_ok; + return pcmk_rc_ok; +} - } else { - CMD_ERR("Attribute '%s' not found for '%s'", attr, rsc->id); +PCMK__OUTPUT_ARGS("property", "pe_resource_t *", "char *") +static int +property_default(pcmk__output_t *out, va_list args) { + pe_resource_t *rsc = va_arg(args, pe_resource_t *); + char *attr = va_arg(args, char *); + + const char *value = crm_element_value(rsc->xml, attr); + + if (value != NULL) { + out->begin_list(out, NULL, NULL, "Properties"); + out->list_item(out, attr, "%s", value); + out->end_list(out); } - g_hash_table_destroy(params); - return rc; + return pcmk_rc_ok; } -// \return Standard Pacemaker return code -int -cli_resource_print_property(pcmk__output_t *out, pe_resource_t *rsc, - const char *attr, pe_working_set_t * data_set) -{ +PCMK__OUTPUT_ARGS("property", "pe_resource_t *", "char *") +static int +property_text(pcmk__output_t *out, va_list args) { + pe_resource_t *rsc = va_arg(args, pe_resource_t *); + char *attr = va_arg(args, char *); + const char *value = crm_element_value(rsc->xml, attr); if (value != NULL) { - fprintf(stdout, "%s\n", value); - return pcmk_rc_ok; + out->info(out, "%s", value); } - return ENXIO; + + return pcmk_rc_ok; } static void @@ -328,6 +339,10 @@ resource_names(pcmk__output_t *out, va_list args) { } static pcmk__message_entry_t fmt_functions[] = { + { "attribute", "default", attribute_default }, + { "attribute", "text", attribute_text }, + { "property", "default", property_default }, + { "property", "text", property_text }, { "resource-names-list", "default", resource_names }, { NULL, NULL, NULL }