Class: SolusVMClient::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/solusvm_client/server.rb

Constant Summary collapse

@@actions =
[
    :reboot,
    :boot,
    :shutdown,
    :status
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_hash) ⇒ Server

Returns a new instance of Server.



17
18
19
20
# File 'lib/solusvm_client/server.rb', line 17

def initialize(api_key, api_hash)
  self.api_key = api_key
  self.api_hash = api_hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/solusvm_client/server.rb', line 22

def method_missing(m, *args, &block)
  if @@actions.include?(m)
    request(m)
  else
    super
  end
end

Instance Attribute Details

#api_hashObject

Returns the value of attribute api_hash.



8
9
10
# File 'lib/solusvm_client/server.rb', line 8

def api_hash
  @api_hash
end

#api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/solusvm_client/server.rb', line 8

def api_key
  @api_key
end

Instance Method Details

#info(options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/solusvm_client/server.rb', line 30

def info(options = {})
  result = request("info", options)
  if options.include?(:ipaddr)
    result[:ipaddr] = result[:ipaddr].split(",")
  end

  [:hdd, :mem, :bw].each do |k|
    if options.include?(k)
      usage = result[k].split(",")
      result[k] = {
          total: usage[0].to_i,
          used: usage[1].to_i,
          free: usage[2].to_i,
          percentage: usage[3].to_i
      }
    end
  end

  result
end

#request(action, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/solusvm_client/server.rb', line 51

def request(action, options = {})
  uri = URI.parse(SolusVMClient.api_url)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  if !SolusVMClient.verify_ssl
    https.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  params = {
      key: self.api_key,
      hash: self.api_hash,
      action: action
  }.merge(options)

  request = Net::HTTP::Post.new(uri)
  request.set_form_data(params)

  response = https.request(request)

  result = {}
  hash = Hash.from_xml("<root>#{response.body}</root>")["root"]

  hash.each do |k,v|
    result[k.to_sym] = v
  end

  raise result[:statusmsg] if result[:status] != "success"

  result
end