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



96
97
98
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 96

def create_time
  raw_snapshot.try(:create_time)
end

#descriptionObject

Returns the value of attribute description.



14
15
16
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 14

def description
  @description
end

#host_idObject

Returns the value of attribute host_id.



14
15
16
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 14

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

#include_ramObject

Returns the value of attribute include_ram.



14
15
16
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 14

def include_ram
  @include_ram
end

#nameObject

Returns the value of attribute name.



14
15
16
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 14

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



17
18
19
20
21
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 17

def self.all_for_host(host)
  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



23
24
25
26
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 23

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



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

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



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

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



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

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



114
115
116
117
118
119
120
121
122
123
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 114

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, include_ram)
      changes_applied
    end
  end
end

#destroyObject



136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 136

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



56
57
58
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 56

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

#hostObject

host accessors



80
81
82
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 80

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

#host=(host) ⇒ Object



90
91
92
93
94
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 90

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

#inspectObject



48
49
50
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 48

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)


60
61
62
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 60

def persisted?
  @id.present?
end

#revertObject



148
149
150
151
152
153
154
155
156
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 148

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



125
126
127
128
129
130
131
132
133
134
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 125

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



52
53
54
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 52

def to_s
  _('Snapshot')
end

#update_attributes(new_attributes) ⇒ Object



108
109
110
111
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 108

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