class Fog::Compute::Fifo::Real

Public Class Methods

new(options = {}) click to toggle source
# File lib/fog/fifo/compute.rb, line 82
def initialize(options = {})

  @connection_options = options[:connection_options] || {}
  @persistent = options[:persistent] || false

  @fifo_username = options[:fifo_username]

  unless @fifo_username
    raise ArgumentError, "options[:fifo_username] required"
  end

  @fifo_url = options[:fifo_url]

  unless @fifo_url
    raise ArgumentError, "options[:fifo_url] required"
  end

  @fifo_password = options[:fifo_password]

  unless @fifo_password
    raise ArgumentError, "options[:fifo_password] required"
  end

  @fifo_password = options[:fifo_password]

  @fifo_token = nil

  @fifo_uri = ::URI.parse(@fifo_url)

  @connection = Fog::Connection.new(
    @fifo_url,
    @persistent,
    @connection_options
  )
  authenticate
end

Public Instance Methods

authenticate() click to toggle source
# File lib/fog/fifo/compute.rb, line 119
def authenticate
  response = @connection.request({
                                   :expects => [200, 204, 303],
                                   #:host    => @fifo_uri.host,
                                   :method  => 'POST',
                                   :path    => create_path('sessions'),
                                   :headers => {
                                     "Content-Type" => "application/json",
                                     "Accept" => "application/json"
                                   },
                                   :body => Fog::JSON.encode({
                                                               "user" => @fifo_username,
                                                               "password" => @fifo_password
                                                             })
                                 })

  @fifo_token = response.headers["x-snarl-token"] || File.basename(response.headers["location"])
end
create_vm(params) click to toggle source
# File lib/fog/fifo/requests/compute/create_vm.rb, line 19
def create_vm(params)
  request(
          :method => "POST",
          :path => "vms",
          :expects => [303],
          :body => params
          )
end
delete_vm(id) click to toggle source
# File lib/fog/fifo/requests/compute/delete_vm.rb, line 18
def delete_vm(id)
  request(
          :method => "DELETE",
          :path => "vms/#{id}",
          :expects => [200]
          )
end
get_dataset(id) click to toggle source
# File lib/fog/fifo/requests/compute/get_dataset.rb, line 18
def get_dataset(id)
  request(
    :method => "GET",
    :path => "datasets/#{id}"
  )
end
get_iprange(id) click to toggle source
# File lib/fog/fifo/requests/compute/get_iprange.rb, line 19
def get_iprange(id)
  request(
          :method => "GET",
          :path => "ipranges/#{id}",
          :expects => 200
          )
end
get_network(id) click to toggle source
# File lib/fog/fifo/requests/compute/get_network.rb, line 19
def get_network(id)
  request(
          :method => "GET",
          :path => "networks/#{id}",
          :expects => 200
          )
end
get_package(id) click to toggle source
# File lib/fog/fifo/requests/compute/get_package.rb, line 19
def get_package(id)
  request(
    :method => "GET",
    :path => "packages/#{id}",
    :expects => 200
  )
end
get_vm(id) click to toggle source
# File lib/fog/fifo/requests/compute/get_vm.rb, line 19
def get_vm(id)
  request(
          :method => "GET",
          :path => "vms/#{id}",
          :expects => [200]
          )
end
list_datasets() click to toggle source
# File lib/fog/fifo/requests/compute/list_datasets.rb, line 15
def list_datasets
  request(
    :method => "GET",
    :path => "datasets"
  )
end
list_ipranges() click to toggle source
# File lib/fog/fifo/requests/compute/list_ipranges.rb, line 15
def list_ipranges
  request(
    :path => "ipranges",
    :method => "GET",
    :expects => 200
  )
end
list_networks() click to toggle source
# File lib/fog/fifo/requests/compute/list_networks.rb, line 15
def list_networks
  request(
    :path => "networks",
    :method => "GET",
    :expects => 200
  )
end
list_packages() click to toggle source
# File lib/fog/fifo/requests/compute/list_packages.rb, line 15
def list_packages
  request(
    :path => "packages",
    :method => "GET",
    :expects => 200
  )
end
list_vms(options={}) click to toggle source
# File lib/fog/fifo/requests/compute/list_vms.rb, line 15
def list_vms(options={})
  request(
    :path => "vms",
    :method => "GET",
    :query => options,
    :expects => 200
  )
end
reboot_vm(id) click to toggle source
# File lib/fog/fifo/requests/compute/reboot_vm.rb, line 18
def reboot_vm(id)
  request(
          :method => "PUT",
          :path => "vms/#{id}",
          :expects => [200],
          :body => {"action" => "reboot"},
          )
end
request(opts = {}) click to toggle source
# File lib/fog/fifo/compute.rb, line 138
def request(opts = {})
  opts[:headers] = {
    "X-Api-Version" => @fifo_version,
    "Content-Type" => "application/json",
    "Accept" => "application/json",
    "x-Snarl-Token" => @fifo_token
  }.merge(opts[:headers] || {})

  if opts[:body]
    opts[:body] = Fog::JSON.encode(opts[:body])
  end

  opts[:path] = create_path(opts[:path])
  
  response = @connection.request(opts)
  if response.headers["Content-Type"] == "application/json"
    response.body = json_decode(response.body)
  end

  response
rescue Excon::Errors::HTTPStatusError => e
  raise_if_error!(e.request, e.response)
end
start_vm(id) click to toggle source
# File lib/fog/fifo/requests/compute/start_vm.rb, line 19
def start_vm(id)
  request(
          :method => "PUT",
          :path => "vms/#{id}",
          :expects => [200],
          :body => {"action" => "start"},
          )
end
stop_vm(id) click to toggle source
# File lib/fog/fifo/requests/compute/stop_vm.rb, line 19
def stop_vm(id)
  request(
          :method => "PUT",
          :path => "vms/#{id}",
          :expects => [200],
          :body => {"action" => "stop"},
          )
end

Private Instance Methods

create_path(path) click to toggle source
# File lib/fog/fifo/compute.rb, line 164
def create_path(path)
  ::File.join(@fifo_uri.path, path)
end
decode_time_attrs(obj) click to toggle source
# File lib/fog/fifo/compute.rb, line 173
def decode_time_attrs(obj)
  if obj.kind_of?(Hash)
    obj["created"] = Time.parse(obj["created"]) unless obj["created"].nil? or obj["created"] == ''
    obj["updated"] = Time.parse(obj["updated"]) unless obj["updated"].nil? or obj["updated"] == ''
  elsif obj.kind_of?(Array)
    obj.map do |o|
      decode_time_attrs(o)
    end
  end

  obj
end
json_decode(body) click to toggle source
# File lib/fog/fifo/compute.rb, line 168
def json_decode(body)
  parsed = Fog::JSON.decode(body)
  decode_time_attrs(parsed)
end
raise_if_error!(request, response) click to toggle source
# File lib/fog/fifo/compute.rb, line 186
def raise_if_error!(request, response)
  case response.status
  when 401 then
    raise Fifo::Errors::Unauthorized.new('Invalid credentials were used', request, response)
  when 403 then
    raise Fifo::Errors::Forbidden.new('No permissions to the specified resource', request, response)
  when 404 then
    raise Fifo::Errors::NotFound.new('Requested resource was not found', request, response)
  when 405 then
    raise Fifo::Errors::MethodNotAllowed.new('Method not supported for the given resource', request, response)
  when 406 then
    raise Fifo::Errors::NotAcceptable.new('Try sending a different Accept header', request, response)
  when 409 then
    raise Fifo::Errors::Conflict.new('Most likely invalid or missing parameters', request, response)
  when 414 then
    raise Fifo::Errors::RequestEntityTooLarge.new('You sent too much data', request, response)
  when 415 then
    raise Fifo::Errors::UnsupportedMediaType.new('You encoded your request in a format we don\'t understand', request, response)
  when 420 then
    raise Fifo::Errors::PolicyNotForfilled.new('You are sending too many requests', request, response)
  when 449 then
    raise Fifo::Errors::RetryWith.new('Invalid API Version requested; try with a different API Version', request, response)
  when 503 then
    raise Fifo::Errors::ServiceUnavailable.new('Either there\'s no capacity in this datacenter, or we\'re in a maintenance window', request, response)
  end
end