class ChefCLI::PolicyfileServices::Undelete
def http_client
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
def initialize(undo_record_id: nil, config: nil, ui: nil)
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
def list
In addition to the #run method, this class also has #list as a public
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
def recreate_and_associate_to_group(policy_info)
def recreate_and_associate_to_group(policy_info) rel_uri = "/policy_groups/#{policy_info.policy_group}/policies/#{policy_info.policy_name}" http_client.put(rel_uri, policy_info.data) ui.msg("Restored policy '#{policy_info.policy_name}'") end
def recreate_as_orphan(policy_info)
def recreate_as_orphan(policy_info) rel_uri = "/policies/#{policy_info.policy_name}/revisions" http_client.post(rel_uri, policy_info.data) ui.msg("Restored policy '#{policy_info.policy_name}'") end
def restore(undo_record)
def restore(undo_record) undo_record.policy_revisions.each do |policy_info| if policy_info.policy_group.nil? recreate_as_orphan(policy_info) else recreate_and_associate_to_group(policy_info) end end if ( restored_policy_group = undo_record.policy_groups.first ) ui.msg("Restored policy group '#{restored_policy_group}'") end end
def run
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
def undo_stack
def undo_stack @undo_stack ||= Policyfile::UndoStack.new end