Class: ForemanSnapshotManagement::Snapshot
- Inherits:
-
Object
- Object
- ForemanSnapshotManagement::Snapshot
- 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
- #create_time ⇒ Object
-
#description ⇒ Object
Returns the value of attribute description.
-
#host_id ⇒ Object
Returns the value of attribute host_id.
-
#id ⇒ Object
Returns the value of attribute id.
-
#include_ram ⇒ Object
Returns the value of attribute include_ram.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#raw_snapshot ⇒ Object
Returns the value of attribute raw_snapshot.
Class Method Summary collapse
- .all_for_host(host) ⇒ Object
- .find_for_host(host, id) ⇒ Object
- .new_from_vmware(host, raw_snapshot, opts = {}) ⇒ Object
Instance Method Summary collapse
- #assign_attributes(new_attributes) ⇒ Object
- #children ⇒ Object
-
#create ⇒ Object
crud.
- #destroy ⇒ Object
- #formatted_create_time ⇒ Object
-
#host ⇒ Object
host accessors.
- #host=(host) ⇒ Object
- #inspect ⇒ Object
- #persisted? ⇒ Boolean
- #revert ⇒ Object
- #save ⇒ Object
- #to_s ⇒ Object
- #update_attributes(new_attributes) ⇒ Object
Instance Attribute Details
#create_time ⇒ Object
96 97 98 |
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 96 def create_time raw_snapshot.try(:create_time) end |
#description ⇒ Object
Returns the value of attribute description.
14 15 16 |
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 14 def description @description end |
#host_id ⇒ Object
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 |
#id ⇒ Object
Returns the value of attribute id.
12 13 14 |
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 12 def id @id end |
#include_ram ⇒ Object
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 |
#name ⇒ Object
Returns the value of attribute name.
14 15 16 |
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 14 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
12 13 14 |
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 12 def parent @parent end |
#raw_snapshot ⇒ Object
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 |
#children ⇒ Object
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 |
#create ⇒ Object
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 |
#destroy ⇒ Object
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_time ⇒ Object
56 57 58 |
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 56 def formatted_create_time create_time.strftime('%F %H:%M') end |
#host ⇒ Object
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 |
#inspect ⇒ Object
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
60 61 62 |
# File 'app/models/foreman_snapshot_management/snapshot.rb', line 60 def persisted? @id.present? end |
#revert ⇒ Object
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 |
#save ⇒ Object
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_s ⇒ Object
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 |