Class: KnifeNodeAttribute::NodeAttributeDelete

Inherits:
Chef::Knife
  • Object
show all
Includes:
Helpers
Defined in:
lib/chef/knife/knife-node-attribute.rb

Instance Method Summary collapse

Methods included from Helpers

#get_all_attributes, #get_default_attributes, #get_node, #get_normal_attributes, #get_override_attributes

Instance Method Details

#parse_argsObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/chef/knife/knife-node-attribute.rb', line 105

def parse_args
  unless node_name = name_args[0]
    show_usage
    ui.error "You need to specify a node"
    exit 1
  end
  if name_args[1].nil?
    show_usage
    ui.error "No attribute level given. (e.g. override, normal, default)"
    exit 1
  else
    attribute_class = name_args[1].downcase.to_sym
    # if we passed a valid attribute class, the next one could be an
    # attribute name. If it's invalid, it might be the attribute name
    unless ATTRIBUTE_CLASSES.include? attribute_class
      show_usage
      ui.error "Wrong attribute level given. (e.g. override, normal, default)"
      exit 1
    end
  end

  unless attribute_name = name_args[2]
    show_usage
    ui.error "No attribute name given."
    exit 1
  end

  {:nodename => node_name,
   :class => attribute_class,
   :name => attribute_name.split(":")}
end

#runObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/chef/knife/knife-node-attribute.rb', line 137

def run
  args = parse_args
  method = "#{args[:class]}_attrs".to_sym

  node = get_node(args[:nodename])
  # this eval business is awful, I should probably come up with something
  # smarter
  if args[:name].length == 1
    delete_method =  "node.#{method}.delete('#{args[:name].last}')"
  else
    parent_hash = args[:name][0, args[:name].length - 1]
    delete_method =  "node.#{method}['#{parent_hash.join("']['")}'].delete('#{args[:name].last}')"
  end
  begin
    eval delete_method
    node.save
    msg = parent_hash.nil? ? "" : " from parent structure #{parent_hash.join(':')}"
    puts "Key #{args[:name].last}#{msg} deleted."
  rescue StandardError => e
    ui.error "An error occured while trying to delete the node key:"
    puts e.to_s
  end
end