Class: Chef::Knife::NodeEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/knife/core/node_editor.rb

Instance Method Summary collapse

Constructor Details

#initialize(node, ui, config) ⇒ NodeEditor

Returns a new instance of NodeEditor

Parameters:


32
33
34
# File 'lib/chef/knife/core/node_editor.rb', line 32

def initialize(node, ui, config)
  @node, @ui, @config = node, ui, config
end

Instance Method Details

#apply_updates(updated_data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/chef/knife/core/node_editor.rb', line 98

def apply_updates(updated_data)
  if node.name && node.name != updated_data["name"]
    ui.warn "Changing the name of a node results in a new node being created, #{node.name} will not be modified or removed."
    ui.confirm "Proceed with creation of new node"
  end

  data = updated_data.dup

  unless config[:all_attributes]
    data["automatic"] = node.automatic_attrs
    data["default"] = node.default_attrs
    data["override"] = node.override_attrs
  end

  @updated_node = Node.from_hash(data)
end

#edit_nodeChef::Node

Opens the node data (as JSON) in the user's editor and returns a new Node reflecting the user's changes.

Returns:


40
41
42
43
44
45
46
47
# File 'lib/chef/knife/core/node_editor.rb', line 40

def edit_node
  abort "You specified the --disable_editing option, nothing to edit" if config[:disable_editing]
  assert_editor_set!

  updated_node_data = ui.edit_hash(view)
  apply_updates(updated_node_data)
  @updated_node
end

#updated?Array<String>, false

Returns an array of the names of properties that have been changed or +false+ if none were changed.

Returns:

  • (Array<String>)

    if any properties have been changed.

  • (false)

    if no properties have been changed.


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/chef/knife/core/node_editor.rb', line 54

def updated?
  return false if @updated_node.nil?

  pristine_copy = Chef::JSONCompat.parse(Chef::JSONCompat.to_json(node))
  updated_copy  = Chef::JSONCompat.parse(Chef::JSONCompat.to_json(@updated_node))

  updated_properties = %w{
    name
    chef_environment
    automatic
    default
    normal
    override
    policy_name
    policy_group
    run_list
  }.reject do |key|
    pristine_copy[key] == updated_copy[key]
  end

  updated_properties.any? && updated_properties
end

#viewObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chef/knife/core/node_editor.rb', line 78

def view
  result = {
    "name" => node.name,
    "chef_environment" => node.chef_environment,
    "normal" => node.normal_attrs,
    "policy_name" => node.policy_name,
    "policy_group" => node.policy_group,
    "run_list" => node.run_list,
  }

  if config[:all_attributes]
    result["default"]   = node.default_attrs
    result["override"]  = node.override_attrs
    result["automatic"] = node.automatic_attrs
  end

  result
end