Class: Pvectl::Services::Get::ResourceService

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/services/get/resource_service.rb,
sig/pvectl/services/get/resource_service.rbs

Overview

Service for fetching and formatting resource data.

Orchestrates the data flow between:

  • ResourceHandler (provides models and presenter)
  • Formatters::Registry (formats output)

This follows ARCHITECTURE.md section 3.4: "Services orchestrate data flow between Repository, Models, and Formatters"

Examples:

Basic usage

handler = ResourceRegistry.for("nodes")
service = ResourceService.new(handler: handler, format: "table")
output = service.list(node: "pve1")
puts output

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler:, format: "table", color_enabled: true) ⇒ ResourceService

Creates a new ResourceService.

Parameters:

  • the resource handler for data fetching

  • (defaults to: "table")

    output format (table, json, yaml, wide)

  • (defaults to: true)

    whether to enable colored output

  • (defaults to: "table")
  • (defaults to: true)


27
28
29
30
31
# File 'lib/pvectl/services/get/resource_service.rb', line 27

def initialize(handler:, format: "table", color_enabled: true)
  @handler = handler
  @format = format
  @color_enabled = color_enabled
end

Instance Attribute Details

#color_enabledBoolean (readonly)

Returns the value of attribute color_enabled.

Returns:



74
75
76
# File 'lib/pvectl/services/get/resource_service.rb', line 74

def color_enabled
  @color_enabled
end

#formatString (readonly)

Returns the value of attribute format.

Returns:



74
75
76
# File 'lib/pvectl/services/get/resource_service.rb', line 74

def format
  @format
end

#handlerObject (readonly)

Returns the value of attribute handler.

Returns:



74
75
76
# File 'lib/pvectl/services/get/resource_service.rb', line 74

def handler
  @handler
end

Instance Method Details

#describe(name:, node: nil, args: [], vmid: nil) ⇒ String

Describes and formats a single resource.

For local storage with multiple instances, returns list of nodes when no node specified, or full describe when node is specified.

Parameters:

  • resource name

  • (defaults to: nil)

    filter by node name (for local storage)

  • (defaults to: nil)

    filter by VM/CT IDs

  • (defaults to: nil)
  • (defaults to: [])
  • (defaults to: nil)

Returns:

  • formatted output string



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pvectl/services/get/resource_service.rb', line 59

def describe(name:, node: nil, args: [], vmid: nil)
  result = @handler.describe(name: name, node: node, args: args, vmid: vmid)
  presenter = @handler.presenter

  if result.is_a?(Array)
    # Multiple instances - format as list
    format_output(result, presenter)
  else
    # Single model - format as describe
    format_output_describe(result, presenter)
  end
end

#format_output(models, presenter) ⇒ String

Formats models for output using the appropriate formatter.

Parameters:

  • collection of models

  • presenter for the resource type

Returns:

  • formatted output



81
82
83
84
# File 'lib/pvectl/services/get/resource_service.rb', line 81

def format_output(models, presenter)
  formatter = Formatters::Registry.for(format)
  formatter.format(models, presenter, color_enabled: color_enabled)
end

#format_output_describe(model, presenter) ⇒ String

Formats single model for describe output.

Parameters:

  • single model

  • presenter for the resource type

Returns:

  • formatted output



91
92
93
94
# File 'lib/pvectl/services/get/resource_service.rb', line 91

def format_output_describe(model, presenter)
  formatter = Formatters::Registry.for(format)
  formatter.format(model, presenter, color_enabled: color_enabled, describe: true)
end

#list(node: nil, name: nil, args: [], storage: nil, vmid: nil, selector: nil, **options) ⇒ String

Fetches and formats resources.

Parameters:

  • (defaults to: nil)

    filter by node name

  • (defaults to: nil)

    filter by resource name

  • (defaults to: [])

    additional positional arguments (e.g., VMIDs for snapshots)

  • (defaults to: nil)

    filter by storage (for backups)

  • (defaults to: nil)

    filter by VM/CT IDs

  • (defaults to: nil)

    client-side selector for filtering results

  • additional options passed through to handler (e.g., limit, since, type_filter)

  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: [])
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)

Returns:

  • formatted output string



43
44
45
46
47
48
# File 'lib/pvectl/services/get/resource_service.rb', line 43

def list(node: nil, name: nil, args: [], storage: nil, vmid: nil, selector: nil, **options)
  models = @handler.list(node: node, name: name, args: args, storage: storage, vmid: vmid, **options)
  models = selector.apply(models) if selector && !selector.empty?
  presenter = @handler.presenter
  format_output(models, presenter)
end