Class: Pvectl::Selectors::Container

Inherits:
Base
  • Object
show all
Defined in:
lib/pvectl/selectors/container.rb,
sig/pvectl/selectors/container.rbs

Overview

Selector for filtering containers.

Extends Base with container-specific field extraction. Supports: status, tags, pool, name, template.

Examples:

Filter running containers

selector = Container.parse("status=running")
running_containers = selector.apply(all_containers)

Filter by multiple criteria

selector = Container.parse("status=running,tags=prod")
filtered = selector.apply(all_containers)

Filter by name pattern

selector = Container.parse("name=~web-*")
web_containers = selector.apply(all_containers)

Constant Summary collapse

SUPPORTED_FIELDS =

Returns:

  • (Array[String])
%w[status tags pool name template].freeze

Instance Attribute Summary

Attributes inherited from Base

#conditions

Instance Method Summary collapse

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(containers) ⇒ Array<Models::Container>

Applies selector to container collection.

Parameters:

Returns:



29
30
31
32
33
# File 'lib/pvectl/selectors/container.rb', line 29

def apply(containers)
  return containers if empty?

  containers.select { |ct| matches?(ct) }
end

#extract_value(container, field) ⇒ String?

Extracts field value from Container model.

Parameters:

  • container (Models::Container)

    Container model

  • field (String)

    Field name (status, tags, pool, name, template)

Returns:

  • (String, nil)

    Field value

Raises:

  • (ArgumentError)

    if field is not supported



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pvectl/selectors/container.rb', line 43

def extract_value(container, field)
  case field
  when "status"
    container.status
  when "tags"
    container.tags
  when "pool"
    container.pool
  when "name"
    container.name
  when "template"
    container.template? ? "yes" : "no"
  else
    raise ArgumentError, "Unknown field: #{field}. Supported: #{SUPPORTED_FIELDS.join(', ')}"
  end
end

#match_condition?(container, 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.

Parameters:

Returns:

  • (Boolean)

    true if matches



67
68
69
70
71
72
# File 'lib/pvectl/selectors/container.rb', line 67

def match_condition?(container, condition)
  return match_tags_condition?(container, condition) if condition[:field] == "tags"
  return super(container, normalize_boolean_condition(condition)) if condition[:field] == "template"

  super
end

#match_tags_condition?(container, condition) ⇒ Boolean

Special matching for tags field. Proxmox tags are semicolon-separated, so we check if the value is contained in the tag list.

Parameters:

Returns:

  • (Boolean)

    true if matches



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pvectl/selectors/container.rb', line 97

def match_tags_condition?(container, condition)
  tags_string = container.tags || ""
  tag_list = tags_string.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