+
+
+class TagsLU(NoHooksLU):
+ """Generic tags LU.
+
+ This is an abstract class which is the parent of all the other tags LUs.
+
+ """
+ def CheckPrereq(self):
+ """Check prerequisites.
+
+ """
+ if self.op.kind == constants.TAG_CLUSTER:
+ self.target = self.cfg.GetClusterInfo()
+ elif self.op.kind == constants.TAG_NODE:
+ name = self.cfg.ExpandNodeName(self.op.name)
+ if name is None:
+ raise errors.OpPrereqError("Invalid node name (%s)" %
+ (self.op.name,))
+ self.op.name = name
+ self.target = self.cfg.GetNodeInfo(name)
+ elif self.op.kind == constants.TAG_INSTANCE:
+ name = self.cfg.ExpandInstanceName(self.op.name)
+ if name is None:
+ raise errors.OpPrereqError("Invalid instance name (%s)" %
+ (self.op.name,))
+ self.op.name = name
+ self.target = self.cfg.GetInstanceInfo(name)
+ else:
+ raise errors.OpPrereqError("Wrong tag type requested (%s)" %
+ str(self.op.kind))
+
+
+class LUGetTags(TagsLU):
+ """Returns the tags of a given object.
+
+ """
+ _OP_REQP = ["kind", "name"]
+
+ def Exec(self, feedback_fn):
+ """Returns the tag list.
+
+ """
+ return self.target.GetTags()
+
+
+class LUAddTags(TagsLU):
+ """Sets a tag on a given object.
+
+ """
+ _OP_REQP = ["kind", "name", "tags"]
+
+ def CheckPrereq(self):
+ """Check prerequisites.
+
+ This checks the type and length of the tag name and value.
+
+ """
+ TagsLU.CheckPrereq(self)
+ for tag in self.op.tags:
+ objects.TaggableObject.ValidateTag(tag)
+
+ def Exec(self, feedback_fn):
+ """Sets the tag.
+
+ """
+ try:
+ for tag in self.op.tags:
+ self.target.AddTag(tag)
+ except errors.TagError, err:
+ raise errors.OpExecError("Error while setting tag: %s" % str(err))
+ try:
+ self.cfg.Update(self.target)
+ except errors.ConfigurationError:
+ raise errors.OpRetryError("There has been a modification to the"
+ " config file and the operation has been"
+ " aborted. Please retry.")
+
+
+class LUDelTags(TagsLU):
+ """Delete a list of tags from a given object.
+
+ """
+ _OP_REQP = ["kind", "name", "tags"]
+
+ def CheckPrereq(self):
+ """Check prerequisites.
+
+ This checks that we have the given tag.
+
+ """
+ TagsLU.CheckPrereq(self)
+ for tag in self.op.tags:
+ objects.TaggableObject.ValidateTag(tag)
+ del_tags = frozenset(self.op.tags)
+ cur_tags = self.target.GetTags()
+ if not del_tags <= cur_tags:
+ diff_tags = del_tags - cur_tags
+ diff_names = ["'%s'" % tag for tag in diff_tags]
+ diff_names.sort()
+ raise errors.OpPrereqError("Tag(s) %s not found" %
+ (",".join(diff_names)))
+
+ def Exec(self, feedback_fn):
+ """Remove the tag from the object.
+
+ """
+ for tag in self.op.tags:
+ self.target.RemoveTag(tag)
+ try:
+ self.cfg.Update(self.target)
+ except errors.ConfigurationError:
+ raise errors.OpRetryError("There has been a modification to the"
+ " config file and the operation has been"
+ " aborted. Please retry.")