Class: RightScale::AgentTagManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/right_agent/agent_tag_manager.rb

Overview

Agent tags management

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Singleton

included

Instance Attribute Details

#agentObject

(Agent) Agent being managed



32
33
34
# File 'lib/right_agent/agent_tag_manager.rb', line 32

def agent
  @agent
end

Instance Method Details

#add_tags(*new_tags) ⇒ Object

Add given tags to agent

Parameters

new_tags(Array)

Tags to be added

Block

A block is optional. If provided, should take one argument which will be set with the raw response

Return

true always return true



113
114
115
# File 'lib/right_agent/agent_tag_manager.rb', line 113

def add_tags(*new_tags)
  update_tags(new_tags, []) { |raw_response| yield raw_response if block_given? }
end

#clearObject

Clear all agent tags

Block

Given block should take one argument which will be set with the raw response

Return

true::Always return true



174
175
176
# File 'lib/right_agent/agent_tag_manager.rb', line 174

def clear
  update_tags([], tags) { |raw_response| yield raw_response }
end

#query_tags(*tags) ⇒ Object

Queries a list of servers in the current deployment which have one or more of the given tags.

Parameters

tags(Array)

tags to query or empty

options(Hash)

Request options

:raw(Boolean)

true to yield raw tag response instead of deserialized tags

:timeout(Integer)

timeout in seconds before giving up and yielding an error message

Block

Given block should take one argument which will be set with an array initialized with the tags of this instance

Return

true

Always return true



72
73
74
75
76
77
78
79
80
81
# File 'lib/right_agent/agent_tag_manager.rb', line 72

def query_tags(*tags)
  if tags.last.respond_to?(:keys)
    tags = tags[0..-2]
    options = tags.last
  else
    options = {}
  end

  do_query(tags, nil, options) { |result| yield result }
end

#query_tags_raw(tags, agent_ids = nil, options = {}) ⇒ Object

Queries a list of servers in the current deployment which have one or more of the given tags. Yields the raw response (for responding locally).

Parameters

tags(Array)

tags to query or empty

agent_ids(Array)

agent IDs to query or empty or nil

options(Hash)

Request options

:timeout(Integer)

timeout in seconds before giving up and yielding an error message

Block

Given block should take one argument which will be set with the raw response

Return

true

Always return true



97
98
99
100
# File 'lib/right_agent/agent_tag_manager.rb', line 97

def query_tags_raw(tags, agent_ids = nil, options={})
  options = options.merge(:raw=>true)
  do_query(tags, agent_ids, options) { |raw_response| yield raw_response }
end

#remove_tags(*old_tags) ⇒ Object

Remove given tags from agent

Parameters

old_tags(Array)

Tags to be removed

Block

A block is optional. If provided, should take one argument which will be set with the raw response

Return

true always return true



128
129
130
# File 'lib/right_agent/agent_tag_manager.rb', line 128

def remove_tags(*old_tags)
  update_tags([], old_tags) { |raw_response| yield raw_response if block_given? }
end

#tags(options = {}) ⇒ Object

Retrieve current agent tags and give result to block

Parameters

options(Hash)

Request options

:raw(Boolean)

true to yield raw tag response instead of deserialized tags

:timeout(Integer)

timeout in seconds before giving up and yielding an error message

Block

Given block should take one argument which will be set with an array initialized with the tags of this instance

Return

true

Always return true



47
48
49
50
51
52
53
54
55
# File 'lib/right_agent/agent_tag_manager.rb', line 47

def tags(options={})
  do_query(nil, @agent.identity, options) do |result|
    if result.kind_of?(Hash)
      yield(result.size == 1 ? result.values.first['tags'] : [])
    else
      yield result
    end
  end
end

#update_tags(new_tags, old_tags, &block) ⇒ Object

Runs a tag update with a list of new and old tags.

Parameters

new_tags(Array)

new tags to add or empty

old_tags(Array)

old tags to remove or empty

block(Block)

optional callback for update response

Block

A block is optional. If provided, should take one argument which will be set with the raw response

Return

true

Always return true



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/right_agent/agent_tag_manager.rb', line 145

def update_tags(new_tags, old_tags, &block)
  agent_check
  tags = @agent.tags
  tags += (new_tags || [])
  tags -= (old_tags || [])
  tags.uniq!

  payload = {:new_tags => new_tags, :obsolete_tags => old_tags}
  request = RightScale::IdempotentRequest.new("/mapper/update_tags", payload)
  if block
    # always yield raw response
    request.callback do |_|
      # refresh agent's copy of tags on successful update
      @agent.tags = tags
      block.call(request.raw_response)
    end
    request.errback { |message| block.call(request.raw_response || message) }
  end
  request.run
  true
end