# # Author:: Christine Draper () # Copyright:: Copyright (c) 2015 ThirdWave Insights LLC # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # require 'chef/knife/topo/consts' module KnifeTopo # Prints attribute file contents to string module ViaCookbookPrint def print_attr_header(contents, cookbook_name, filename) copyright = @config['cookbook_copyright'] contents << " # # THIS FILE IS GENERATED BY KNIFE TOPO - MANUAL CHANGES WILL BE OVERWRITTEN # # Cookbook Name:: #{cookbook_name} # Attribute File:: #{filename} # " return unless copyright contents << " # Copyright (c) #{Time.now.year} #{copyright} # " end def print_attr(contents, lhs, value1) if value1.is_a?(Hash) value1.each do |key, value2| print_attr(contents, "#{lhs}['#{key}']", value2) end else ruby_str = value1.nil? ? 'nil' : Chef::JSONCompat.to_json(value1) contents << "#{lhs} = #{ruby_str}\n" end end def print_attrs(contents, attrs, indent = 0) return unless attrs KnifeTopo::PRIORITIES.each do |priority| next unless attrs[priority] lhs = '' indent.times { lhs << ' ' } lhs << priority print_attr(contents, lhs, attrs[priority]) end end def print_attrs_for_node(contents, n) if n['node_type'] print_node_type_attr_header(contents, n['node_type']) else print_node_name_attr_header(contents, n['name']) end print_attrs(contents, n, 2) contents << "end\n" end def print_node_type_attr_header(contents, node_type) contents << "# Attributes for node type #{node_type}\n" contents << "if node['topo'] && node['topo']['node_type']" \ " == '#{node_type}'\n" end def print_node_name_attr_header(contents, node_name) contents << "# Attributes for node name #{node_name}\n" contents << "if node.name == '#{node_name}'\n" end def cookbook_contents contents = '' print_attr_header(contents, @cookbook, @filename) print_attrs(contents, @topo['attributes']) @topo.nodes.each do |n| print_attrs_for_node(contents, n) end contents end def copyright Chef::Config.copyright || @cmd.config['copyright'] end end end