Class: Chef::Topology

Inherits:
DataBagItem
  • Object
show all
Defined in:
lib/chef/topology.rb

Overview

Topology

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#strategyObject

Returns the value of attribute strategy.



26
27
28
# File 'lib/chef/topology.rb', line 26

def strategy
  @strategy
end

Class Method Details

.convert_from(format, data) ⇒ Object



45
46
47
# File 'lib/chef/topology.rb', line 45

def self.convert_from(format, data)
  from_json(Chef::Topo::Converter.convert(format, data))
end

.from_json(data) ⇒ Object



49
50
51
52
53
# File 'lib/chef/topology.rb', line 49

def self.from_json(data)
  topo = new
  topo.raw_data = data
  topo
end

Instance Method Details

#add_topo_attrs(attrs) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/chef/topology.rb', line 136

def add_topo_attrs(attrs)
  a = attrs || {}
  a['topo'] ||= {}
  a['topo']['name'] = topo_name
  a['topo']['node_type'] = a['node_type'] if a['node_type']
  a
end

#default_attrs(priority) ⇒ Object



131
132
133
134
# File 'lib/chef/topology.rb', line 131

def default_attrs(priority)
  return raw_data[priority] unless priority == 'normal'
  add_topo_attrs(raw_data['normal'])
end

#display_infoObject



79
80
81
82
83
# File 'lib/chef/topology.rb', line 79

def display_info
  buildstamp = raw_data['buildstamp']
  info = buildstamp ? ' buildstamp: ' + buildstamp : ''
  display_name + info
end

#display_nameObject



85
86
87
88
# File 'lib/chef/topology.rb', line 85

def display_name
  version = topo_version ? ' version: ' + topo_version : ''
  topo_name + version
end

#merge_attrsObject



106
107
108
# File 'lib/chef/topology.rb', line 106

def merge_attrs
  raw_data['strategy_data'] && raw_data['strategy_data']['merge_attrs']
end

#merged_nodesObject

nodes with topo properties merged in



111
112
113
114
115
# File 'lib/chef/topology.rb', line 111

def merged_nodes
  nodes.map do |n|
    Chef::Mixin::DeepMerge.merge(node_defaults, n)
  end
end

#node_defaultsObject



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/chef/topology.rb', line 117

def node_defaults
  defaults = {}
  %w(chef_environment tags).each do |k|
    defaults[k] = raw_data[k] if raw_data[k]
  end

  PRIORITIES.reverse_each do |p|
    a = default_attrs(p)
    defaults[p] = a if a
  end
  # Make sure we're not sharing objects
  Mash.from_hash(Marshal.load(Marshal.dump(defaults)))
end

#nodesObject



102
103
104
# File 'lib/chef/topology.rb', line 102

def nodes
  raw_data['nodes']
end

#normalize(data) ⇒ Object

clean up some variations so we only have to process one way in particular, allow ‘attributes’ as a synonym for ‘normal’



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/chef/topology.rb', line 66

def normalize(data)
  data['nodes'] = data['nodes'].map do |n|
    if n.key?('attributes')
      n['normal'] = Chef::Mixin::DeepMerge.merge(
        n['normal'], n['attributes']
      )
      n.delete('attributes')
    end
    n
  end
  data
end

#raw_data=(new_data) ⇒ Object

Make sure the JSON has an id and other expected fields



56
57
58
59
60
61
62
# File 'lib/chef/topology.rb', line 56

def raw_data=(new_data)
  @strategy = new_data['strategy'] || 'direct_to_node'
  new_data['id'] ||= (new_data['name'] || 'undefined')
  new_data['name'] ||= (new_data['id'])
  new_data['nodes'] ||= []
  super(normalize(new_data))
end

#to_json(*a) ⇒ Object

Have to override and say this is a data bag json_class or get error on upload re ‘must specify id’



34
35
36
37
38
39
40
41
42
43
# File 'lib/chef/topology.rb', line 34

def to_json(*a)
  result = {
    'name'       => object_name,
    'json_class' => Chef::DataBagItem.name,
    'chef_type'  => 'data_bag_item',
    'data_bag'   => data_bag,
    'raw_data'   => raw_data
  }
  Chef::JSONCompat.to_json(result, *a)
end

#topo_nameObject



98
99
100
# File 'lib/chef/topology.rb', line 98

def topo_name
  raw_data['name']
end

#topo_versionObject



90
91
92
93
94
95
96
# File 'lib/chef/topology.rb', line 90

def topo_version
  version = raw_data['version']
  if version
    version = version + '-' + raw_data['buildid'] if raw_data['buildid']
  end
  version
end