Class: Pvectl::Commands::Config::SetCluster

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/commands/config/set_cluster.rb

Overview

Handler for the ‘pvectl config set-cluster` command.

Creates a new cluster or modifies an existing one in the configuration. Requires –server flag for new clusters.

Examples:

Usage

pvectl config set-cluster staging --server=https://pve-staging.example.com:8006
pvectl config set-cluster prod --server=https://pve.example.com:8006 --certificate-authority=/path/to/ca.crt
pvectl config set-cluster dev --server=https://pve-dev.local:8006 --insecure-skip-tls-verify

Class Method Summary collapse

Class Method Details

.execute(cluster_name, options, global_options) ⇒ Integer

Executes the set-cluster command.

Parameters:

  • cluster_name (String)

    name of the cluster to create or modify

  • options (Hash)

    command options (:server, :certificate_authority, :insecure_skip_tls_verify)

  • global_options (Hash)

    global CLI options (includes :config)

Returns:

  • (Integer)

    exit code (0 for success)



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pvectl/commands/config/set_cluster.rb', line 62

def self.execute(cluster_name, options, global_options)
  config_path = global_options[:config]
  service = Pvectl::Config::Service.new
  service.load(config: config_path)

  existing_cluster = service.cluster(cluster_name)
  action = existing_cluster ? "modified" : "created"

  # Use existing values if not provided
  server = options[:server] || existing_cluster&.server
  certificate_authority = options[:"certificate-authority"] ||
                          options[:certificate_authority] ||
                          existing_cluster&.certificate_authority

  # Handle insecure-skip-tls-verify flag
  insecure_skip = options[:"insecure-skip-tls-verify"]
  verify_ssl = if insecure_skip.nil?
                 existing_cluster ? existing_cluster.verify_ssl : true
               else
                 !insecure_skip
               end

  # Validate required fields for new clusters
  if server.nil?
    $stderr.puts "Error: --server is required for new clusters"
    return ExitCodes::USAGE_ERROR
  end

  service.set_cluster(
    name: cluster_name,
    server: server,
    verify_ssl: verify_ssl,
    certificate_authority: certificate_authority
  )

  puts "Cluster \"#{cluster_name}\" #{action}."
  0
end

.register_subcommand(parent) ⇒ void

This method returns an undefined value.

Registers the set-cluster subcommand.

Parameters:

  • parent (GLI::Command)

    parent config command



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pvectl/commands/config/set_cluster.rb', line 21

def self.register_subcommand(parent)
  parent.desc "Create or modify a cluster"
  parent.long_desc <<~HELP
    Create a new cluster definition or modify an existing one.

    EXAMPLES
      Add a cluster:
        $ pvectl config set-cluster production --server=https://pve.example.com:8006

      Skip TLS verification (testing only):
        $ pvectl config set-cluster lab --server=https://pve-lab:8006 --insecure-skip-tls-verify
  HELP
  parent.command :"set-cluster" do |set_cluster|
    set_cluster.arg_name "CLUSTER_NAME"

    set_cluster.desc "Proxmox server URL (e.g., https://pve.example.com:8006)"
    set_cluster.flag [:server]

    set_cluster.desc "Path to CA certificate file"
    set_cluster.flag [:"certificate-authority"]

    set_cluster.desc "Skip TLS certificate verification"
    set_cluster.switch [:"insecure-skip-tls-verify"], negatable: false

    set_cluster.action do |global_options, options, args|
      if args.empty?
        $stderr.puts "Error: cluster name is required"
        exit ExitCodes::USAGE_ERROR
      end
      exit_code = execute(args[0], options, global_options)
      exit exit_code if exit_code != 0
    end
  end
end