glazed-oxidized 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,69 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require 'glazed/oxidized/errors'
7
+ require 'glazed/proto'
8
+ require 'glazed/secret'
9
+
10
+ module Glazed
11
+ module Oxidized
12
+ # A glazed node as an Oxidized model sees it through `@node` and `vars`.
13
+ #
14
+ # Variables are the node's params, with "true" and "false" read as
15
+ # booleans, and its secrets, resolved on first use.
16
+ class NodeView
17
+ # @param node [Proto::V1::Node] node being collected
18
+ # @param prompt [Regexp] prompt pattern of the dialect
19
+ #: (node: Proto::V1::Node, prompt: Regexp) -> void
20
+ def initialize(node:, prompt:)
21
+ @node = node #: Proto::V1::Node
22
+ @prompt = prompt #: Regexp
23
+ @vars = nil #: Hash[Symbol, untyped]?
24
+ end
25
+
26
+ # @return [Regexp] prompt pattern of the dialect
27
+ #: Regexp
28
+ attr_reader :prompt
29
+
30
+ # @return [String] node name
31
+ #: () -> String
32
+ def name = @node.name
33
+
34
+ # @return [String] address connected to
35
+ #: () -> String
36
+ def ip = @node.host.empty? ? @node.name : @node.host
37
+
38
+ # @return [String] node group
39
+ #: () -> String
40
+ def group = @node.group
41
+
42
+ # @return [Hash{Symbol => String, nil}] login credentials
43
+ # @raise [Secret::Unavailable] when the password cannot be read
44
+ #: () -> Hash[Symbol, String?]
45
+ def auth = {username: @node.username, password: Secret.maybe(@node.password)&.value}
46
+
47
+ # @return [Hash{Symbol => Object}] params and secrets of the node
48
+ # @raise [Secret::Unavailable] when a secret cannot be read
49
+ #: () -> Hash[Symbol, untyped]
50
+ def vars
51
+ @vars ||= {}.tap do |vars|
52
+ @node.params.each { |key, value| vars[key.to_sym] = cast(value) }
53
+ @node.secrets.each { |key, ref| vars[key.to_sym] = Secret.new(ref).value }
54
+ end
55
+ end
56
+
57
+ # @param value [String] param value
58
+ # @return [String, Boolean] booleans for "true" and "false", the value otherwise
59
+ #: (String value) -> (String | bool)
60
+ private def cast(value)
61
+ case value
62
+ when 'true' then true
63
+ when 'false' then false
64
+ else value
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end