Class: Pvectl::Selectors::Vm
- Defined in:
- lib/pvectl/selectors/vm.rb,
sig/pvectl/selectors/vm.rbs
Overview
Selector for filtering VMs.
Extends Base with VM-specific field extraction. Supports: status, tags, pool, name, template.
Constant Summary collapse
- SUPPORTED_FIELDS =
%w[status tags pool name template].freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#apply(vms) ⇒ Array<Models::Vm>
Applies selector to VM collection.
-
#extract_value(vm, field) ⇒ String?
Extracts field value from VM model.
-
#match_condition?(vm, condition) ⇒ Boolean
Override to handle tags specially.
-
#match_tags_condition?(vm, condition) ⇒ Boolean
Special matching for tags field.
Methods inherited from Base
#compare_value, #empty?, #initialize, #matches?, parse, parse_all, #wildcard_match?
Constructor Details
This class inherits a constructor from Pvectl::Selectors::Base
Instance Method Details
#apply(vms) ⇒ Array<Models::Vm>
Applies selector to VM collection.
29 30 31 32 33 |
# File 'lib/pvectl/selectors/vm.rb', line 29 def apply(vms) return vms if empty? vms.select { |vm| matches?(vm) } end |
#extract_value(vm, field) ⇒ String?
Extracts field value from VM model.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pvectl/selectors/vm.rb', line 43 def extract_value(vm, field) case field when "status" vm.status when "tags" vm. when "pool" vm.pool when "name" vm.name when "template" vm.template? ? "yes" : "no" else raise ArgumentError, "Unknown field: #{field}. Supported: #{SUPPORTED_FIELDS.join(', ')}" end end |
#match_condition?(vm, condition) ⇒ Boolean
Override to handle tags specially. Tags in Proxmox are semicolon-separated: "tag1;tag2;tag3" Selector "tags=prod" should match if "prod" is one of the tags.
67 68 69 70 71 72 |
# File 'lib/pvectl/selectors/vm.rb', line 67 def match_condition?(vm, condition) return (vm, condition) if condition[:field] == "tags" return super(vm, normalize_boolean_condition(condition)) if condition[:field] == "template" super end |
#match_tags_condition?(vm, condition) ⇒ Boolean
Special matching for tags field. Proxmox tags are semicolon-separated, so we check if the value is contained in the tag list.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/pvectl/selectors/vm.rb', line 97 def (vm, condition) = vm. || "" tag_list = .split(";").map(&:strip) case condition[:operator] when :eq tag_list.include?(condition[:value]) when :neq !tag_list.include?(condition[:value]) when :match tag_list.any? { |tag| wildcard_match?(tag, condition[:value]) } when :in (tag_list & condition[:value]).any? else false end end |