Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse VM Snapshots #270

Merged
merged 3 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ def disks(extra_attributes = {})
super(attributes.merge(extra_attributes))
end

def snapshots(extra_attributes = {})
attributes = {
:manager_ref => [:vm_or_template, :uid],
:parent_inventory_collections => [:vms_and_templates],
}
super(attributes.merge(extra_attributes))
end

def operating_systems(extra_attributes = {})
attributes = {:parent_inventory_collections => [:vms_and_templates]}
super(attributes.merge(extra_attributes))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,40 @@ def parse_virtual_machine_custom_attributes(vm, props)
end

def parse_virtual_machine_snapshots(vm, props)
snapshots = props[:snapshot]
return if snapshots.blank?

current = snapshots[:currentSnapshot]
return if current.nil?

snapshots[:rootSnapshotList].to_a.each do |snapshot|
parse_virtual_machine_snapshot(vm, snapshot, current)
end
end

def parse_virtual_machine_snapshot(vm, snapshot, current, parent_uid = nil)
snap_ref = snapshot[:snapshot]&._ref
return if snap_ref.nil?

create_time = snapshot[:createTime]

snapshot_hash = {
:vm_or_template => vm,
:ems_ref => snap_ref,
:uid_ems => create_time.to_s,
:uid => create_time.iso8601(6),
:parent_uid => parent_uid,
:name => CGI.unescape(snapshot[:name]),
:description => snapshot[:description],
:create_time => create_time.to_s,
:current => snap_ref == current._ref,
}

persister.snapshots.build(snapshot_hash)

snapshot[:childSnapshotList].to_a.each do |child_snapshot|
parse_virtual_machine_snapshot(vm, child_snapshot, current, snapshot_hash[:uid_ems])
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def initialize_inventory_collections
:dependency_attributes => {:ems_folders => [collections[:ems_folders]]},
)
)

add_inventory_collection(
default_inventory_collections.snapshot_parent(
:dependency_attributes => {:snapshots => [collections[:snapshots]]},
)
)
end

def default_inventory_collections
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
let(:vim) { RbVmomi::VIM.new(:ns => "urn2", :rev => "6.5") }
let(:property_filter) { RbVmomi::VIM.PropertyFilter(vim, "session[6f2dcefd-41de-6dfb-0160-1ee1cc024553]") }
let(:cache) { collector.send(:inventory_cache) }
let(:persister) { ems.class::Inventory::Persister::Targeted.new(ems) }
let(:parser) { ems.class::Inventory::Parser.new(cache, persister) }

before do
# Use the VCR to prime the cache and do the initial save_inventory
Expand Down Expand Up @@ -101,9 +99,42 @@
expect(prev_respool.reload.children).not_to include(vm)
end

it "creating and deleting a snapshot" do
vm = ems.vms.find_by(:ems_ref => "vm-107")

expect(vm.snapshots.count).to eq(0)

run_targeted_refresh(targeted_update_set([vm_create_snapshot_object_update]))

vm.reload

expect(vm.snapshots.count).to eq(1)
expect(vm.snapshots.first).to have_attributes(
:uid => "2018-05-19T06:47:56.000000Z",
:parent_uid => nil,
:name => "VM Snapshot 5%2f19%2f2018, 9:54:04 AM",
:description => "",
:current => 1,
:create_time => Time.parse("2018-05-19 06:47:56 UTC").utc,
:parent_id => nil,
:uid_ems => "2018-05-19 06:47:56 UTC",
:ems_ref => "snapshot-1100",
)

run_targeted_refresh(targeted_update_set([vm_delete_snapshot_object_update]))

vm.reload

expect(vm.snapshots.count).to eq(0)
end

def run_targeted_refresh(update_set)
persister = ems.class::Inventory::Persister::Targeted.new(ems)
parser = ems.class::Inventory::Parser.new(cache, persister)
update_set = collector.send(:process_update_set, property_filter, update_set)

update_set.each { |managed_object, kind, props| parser.parse(managed_object, kind, props) }

collector.send(:save_inventory, persister)
end

Expand Down Expand Up @@ -226,6 +257,136 @@ def vm_new_folder_object_updates
end
end

def vm_create_snapshot_object_update
RbVmomi::VIM.ObjectUpdate(
:dynamicProperty => [],
:kind => "modify",
:obj => RbVmomi::VIM.VirtualMachine(vim, "vm-107"),
:changeSet => [
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "config.hardware.device[2000].backing.deltaDiskFormat",
:op => "assign",
:val => "redoLogFormat",
),
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "config.hardware.device[2000].backing.deltaDiskFormatVariant",
:op => "assign",
:val => "vmfsSparseVariant",
),
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "config.hardware.device[2000].backing.fileName",
:op => "assign",
:val => "[GlobalDS_0] DC0_C1_RP1_VM0/DC0_C1_RP1_VM0-000001.vmdk",
),
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "config.hardware.device[2000].backing.parent",
:op => "assign",
:val => RbVmomi::VIM.VirtualDiskFlatVer2BackingInfo(
:dynamicProperty => [],
:fileName => "[GlobalDS_0] DC0_C1_RP1_VM0/DC0_C1_RP1_VM0.vmdk",
:datastore => RbVmomi::VIM.Datastore(vim, "datastore-15"),
:backingObjectId => "",
:diskMode => "persistent",
:thinProvisioned => true,
:uuid => "52dab7a1-6c3e-1f7b-fe00-a2c6213343b7",
:contentId => "2929a7a583fe0c83749f9402fffffffe",
:digestEnabled => false,
),
),
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "snapshot",
:op => "assign",
:val => RbVmomi::VIM.VirtualMachineSnapshotInfo(
:dynamicProperty => [],
:currentSnapshot => RbVmomi::VIM.VirtualMachineSnapshot(vim, "snapshot-1100"),
:rootSnapshotList => [
RbVmomi::VIM.VirtualMachineSnapshotTree(
:dynamicProperty => [],
:snapshot => RbVmomi::VIM.VirtualMachineSnapshot(vim, "snapshot-1100"),
:vm => RbVmomi::VIM.VirtualMachine(vim, "vm-107"),
:name => "VM Snapshot 5%252f19%252f2018, 9:54:04 AM",
:description => "",
:id => 5,
:createTime => Time.parse("2018-05-19 06:47:56 UTC").utc,
:state => "poweredOff",
:quiesced => false,
:childSnapshotList => [],
:replaySupported => false,
),
],
),
),
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "summary.storage.committed",
:op => "assign",
:val => 54_177,
),
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "summary.storage.unshared",
:op => "assign",
:val => 41_855,
),
],
:missingSet => [],
)
end

def vm_delete_snapshot_object_update
RbVmomi::VIM.ObjectUpdate(
:dynamicProperty => [],
:kind => "modify",
:obj => RbVmomi::VIM.VirtualMachine(vim, "vm-107"),
:changeSet => [
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "config.hardware.device[2000].backing.deltaDiskFormat",
:op => "assign",
),
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "config.hardware.device[2000].backing.deltaDiskFormatVariant",
:op => "assign",
),
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "config.hardware.device[2000].backing.fileName",
:op => "assign",
:val => "[GlobalDS_0] DC0_C1_RP1_VM0/DC0_C1_RP1_VM0.vmdk",
),
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "config.hardware.device[2000].backing.parent",
:op => "assign",
),
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "snapshot",
:op => "assign",
),
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "summary.storage.committed",
:op => "assign",
:val => 2316,
),
RbVmomi::VIM.PropertyChange(
:dynamicProperty => [],
:name => "summary.storage.unshared",
:op => "assign",
:val => 538,
),
],
:missingSet => [],
)
end

def run_full_refresh
# All VIM API calls go to uri https://hostname/sdk so we have to match on the body
VCR.use_cassette(described_class.name.underscore, :match_requests_on => [:body]) do
Expand Down