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

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Real

Returns a new instance of Real.



57
58
59
60
61
62
63
# File 'lib/fog/serverlove/compute.rb', line 57

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::Connection.new("https://#{@api_uuid}:#{@api_key}@#{@api_host}")
end

Instance Method Details

#create_image(options) ⇒ Object



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

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



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

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



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

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

#destroy_server(server_id) ⇒ Object



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

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

#encode_pairs(params) ⇒ Object



84
85
86
87
88
# File 'lib/fog/serverlove/compute.rb', line 84

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

#get_image(image_id) ⇒ Object



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

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

#get_imagesObject



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

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

#get_server(server_id) ⇒ Object



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

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

#get_serversObject



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

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

#load_standard_image(destination_image, source_image) ⇒ Object



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

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



91
92
93
94
95
96
# File 'lib/fog/serverlove/compute.rb', line 91

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

#request(params) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fog/serverlove/compute.rb', line 65

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



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

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

#shutdown_server(server_id) ⇒ Object



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

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

#start_server(server_id) ⇒ Object



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

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

#stop_server(server_id) ⇒ Object



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

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

#update_image(identifier, options) ⇒ Object



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

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



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

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