Class: Fog::Compute::Serverlove::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/compute/serverlove.rb,
lib/fog/compute/serverlove/requests/get_image.rb,
lib/fog/compute/serverlove/requests/get_images.rb,
lib/fog/compute/serverlove/requests/get_server.rb,
lib/fog/compute/serverlove/requests/get_servers.rb,
lib/fog/compute/serverlove/requests/stop_server.rb,
lib/fog/compute/serverlove/requests/create_image.rb,
lib/fog/compute/serverlove/requests/reset_server.rb,
lib/fog/compute/serverlove/requests/start_server.rb,
lib/fog/compute/serverlove/requests/update_image.rb,
lib/fog/compute/serverlove/requests/create_server.rb,
lib/fog/compute/serverlove/requests/destroy_image.rb,
lib/fog/compute/serverlove/requests/update_server.rb,
lib/fog/compute/serverlove/requests/destroy_server.rb,
lib/fog/compute/serverlove/requests/shutdown_server.rb,
lib/fog/compute/serverlove/requests/load_standard_image.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Real

Returns a new instance of Real.



52
53
54
55
56
57
58
# File 'lib/fog/compute/serverlove.rb', line 52

def initialize(options)
  @api_uuid = options[:serverlove_uuid] || Fog.credentials[:serverlove_uuid]
  @api_key = options[:serverlove_api_key] || Fog.credentials[:serverlove_api_key]
  @api_host = options[:serverlove_api_url] || Fog.credentials[:serverlove_api_url] || API_HOST

  @connection = Fog::Core::Connection.new("https://#{@api_uuid}:#{@api_key}@#{@api_host}")
end

Instance Method Details

#create_image(options) ⇒ Object



5
6
7
8
# File 'lib/fog/compute/serverlove/requests/create_image.rb', line 5

def create_image(options)
  return nil if options.empty? || options.nil?
  request(:method => "post", :path => "/drives/create", :expects => 200, :options => options)
end

#create_server(options) ⇒ Object



5
6
7
8
# File 'lib/fog/compute/serverlove/requests/create_server.rb', line 5

def create_server(options)
  return nil if options.empty? || options.nil?
  request(:method => "post", :path => "/servers/create/stopped", :expects => 200, :options => options)
end

#destroy_image(drive_id) ⇒ Object



5
6
7
# File 'lib/fog/compute/serverlove/requests/destroy_image.rb', line 5

def destroy_image(drive_id)
  request(:method => "post", :path => "/drives/#{drive_id}/destroy", :expects => 204)
end

#destroy_server(server_id) ⇒ Object



5
6
7
# File 'lib/fog/compute/serverlove/requests/destroy_server.rb', line 5

def destroy_server(server_id)
  request(:method => "post", :path => "/servers/#{server_id}/destroy", :expects => 204)
end

#encode_pairs(params) ⇒ Object



79
80
81
82
83
# File 'lib/fog/compute/serverlove.rb', line 79

def encode_pairs(params)
  params.keys.map do |key|
    "#{key} #{params[key]}"
  end.join("\n")
end

#get_image(image_id) ⇒ Object



5
6
7
# File 'lib/fog/compute/serverlove/requests/get_image.rb', line 5

def get_image(image_id)
  request(:method => "get", :path => "/drives/#{image_id}/info", :expects => 200)
end

#get_imagesObject



5
6
7
# File 'lib/fog/compute/serverlove/requests/get_images.rb', line 5

def get_images
  request(:method => "get", :path => "/drives/info", :expects => 200)
end

#get_server(server_id) ⇒ Object



5
6
7
# File 'lib/fog/compute/serverlove/requests/get_server.rb', line 5

def get_server(server_id)
  request(:method => "get", :path => "/servers/#{server_id}/info", :expects => 200)
end

#get_serversObject



5
6
7
# File 'lib/fog/compute/serverlove/requests/get_servers.rb', line 5

def get_servers
  request(:method => "get", :path => "/servers/info", :expects => 200)
end

#load_standard_image(destination_image, source_image) ⇒ Object



5
6
7
# File 'lib/fog/compute/serverlove/requests/load_standard_image.rb', line 5

def load_standard_image(destination_image, source_image)
  request(:method => "post", :path => "/drives/#{destination_image}/image/#{source_image}/gunzip", :expects => 204)
end

#raise_if_error!(response) ⇒ Object

TODO



86
87
88
89
90
91
# File 'lib/fog/compute/serverlove.rb', line 86

def raise_if_error!(response)
  case response.status
  when 400 then
    raise "omg"
  end
end

#request(params) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fog/compute/serverlove.rb', line 60

def request(params)
  params = params.merge!(
    :headers => {
      "Accept" => "application/json"
    }
  )
  unless params[:options].nil?
    params[:body] = encode_pairs(params[:options])
    params.delete(:options)
  end
  response = @connection.request(params)

  raise_if_error!(response)

  response.body = Fog::JSON.decode(response.body) if response.body && response.body.length > 0

  response
end

#reset_server(server_id) ⇒ Object



5
6
7
# File 'lib/fog/compute/serverlove/requests/reset_server.rb', line 5

def reset_server(server_id)
  request(:method => "post", :path => "/servers/#{server_id}/reset", :expects => 204)
end

#shutdown_server(server_id) ⇒ Object



5
6
7
# File 'lib/fog/compute/serverlove/requests/shutdown_server.rb', line 5

def shutdown_server(server_id)
  request(:method => "post", :path => "/servers/#{server_id}/shutdown", :expects => 204)
end

#start_server(server_id) ⇒ Object



5
6
7
# File 'lib/fog/compute/serverlove/requests/start_server.rb', line 5

def start_server(server_id)
  request(:method => "post", :path => "/servers/#{server_id}/start", :expects => 200)
end

#stop_server(server_id) ⇒ Object



5
6
7
# File 'lib/fog/compute/serverlove/requests/stop_server.rb', line 5

def stop_server(server_id)
  request(:method => "post", :path => "/servers/#{server_id}/stop", :expects => 204)
end

#update_image(identifier, options) ⇒ Object



5
6
7
8
9
# File 'lib/fog/compute/serverlove/requests/update_image.rb', line 5

def update_image(identifier, options)
  return nil if identifier.nil? || identifier == ""
  return nil if options.empty? || options.nil?
  request(:method => "post", :path => "/drives/#{identifier}/set", :expects => 200, :options => options)
end

#update_server(identifier, options) ⇒ Object



5
6
7
8
9
# File 'lib/fog/compute/serverlove/requests/update_server.rb', line 5

def update_server(identifier, options)
  return nil if identifier.nil? || identifier == ""
  return nil if options.empty? || options.nil?
  request(:method => "post", :path => "/servers/#{identifier}/set", :expects => 200, :options => options)
end