Class: ChefCLI::PolicyfileServices::Undelete

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-cli/policyfile_services/undelete.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(undo_record_id: nil, config: nil, ui: nil) ⇒ Undelete

Returns a new instance of Undelete.



32
33
34
35
36
37
38
39
# File 'lib/chef-cli/policyfile_services/undelete.rb', line 32

def initialize(undo_record_id: nil, config: nil, ui: nil)
  @chef_config = config
  @ui = ui
  @undo_record_id = undo_record_id

  @http_client = nil
  @undo_stack = nil
end

Instance Attribute Details

#chef_configObject (readonly)

Returns the value of attribute chef_config.



28
29
30
# File 'lib/chef-cli/policyfile_services/undelete.rb', line 28

def chef_config
  @chef_config
end

#uiObject (readonly)

Returns the value of attribute ui.



26
27
28
# File 'lib/chef-cli/policyfile_services/undelete.rb', line 26

def ui
  @ui
end

#undo_record_idObject (readonly)

Returns the value of attribute undo_record_id.



30
31
32
# File 'lib/chef-cli/policyfile_services/undelete.rb', line 30

def undo_record_id
  @undo_record_id
end

Instance Method Details

#http_clientObject



73
74
75
76
77
# File 'lib/chef-cli/policyfile_services/undelete.rb', line 73

def http_client
  @http_client ||= Chef::ServerAPI.new(chef_config.chef_server_url,
    signing_key_filename: chef_config.client_key,
    client_name: chef_config.node_name)
end

#listObject

In addition to the #run method, this class also has #list as a public entry point. This prints the list of undoable items, with descriptions.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chef-cli/policyfile_services/undelete.rb', line 43

def list
  if undo_stack.empty?
    ui.err("Nothing to undo.")
  else
    messages = []
    undo_stack.each_with_id do |timestamp, undo_record|
      messages.unshift("#{timestamp}: #{undo_record.description}")
    end
    messages.each { |m| ui.msg(m) }
  end
end

#runObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chef-cli/policyfile_services/undelete.rb', line 55

def run
  if undo_record_id
    if undo_stack.has_id?(undo_record_id)
      undo_stack.delete(undo_record_id) { |undo_record| restore(undo_record) }
    else
      ui.err("No undo record with id '#{undo_record_id}' exists")
    end
  else
    undo_stack.pop { |undo_record| restore(undo_record) }
  end
rescue => e
  raise UndeleteError.new("Failed to undelete.", e)
end

#undo_stackObject



69
70
71
# File 'lib/chef-cli/policyfile_services/undelete.rb', line 69

def undo_stack
  @undo_stack ||= Policyfile::UndoStack.new
end