Class: Pvectl::Services::SetVm

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/services/set_vm.rb

Overview

Orchestrates non-interactive VM configuration updates.

Takes key-value pairs directly (no editor), computes diff against current config, and applies changes via the API. Supports dry-run mode and optimistic locking via digest.

Examples:

Basic usage

service = SetVm.new(vm_repository: repo)
result = service.execute(vmid: 100, params: { memory: "8192", cores: "4" })

Dry run

service = SetVm.new(vm_repository: repo, options: { dry_run: true })
result = service.execute(vmid: 100, params: { memory: "8192" })

Instance Method Summary collapse

Constructor Details

#initialize(vm_repository:, options: {}) ⇒ SetVm

Creates a new SetVm service.

Parameters:

  • vm_repository (Repositories::Vm)

    VM repository

  • options (Hash) (defaults to: {})

    options (dry_run)



24
25
26
27
# File 'lib/pvectl/services/set_vm.rb', line 24

def initialize(vm_repository:, options: {})
  @vm_repository = vm_repository
  @options = options
end

Instance Method Details

#execute(vmid:, params:) ⇒ Models::VmOperationResult?

Executes the non-interactive VM config update.

Fetches current config, computes diff against requested params, and applies changes via the API (unless dry-run).

Parameters:

  • vmid (Integer)

    VM identifier

  • params (Hash)

    key-value pairs to set

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pvectl/services/set_vm.rb', line 37

def execute(vmid:, params:)
  vm = @vm_repository.get(vmid)
  return not_found_result(vmid) unless vm

  config = @vm_repository.fetch_config(vm.node, vmid)
  resource_info = { vmid: vmid, node: vm.node, status: vm.status }

  changes = compute_diff(config, params)

  if changes[:changed].empty? && changes[:added].empty? && changes[:removed].empty?
    return nil
  end

  resource_info[:diff] = changes

  if @options[:dry_run]
    return build_result(resource_info, success: true)
  end

  update_params = build_update_params(changes, config)
  @vm_repository.update(vmid, vm.node, update_params)
  build_result(resource_info, success: true)
rescue StandardError => e
  build_result({ vmid: vmid }, success: false, error: e.message)
end