@@ -1515,72 +1515,61 @@ def __init__(self, peer=None):
15151515 Init function
15161516 when peer set, parse peer node's results
15171517 """
1518- self .xml_elem = None
15191518 self .peer = peer
1519+ self .xml_elem = self ._load ()
15201520
15211521 def _load (self ):
15221522 """
15231523 Load xml output of crm_mon
15241524 """
1525- rc , output , stderr = sh .cluster_shell ().get_rc_stdout_stderr_without_input (self .peer , constants .CRM_MON_XML_OUTPUT )
1526- self . xml_elem = text2elem (output )
1525+ _ , output , _ = sh .cluster_shell ().get_rc_stdout_stderr_without_input (self .peer , constants .CRM_MON_XML_OUTPUT )
1526+ return text2elem (output )
15271527
1528- @classmethod
1529- def is_node_online (cls , node ):
1528+ def is_node_online (self , node ):
15301529 """
1531- Check if node online
1530+ Check if a node is online
15321531 """
1533- cls_inst = cls ()
1534- cls_inst ._load ()
1535- elem_list = cls_inst .xml_elem .xpath ('//node[@name="{}" and @online="true"]' .format (node ))
1536- return len (elem_list ) != 0
1532+ xpath = f'//node[@name="{ node } " and @online="true"]'
1533+ return bool (self .xml_elem .xpath (xpath ))
15371534
1538- @classmethod
1539- def is_resource_configured (cls , ra_type , peer = None ):
1535+ def get_node_list (self , attr = None ):
15401536 """
1541- Check if the RA configured
1537+ Get a list of nodes based on the given attribute
15421538 """
1543- cls_inst = cls (peer = peer )
1544- cls_inst ._load ()
1545- elem_list = cls_inst .xml_elem .xpath ('//resource[@resource_agent="{}"]' .format (ra_type ))
1546- return len (elem_list ) != 0
1539+ attr_dict = {
1540+ 'standby' : '[@standby="true"]' ,
1541+ 'online' : '[@standby="false"]'
1542+ }
1543+ xpath_str = f'//node{ attr_dict .get (attr , "" )} '
1544+ return [e .get ('name' ) for e in self .xml_elem .xpath (xpath_str )]
1545+
1546+ def is_resource_configured (self , ra_type ):
1547+ """
1548+ Check if the RA is configured
1549+ """
1550+ xpath = f'//resource[@resource_agent="{ ra_type } "]'
1551+ return bool (self .xml_elem .xpath (xpath ))
15471552
1548- @classmethod
1549- def is_any_resource_running (cls , peer = None ):
1553+ def is_any_resource_running (self ):
15501554 """
15511555 Check if any RA is running
15521556 """
1553- cls_inst = cls (peer = peer )
1554- cls_inst ._load ()
1555- elem_list = cls_inst .xml_elem .xpath ('//resource[@active="true"]' )
1556- return len (elem_list ) != 0
1557+ xpath = '//resource[@active="true"]'
1558+ return bool (self .xml_elem .xpath (xpath ))
15571559
1558- @classmethod
1559- def is_resource_started (cls , ra , peer = None ):
1560+ def is_resource_started (self , ra ):
15601561 """
15611562 Check if the RA started(in all clone instances if configured as clone)
15621563
15631564 @ra could be resource id or resource type
15641565 """
1565- cls_inst = cls (peer = peer )
1566- cls_inst ._load ()
1567- elem_list = cls_inst .xml_elem .xpath ('//resource[(@id="{ra}" or @resource_agent="{ra}") and @active="true"]' .format (ra = ra ))
1568- # Stopped or not exist
1569- if not elem_list :
1570- return False
1571- # Starting will return False
1572- return all ([True if elem .get ('role' ) == 'Started' else False for elem in elem_list ])
1566+ xpath = f'//resource[(@id="{ ra } " or @resource_agent="{ ra } ") and @active="true" and @role="Started"]'
1567+ return bool (self .xml_elem .xpath (xpath ))
15731568
1574- @classmethod
1575- def get_resource_id_list_via_type (cls , ra_type , peer = None ):
1569+ def get_resource_id_list_via_type (self , ra_type ):
15761570 """
15771571 Given configured ra type, get the ra id list
15781572 """
1579- id_list = []
1580- cls_inst = cls (peer = peer )
1581- cls_inst ._load ()
1582- elem_list = cls_inst .xml_elem .xpath ('//resource[@resource_agent="{ra_type}"]' .format (ra_type = ra_type ))
1583- if not elem_list :
1584- return id_list
1585- return [elem .get ('id' ) for elem in elem_list ]
1573+ xpath = f'//resource[@resource_agent="{ ra_type } "]'
1574+ return [elem .get ('id' ) for elem in self .xml_elem .xpath (xpath )]
15861575# vim:ts=4:sw=4:et:
0 commit comments