<s> """<STR_LIT>""" <EOL> import re <EOL> from ganeti import constants <EOL> from ganeti import errors <EOL> from ganeti import locking <EOL> from ganeti import objects <EOL> from ganeti import utils <EOL> from ganeti . cmdlib . base import NoHooksLU <EOL> from ganeti . cmdlib . common import ExpandNodeUuidAndName , ExpandInstanceUuidAndName , ShareAll <EOL> class TagsLU ( NoHooksLU ) : <EOL> """<STR_LIT>""" <EOL> def ExpandNames ( self ) : <EOL> self . group_uuid = None <EOL> self . needed_locks = { } <EOL> if self . op . kind == constants . TAG_NODE : <EOL> ( self . node_uuid , _ ) = ExpandNodeUuidAndName ( self . cfg , None , self . op . name ) <EOL> lock_level = locking . LEVEL_NODE <EOL> lock_name = self . node_uuid <EOL> elif self . op . kind == constants . TAG_INSTANCE : <EOL> ( self . inst_uuid , inst_name ) = ExpandInstanceUuidAndName ( self . cfg , None , self . op . name ) <EOL> lock_level = locking . LEVEL_INSTANCE <EOL> lock_name = inst_name <EOL> elif self . op . kind == constants . TAG_NODEGROUP : <EOL> self . group_uuid = self . cfg . LookupNodeGroup ( self . op . name ) <EOL> lock_level = locking . LEVEL_NODEGROUP <EOL> lock_name = self . group_uuid <EOL> elif self . op . kind == constants . TAG_NETWORK : <EOL> self . network_uuid = self . cfg . LookupNetwork ( self . op . name ) <EOL> lock_level = locking . LEVEL_NETWORK <EOL> lock_name = self . network_uuid <EOL> else : <EOL> lock_level = None <EOL> lock_name = None <EOL> if lock_level and getattr ( self . op , "<STR_LIT>" , True ) : <EOL> self . needed_locks [ lock_level ] = lock_name <EOL> def CheckPrereq ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . op . kind == constants . TAG_CLUSTER : <EOL> self . target = self . cfg . GetClusterInfo ( ) <EOL> elif self . op . kind == constants . TAG_NODE : <EOL> self . target = self . cfg . GetNodeInfo ( self . node_uuid ) <EOL> elif self . op . kind == constants . TAG_INSTANCE : <EOL> self . target = self . cfg . GetInstanceInfo ( self . inst_uuid ) <EOL> elif self . op . kind == constants . TAG_NODEGROUP : <EOL> self . target = self . cfg . GetNodeGroup ( self . group_uuid ) <EOL> elif self . op . kind == constants . TAG_NETWORK : <EOL> self . target = self . cfg . GetNetwork ( self . network_uuid ) <EOL> else : <EOL> raise errors . OpPrereqError ( "<STR_LIT>" % <EOL> str ( self . op . kind ) , errors . ECODE_INVAL ) <EOL> class LUTagsGet ( TagsLU ) : <EOL> """<STR_LIT>""" <EOL> REQ_BGL = False <EOL> def ExpandNames ( self ) : <EOL> TagsLU . ExpandNames ( self ) <EOL> self . share_locks = ShareAll ( ) <EOL> def Exec ( self , feedback_fn ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . target . GetTags ( ) ) <EOL> class LUTagsSearch ( NoHooksLU ) : <EOL> """<STR_LIT>""" <EOL> REQ_BGL = False <EOL> def ExpandNames ( self ) : <EOL> self . needed_locks = { } <EOL> def CheckPrereq ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . re = re . compile ( self . op . pattern ) <EOL> except re . error , err : <EOL> raise errors . OpPrereqError ( "<STR_LIT>" % <EOL> ( self . op . pattern , err ) , errors . ECODE_INVAL ) <EOL> @ staticmethod <EOL> def _ExtendTagTargets ( targets , object_type_name , object_info_dict ) : <EOL> return targets . extend ( ( "<STR_LIT>" % ( object_type_name , o . name ) , o ) <EOL> for o in object_info_dict . values ( ) ) <EOL> def Exec ( self , feedback_fn ) : <EOL> """<STR_LIT>""" <EOL> tgts = [ ( "<STR_LIT>" , self . cfg . GetClusterInfo ( ) ) ] <EOL> LUTagsSearch . _ExtendTagTargets ( tgts , "<STR_LIT>" , <EOL> self . cfg . GetAllInstancesInfo ( ) ) <EOL> LUTagsSearch . _ExtendTagTargets ( tgts , "<STR_LIT>" , <EOL> self . cfg . GetAllNodesInfo ( ) ) <EOL> LUTagsSearch . _ExtendTagTargets ( tgts , "<STR_LIT>" , <EOL> self . cfg . GetAllNodeGroupsInfo ( ) ) <EOL> LUTagsSearch . _ExtendTagTargets ( tgts , "<STR_LIT>" , <EOL> self . cfg . GetAllNetworksInfo ( ) ) <EOL> results = [ ] <EOL> for path , target in tgts : <EOL> for tag in target . GetTags ( ) : <EOL> if self . re . search ( tag ) : <EOL> results . append ( ( path , tag ) ) <EOL> return results <EOL> class LUTagsSet ( TagsLU ) : <EOL> """<STR_LIT>""" <EOL> REQ_BGL = False <EOL> def CheckPrereq ( self ) : <EOL> """<STR_LIT>""" <EOL> TagsLU . CheckPrereq ( self ) <EOL> for tag in self . op . tags : <EOL> objects . TaggableObject . ValidateTag ( tag ) <EOL> def Exec ( self , feedback_fn ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> for tag in self . op . tags : <EOL> self . target . AddTag ( tag ) <EOL> except errors . TagError , err : <EOL> raise errors . OpExecError ( "<STR_LIT>" % str ( err ) ) <EOL> self . cfg . Update ( self . target , feedback_fn ) <EOL> class LUTagsDel ( TagsLU ) : <EOL> """<STR_LIT>""" <EOL> REQ_BGL = False <EOL> def CheckPrereq ( self ) : <EOL> """<STR_LIT>""" <EOL> TagsLU . CheckPrereq ( self ) <EOL> for tag in self . op . tags : <EOL> objects . TaggableObject . ValidateTag ( tag ) <EOL> del_tags = frozenset ( self . op . tags ) <EOL> cur_tags = self . target . GetTags ( ) <EOL> diff_tags = del_tags - cur_tags <EOL> if diff_tags : <EOL> diff_names = ( "<STR_LIT>" % i for i in sorted ( diff_tags ) ) <EOL> raise errors . OpPrereqError ( "<STR_LIT>" % <EOL> ( utils . CommaJoin ( diff_names ) , ) , <EOL> errors . ECODE_NOENT ) <EOL> def Exec ( self , feedback_fn ) : <EOL> """<STR_LIT>""" <EOL> for tag in self . op . tags : <EOL> self . target . RemoveTag ( tag ) <EOL> self . cfg . Update ( self . target , feedback_fn ) </s>
