summaryrefslogtreecommitdiff
path: root/lib/chef/node/attribute.rb
blob: 86de146b234500c8ef94b870773472580fedcc57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
require 'chef/node/attribute_constants'
require 'chef/node/attribute_cell'
require 'chef/node/un_method_chain'
require 'chef/node/attribute_traits'

class Chef
  class Node
    class Attribute
      include AttributeTrait::Decorator
      include AttributeTrait::SymbolConvert
      include AttributeTrait::MethodMissing
      include AttributeTrait::Immutable
      include AttributeTrait::DeepMergeCache
      include AttributeTrait::PathTracking
      include AttributeConstants

      def initialize(normal: nil, default: nil, override: nil, automatic: nil, **args)
        super(**args)
        @wrapped_object ||= AttributeCell.new(
          default: default || {},
          env_default: {},
          role_default: {},
          force_default: {},
          normal: normal || {},
          override: override || {},
          role_override: {},
          env_override: {},
          force_override: {},
          automatic: automatic || {},
          node: __node,
          deep_merge_cache: __deep_merge_cache
        )
      end

      COMPONENTS_AS_SYMBOLS.each do |component|
        attr_writer component

        define_method component do
          wrapped_object.public_send(component)
        end

        define_method :"#{component}=" do |value|
          __deep_merge_cache.clear
          wrapped_object.public_send(:"#{component}=", value)
        end
      end

      def wrap_automatic_attrs(value)
        __deep_merge_cache.clear
        wrapped_object.wrap_automatic_attrs(value)
      end

      def combined_default
        self.class.new(wrapped_object: wrapped_object.combined_default)
      end

      def combined_override
        self.class.new(wrapped_object: wrapped_object.combined_override)
      end

      def normal_unless(*args)
        return UnMethodChain.new(wrapped_object: self, method_to_call: :normal_unless) unless args.length > 0
        write_value(:normal, *args) if args_to_cell(*args).nil?
      end

      def default_unless(*args)
        return UnMethodChain.new(wrapped_object: self, method_to_call: :default_unless) unless args.length > 0
        write_value(:default, *args) if args_to_cell(*args).nil?
      end

      def override_unless(*args)
        return UnMethodChain.new(wrapped_object: self, method_to_call: :override_unless) unless args.length > 0
        write_value(:override, *args) if args_to_cell(*args).nil?
      end

      # should deprecate all of these, epecially #set
      alias_method :set, :normal
      alias_method :set_unless, :normal_unless
      alias_method :default_attrs, :default
      alias_method :default_attrs=, :default=
      alias_method :normal_attrs, :normal
      alias_method :normal_attrs=, :normal=
      alias_method :override_attrs, :override
      alias_method :override_attrs=, :override=
      alias_method :automatic_attrs, :automatic
      alias_method :automatic_attrs=, :automatic=

      def has_key?(key)
        self.public_send(:key?, key)
      end

      alias_method :attribute?, :has_key?
      alias_method :member?, :has_key?

      def include?(val)
        wrapped_object.public_send(:include?, val)
      end

      def each_attribute(&block)
        self.public_send(:each, &block)
      end

      # Debug what's going on with an attribute. +args+ is a path spec to the
      # attribute you're interested in. For example, to debug where the value
      # of `node[:network][:default_interface]` is coming from, use:
      #   debug_value(:network, :default_interface).
      # The return value is an Array of Arrays. The first element is
      # `["set_unless_enabled?", Boolean]`, which describes whether the
      # attribute collection is in "set_unless" mode. The rest of the Arrays
      # are pairs of `["precedence_level", value]`, where precedence level is
      # the component, such as role default, normal, etc. and value is the
      # attribute value set at that precedence level. If there is no value at
      # that precedence level, +value+ will be the symbol +:not_present+.
      def debug_value(*args)
        COMPONENTS_AS_SYMBOLS.map do |component|
          ivar = wrapped_object.send(component)
          value = args.inject(ivar) do |so_far, key|
            if so_far == :not_present
              :not_present
            elsif so_far.has_key?(key)
              so_far[key]
            else
              :not_present
            end
          end
          [component.to_s, value]
        end
      end

      def to_s
        wrapped_object.to_s
      end

      def eql?(other)
        wrapped_object.eql?(other)
      end

      def ===(other)
        wrapped_object === other
      end

      def ==(other)
        wrapped_object == other
      end

      def is_a?(klass)
        wrapped_object.is_a?(klass) || super(klass)
      end

      def kind_of?(klass)
        wrapped_object.kind_of?(klass) || super(klass)
      end

      # clears attributes from all precedence levels
      #
      # - does not autovivify
      # - does not trainwreck if interior keys do not exist
      def rm(*args)
        cell = args_to_cell(*args)
        return nil unless cell.is_a?(Hash)
        ret = cell[args.last]
        rm_default(*args)
        rm_normal(*args)
        rm_override(*args)
        ret
      end

      # clears attributes from all default precedence levels
      #
      # - similar to: force_default!['foo']['bar'].delete('baz')
      # - does not autovivify
      # - does not trainwreck if interior keys do not exist
      def rm_default(*args)
        cell = args_to_cell(*args)
        return nil unless cell.is_a?(Hash)
        ret = if cell.combined_default.is_a?(Hash)
                cell.combined_default[args.last]
              end
        cell.default.delete(args.last) if cell.default.is_a?(Hash)
        cell.role_default.delete(args.last) if cell.role_default.is_a?(Hash)
        cell.env_default.delete(args.last) if cell.env_default.is_a?(Hash)
        cell.force_default.delete(args.last) if cell.force_default.is_a?(Hash)
        ret
      end

      # clears attributes from normal precedence
      #
      # - similar to: normal!['foo']['bar'].delete('baz')
      # - does not autovivify
      # - does not trainwreck if interior keys do not exist
      def rm_normal(*args)
        cell = args_to_cell(*args)
        return nil unless cell.is_a?(Hash)
        cell.normal.delete(args.last) if cell.normal.is_a?(Hash)
      end

      # clears attributes from all override precedence levels
      #
      # - similar to: force_override!['foo']['bar'].delete('baz')
      # - does not autovivify
      # - does not trainwreck if interior keys do not exist
      def rm_override(*args)
        cell = args_to_cell(*args)
        return nil unless cell.is_a?(Hash)
        ret = if cell.combined_override.is_a?(Hash)
                cell.combined_override[args.last]
              end
        cell.override.delete(args.last) if cell.override.is_a?(Hash)
        cell.role_override.delete(args.last) if cell.role_override.is_a?(Hash)
        cell.env_override.delete(args.last) if cell.env_override.is_a?(Hash)
        cell.force_override.delete(args.last) if cell.force_override.is_a?(Hash)
        ret
      end

      def args_to_cell(*args)
        begin
          last = args.pop
          cell = args.inject(self) do |memo, arg|
            memo[arg]
          end
          cell
        rescue NoMethodError
          nil
        end
      end

      private :args_to_cell

      # FIXME: should probably be another decorator behavior that changes :[] and :[]= to wipe
      # out intermediate non-hash things and replace them with hashes in addition to autovivifying
      # and/or add #hashifying_accessor and #hashifying_writer methods directly to VividMash.
      def write_value(level, *args)
        value = args.pop
        last = args.pop
        previous_memo = previous_arg = nil
        my_level = self.send(level)
        chain = args.inject(self.send(level)) do |memo, arg|
          unless memo.respond_to?(:[])
            # The public API will never get previous_memo set to nil, so we do not care.
            previous_memo[previous_arg] = {}
            memo = previous_memo[previous_arg]
          end
          previous_memo = memo
          previous_arg = arg
          memo[arg]
        end
        unless chain.respond_to?(:[]=)
          # The public API will never get previous_memo set to nil, so we do not care.
          previous_memo[previous_arg] = {}
          chain = previous_memo[previous_arg]
        end
        chain[last] = value
      end

      private :write_value

      # sets default attributes without merging.
      #
      # - this API autovivifies (and cannot tranwreck)
      def default!(*args)
        return UnMethodChain.new(wrapped_object: self, method_to_call: :default!) unless args.length > 0
        write_value(:default, *args)
      end

      # set normal attributes without merging.
      #
      # - this API autovivifies (and cannot tranwreck)
      def normal!(*args)
        return UnMethodChain.new(wrapped_object: self, method_to_call: :normal!) unless args.length > 0
        write_value(:normal, *args)
      end

      # set override attributes without merging.
      #
      # - this API autovivifies (and cannot tranwreck)
      def override!(*args)
        return UnMethodChain.new(wrapped_object: self, method_to_call: :override!) unless args.length > 0
        write_value(:override, *args)
      end

      # set force_default attributes without merging.
      #
      # - this also clears all of the other default levels as well.
      # - this API autovivifies (and cannot tranwreck)
      def force_default!(*args)
        return UnMethodChain.new(wrapped_object: self, method_to_call: :force_default!) unless args.length > 0
        value = args.pop
        rm_default(*args)
        write_value(:force_default, *args, value)
      end

      # set force_override attributes without merging.
      #
      # - this also clears all of the other override levels as well.
      # - this API autovivifies (and cannot tranwreck)
      def force_override!(*args)
        return UnMethodChain.new(wrapped_object: self, method_to_call: :force_override!) unless args.length > 0
        value = args.pop
        rm_override(*args)
        write_value(:force_override, *args, value)
      end
    end
  end
end