class Ncio::Api::V1
Node Classifier API Version 1
See [Groups endpoint](docs.puppet.com/pe/2016.1/nc_groups.html#get-v1groups)
Constants
- DEFAULT_HEADERS
Attributes
Public Class Methods
Initialize and return a new API instance.
@param [Hash<Symbol, String>] The global options hash created by the
application instanece.
# File lib/ncio/api/v1.rb, line 33 def initialize(opts) @opts = opts uri = URI(opts[:uri]) @host = uri.host @port = uri.port end
Public Instance Methods
Return a URI instance with the given path and parameters. The
connection base URI is used to construct the full URI to the service.
@param [String] path The path relative to the classifier base service
path and the API version, e.g. 'groups'.
@param [Hash] params The query parameters to encode into the URI, e.g.
`{inherited: 'false'}`.
@return [URI] The API uri with query parameters and a fully constructed
path.
# File lib/ncio/api/v1.rb, line 151 def build_uri(path, params = {}) uri = connection.uri uri.path = "/classifier-api/v1/#{path}" uri.query = URI.encode_www_form(params) uri end
Return a memoized HTTP connection
# File lib/ncio/api/v1.rb, line 46 def connection return @connection if @connection conn_opts = { host: host, port: port, cert: opts[:cert], key: opts[:key], cacert: opts[:cacert] } @connection = Ncio::HttpClient.new(conn_opts) end
Return all of the groups currently defined in the node classifier API.
@param [Boolean] inherited If set to any value besides 0 or false, the
node group will include the classes, class parameters, and variables that it inherits from its ancestors.
@return [Array]
# File lib/ncio/api/v1.rb, line 97 def groups(inherited = false) uri = build_uri('groups', inherited: inherited.to_s) req = Net::HTTP::Get.new(uri, DEFAULT_HEADERS) resp = request(req) obj = if resp.code == '200' JSON.parse(resp.body) else raise_on_non_200(resp, 200) end obj end
Import all of the classification groups using the POST /v1/import-hierarchy endpoint. See: [NC Import Hierarchy](docs.puppet.com/pe/2016.1/nc_import-hierarchy.html)
@return [Boolean] true if the upload was successful
# File lib/ncio/api/v1.rb, line 129 def import_hierarchy(stream) uri = build_uri('import-hierarchy') req = Net::HTTP::Post.new(uri, DEFAULT_HEADERS) req['Transfer-Encoding'] = ['chunked'] req.body_stream = stream resp = request(req) return true if resp.code == '204' raise_on_non_200(resp, 204) end
# File lib/ncio/api/v1.rb, line 40 def log Ncio::Support.log end
Handle a non 200 response.
# File lib/ncio/api/v1.rb, line 111 def raise_on_non_200(resp, expected_code=200) if resp.code == '401' && %r{rbac/user-unauthenticated}.match(resp.body) obj = JSON.parse(resp.body) msg = obj['msg'] || '401 User Unauthenticated Error' raise ApiAuthenticationError, msg else msg = "Expected #{expected_code} response, got #{resp.code} "\ "body: #{resp.body}" raise ApiError, msg end end
Make a request, return a response
# File lib/ncio/api/v1.rb, line 81 def request(req) if opts[:retry_connections] request_with_timeout(req) else request_without_timeout(req) end end
Make a request respecting the timeout global option
Assumes the timeout value is available in opts
# File lib/ncio/api/v1.rb, line 62 def request_with_timeout(req) params = { timeout: opts[:connect_timeout], retry_exceptions: [Errno::ECONNREFUSED], log: self.log, } Ncio::Support::RetryAction.retry_action(params) do connection.request(req) end end
Make a request without a timeout
# File lib/ncio/api/v1.rb, line 75 def request_without_timeout(req) connection.request(req) end