Project

General

Profile

Download (6.79 KB) Statistics
| Branch: | Tag: | Revision:

runcible / lib / runcible / base.rb @ 14da342c

1
require 'rest_client'
2
require 'oauth'
3
require 'json'
4
require 'thread'
5

    
6
module Runcible
7
  class Base
8
    def initialize(config = {})
9
      @mutex = Mutex.new
10
      @config = config
11
    end
12

    
13
    def lazy_config=(a_block)
14
      @mutex.synchronize { @lazy_config = a_block }
15
    end
16

    
17
    def config
18
      @mutex.synchronize do
19
        @config = @lazy_config.call if defined?(@lazy_config)
20
        fail Runcible::ConfigurationUndefinedError, Runcible::ConfigurationUndefinedError.message unless @config
21
        @config
22
      end
23
    end
24

    
25
    def path(*args)
26
      self.class.path(*args)
27
    end
28

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

    
34
      RestClient.log = []
35
      headers = clone_config[:headers].clone
36

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

    
40
      client_options = {}
41
      client_options[:timeout] = clone_config[:timeout] if clone_config[:timeout]
42
      client_options[:open_timeout] = clone_config[:open_timeout] if clone_config[:open_timeout]
43
      client_options[:verify_ssl] = clone_config[:verify_ssl] unless clone_config[:verify_ssl].nil?
44

    
45
      if clone_config[:oauth]
46
        self.logger.warn('[DEPRECATION] Pulp oauth is deprecated.  Please use cert_auth instead.')
47
        headers = add_oauth_header(method, path, headers)
48
        headers['pulp-user'] = clone_config[:user]
49
      elsif clone_config[:cert_auth]
50
        if !clone_config[:cert_auth][:ssl_client_cert] || !clone_config[:cert_auth][:ssl_client_key]
51
          fail Runcible::ConfigurationUndefinedError, "Missing SSL certificate or key configuration."
52
        end
53
        client_options[:ssl_client_cert] = clone_config[:cert_auth][:ssl_client_cert]
54
        client_options[:ssl_client_key] = clone_config[:cert_auth][:ssl_client_key]
55
      else
56
        client_options[:user] = clone_config[:user]
57
        client_options[:password] = config[:http_auth][:password]
58
      end
59

    
60
      client_options[:ssl_ca_file] = config[:ca_cert_file] unless config[:ca_cert_file].nil?
61
      client = RestClient::Resource.new(clone_config[:url], client_options)
62

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

    
67
      response = get_response(client, path, *args)
68
      process_response(response)
69

    
70
    rescue RestClient::ResourceNotFound => e
71
      log_info
72
      raise e
73
    rescue => e
74
      log_exception
75
      raise e
76
    end
77

    
78
    def get_response(client, path, *args)
79
      client[path].send(*args) do |response, _request, _result, &_block|
80
        resp = response.return!
81
        log_debug
82
        return resp
83
      end
84
    end
85

    
86
    def combine_get_params(path, params)
87
      query_string = params.map do |k, v|
88
        if v.is_a? Array
89
          v.map { |y| "#{k}=#{y}" }.join('&')
90
        else
91
          "#{k}=#{v}"
92
        end
93
      end
94
      query_string = query_string.flatten.join('&')
95
      path + "?#{query_string}"
96
    end
97

    
98
    def generate_payload(options)
99
      if options[:payload].is_a?(String)
100
        return options[:payload]
101
      elsif options[:payload].is_a?(Hash)
102
        format_payload_json(options[:payload])
103
      end
104
    end
105

    
106
    def format_payload_json(payload_hash)
107
      if payload_hash
108
        if payload_hash[:optional]
109
          payload = if payload_hash[:required]
110
                      payload_hash[:required].merge(payload_hash[:optional])
111
                    else
112
                      payload_hash[:optional]
113
                    end
114
        elsif payload_hash[:delta]
115
          payload = payload_hash
116
        else
117
          payload = payload_hash[:required]
118
        end
119
      else
120
        payload = {}
121
      end
122

    
123
      return payload.to_json
124
    end
125

    
126
    def process_response(response)
127
      begin
128
        body = response.body == "null" ? nil : JSON.parse(response.body)
129
        if body.respond_to? :with_indifferent_access
130
          body = body.with_indifferent_access
131
        elsif body.is_a? Array
132
          body = body.map do |i|
133
            i.respond_to?(:with_indifferent_access) ? i.with_indifferent_access : i
134
          end
135
        end
136
        response = Runcible::Response.new(body, response)
137
      rescue JSON::ParserError
138
        log_exception
139
      end
140

    
141
      return response
142
    end
143

    
144
    def required_params(local_names, binding, keys_to_remove = [])
145
      local_names = local_names.each_with_object({}) do |v, acc|
146
        value = binding.eval(v.to_s) unless v == :_
147
        acc[v] = value unless value.nil?
148
        acc
149
      end
150

    
151
      #The double delete is to support 1.8.7 and 1.9.3
152
      local_names.delete(:payload)
153
      local_names.delete(:optional)
154
      local_names.delete('payload')
155
      local_names.delete('optional')
156
      keys_to_remove.each do |key|
157
        local_names.delete(key)
158
        local_names.delete(key.to_sym)
159
      end
160

    
161
      return local_names
162
    end
163

    
164
    def add_http_auth_header
165
      return {:user => config[:user], :password => config[:http_auth][:password]}
166
    end
167

    
168
    def add_oauth_header(method, path, headers)
169
      default_options = { :site               => config[:url],
170
                          :http_method        => method,
171
                          :request_token_path => '',
172
                          :authorize_path     => '',
173
                          :access_token_path  => '' }
174

    
175
      consumer = OAuth::Consumer.new(config[:oauth][:oauth_key], config[:oauth][:oauth_secret], default_options)
176

    
177
      method_to_http_request = { :get    => Net::HTTP::Get,
178
                                 :post   => Net::HTTP::Post,
179
                                 :put    => Net::HTTP::Put,
180
                                 :delete => Net::HTTP::Delete }
181

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

    
185
      headers['Authorization'] = http_request['Authorization']
186
      return headers
187
    end
188

    
189
    def log_debug
190
      if self.config[:logging][:debug]
191
        log_message = generate_log_message
192
        self.config[:logging][:logger].debug(log_message)
193
      end
194
    end
195

    
196
    def log_exception
197
      if self.config[:logging][:exception]
198
        log_message = generate_log_message
199
        self.config[:logging][:logger].error(log_message)
200
      end
201
    end
202

    
203
    def log_info
204
      if self.config[:logging][:info]
205
        log_message = generate_log_message
206
        self.config[:logging][:logger].info(log_message)
207
      end
208
    end
209

    
210
    def generate_log_message
211
      RestClient.log.join('\n')
212
    end
213

    
214
    def logger
215
      self.config[:logging][:logger]
216
    end
217
  end
218

    
219
  class ConfigurationUndefinedError < StandardError
220
    def self.message
221
      # override me to change the error message
222
      'Configuration not set. Runcible::Base.config= must be called before Runcible::Base.config.'
223
    end
224
  end
225
end