diff --git a/include/crm/common/cib.h b/include/crm/common/cib.h index a8658cc8d5..1b0abf2a4c 100644 --- a/include/crm/common/cib.h +++ b/include/crm/common/cib.h @@ -1,24 +1,27 @@ /* * Copyright 2021 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. */ #ifndef PCMK__COMMON_CIB__H # define PCMK__COMMON_CIB__H #ifdef __cplusplus extern "C" { #endif +#include // xmlNode + const char *pcmk_cib_xpath_for(const char *element_name); const char *pcmk_cib_parent_name_for(const char *element_name); +xmlNode *pcmk_find_cib_element(xmlNode *cib, const char *element_name); #ifdef __cplusplus } #endif #endif // PCMK__COMMON_CIB__H diff --git a/lib/common/cib.c b/lib/common/cib.c index 042fcd5253..74d486d3cd 100644 --- a/lib/common/cib.c +++ b/lib/common/cib.c @@ -1,143 +1,159 @@ /* * Original copyright 2004 International Business Machines * Later changes copyright 2008-2021 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 #include +#include // xmlNode #include /* * Functions to help find particular sections of the CIB */ // Map CIB element names to their parent elements and XPath searches static struct { const char *name; // Name of this CIB element const char *parent; // CIB element that this element is a child of const char *path; // XPath to find this CIB element } cib_sections[] = { { NULL, NULL, "//" XML_TAG_CIB }, { XML_TAG_CIB, NULL, "//" XML_TAG_CIB }, { XML_CIB_TAG_STATUS, "/" XML_TAG_CIB, "//" XML_TAG_CIB "/" XML_CIB_TAG_STATUS }, { XML_CIB_TAG_CONFIGURATION, "/" XML_TAG_CIB, "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION }, { XML_CIB_TAG_CRMCONFIG, "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION, "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_CRMCONFIG }, { XML_CIB_TAG_NODES, "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION, "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_NODES }, { XML_CIB_TAG_RESOURCES, "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION, "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_RESOURCES }, { XML_CIB_TAG_CONSTRAINTS, "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION, "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_CONSTRAINTS }, { XML_CIB_TAG_OPCONFIG, "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION, "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_OPCONFIG }, { XML_CIB_TAG_RSCCONFIG, "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION, "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_RSCCONFIG }, { XML_CIB_TAG_ACLS, "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION, "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_ACLS }, { XML_TAG_FENCING_TOPOLOGY, "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION, "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_TAG_FENCING_TOPOLOGY }, { XML_CIB_TAG_TAGS, "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION, "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_TAGS }, { XML_CIB_TAG_ALERTS, "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION, "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_ALERTS }, { XML_CIB_TAG_SECTION_ALL, NULL, "//" XML_TAG_CIB }, }; /*! * \brief Get the XPath needed to find a specified CIB element name * * \param[in] element_name Name of CIB element * * \return XPath for finding \p element_name in CIB XML (or NULL if unknown) * \note The return value is constant and should not be freed. */ const char * pcmk_cib_xpath_for(const char *element_name) { for (int lpc = 0; lpc < PCMK__NELEM(cib_sections); lpc++) { if (((element_name == NULL) && (cib_sections[lpc].name == NULL)) || pcmk__str_eq(element_name, cib_sections[lpc].name, pcmk__str_none)) { return cib_sections[lpc].path; } } return NULL; } /*! * \brief Get the parent element name of a given CIB element name * * \param[in] element_name Name of CIB element * * \return Parent element of \p element_name (or NULL if unknown) * \note The return value is constant and should not be freed. */ const char * pcmk_cib_parent_name_for(const char *element_name) { for (int lpc = 0; lpc < PCMK__NELEM(cib_sections); lpc++) { if (pcmk__str_eq(element_name, cib_sections[lpc].name, pcmk__str_none)) { return cib_sections[lpc].parent; } } return NULL; } + +/*! + * \brief Find an element in the CIB + * + * \param[in] cib Top-level CIB XML to search + * \param[in] element_name Name of CIB element to search for + * + * \return XML element in \p cib corresponding to \p element_name + * (or \p cib itself if element is unknown or not found) + */ +xmlNode * +pcmk_find_cib_element(xmlNode *cib, const char *element_name) +{ + return get_xpath_object(pcmk_cib_xpath_for(element_name), cib, LOG_TRACE); +}