Skip to content

Commit

Permalink
Track changes to VMs cpu/memory
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Dec 21, 2022
1 parent 4ddb6f1 commit a236879
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def initialize_inventory_collections
add_collection(infra, :ems_licenses)
add_collection(infra, :ext_management_system)
add_collection(infra, :guest_devices, :parent_inventory_collections => %i[vms_and_templates])
add_collection(infra, :hardwares, :parent_inventory_collections => %i[vms_and_templates])
add_collection(infra, :hardwares, :parent_inventory_collections => %i[vms_and_templates]) do |builder|
builder.add_properties(:track_record_changes => %i[cpu_sockets memory_mb cpu_cores_per_socket cpu_total_cores])
end

add_collection(infra, :hosts) do |builder|
builder.add_properties(:custom_reconnect_block => hosts_reconnect_block)
end
Expand Down Expand Up @@ -60,6 +63,7 @@ def initialize_inventory_collections
add_collection(infra, :root_folder_relationship)
add_collection(infra, :orchestration_templates)
vms_and_templates_assign_created_on if ::Settings.ems_refresh.capture_vm_created_on_date
track_vm_cpu_memory_changes
end

def vim_class_to_collection(managed_object)
Expand All @@ -79,6 +83,23 @@ def vim_class_to_collection(managed_object)

private

def track_vm_cpu_memory_changes
custom_save_block = lambda do |ems, inventory_collection|
hardwares = inventory_collection.dependency_attributes[:hardwares]&.first
return if hardwares.nil?
return if hardwares.record_changes.blank?

# TODO raise an event
end

settings = {:without_model_class => true, :auto_inventory_attributes => false}

add_collection(infra, :track_vm_cpu_memory_changes, {}, settings) do |builder|
builder.add_custom_save_block(custom_save_block)
builder.add_dependency_attributes(:hardwares => ->(persister) { [persister.hardwares] })
end
end

def vms_and_templates_assign_created_on
custom_save_block = lambda do |ems, inventory_collection|
vms_and_templates = inventory_collection.dependency_attributes[:vms_and_templates]&.first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@
expect(prev_respool.reload.children).not_to include(vm)
end

it "changing the cpu and memory" do
vm = ems.vms.find_by(:ems_ref => "vm-107")

run_targeted_refresh(targeted_update_set(vm_change_cpu_memory))
end

it "skip disconnected vms" do
run_targeted_refresh(targeted_update_set(vm_disconnected_object_updates))

Expand Down Expand Up @@ -770,6 +776,33 @@ def vm_new_folder_object_updates
end
end

def vm_change_cpu_memory
[
RbVmomi::VIM.ObjectUpdate(
:kind => "modify",
:obj => RbVmomi::VIM.VirtualMachine(vim, "vm-107"),
:changeSet => [
RbVmomi::VIM.PropertyChange(
:name => "summary.config.memorySizeMB",
:op => "assign",
:val => 1024
),
RbVmomi::VIM.PropertyChange(
:name => "summary.config.numCpu",
:op => "assign",
:val => 4
),
RbVmomi::VIM.PropertyChange(
:name => "config.hardware.numCoresPerSocket",
:op => "assign",
:val => 2
),
],
:missingSet => []
)
]
end

def vm_create_snapshot_object_update
RbVmomi::VIM.ObjectUpdate(
:kind => "modify",
Expand Down

0 comments on commit a236879

Please sign in to comment.