module RightScale::Api::Taggable

Public Instance Methods

add_tags(*args) click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_taggable.rb, line 27
def add_tags(*args)
  return false if args.empty?
  Tag.set(self.href, args.uniq)
  self.tags(true)
end
clear_tags(namespace = nil) click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_taggable.rb, line 107
def clear_tags(namespace = nil)
  tag_ary = self.tags(true)
  tag_ary = tag_ary.select { |tag| tag.start_with?("#{namespace}:") } if namespace
  self.remove_tags(*tag_ary)
end
get_info_tags(*tag_keys) click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_taggable.rb, line 55
def get_info_tags(*tag_keys)
  tags = get_tags_by_namespace("info")
  tags.each { |resource,hsh|
    hsh.reject! { |key,value|
      rej = false
      rej = !tag_keys.include?(key) unless tag_keys.empty?
      rej
    }
  }
  return tags
end
get_tags_by_namespace(namespace) click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_taggable.rb, line 86
def get_tags_by_namespace(namespace)
  ret = {}
  tags = {"self" => self.tags(true)}
  tags.each { |res,ary|
    ret[res] ||= {}
    ary.each { |tag|
      next unless tag.start_with?("#{namespace}:")
      key = tag.split("=").first.split(":")[1..-1].join(":")
      value = tag.split(":")[1..-1].join(":").split("=")[1..-1].join("=")
      ret[res][key] = value
    }
  }
  return ret
end
remove_info_tags(*tag_keys) click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_taggable.rb, line 47
def remove_info_tags(*tag_keys)
  remove_tags_by_namespace("info", *tag_keys)
end
remove_tags(*args) click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_taggable.rb, line 33
def remove_tags(*args)
  return false if args.empty?
  Tag.unset(self.href, args.uniq)
  @params["tags"] -= args
  self.tags(true)
end
remove_tags_by_namespace(namespace, *tag_keys) click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_taggable.rb, line 67
def remove_tags_by_namespace(namespace, *tag_keys)
  tags_to_unset = []
  tags = get_tags_by_namespace(namespace)
  tags.each { |res,hsh|
    hsh.each { |k,v|
      tags_to_unset << "#{namespace}:#{k}=#{v}" if tag_keys.include?(k)
    }
  }
  self.remove_tags(*tags_to_unset)
end
set_info_tags(hsh={}) click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_taggable.rb, line 51
def set_info_tags(hsh={})
  set_tags_by_namespace("info", hsh)
end
set_tags_by_namespace(namespace, hsh={}) click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_taggable.rb, line 78
def set_tags_by_namespace(namespace, hsh={})
  keys_to_change = []
  tags_to_set = []
  hsh.each { |k,v| keys_to_change << k; tags_to_set << "#{namespace}:#{k}=#{v}" }
  self.remove_tags_by_namespace(namespace, *keys_to_change)
  self.add_tags(*tags_to_set)
end
set_tags_to(*args) click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_taggable.rb, line 101
def set_tags_to(*args)
  STDERR.puts "set_tags_to(...) is deprecated"
  self.clear_tags("info")
  self.add_tags(*(args.uniq))
end
tags(reload=false) click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_taggable.rb, line 40
def tags(reload=false)
  @params["tags"] ||= []
  @params["tags"].map! { |item| item.is_a?(Hash) ? item["name"] : item }
  @params["tags"].deep_merge!(Tag.search_by_href(self.href).map { |hsh| hsh["name"] }) if reload or @params["tags"].empty?
  @params["tags"]
end