Class: ChefCLI::Policyfile::UndoRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-cli/policyfile/undo_record.rb

Defined Under Namespace

Classes: PolicyGroupRestoreData

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUndoRecord

Returns a new instance of UndoRecord.

[View source]

50
51
52
# File 'lib/chef-cli/policyfile/undo_record.rb', line 50

def initialize
  reset!
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.


48
49
50
# File 'lib/chef-cli/policyfile/undo_record.rb', line 48

def description
  @description
end

#policy_groupsObject (readonly)

Returns the value of attribute policy_groups.


44
45
46
# File 'lib/chef-cli/policyfile/undo_record.rb', line 44

def policy_groups
  @policy_groups
end

#policy_revisionsObject (readonly)

Returns the value of attribute policy_revisions.


46
47
48
# File 'lib/chef-cli/policyfile/undo_record.rb', line 46

def policy_revisions
  @policy_revisions
end

Instance Method Details

#==(other) ⇒ Object

[View source]

54
55
56
57
58
# 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) ⇒ Object

[View source]

60
61
62
# 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) ⇒ Object

[View source]

64
65
66
# 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_serializationObject

[View source]

120
121
122
123
124
125
126
127
128
129
# 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) ⇒ Object

[View source]

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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