Class: Chef::Topo::TopoV1Converter

Inherits:
Converter
  • Object
show all
Defined in:
lib/chef/topo/converter/topo_v1.rb

Overview

Convert V1 topology JSON to V2

Constant Summary collapse

PRIORITIES =
%w(
  default force_default normal override force_override
).freeze

Instance Attribute Summary

Attributes inherited from Converter

#input

Instance Method Summary collapse

Methods inherited from Converter

convert, converter, #initialize, load_converter, register_converter

Constructor Details

This class inherits a constructor from Chef::Topo::Converter

Instance Method Details

#cleanup_outputObject



120
121
122
123
# File 'lib/chef/topo/converter/topo_v1.rb', line 120

def cleanup_output
  @output.delete('cookbook_attributes')
  @output
end

#convert(data = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/chef/topo/converter/topo_v1.rb', line 35

def convert(data = nil)
  @input = data if data
  @output = @input.dup
  determine_strategy
  @output['nodes'] = []
  @input['nodes'].each do |n|
    @output['nodes'] << convert_node(n)
  end
  cleanup_output
end

#convert_node(n) ⇒ Object



46
47
48
49
50
51
# File 'lib/chef/topo/converter/topo_v1.rb', line 46

def convert_node(n)
  combined = merge_cookbook_attrs(n)
  type = node_type(n)
  combined['node_type'] ||= type if type
  combined
end

#determine_strategyObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chef/topo/converter/topo_v1.rb', line 53

def determine_strategy
  @output['strategy'] = 'direct_to_node'
  cookbooks = @input['cookbook_attributes']
  return unless cookbooks && !cookbooks.empty?

  cookbooks.each do |cb|
    cond = cb['conditional'] || []
    next unless !cond.empty? || PRIORITIES.any? { |k| cb.key?(k) }
    via_cookbook_strategy(cb)
    break
  end
end

#merge_cookbook_attrs(node) ⇒ Object

Combine cookbook attributes into node



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/chef/topo/converter/topo_v1.rb', line 85

def merge_cookbook_attrs(node)
  cb_attr_array = @input['cookbook_attributes']
  combined = node.dup
  return combined unless cb_attr_array

  # merge unqualified attributes into node
  cb_attr_array.each do |cb_attrs|
    merge_copy(combined, cb_attrs)

    # find qualified attributes for node
    cond = cb_attrs['conditional']
    next unless cond

    merge_copy_cond_attrs(combined, cond)
  end

  combined
end

#merge_copy(dest, source) ⇒ Object

move source contents and merge into dest



75
76
77
78
79
80
81
82
# File 'lib/chef/topo/converter/topo_v1.rb', line 75

def merge_copy(dest, source)
  # Go in reverse order so higher priority attrs are at top
  PRIORITIES.reverse_each do |p|
    if source.key?(p)
      dest[p] = Chef::Mixin::DeepMerge.merge(dest[p], source[p])
    end
  end
end

#merge_copy_cond_attrs(combined, cond) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/chef/topo/converter/topo_v1.rb', line 104

def merge_copy_cond_attrs(combined, cond)
  topo = (combined['normal'] || {})['topo'] || {}
  cond.each do |cond_attrs|
    if topo[cond_attrs['qualifier']] == cond_attrs['value']
      merge_copy(combined, cond_attrs)
    end
  end
  combined
end

#node_type(node) ⇒ Object



114
115
116
117
118
# File 'lib/chef/topo/converter/topo_v1.rb', line 114

def node_type(node)
  return node['node_type'] if node['node_type']
  return nil unless node['normal'] && node['normal']['topo']
  node['normal']['topo']['node_type']
end

#via_cookbook_strategy(cb) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/chef/topo/converter/topo_v1.rb', line 66

def via_cookbook_strategy(cb)
  @output['strategy'] = 'via_cookbook'
  @output['strategy_data'] = {
    'cookbook' => cb['cookbook'] || @output['name'],
    'filename' => cb['filename'] || 'attributes'
  }
end