root/lib/common/cib.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. pcmk_cib_xpath_for
  2. pcmk_cib_parent_name_for
  3. pcmk_find_cib_element

   1 /*
   2  * Original copyright 2004 International Business Machines
   3  * Later changes copyright 2008-2021 the Pacemaker project contributors
   4  *
   5  * The version control history for this file may have further details.
   6  *
   7  * This source code is licensed under the GNU Lesser General Public License
   8  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   9  */
  10 
  11 #include <crm_internal.h>
  12 
  13 #include <stdio.h>
  14 #include <libxml/tree.h>    // xmlNode
  15 
  16 #include <crm/msg_xml.h>
  17 
  18 /*
  19  * Functions to help find particular sections of the CIB
  20  */
  21 
  22 // Map CIB element names to their parent elements and XPath searches
  23 static struct {
  24     const char *name;   // Name of this CIB element
  25     const char *parent; // CIB element that this element is a child of
  26     const char *path;   // XPath to find this CIB element
  27 } cib_sections[] = {
  28     {
  29         // This first entry is also the default if a NULL is compared
  30         XML_TAG_CIB,
  31         NULL,
  32         "//" XML_TAG_CIB
  33     },
  34     {
  35         XML_CIB_TAG_STATUS,
  36         "/" XML_TAG_CIB,
  37         "//" XML_TAG_CIB "/" XML_CIB_TAG_STATUS
  38     },
  39     {
  40         XML_CIB_TAG_CONFIGURATION,
  41         "/" XML_TAG_CIB,
  42         "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION
  43     },
  44     {
  45         XML_CIB_TAG_CRMCONFIG,
  46         "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION,
  47         "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_CRMCONFIG
  48     },
  49     {
  50         XML_CIB_TAG_NODES,
  51         "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION,
  52         "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_NODES
  53     },
  54     {
  55         XML_CIB_TAG_RESOURCES,
  56         "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION,
  57         "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_RESOURCES
  58     },
  59     {
  60         XML_CIB_TAG_CONSTRAINTS,
  61         "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION,
  62         "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_CONSTRAINTS
  63     },
  64     {
  65         XML_CIB_TAG_OPCONFIG,
  66         "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION,
  67         "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_OPCONFIG
  68     },
  69     {
  70         XML_CIB_TAG_RSCCONFIG,
  71         "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION,
  72         "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_RSCCONFIG
  73     },
  74     {
  75         XML_CIB_TAG_ACLS,
  76         "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION,
  77         "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_ACLS
  78     },
  79     {
  80         XML_TAG_FENCING_TOPOLOGY,
  81         "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION,
  82         "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_TAG_FENCING_TOPOLOGY
  83     },
  84     {
  85         XML_CIB_TAG_TAGS,
  86         "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION,
  87         "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_TAGS
  88     },
  89     {
  90         XML_CIB_TAG_ALERTS,
  91         "/" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION,
  92         "//" XML_TAG_CIB "/" XML_CIB_TAG_CONFIGURATION "/" XML_CIB_TAG_ALERTS
  93     },
  94     {
  95         XML_CIB_TAG_SECTION_ALL,
  96         NULL,
  97         "//" XML_TAG_CIB
  98     },
  99 };
 100 
 101 /*!
 102  * \brief Get the XPath needed to find a specified CIB element name
 103  *
 104  * \param[in] element_name  Name of CIB element
 105  *
 106  * \return XPath for finding \p element_name in CIB XML (or NULL if unknown)
 107  * \note The return value is constant and should not be freed.
 108  */
 109 const char *
 110 pcmk_cib_xpath_for(const char *element_name)
     /* [previous][next][first][last][top][bottom][index][help] */
 111 {
 112     for (int lpc = 0; lpc < PCMK__NELEM(cib_sections); lpc++) {
 113         // A NULL element_name will match the first entry
 114         if (pcmk__str_eq(element_name, cib_sections[lpc].name,
 115                          pcmk__str_null_matches)) {
 116             return cib_sections[lpc].path;
 117         }
 118     }
 119     return NULL;
 120 }
 121 
 122 /*!
 123  * \brief Get the parent element name of a given CIB element name
 124  *
 125  * \param[in] element_name  Name of CIB element
 126  *
 127  * \return Parent element of \p element_name (or NULL if none or unknown)
 128  * \note The return value is constant and should not be freed.
 129  */
 130 const char *
 131 pcmk_cib_parent_name_for(const char *element_name)
     /* [previous][next][first][last][top][bottom][index][help] */
 132 {
 133     for (int lpc = 0; lpc < PCMK__NELEM(cib_sections); lpc++) {
 134         // A NULL element_name will match the first entry
 135         if (pcmk__str_eq(element_name, cib_sections[lpc].name,
 136                          pcmk__str_null_matches)) {
 137             return cib_sections[lpc].parent;
 138         }
 139     }
 140     return NULL;
 141 }
 142 
 143 /*!
 144  * \brief Find an element in the CIB
 145  *
 146  * \param[in,out] cib           Top-level CIB XML to search
 147  * \param[in]     element_name  Name of CIB element to search for
 148  *
 149  * \return XML element in \p cib corresponding to \p element_name
 150  *         (or \p cib itself if element is unknown or not found)
 151  */
 152 xmlNode *
 153 pcmk_find_cib_element(xmlNode *cib, const char *element_name)
     /* [previous][next][first][last][top][bottom][index][help] */
 154 {
 155     return get_xpath_object(pcmk_cib_xpath_for(element_name), cib, LOG_TRACE);
 156 }

/* [previous][next][first][last][top][bottom][index][help] */