Module: Fog::CloudSigma::CloudSigmaConnection::Real

Included in:
Fog::Compute::CloudSigma::Real
Defined in:
lib/fog/cloudsigma/connection.rb

Instance Method Summary collapse

Instance Method Details

#auth_header(type = :basic) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fog/cloudsigma/connection.rb', line 7

def auth_header(type = :basic)
  case type
    when :basic
      unless @username and @password
        raise ArgumentError, 'Username and password required for basic auth'
      end
      {'Authorization' => 'Basic ' << Base64.encode64("#{@username}:#{@password}").gsub("\n", '')}
    else
      unless @username and @password
        raise ArgumentError, 'Username and password required for basic auth'
      end
      {'Authorization' => 'Basic ' << Base64.encode64("#{@username}:#{@password}").gsub("\n", '')}
  end
end

#create_request(path, data, override_params = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/fog/cloudsigma/connection.rb', line 89

def create_request(path, data, override_params={})
  default_params = {:method => 'POST', :expects => [200, 201, 202]}

  override_params[:path] = path
  override_params[:body] = data
  params = default_params.merge(override_params)

  request(params)
end

#delete_request(path, override_params = {}) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/fog/cloudsigma/connection.rb', line 81

def delete_request(path, override_params={})
  default_params = {:method => 'DELETE', :expects => 204}
  override_params[:path] = path
  params = default_params.merge(override_params)

  request(params)
end

#get_request(path, override_params = {}) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/fog/cloudsigma/connection.rb', line 73

def get_request(path, override_params={})
  default_params = {:method => 'GET', :expects => 200}
  override_params[:path] = path
  params = default_params.merge(override_params)

  request(params)
end

#list_request(path, override_params = {}) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/fog/cloudsigma/connection.rb', line 65

def list_request(path, override_params={})
  default_params = {:method => 'GET', :expects => 200, :query => {:limit => 0}}
  override_params[:path] = path
  params = default_params.merge(override_params)

  request(params)
end

#request(params) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fog/cloudsigma/connection.rb', line 42

def request(params)
  params[:headers] = params.fetch(:headers, {}).merge(auth_header(@auth_type))
  params[:headers]['Content-Type'] = 'application/json; charset=utf-8'

  req_path = params[:path]
  params[:path] = "#{@path_prefix}#{req_path}"

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

  begin
    response = @connection.request(params)
  rescue Excon::Errors::HTTPStatusError => e

    e.response.data[:body] = Fog::JSON.decode(e.response[:body]) unless e.response[:body].empty?
    err = Fog::CloudSigma::Errors.slurp_http_status_error(e)

    raise err
  end
  response.body = Fog::JSON.decode(response.body) unless response.body.empty?

  response
end

#setup_connection(options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/cloudsigma/connection.rb', line 22

def setup_connection(options)
  @persistent = options[:persistent] || false
  @connection_options = options[:connection_options] || {}
  @connection_options[:ssl_verify_peer] = false

  @auth_type = options[:cloudsigma_auth_type] || :basic

  @username = options[:cloudsigma_username]
  @password = options[:cloudsigma_password]

  @scheme = options[:cloudsigma_scheme] || 'https'
  @host = options[:cloudsigma_host] || 'lvs.cloudsigma.com'
  @port = options[:cloudsigma_port] || '443'
  @api_path_prefix = options[:cloudsigma_api_path_prefix] || 'api'
  @api_version = options[:cloudsigma_api_version] || '2.0'
  @path_prefix = "#{@api_path_prefix}/#{@api_version}/"

  @connection = Fog::XML::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end

#update_request(path, data, override_params = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/fog/cloudsigma/connection.rb', line 99

def update_request(path, data, override_params={})
  default_params = {:method => 'PUT', :expects => [200, 202]}

  override_params[:path] = path
  override_params[:body] = data
  params = default_params.merge(override_params)

  request(params)
end