def test_success(self): xml = etree.fromstring( """ <parent> <meta_attributes id="set1" /> <instance_attributes id="set2" /> <not_an_nvset id="set3" /> <meta_attributes id="set4" /> </parent> """ ) element_list, report_list = nvpair_multi.find_nvsets_by_ids( xml, ["set1", "set2", "set3", "setX"] ) self.assertEqual( ["set1", "set2"], [el.get("id") for el in element_list], ) assert_report_item_list_equal( report_list, [ fixture.report_unexpected_element( "set3", "not_an_nvset", ["options set"] ), fixture.report_not_found( "setX", context_type="parent", expected_types=["options set"], ), ], )
def _defaults_remove(env: LibraryEnvironment, cib_section_name: str, nvset_id_list: Iterable[str]) -> None: if not nvset_id_list: return nvset_elements, report_list = nvpair_multi.find_nvsets_by_ids( sections.get(env.get_cib(), cib_section_name), nvset_id_list) if env.report_processor.report_list(report_list).has_errors: raise LibraryError() nvpair_multi.nvset_remove(nvset_elements) env.push_cib()
def _defaults_update( env: LibraryEnvironment, cib_section_name: str, nvset_id: Optional[str], nvpairs: Mapping[str, str], pcs_command: reports.types.PcsCommand, ) -> None: cib = env.get_cib() id_provider = IdProvider(cib) if nvset_id is None: # Backward compatibility code to support an old use case where no id # was requested and provided and the first meta_attributes nvset was # created / updated. However, we check that there is only one nvset # present in the CIB to prevent breaking the configuration with # multiple nvsets in place. # This is to be supported as it provides means of easily managing # defaults if only one set of defaults is needed. # TODO move this to a separate lib command. if not nvpairs: return # Do not create new defaults element if we are only removing values # from it. only_removing = True for value in nvpairs.values(): if value != "": only_removing = False break if only_removing and not sections.exists(cib, cib_section_name): env.report_processor.report( ReportItem.warning(reports.messages.DefaultsCanBeOverriden())) return nvset_elements = nvpair_multi.find_nvsets( sections.get(cib, cib_section_name)) if len(nvset_elements) > 1: env.report_processor.report( reports.item.ReportItem.error( reports.messages.CibNvsetAmbiguousProvideNvsetId( pcs_command))) raise LibraryError() env.report_processor.report( ReportItem.warning(reports.messages.DefaultsCanBeOverriden())) if len(nvset_elements) == 1: nvpair_multi.nvset_update(nvset_elements[0], id_provider, nvpairs) elif only_removing: # do not create new nvset if there is none and we are only removing # nvpairs return else: nvpair_multi.nvset_append_new( sections.get(cib, cib_section_name), id_provider, nvpair_multi.NVSET_META, nvpairs, {}, ) env.push_cib() return nvset_elements, report_list = nvpair_multi.find_nvsets_by_ids( sections.get(cib, cib_section_name), [nvset_id]) if env.report_processor.report_list(report_list).has_errors: raise LibraryError() nvpair_multi.nvset_update(nvset_elements[0], id_provider, nvpairs) env.report_processor.report( ReportItem.warning(reports.messages.DefaultsCanBeOverriden())) env.push_cib()