class Maas::Client

Attributes

access_token[R]
api_key[R]
site_uri[R]
ui[R]

Public Class Methods

new(api_key, site_uri) click to toggle source
# File lib/chef/maas/client.rb, line 17
def initialize(api_key, site_uri)
  @api_key = api_key
  @site_uri = site_uri
  @access_token = access_token
  @ui = Chef::Knife.ui
end

Public Instance Methods

acquire_node(hostname: nil, zone: nil) click to toggle source
# File lib/chef/maas/client.rb, line 26
def acquire_node(hostname: nil, zone: nil)
  if hostname && zone
    ui.error 'Please specify either a zone or hostname'
  elsif !hostname && !zone
    ui.error 'Please specify a zone or hostname'
  end

  if hostname
    response = post('/machines/', 'op' => 'allocate', 'name' => "#{hostname}")
  elsif zone
    response = post('/machines/', 'op' => 'allocate', 'zone' => "#{zone}")
  end

  parse_response(response)
end
commission_node(system_id) click to toggle source
# File lib/chef/maas/client.rb, line 78
def commission_node(system_id)
  parse_response(post("/machines/#{system_id}/?op=commission"))
end
delete_node(system_id) click to toggle source

TODO: Maybe use method_missing to dynamically build these helpers

# File lib/chef/maas/client.rb, line 44
def delete_node(system_id)
  parse_response(delete("/nodes/#{system_id}/"))
end
deploy_node(system_id) click to toggle source
# File lib/chef/maas/client.rb, line 73
def deploy_node(system_id)
  parse_response(post("/machines/#{system_id}/?op=deploy"))
end
list_node(system_id) click to toggle source
# File lib/chef/maas/client.rb, line 52
def list_node(system_id)
  list = parse_response(get("/nodes/?id=#{system_id}"))
  if list.is_a? Array
    return list.first
  else
    return Hash.new
  end
end
list_nodes() click to toggle source
# File lib/chef/maas/client.rb, line 61
def list_nodes
  parse_response(get('/nodes/'))
end
release_node(system_id) click to toggle source
# File lib/chef/maas/client.rb, line 65
def release_node(system_id)
  parse_response(post("/machines/#{system_id}/?op=release"))
end
show_node(system_id) click to toggle source
# File lib/chef/maas/client.rb, line 48
def show_node(system_id)
  bson_parse(get("/nodes/#{system_id}/?op=details"))
end
start_node(system_id) click to toggle source
# File lib/chef/maas/client.rb, line 69
def start_node(system_id)
  parse_response(post("/machines/#{system_id}/?op=power_on"))
end
stop_node(system_id) click to toggle source
# File lib/chef/maas/client.rb, line 82
def stop_node(system_id)
  parse_response(post("/machines/#{system_id}/?op=power_off"))
end

Private Instance Methods

bson_parse(response) click to toggle source
# File lib/chef/maas/client.rb, line 118
def bson_parse(response)
  decoded = {}
  Hash.from_bson(StringIO.new(response.body)).each_pair do |type, binary|
    decoded[type] = XmlSimple.xml_in(binary.data)
  end
  decoded
end
parse_response(response) click to toggle source
# File lib/chef/maas/client.rb, line 102
def parse_response(response)
  case response
  when Net::HTTPSuccess, Net::HTTPRedirection
    parse_response_json(response)
  else
    ui.error(parse_response_json(response))
    exit 1
  end
end
parse_response_json(response) click to toggle source
# File lib/chef/maas/client.rb, line 112
def parse_response_json(response)
  Chef::JSONCompat.parse(response.body)
rescue Chef::Exceptions::JSON::ParseError
  response.body
end
wait_with_dots(sleep_seconds = 1) click to toggle source
# File lib/chef/maas/client.rb, line 139
def wait_with_dots(sleep_seconds = 1)
  print('.') && sleep(sleep_seconds)
end
with_timeout(max_time = nil, &block) click to toggle source
# File lib/chef/maas/client.rb, line 126
def with_timeout(max_time = nil, &block)
  if max_time
    Timeout.timeout(max_time) do
      block.call
    end
  else
    block.call
  end
rescue Timeout::Error
  ui.error "Request took longer than #{max_time}"
  exit 1
end