Class: ForemanSnapshotManagement::Snapshot

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::ForbiddenAttributesProtection, ActiveModel::Model
Defined in:
app/models/foreman_snapshot_management/snapshot.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#create_timeObject

Returns the value of attribute create_time.



12
13
14
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 12

def create_time
  @create_time
end

#descriptionObject

Returns the value of attribute description.



12
13
14
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 12

def description
  @description
end

#host_idObject

Returns the value of attribute host_id.



12
13
14
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 12

def host_id
  @host_id
end

#idObject

Returns the value of attribute id.



12
13
14
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 12

def id
  @id
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 12

def name
  @name
end

#parentObject

Returns the value of attribute parent.



12
13
14
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 12

def parent
  @parent
end

#raw_snapshotObject

Returns the value of attribute raw_snapshot.



12
13
14
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 12

def raw_snapshot
  @raw_snapshot
end

Class Method Details

.all_for_host(host) ⇒ Object



15
16
17
18
19
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 15

def self.all_for_host(host)
  snapshots = host.compute_resource.get_snapshots(host.uuid).map do |raw_snapshot|
    new_from_vmware(host, raw_snapshot)
  end
end

.find_for_host(host, id) ⇒ Object



21
22
23
24
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 21

def self.find_for_host(host, id)
  raw_snapshot = host.compute_resource.get_snapshot(host.uuid, id)
  new_from_vmware(host, raw_snapshot) if raw_snapshot
end

.new_from_vmware(host, raw_snapshot, opts = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 26

def self.new_from_vmware(host, raw_snapshot, opts = {})
  new(
    host: host,
    id: raw_snapshot.ref,
    raw_snapshot: raw_snapshot,
    name: raw_snapshot.name,
    description: raw_snapshot.description,
    parent: opts[:parent],
    create_time: raw_snapshot.create_time
  )
end

Instance Method Details

#assign_attributes(new_attributes) ⇒ Object



95
96
97
98
99
100
101
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 95

def assign_attributes(new_attributes)
  attributes = new_attributes.stringify_keys
  attributes = sanitize_for_mass_assignment(attributes)
  attributes.each do |k, v|
    public_send("#{k}=", v)
  end
end

#childrenObject



38
39
40
41
42
43
44
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 38

def children
  return [] unless raw_snapshot
  child_snapshots = raw_snapshot.child_snapshots.flat_map do |child_snapshot|
    self.class.new_from_vmware(host, child_snapshot, parent: self)
  end
  child_snapshots + child_snapshots.flat_map(&:children)
end

#createObject

crud



109
110
111
112
113
114
115
116
117
118
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 109

def create
  run_callbacks(:create) do
    handle_snapshot_errors do
      host.audit_comment = "Create snapshot #{name}"
      host.save!
      host.compute_resource.create_snapshot(host.uuid, name, description)
      changes_applied
    end
  end
end

#destroyObject



131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 131

def destroy
  run_callbacks(:destroy) do
    result = handle_snapshot_errors do
      host.audit_comment = "Destroy snapshot #{name}"
      host.save!
      result = host.compute_resource.remove_snapshot(raw_snapshot, false)
    end
    @id = nil
    result
  end
end

#formatted_create_timeObject



54
55
56
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 54

def formatted_create_time()
  create_time.strftime("%F %H:%M")
end

#hostObject

host accessors



73
74
75
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 73

def host
  @host ||= Host.find(@host_id)
end

#host=(host) ⇒ Object



84
85
86
87
88
89
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 84

def host=(host)
  if @host_id != host.id
    @host_id = host.id
    @host = host
  end
end

#inspectObject



46
47
48
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 46

def inspect
  "#<#{self.class}:0x#{self.__id__.to_s(16)} name=#{name} id=#{id} description=#{description} host_id=#{host_id} parent=#{parent.try(:id)} children=#{children.map(&:id).inspect}>"
end

#persisted?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 58

def persisted?
  @id.present?
end

#revertObject



143
144
145
146
147
148
149
150
151
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 143

def revert
  run_callbacks(:revert) do
    handle_snapshot_errors do
      host.audit_comment = "Revert snapshot #{name}"
      host.save!
      host.compute_resource.revert_snapshot(raw_snapshot)
    end
  end
end

#saveObject



120
121
122
123
124
125
126
127
128
129
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 120

def save
  run_callbacks(:save) do
    handle_snapshot_errors do
      host.audit_comment = "Update snapshot #{name}"
      host.save!
      host.compute_resource.update_snapshot(raw_snapshot, name, description)
      changes_applied
    end
  end
end

#to_sObject



50
51
52
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 50

def to_s
  _('Snapshot')
end

#update_attributes(new_attributes) ⇒ Object



103
104
105
106
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 103

def update_attributes(new_attributes)
  assign_attributes(new_attributes)
  save if changed?
end