Class: Ncio::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ncio/http_client.rb

Overview

HttpClient provides a Net::HTTP instance pre-configured to communicate with the Puppet Node Classification Service. The client will return Ruby native objects where possible, parsing JSON responses from the service.

This client implements v1 of the Node Classification API.

Defined Under Namespace

Classes: ApiError

Constant Summary collapse

OPTION_DEFAULTS =
{
  host: Socket.gethostname,
  port: 4433,
  use_ssl: true,
  cert: ssldir + '/certs/pe-internal-orchestrator.pem',
  key: ssldir + '/private_keys/pe-internal-orchestrator.pem',
  cacert: ssldir + '/certs/ca.pem'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ HttpClient

initialize a new HttpClient instance

Parameters:

  • opts (Hash) (defaults to: {})

    Options

Options Hash (opts):

  • :host (String)

    The API host, e.g. "master1.puppet.vm". Defaults to the local hostname returned by Socket.gethostname

  • :port (Fixnum)

    The API tcp port, Defaults to 4433

  • :cert (String)

    The path to the PEM encoded client certificate. Defaults to "/etc/puppetlabs/puppet/ssl/certs/pe-internal-orchestrator.pem"

  • :key (String)

    The path to the PEM encoded RSA private key used for the SSL client connection. Defaults to "/etc/puppetlabs/puppet/ssl/private_keys/pe-internal-orchestrator.pem"

  • :cacert (String)

    The path to the PEM encoded CA certificate used to authenticate the service URL. Defaults to "/etc/puppetlabs/puppet/ssl/certs/ca.pem"



56
57
58
59
60
61
62
63
64
65
# File 'lib/ncio/http_client.rb', line 56

def initialize(opts = {})
  opts = OPTION_DEFAULTS.merge(opts)
  @use_ssl = opts[:use_ssl]
  @host = opts[:host]
  @port = opts[:port]
  @cert = opts[:cert]
  @key = opts[:key]
  @cacert = opts[:cacert]
  @protocol = use_ssl ? 'https' : 'http'
end

Instance Attribute Details

#cacertObject (readonly)

Returns the value of attribute cacert.



19
20
21
# File 'lib/ncio/http_client.rb', line 19

def cacert
  @cacert
end

#certObject (readonly)

Returns the value of attribute cert.



17
18
19
# File 'lib/ncio/http_client.rb', line 17

def cert
  @cert
end

#hostObject (readonly)

Returns the value of attribute host.



14
15
16
# File 'lib/ncio/http_client.rb', line 14

def host
  @host
end

#keyObject (readonly)

Returns the value of attribute key.



18
19
20
# File 'lib/ncio/http_client.rb', line 18

def key
  @key
end

#portObject (readonly)

Returns the value of attribute port.



15
16
17
# File 'lib/ncio/http_client.rb', line 15

def port
  @port
end

#protocolObject (readonly)

Returns the value of attribute protocol.



20
21
22
# File 'lib/ncio/http_client.rb', line 20

def protocol
  @protocol
end

#use_sslObject (readonly)

Returns the value of attribute use_ssl.



16
17
18
# File 'lib/ncio/http_client.rb', line 16

def use_ssl
  @use_ssl
end

Instance Method Details

#request(req, body = nil) ⇒ Net::HTTPResponse

make a request, pass through to Net::HTTP#request

Parameters:

  • req (Net::HTTPRequest)

    The HTTP request, e.g. an instance of Net::HTTP::Get, Net::HTTP::Post, or Net::HTTP::Head.

  • body (String) (defaults to: nil)

    The request body, if any.

Returns:

  • (Net::HTTPResponse)

    response



76
77
78
# File 'lib/ncio/http_client.rb', line 76

def request(req, body = nil)
  http.request(req, body)
end

#uriURI

Provide a URL to the endpoint this client connects to. This is intended to construct URL's and add query parameters easily.

Returns:

  • (URI)

    the URI of the server this client connects to.



85
86
87
88
# File 'lib/ncio/http_client.rb', line 85

def uri
  return @uri if @uri
  @uri = URI("#{protocol}://#{host}:#{port}")
end