Class: Wavefront::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront/metadata.rb

Constant Summary collapse

DEFAULT_PATH =
'/api/manage/source/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, host = DEFAULT_HOST, debug = false) ⇒ Metadata

Returns a new instance of Metadata.



30
31
32
33
34
# File 'lib/wavefront/metadata.rb', line 30

def initialize(token, host=DEFAULT_HOST, debug=false)
  @headers = {'X-AUTH-TOKEN' => token}
  @base_uri = URI::HTTPS.build(:host => host, :path => DEFAULT_PATH)
  debug(debug)
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



28
29
30
# File 'lib/wavefront/metadata.rb', line 28

def base_uri
  @base_uri
end

#headersObject (readonly)

Returns the value of attribute headers.



28
29
30
# File 'lib/wavefront/metadata.rb', line 28

def headers
  @headers
end

Instance Method Details

#add_tags(hostnames, tags) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wavefront/metadata.rb', line 54

def add_tags(hostnames, tags)
  
  # Build and call tagging URI for each host and tag.
  hostnames.each do |hostname|
    host_uri = URI.join(@base_uri.to_s,"#{hostname}/")
    extended_uri = URI.join(host_uri.to_s,"tags/")        
    tags.each do |tag|
      final_uri = URI.join(extended_uri.to_s,tag)
      RestClient.put(final_uri.to_s, {}, @headers)
    end
  end

end

#get_tags(hostname = '') ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wavefront/metadata.rb', line 36

def get_tags(hostname='')
  uri = @base_uri
  
  unless hostname.empty?
    uri = URI.join(@base_uri.to_s, hostname)
  end
  
  begin
    response = RestClient.get(uri.to_s, @headers)
  rescue RestClient::ResourceNotFound
    # If a 404 is returned, we return an empty JSON as this is the expected behaviour for untagged hosts
    response = {}        
  end

  return response
 
end

#remove_tags(hostnames, tags, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/wavefront/metadata.rb', line 68

def remove_tags(hostnames, tags, options={})
      
  hostnames.each do |hostname|
    host_uri = URI.join(@base_uri.to_s,"#{hostname}/")
    extended_uri = URI.join(host_uri.to_s,"tags/")
    tags.each do |tag|
      final_uri = URI.join(extended_uri.to_s,tag)
      RestClient.delete(final_uri.to_s, @headers)
    end
  end

end