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



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

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



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

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



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

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



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

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



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

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



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

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



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

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::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end

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



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

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