summaryrefslogtreecommitdiffstats
path: root/lib/lrmd/lrmd_output.c
blob: b0524c6346df47dae6748f5e4e81f68204c2ead1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright 2020 the Pacemaker project contributors
 *
 * The version control history for this file may have further details.
 *
 * This source code is licensed under the GNU Lesser General Public License
 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
 */

#include <crm_internal.h>
#include <stdarg.h>

#include <crm/lrmd_internal.h>
#include <crm/common/output_internal.h>

static int
default_list(pcmk__output_t *out, lrmd_list_t *list, const char *title) {
    lrmd_list_t *iter = NULL;

    out->begin_list(out, NULL, NULL, "%s", title);

    for (iter = list; iter != NULL; iter = iter->next) {
        out->list_item(out, NULL, "%s", iter->val);
    }

    out->end_list(out);
    lrmd_list_freeall(list);
    return pcmk_rc_ok;
}

static int
xml_list(pcmk__output_t *out, lrmd_list_t *list, const char *ele) {
    lrmd_list_t *iter = NULL;

    for (iter = list; iter != NULL; iter = iter->next) {
        pcmk__output_create_xml_text_node(out, ele, iter->val);
    }

    lrmd_list_freeall(list);
    return pcmk_rc_ok;
}

PCMK__OUTPUT_ARGS("alternatives-list", "lrmd_list_t *", "const char *")
static int
lrmd__alternatives_list_xml(pcmk__output_t *out, va_list args) {
    lrmd_list_t *list = va_arg(args, lrmd_list_t *);
    const char *agent_spec = va_arg(args, const char *);

    pcmk__output_xml_create_parent(out, "providers",
                                   "for", agent_spec,
                                   NULL);
    return xml_list(out, list, "provider");
}

PCMK__OUTPUT_ARGS("alternatives-list", "lrmd_list_t *", "const char *")
static int
lrmd__alternatives_list(pcmk__output_t *out, va_list args) {
    lrmd_list_t *list = va_arg(args, lrmd_list_t *);
    const char *agent_spec G_GNUC_UNUSED = va_arg(args, const char *);

    return default_list(out, list, "Providers");
}

PCMK__OUTPUT_ARGS("agents-list", "lrmd_list_t *", "const char *", "const char *")
static int
lrmd__agents_list_xml(pcmk__output_t *out, va_list args) {
    lrmd_list_t *list = va_arg(args, lrmd_list_t *);
    const char *agent_spec = va_arg(args, const char *);
    const char *provider = va_arg(args, const char *);

    xmlNodePtr node = pcmk__output_xml_create_parent(out, "agents",
                                                     "standard", agent_spec,
                                                     NULL);

    if (!pcmk__str_empty(provider)) {
        crm_xml_add(node, "provider", provider);
    }

    return xml_list(out, list, "agent");
}

PCMK__OUTPUT_ARGS("agents-list", "lrmd_list_t *", "const char *", "const char *")
static int
lrmd__agents_list(pcmk__output_t *out, va_list args) {
    lrmd_list_t *list = va_arg(args, lrmd_list_t *);
    const char *agent_spec = va_arg(args, const char *);
    const char *provider = va_arg(args, const char *);

    int rc;
    char *title = crm_strdup_printf("%s agents", pcmk__str_empty(provider) ? agent_spec : provider);

    rc = default_list(out, list, title);
    free(title);
    return rc;
}

PCMK__OUTPUT_ARGS("providers-list", "lrmd_list_t *", "const char *")
static int
lrmd__providers_list_xml(pcmk__output_t *out, va_list args) {
    lrmd_list_t *list = va_arg(args, lrmd_list_t *);
    const char *agent_spec = va_arg(args, const char *);

    xmlNodePtr node = pcmk__output_xml_create_parent(out, "providers",
                                                     "standard", "ocf",
                                                     NULL);

    if (agent_spec != NULL) {
        crm_xml_add(node, "agent", agent_spec);
    }

    return xml_list(out, list, "provider");
}

PCMK__OUTPUT_ARGS("providers-list", "lrmd_list_t *", "const char *")
static int
lrmd__providers_list(pcmk__output_t *out, va_list args) {
    lrmd_list_t *list = va_arg(args, lrmd_list_t *);
    const char *agent_spec G_GNUC_UNUSED = va_arg(args, const char *);

    return default_list(out, list, "Providers");
}

PCMK__OUTPUT_ARGS("standards-list", "lrmd_list_t *")
static int
lrmd__standards_list(pcmk__output_t *out, va_list args) {
    lrmd_list_t *list = va_arg(args, lrmd_list_t *);

    return default_list(out, list, "Standards");
}

static pcmk__message_entry_t fmt_functions[] = {
    { "alternatives-list", "default", lrmd__alternatives_list },
    { "alternatives-list", "xml", lrmd__alternatives_list_xml },
    { "agents-list", "default", lrmd__agents_list },
    { "agents-list", "xml", lrmd__agents_list_xml },
    { "providers-list", "default", lrmd__providers_list },
    { "providers-list", "xml", lrmd__providers_list_xml },
    { "standards-list", "default", lrmd__standards_list },

    { NULL, NULL, NULL }
};

void
lrmd__register_messages(pcmk__output_t *out) {
    pcmk__register_messages(out, fmt_functions);
}