class ChefCLI::Policyfile::UndoRecord
def ==(other)
def ==(other) other.is_a?(UndoRecord) && other.policy_groups == policy_groups && other.policy_revisions == policy_revisions end
def add_policy_group(name)
def add_policy_group(name) @policy_groups << name end
def add_policy_revision(policy_name, policy_group, data)
def add_policy_revision(policy_name, policy_group, data) @policy_revisions << PolicyGroupRestoreData.new(policy_name, policy_group, data) end
def for_serialization
def for_serialization { "format_version" => 1, "description" => description, "backup_data" => { "policy_groups" => policy_groups, "policy_revisions" => policy_revisions.map(&:for_serialization), }, } end
def initialize
def initialize reset! end
def load(data)
def load(data) reset! unless data.is_a?(Hash) raise InvalidUndoRecord, "Undo data is incorrectly formatted. Must be a Hash, got '#{data}'." end missing_fields = %w{ format_version description backup_data }.select { |key| !data.key?(key) } unless missing_fields.empty? raise InvalidUndoRecord, "Undo data is missing mandatory field(s) #{missing_fields.join(", ")}. Undo data: '#{data}'" end @description = data["description"] policy_data = data["backup_data"] unless policy_data.is_a?(Hash) raise InvalidUndoRecord, "'backup_data' in the undo record is incorrectly formatted. Must be a Hash, got '#{policy_data}'" end missing_policy_data_fields = %w{ policy_groups policy_revisions }.select { |key| !policy_data.key?(key) } unless missing_policy_data_fields.empty? raise InvalidUndoRecord, "'backup_data' in the undo record is missing mandatory field(s) #{missing_policy_data_fields.join(", ")}. Backup data: #{policy_data}" end policy_groups = policy_data["policy_groups"] unless policy_groups.is_a?(Array) raise InvalidUndoRecord, "'policy_groups' data in the undo record is incorrectly formatted. Must be an Array, got '#{policy_groups}'" end @policy_groups = policy_groups policy_revisions = policy_data["policy_revisions"] unless policy_revisions.is_a?(Array) raise InvalidUndoRecord, "'policy_revisions' data in the undo record is incorrectly formatted. Must be an Array, got '#{policy_revisions}'" end policy_revisions.each do |revision| unless revision.is_a?(Hash) raise InvalidUndoRecord, "Invalid item in 'policy_revisions' in the undo record. Must be a Hash, got '#{revision}'" end @policy_revisions << PolicyGroupRestoreData.new.load(revision) end self end
def reset!
def reset! @description = "" @policy_groups = [] @policy_revisions = [] end