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, default_row_lock_mode=, install_data, install_data_hooks, #to_hash, unlock!, #with_timestamps?

Class Method Details

.find_tag_class(name) ⇒ Object



127
128
129
130
131
# File 'lib/dcmgr/models/tag.rb', line 127

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

.lock!Object



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

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)


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

def accept_mapping?(taggable_obj)
  raise NotImplementedError 
end

#before_destroyObject

model hook



141
142
143
144
# File 'lib/dcmgr/models/tag.rb', line 141

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

#label(taggable_or_uuid) ⇒ Object

Associate the tag to the taggable object.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dcmgr/models/tag.rb', line 86

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)


77
78
79
80
81
# File 'lib/dcmgr/models/tag.rb', line 77

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



102
103
104
105
106
107
108
# File 'lib/dcmgr/models/tag.rb', line 102

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

#to_api_documentObject



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

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.



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dcmgr/models/tag.rb', line 113

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