Class: Ncio::Api::V1

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

Overview

Node Classifier API Version 1

See Groups endpoint

Defined Under Namespace

Classes: ApiAuthenticationError, ApiError

Constant Summary collapse

DEFAULT_HEADERS =
{
  'Content-Type' => 'application/json',
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ V1

Initialize and return a new API instance.

Parameters:

  • The (Hash<Symbol, String>)

    global options hash created by the application instanece.



33
34
35
36
37
38
# File 'lib/ncio/api/v1.rb', line 33

def initialize(opts)
  @opts = opts
  uri = URI(opts[:uri])
  @host = uri.host
  @port = uri.port
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#build_uri(path, params = {}) ⇒ URI

Return a URI instance with the given path and parameters. The connection base URI is used to construct the full URI to the service.

Parameters:

  • path (String)

    The path relative to the classifier base service path and the API version, e.g. 'groups'.

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

    The query parameters to encode into the URI, e.g. {inherited: 'false'}.

Returns:

  • (URI)

    The API uri with query parameters and a fully constructed path.



151
152
153
154
155
156
# 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

#connectionObject

Return a memoized HTTP connection



46
47
48
49
50
51
52
53
54
55
56
# 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

#groups(inherited = false) ⇒ Array

Return all of the groups currently defined in the node classifier API.

Parameters:

  • inherited (Boolean) (defaults to: false)

    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.

Returns:

  • (Array)


97
98
99
100
101
102
103
104
105
106
107
# 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_hierarchy(stream) ⇒ Boolean

Import all of the classification groups using the POST /v1/import-hierarchy endpoint. See: NC Import Hierarchy

Returns:

  • (Boolean)

    true if the upload was successful



129
130
131
132
133
134
135
136
137
# 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

#logObject



40
41
42
# File 'lib/ncio/api/v1.rb', line 40

def log
  Ncio::Support.log
end

#raise_on_non_200(resp, expected_code = 200) ⇒ Object

Handle a non 200 response.



111
112
113
114
115
116
117
118
119
120
121
# 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

#request(req) ⇒ Object

Make a request, return a response



81
82
83
84
85
86
87
# 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

#request_with_timeout(req) ⇒ Object

Make a request respecting the timeout global option

Assumes the timeout value is available in opts[:connect_timeout]



62
63
64
65
66
67
68
69
70
71
# 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

#request_without_timeout(req) ⇒ Object

Make a request without a timeout



75
76
77
# File 'lib/ncio/api/v1.rb', line 75

def request_without_timeout(req)
  connection.request(req)
end