Class: ScoutScout::Server

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/scout_scout/server.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Server

Returns a new instance of Server.



2
3
4
5
6
7
8
# File 'lib/scout_scout/server.rb', line 2

def initialize(hash)
  if hash['active_alerts']
    @alert_hash = hash['active_alerts']
    hash.delete('active_alerts')
  end
  super(hash)
end

Class Method Details

.all(options) ⇒ Array

Search for servers by matching hostname via :host.

Example: ScoutScout::Server.all(:host => ‘soawesome.org’)

Returns:

  • (Array)

    An array of ScoutScout::Server objects

Raises:



60
61
62
63
64
65
# File 'lib/scout_scout/server.rb', line 60

def self.all(options)
  hostname = options[:host]
  raise ScoutScout::Error, "Please specify a host via :host" if hostname.nil?
  response = ScoutScout.get("/#{ScoutScout.}/clients.xml?host=#{hostname}")
  response['clients'] ? response['clients'].map { |client| ScoutScout::Server.new(client) } : Array.new
end

.create(name, options = {}) ⇒ ScoutScout::Server

Creates a new server. If an error occurs, a ScoutScout::Error is raised.

An optional existing server id can be used as a template: ScoutScout::Server.create(‘web server 12’,:id => 99999)

Returns:

Raises:



30
31
32
33
34
35
36
37
38
# File 'lib/scout_scout/server.rb', line 30

def self.create(name,options = {})
  id = options[:id]
  response = ScoutScout.post("/#{ScoutScout.}/clients.xml", 
  :query => {:client => {:name => name, :copy_plugins_from_client_id => id}})
  
  raise ScoutScout::Error, response['errors']['error'] if response['errors']
  
  first(response.headers['id'].first.to_i)
end

.delete(id) ⇒ true

Delete a server by id. If an error occurs, a ScoutScout::Error is raised.

Returns:

  • (true)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/scout_scout/server.rb', line 43

def self.delete(id)
  response = ScoutScout.delete("/#{ScoutScout.}/clients/#{id}.xml")

  if response.headers['status'].first.match('404')
    raise ScoutScout::Error, "Server Not Found"
  elsif !response.headers['status'].first.match('200')
    raise ScoutScout::Error, "An error occured"
  else
    return true
  end
end

.first(server_id_or_hostname) ⇒ ScoutScout::Server

Search for a server by id or matching hostname

Returns:



13
14
15
16
17
18
19
20
21
22
# File 'lib/scout_scout/server.rb', line 13

def self.first(server_id_or_hostname)
  if server_id_or_hostname.is_a?(Fixnum)
    response = ScoutScout.get("/#{ScoutScout.}/clients/#{server_id_or_hostname}.xml")
    ScoutScout::Server.new(response['client'])
  else
    response = ScoutScout.get("/#{ScoutScout.}/clients.xml?host=#{server_id_or_hostname}")
    raise ScoutScout::Error, 'Not Found' if response['clients'].nil?
    ScoutScout::Server.new(response['clients'].first)
  end
end

Instance Method Details

#active_alertsArray

Active alerts for this server

Returns:

  • (Array)

    An array of ScoutScout::Alert objects



70
71
72
# File 'lib/scout_scout/server.rb', line 70

def active_alerts
  @active_alerts ||= @alert_hash.map { |a| decorate_with_server(ScoutScout::Alert.new(a)) }
end

#alertsArray

Recent alerts for this server

Returns:

  • (Array)

    An array of ScoutScout::Alert objects



77
78
79
80
# File 'lib/scout_scout/server.rb', line 77

def alerts
  response = ScoutScout.get("/#{ScoutScout.}/clients/#{self.id}/activities.xml")
  response['alerts'].map { |alert| decorate_with_server(ScoutScout::Alert.new(alert)) }
end

#descriptorsArray

All descriptors for this server

Returns:

  • (Array)

    An array of ScoutScout::Descriptor objects



101
102
103
# File 'lib/scout_scout/server.rb', line 101

def descriptors
  ScoutScout::Descriptor.all(:host => hostname).map { |d| decorate_with_server(d) }
end

#plugin(id) ⇒ ScoutScout::Plugin

Details about a specific plugin

Returns:



93
94
95
96
# File 'lib/scout_scout/server.rb', line 93

def plugin(id)
  response = ScoutScout.get("/#{ScoutScout.}/clients/#{self.id}/plugins/#{id}.xml")
  decorate_with_server(ScoutScout::Plugin.new(response['plugin']))
end

#pluginsArray

Details about all plugins for this server

Returns:

  • (Array)

    An array of ScoutScout::Plugin objects



85
86
87
88
# File 'lib/scout_scout/server.rb', line 85

def plugins
  response = ScoutScout.get("/#{ScoutScout.}/clients/#{self.id}/plugins.xml")
  response['plugins'].map { |plugin| decorate_with_server(ScoutScout::Plugin.new(plugin)) }
end

#triggersArray

Details about all triggers for this server

Returns:

  • (Array)

    An array of ScoutScout::Trigger objects



108
109
110
111
# File 'lib/scout_scout/server.rb', line 108

def triggers
  response = ScoutScout.get("/#{ScoutScout.}/clients/#{self.id}/triggers.xml")
  response['triggers'].map { |trigger| decorate_with_server(ScoutScout::Trigger.new(trigger)) }
end