class ChefCLI::Policyfile::UndoRecord

Constants

PolicyGroupRestoreData

Attributes

description[RW]
policy_groups[R]
policy_revisions[R]

Public Class Methods

new() click to toggle source
# File lib/chef-cli/policyfile/undo_record.rb, line 50
def initialize
  reset!
end

Public Instance Methods

==(other) click to toggle source
# File lib/chef-cli/policyfile/undo_record.rb, line 54
def ==(other)
  other.is_a?(UndoRecord) &&
    other.policy_groups == policy_groups &&
    other.policy_revisions == policy_revisions
end
add_policy_group(name) click to toggle source
# File lib/chef-cli/policyfile/undo_record.rb, line 60
def add_policy_group(name)
  @policy_groups << name
end
add_policy_revision(policy_name, policy_group, data) click to toggle source
# File lib/chef-cli/policyfile/undo_record.rb, line 64
def add_policy_revision(policy_name, policy_group, data)
  @policy_revisions << PolicyGroupRestoreData.new(policy_name, policy_group, data)
end
for_serialization() click to toggle source
# File lib/chef-cli/policyfile/undo_record.rb, line 120
def for_serialization
  {
    "format_version" => 1,
    "description" => description,
    "backup_data" => {
      "policy_groups" => policy_groups,
      "policy_revisions" => policy_revisions.map(&:for_serialization),
    },
  }
end
load(data) click to toggle source
# File lib/chef-cli/policyfile/undo_record.rb, line 68
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

Private Instance Methods

reset!() click to toggle source
# File lib/chef-cli/policyfile/undo_record.rb, line 133
def reset!
  @description = ""
  @policy_groups = []
  @policy_revisions = []
end