Module: PuppetX::Gitlab::APIClient

Defined in:
lib/puppet_x/gitlab/runner.rb

Class Method Summary collapse

Class Method Details

.delete(url, options, proxy, ca_file) ⇒ Object



11
12
13
14
15
16
# File 'lib/puppet_x/gitlab/runner.rb', line 11

def self.delete(url, options, proxy, ca_file)
  response = request(url, Net::HTTP::Delete, options, proxy, ca_file)
  validate(response)

  {}
end

.post(url, options, proxy, ca_file) ⇒ Object



18
19
20
21
22
23
# File 'lib/puppet_x/gitlab/runner.rb', line 18

def self.post(url, options, proxy, ca_file)
  response = request(url, Net::HTTP::Post, options, proxy, ca_file)
  validate(response)

  JSON.parse(response.body)
end

.request(url, http_method, options, proxy, ca_file) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/puppet_x/gitlab/runner.rb', line 25

def self.request(url, http_method, options, proxy, ca_file)
  uri     = URI.parse(url)
  headers = {
    'Accept' => 'application/json',
    'Content-Type' => 'application/json'
  }
  if proxy
    proxy_uri = URI.parse(proxy)
    proxy_host = proxy_uri.host
    proxy_port = proxy_uri.port
  else
    proxy_host = nil
    proxy_port = nil
  end

  http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port)
  if uri.scheme == 'https'
    http.use_ssl     = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    http.ca_file = ca_file if ca_file
  end
  request          = http_method.new(uri.request_uri, headers)
  request.body     = options.to_json

  begin
    http.request(request)
  rescue Net::OpenTimeout
    msg = if http.proxy?
            "Timeout connecting to proxy #{http.proxy_address} when trying to register/unregister gitlab runner"
          else
            "Timeout connecting to #{http.address} when trying to register/unregister gitlab runner"
          end
    raise Puppet::Error, msg
  end
end

.validate(response) ⇒ Object

Raises:

  • (Net::HTTPError)


61
62
63
# File 'lib/puppet_x/gitlab/runner.rb', line 61

def self.validate(response)
  raise Net::HTTPError.new(response.message, response) unless response.code.start_with?('2')
end