Package ganeti :: Package cmdlib :: Module tags
[hide private]
[frames] | no frames]

Source Code for Module ganeti.cmdlib.tags

  1  # 
  2  # 
  3   
  4  # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc. 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions are 
  9  # met: 
 10  # 
 11  # 1. Redistributions of source code must retain the above copyright notice, 
 12  # this list of conditions and the following disclaimer. 
 13  # 
 14  # 2. Redistributions in binary form must reproduce the above copyright 
 15  # notice, this list of conditions and the following disclaimer in the 
 16  # documentation and/or other materials provided with the distribution. 
 17  # 
 18  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 19  # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 20  # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 21  # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 22  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 23  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 24  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 25  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 26  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 27  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 28  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 29   
 30   
 31  """Logical units dealing with tags.""" 
 32   
 33  import re 
 34   
 35  from ganeti import constants 
 36  from ganeti import errors 
 37  from ganeti import locking 
 38  from ganeti import objects 
 39  from ganeti import utils 
 40  from ganeti.cmdlib.base import NoHooksLU 
 41  from ganeti.cmdlib.common import ExpandNodeUuidAndName, \ 
 42    ExpandInstanceUuidAndName, ShareAll 
43 44 45 -class TagsLU(NoHooksLU): # pylint: disable=W0223
46 """Generic tags LU. 47 48 This is an abstract class which is the parent of all the other tags LUs. 49 50 """
51 - def ExpandNames(self):
52 self.group_uuid = None 53 self.needed_locks = {} 54 55 if self.op.kind == constants.TAG_NODE: 56 (self.node_uuid, _) = \ 57 ExpandNodeUuidAndName(self.cfg, None, self.op.name) 58 lock_level = locking.LEVEL_NODE 59 lock_name = self.node_uuid 60 elif self.op.kind == constants.TAG_INSTANCE: 61 (self.inst_uuid, inst_name) = \ 62 ExpandInstanceUuidAndName(self.cfg, None, self.op.name) 63 lock_level = locking.LEVEL_INSTANCE 64 lock_name = inst_name 65 elif self.op.kind == constants.TAG_NODEGROUP: 66 self.group_uuid = self.cfg.LookupNodeGroup(self.op.name) 67 lock_level = locking.LEVEL_NODEGROUP 68 lock_name = self.group_uuid 69 elif self.op.kind == constants.TAG_NETWORK: 70 self.network_uuid = self.cfg.LookupNetwork(self.op.name) 71 lock_level = locking.LEVEL_NETWORK 72 lock_name = self.network_uuid 73 else: 74 lock_level = None 75 lock_name = None 76 77 if lock_level and getattr(self.op, "use_locking", True): 78 self.needed_locks[lock_level] = lock_name
79 80 # FIXME: Acquire BGL for cluster tag operations (as of this writing it's 81 # not possible to acquire the BGL based on opcode parameters) 82
83 - def CheckPrereq(self):
84 """Check prerequisites. 85 86 """ 87 if self.op.kind == constants.TAG_CLUSTER: 88 self.target = self.cfg.GetClusterInfo() 89 elif self.op.kind == constants.TAG_NODE: 90 self.target = self.cfg.GetNodeInfo(self.node_uuid) 91 elif self.op.kind == constants.TAG_INSTANCE: 92 self.target = self.cfg.GetInstanceInfo(self.inst_uuid) 93 elif self.op.kind == constants.TAG_NODEGROUP: 94 self.target = self.cfg.GetNodeGroup(self.group_uuid) 95 elif self.op.kind == constants.TAG_NETWORK: 96 self.target = self.cfg.GetNetwork(self.network_uuid) 97 else: 98 raise errors.OpPrereqError("Wrong tag type requested (%s)" % 99 str(self.op.kind), errors.ECODE_INVAL)
100
101 102 -class LUTagsGet(TagsLU):
103 """Returns the tags of a given object. 104 105 """ 106 REQ_BGL = False 107
108 - def ExpandNames(self):
109 TagsLU.ExpandNames(self) 110 111 # Share locks as this is only a read operation 112 self.share_locks = ShareAll()
113
114 - def Exec(self, feedback_fn):
115 """Returns the tag list. 116 117 """ 118 return list(self.target.GetTags())
119
120 121 -class LUTagsSearch(NoHooksLU):
122 """Searches the tags for a given pattern. 123 124 """ 125 REQ_BGL = False 126
127 - def ExpandNames(self):
128 self.needed_locks = {}
129
130 - def CheckPrereq(self):
131 """Check prerequisites. 132 133 This checks the pattern passed for validity by compiling it. 134 135 """ 136 try: 137 self.re = re.compile(self.op.pattern) 138 except re.error, err: 139 raise errors.OpPrereqError("Invalid search pattern '%s': %s" % 140 (self.op.pattern, err), errors.ECODE_INVAL)
141 142 @staticmethod
143 - def _ExtendTagTargets(targets, object_type_name, object_info_dict):
144 return targets.extend(("/%s/%s" % (object_type_name, o.name), o) 145 for o in object_info_dict.values())
146
147 - def Exec(self, feedback_fn):
148 """Returns the tag list. 149 150 """ 151 tgts = [("/cluster", self.cfg.GetClusterInfo())] 152 153 LUTagsSearch._ExtendTagTargets(tgts, "instances", 154 self.cfg.GetAllInstancesInfo()) 155 LUTagsSearch._ExtendTagTargets(tgts, "nodes", 156 self.cfg.GetAllNodesInfo()) 157 LUTagsSearch._ExtendTagTargets(tgts, "nodegroup", 158 self.cfg.GetAllNodeGroupsInfo()) 159 LUTagsSearch._ExtendTagTargets(tgts, "network", 160 self.cfg.GetAllNetworksInfo()) 161 162 results = [] 163 for path, target in tgts: 164 for tag in target.GetTags(): 165 if self.re.search(tag): 166 results.append((path, tag)) 167 return results
168
169 170 -class LUTagsSet(TagsLU):
171 """Sets a tag on a given object. 172 173 """ 174 REQ_BGL = False 175
176 - def CheckPrereq(self):
177 """Check prerequisites. 178 179 This checks the type and length of the tag name and value. 180 181 """ 182 TagsLU.CheckPrereq(self) 183 for tag in self.op.tags: 184 objects.TaggableObject.ValidateTag(tag)
185
186 - def Exec(self, feedback_fn):
187 """Sets the tag. 188 189 """ 190 try: 191 for tag in self.op.tags: 192 self.target.AddTag(tag) 193 except errors.TagError, err: 194 raise errors.OpExecError("Error while setting tag: %s" % str(err)) 195 self.cfg.Update(self.target, feedback_fn)
196
197 198 -class LUTagsDel(TagsLU):
199 """Delete a list of tags from a given object. 200 201 """ 202 REQ_BGL = False 203
204 - def CheckPrereq(self):
205 """Check prerequisites. 206 207 This checks that we have the given tag. 208 209 """ 210 TagsLU.CheckPrereq(self) 211 for tag in self.op.tags: 212 objects.TaggableObject.ValidateTag(tag) 213 del_tags = frozenset(self.op.tags) 214 cur_tags = self.target.GetTags() 215 216 diff_tags = del_tags - cur_tags 217 if diff_tags: 218 diff_names = ("'%s'" % i for i in sorted(diff_tags)) 219 raise errors.OpPrereqError("Tag(s) %s not found" % 220 (utils.CommaJoin(diff_names), ), 221 errors.ECODE_NOENT)
222
223 - def Exec(self, feedback_fn):
224 """Remove the tag from the object. 225 226 """ 227 for tag in self.op.tags: 228 self.target.RemoveTag(tag) 229 self.cfg.Update(self.target, feedback_fn)
230