Class: Dcmgr::Models::Tag

Inherits:
AccountResource show all
Defined in:
lib/dcmgr/models/tag.rb

Overview

Tag is a label which groups arbitrary resource(s) having canonical uuid. A tag object consists of three items: Account ID, Type, Name

Account ID is top level scope that represents the owner of the tag. Each tag instance is created in the scope of the account.

Type field is second level scope to filter the object out to be labeled. If the tag is only for grouping resource A, it will fail at labling the tag other than resource A.

Name represents the instance ID of the tag. This is a string that must be unique in the scope of Account ID and Type. Below is Type & Name matrix in single account: TypeA, name1 TypeA, name1 # can not create TypeB, name2 # ok TypeB, name1 # nop

The resource can be labeled is called “Taggable” resource. The model framework is provided to declare simply.

class A < Dcmgr::Models::Base

taggable 'xxx'

end

t = Tag.declare(account_id, :NetworkPool, ‘xxxxx’) t.mapped_uuids # => [‘nw-11111’, ‘nw-22222’ ,‘nw-33333’]

t = Tag.declare(account_id, :NetworkPool, ‘nwgroup1’) t.lable(‘nw-xxxxx’) t.lable(‘nw-yyyyy’)

t = Tag.declare(account_id, :NetworkPool, ‘nwgroup1’) nw = Network nw.label_tag(t) nw.label_tag(‘tag-xxxxx’)

Examples:

Retrieve tag

Lable a tag from tag

Label a tag from resource

Defined Under Namespace

Classes: TagAlreadyLabeled, TagAlreadyUnlabeled, UnacceptableTagType

Constant Summary

Constants inherited from BaseNew

BaseNew::LOCK_TABLES_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AccountResource

#account

Methods inherited from BaseNew

Proxy, dataset, install_data, install_data_hooks, unlock!

Class Method Details

.find_tag_class(name) ⇒ Object



136
137
138
139
140
# File 'lib/dcmgr/models/tag.rb', line 136

def self.find_tag_class(name)
  self.subclasses.find { |m|
    m == name || m.split('::').last == name
  }
end

.lock!Object



155
156
157
158
# File 'lib/dcmgr/models/tag.rb', line 155

def self.lock!
  super
  TagMapping.lock!
end

Instance Method Details

#accept_mapping?(taggable_obj) ⇒ Boolean

Check the object class type before associating to the Tag. Child class must implement this method.

Parameters:

  • taggable_obj

    any object kind_of?(Model::Taggable)

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


145
146
147
# File 'lib/dcmgr/models/tag.rb', line 145

def accept_mapping?(taggable_obj)
  raise NotImplementedError 
end

#before_destroyObject

model hook



150
151
152
153
# File 'lib/dcmgr/models/tag.rb', line 150

def before_destroy
  return false if !mapped_uuids_dataset.empty?
  super
end

#label(taggable_or_uuid) ⇒ Object

Associate the tag to the taggable object.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dcmgr/models/tag.rb', line 95

def label(taggable_or_uuid)
  tgt = case taggable_or_uuid
        when String
          Taggable.find(taggable_or_uuid)
        when Models::Taggable
          taggable_or_uuid
        else
          raise TypeError
        end
  
  raise(UnacceptableTagType.new("", self, tgt)) unless accept_mapping?(tgt)
  raise(TagAlreadyLabeled) if labeled?(tgt.canonical_uuid)
  TagMapping.create(:uuid=>tgt.canonical_uuid, :tag_id=>self.pk)
  self
end

#labeled?(canonical_uuid) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
# File 'lib/dcmgr/models/tag.rb', line 86

def labeled?(canonical_uuid)
  # TODO: check if the uuid exists
  
  !TagMapping.filter(:tag_id=>self.pk, :uuid=>canonical_uuid).empty?
end

#lable_ifnot(t) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/dcmgr/models/tag.rb', line 111

def lable_ifnot(t)
  begin
    lable(t)
  rescue TagAlreadyLabeled
  end
  self
end

#to_api_documentObject



160
161
162
# File 'lib/dcmgr/models/tag.rb', line 160

def to_api_document
  to_hash.merge({:type_id=>self.class.to_s.split('::').last})
end

#unlabel(taggable_or_uuid) ⇒ Object

Disassociate the tag from the taggable object.



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dcmgr/models/tag.rb', line 122

def unlabel(taggable_or_uuid)
  tgt = case taggable_or_uuid
        when String
          Taggable.find(taggable_or_uuid) || raise("Not found Taggable object: #{taggable_or_uuid}")
        when Models::Taggable
          taggable_or_uuid
        else
          raise TypeError
        end
  t = TagMapping.find(:tag_id=>self.pk, :uuid=>tgt.canonical_uuid) || raise(TagAlreadyUnlabeled)
  t.destroy
  self
end