Class: Runcible::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/runcible/base.rb

Class Method Summary collapse

Class Method Details

.add_http_auth_headerObject



179
180
181
# File 'lib/runcible/base.rb', line 179

def self.add_http_auth_header
  return {:user => config[:user], :password => config[:http_auth][:password]}
end

.add_oauth_header(method, path, headers) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/runcible/base.rb', line 183

def self.add_oauth_header(method, path, headers)
  default_options = { :site               => config[:url],
                      :http_method        => method,
                      :request_token_path => "",
                      :authorize_path     => "",
                      :access_token_path  => "" }

  default_options[:ca_file] = config[:ca_cert_file] unless config[:ca_cert_file].nil?
  consumer = OAuth::Consumer.new(config[:oauth][:oauth_key], config[:oauth][:oauth_secret], default_options)

  method_to_http_request = { :get    => Net::HTTP::Get,
                             :post   => Net::HTTP::Post,
                             :put    => Net::HTTP::Put,
                             :delete => Net::HTTP::Delete }

  http_request = method_to_http_request[method].new(path)
  consumer.sign!(http_request)

  headers['Authorization'] = http_request['Authorization']
  return headers
end

.call(method, path, options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/runcible/base.rb', line 62

def self.call(method, path, options={})
  clone_config = self.config.clone
  #on occation path will already have prefix (sync cancel)
  path = clone_config[:api_path] + path if !path.start_with?(clone_config[:api_path])

  RestClient.log    = []
  logger            = clone_config[:logging][:logger]
  debug_logging     = clone_config[:logging][:debug]
  exception_logging = clone_config[:logging][:exception]

  headers = clone_config[:headers].clone

  get_params = options[:params] if options[:params]
  path = combine_get_params(path, get_params) if get_params

  client_options = {}
  client_options[:timeout] =  clone_config[:timeout] if clone_config[:timeout]
  client_options[:open_timeout] =  clone_config[:open_timeout] if clone_config[:open_timeout]

  if clone_config[:oauth]
    headers = add_oauth_header(method, path, headers) if clone_config[:oauth]
    headers["pulp-user"] = clone_config[:user]
    client = RestClient::Resource.new(clone_config[:url], client_options)
  else
    client_options[:user] =  clone_config[:user]
    client_options[:password] = config[:http_auth][:password]
    client = RestClient::Resource.new(clone_config[:url], client_options)
  end

  args = [method]
  args << generate_payload(options) if [:post, :put].include?(method)
  args << headers 

  response = get_response(client, path, *args)
  process_response(response)

rescue => e
  log_exception
  raise e
end

.combine_get_params(path, params) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/runcible/base.rb', line 111

def self.combine_get_params(path, params)
  query_string  = params.collect do |k, v|
    if v.is_a? Array
      v.collect{|y| "#{k.to_s}=#{y.to_s}" }.join('&')
    else
      "#{k.to_s}=#{v.to_s}"
    end
  end.flatten().join('&')
  path + "?#{query_string}"
end

.configObject



54
55
56
57
58
59
60
# File 'lib/runcible/base.rb', line 54

def self.config
  if defined?(@@config)
    @@config
  else
    raise Runcible::ConfigurationUndefinedError, Runcible::ConfigurationUndefinedError.message
  end
end

.config=(conf = {}) ⇒ Object

Accepted Configuration Values

:user Pulp username :password Password for this user :oauth Oauth credentials :headers Additional headers e.g. content-type => “application/json” :url Scheme and hostname for the pulp server e.g. localhost/ :api_path URL path for the api e.g. pulp/api/v2/ :timeout timeout in seconds for the connection (defaults to rest client’s default) :open_timeout timeout in seconds for the connection to open(defaults to rest client’s default)



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/runcible/base.rb', line 42

def self.config=(conf={})
  @@config = {
    :api_path   => '/pulp/api/v2/',
    :url        => 'https://localhost',
    :user       => '',
    :http_auth  => {:password => {} },
    :headers    => {:content_type => 'application/json',
                    :accept       => 'application/json'},
    :logging    => {}
  }.merge(conf)
end

.generate_log_messageObject



219
220
221
# File 'lib/runcible/base.rb', line 219

def self.generate_log_message
  RestClient.log.join('\n')
end

.generate_payload(options) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/runcible/base.rb', line 122

def self.generate_payload(options)
  if options[:payload]
    if options[:payload][:optional]
      if options[:payload][:required]
        payload = options[:payload][:required].merge(options[:payload][:optional])
      else
        payload = options[:payload][:optional]
      end
    elsif options[:payload][:delta]
      payload = options[:payload]
    else
      payload = options[:payload][:required]
    end
  else
    payload = {}
  end

  return payload.to_json
end

.get_response(client, path, *args) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/runcible/base.rb', line 103

def self.get_response(client, path, *args)
  client[path].send(*args) do |response, request, result, &block|
    resp = response.return!(request, result)
    log_debug
    return resp
  end
end

.log_debugObject



205
206
207
208
209
210
# File 'lib/runcible/base.rb', line 205

def self.log_debug
  if self.config[:logging][:debug]
    log_message = generate_log_message                  
    self.config[:logging][:logger].debug(log_message)
  end
end

.log_exceptionObject



212
213
214
215
216
217
# File 'lib/runcible/base.rb', line 212

def self.log_exception
  if self.config[:logging][:exception]
    log_message = generate_log_message
    self.config[:logging][:logger].error(log_message)
  end
end

.process_response(response) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/runcible/base.rb', line 142

def self.process_response(response)
  begin
    body = JSON.parse(response.body)
    if body.respond_to? :with_indifferent_access
      body = body.with_indifferent_access
    elsif body.is_a? Array
      body = body.collect  do |i|
        i.respond_to?(:with_indifferent_access) ? i.with_indifferent_access : i
      end
    end
    response = RestClient::Response.create(body, response.net_http_res, response.args)
  rescue JSON::ParserError
  end

  return response
end

.required_params(local_names, binding, keys_to_remove = []) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/runcible/base.rb', line 159

def self.required_params(local_names, binding, keys_to_remove=[])
  local_names = local_names.reduce({}) do |acc, v|
    value = binding.eval(v.to_s) unless v == :_
    acc[v] = value unless value.nil?
    acc
  end

  #The double delete is to support 1.8.7 and 1.9.3
  local_names.delete(:payload)
  local_names.delete(:optional)
  local_names.delete("payload")
  local_names.delete("optional")
  keys_to_remove.each do |key|
    local_names.delete(key)
    local_names.delete(key.to_sym)
  end

  return local_names
end