class Chef::Topology

Topology

Constants

PRIORITIES

Attributes

strategy[RW]

Public Class Methods

convert_from(format, data) click to toggle source
# 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) click to toggle source
# File lib/chef/topology.rb, line 49
def self.from_json(data)
  topo = new
  topo.raw_data = data
  topo
end

Public Instance Methods

add_topo_attrs(attrs) click to toggle source
# 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) click to toggle source
# 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_info() click to toggle source
# File lib/chef/topology.rb, line 79
def display_info
  buildstamp = raw_data['buildstamp']
  info = buildstamp ? ' buildstamp: ' + buildstamp : ''
  display_name + info
end
display_name() click to toggle source
# File lib/chef/topology.rb, line 85
def display_name
  version = topo_version ? ' version: ' + topo_version : ''
  topo_name + version
end
merge_attrs() click to toggle source
# File lib/chef/topology.rb, line 106
def merge_attrs
  raw_data['strategy_data'] && raw_data['strategy_data']['merge_attrs']
end
merged_nodes() click to toggle source

nodes with topo properties merged in

# File lib/chef/topology.rb, line 111
def merged_nodes
  nodes.map do |n|
    Chef::Mixin::DeepMerge.merge(node_defaults, n)
  end
end
node_defaults() click to toggle source
# 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
nodes() click to toggle source
# File lib/chef/topology.rb, line 102
def nodes
  raw_data['nodes']
end
normalize(data) click to toggle source

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

# 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) click to toggle source

Make sure the JSON has an id and other expected fields

Calls superclass method
# 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) click to toggle source

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

# 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_name() click to toggle source
# File lib/chef/topology.rb, line 98
def topo_name
  raw_data['name']
end
topo_version() click to toggle source
# 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