Module: Mackerel::REST::Host

Included in:
Client
Defined in:
lib/mackerel/host.rb

Instance Method Summary collapse

Instance Method Details

#get_host(host_id) ⇒ Object



64
65
66
67
68
# File 'lib/mackerel/host.rb', line 64

def get_host(host_id)
  command = ApiCommand.new(:get, "/api/v0/hosts/#{host_id}", @api_key)
  data = command.execute(client)
  Mackerel::Host.new(data['host'])
end

#get_host_metric_names(host_id) ⇒ Object



97
98
99
100
101
# File 'lib/mackerel/host.rb', line 97

def get_host_metric_names(host_id)
  command = ApiCommand.new(:get, "/api/v0/hosts/#{host_id}/metric-names", @api_key)
  data = command.execute(client)
  data["names"]
end

#get_hosts(opts = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/mackerel/host.rb', line 86

def get_hosts(opts = {})
  command = ApiCommand.new(:get, '/api/v0/hosts', @api_key)
  command.params['service']          = opts[:service] if opts[:service]
  command.params['role']             = opts[:roles]   if opts[:roles]
  command.params['name']             = opts[:name]    if opts[:name]
  command.params['status']           = opts[:status]  if opts[:status]
  command.params['customIdentifier'] = opts[:customIdentifier] if opts[:customIdentifier]
  data = command.execute(client)
  data['hosts'].map{ |host_json| Mackerel::Host.new(host_json) }
end

#post_host(host) ⇒ Object



45
46
47
48
49
# File 'lib/mackerel/host.rb', line 45

def post_host(host)
  command = ApiCommand.new(:post, "/api/v0/hosts", @api_key)
  command.body = host.to_json
  command.execute(client)
end

#retire_host(host_id) ⇒ Object



80
81
82
83
84
# File 'lib/mackerel/host.rb', line 80

def retire_host(host_id)
  command = ApiCommand.new(:post, "/api/v0/hosts/#{host_id}/retire", @api_key)
  command.body = { }.to_json
  command.execute(client)
end

#update_host(host_id, host) ⇒ Object



51
52
53
54
55
# File 'lib/mackerel/host.rb', line 51

def update_host(host_id, host)
  command = ApiCommand.new(:put, "/api/v0/hosts/#{host_id}", @api_key)
  command.body = host.to_json
  command.execute(client)
end

#update_host_roles(host_id, roles) ⇒ Object



57
58
59
60
61
62
# File 'lib/mackerel/host.rb', line 57

def update_host_roles(host_id, roles)
  roles = [roles] if roles.is_a?(String)
  command = ApiCommand.new(:put, "/api/v0/hosts/#{host_id}/role-fullnames", @api_key)
  command.body = { "roleFullnames" => roles }.to_json
  command.execute(client)
end

#update_host_status(host_id, status) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/mackerel/host.rb', line 70

def update_host_status(host_id, status)
  unless [:standby, :working, :maintenance, :poweroff].include?(status.to_sym)
    raise "no such status: #{status}"
  end

  command = ApiCommand.new(:post, "/api/v0/hosts/#{host_id}/status", @api_key)
  command.body = { "status" => status }.to_json
  command.execute(client)
end