Class: Pvectl::Commands::Config::SetContext

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

Overview

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

Creates a new context or modifies an existing one in the configuration. Requires –cluster and –user flags for new contexts.

Examples:

Usage

pvectl config set-context production --cluster=pve-prod --user=admin
pvectl config set-context dev --cluster=pve-dev --user=admin --default-node=pve1

Class Method Summary collapse

Class Method Details

.execute(context_name, options, global_options) ⇒ Integer

Executes the set-context command.

Parameters:

  • context_name (String)

    name of the context to create or modify

  • options (Hash)

    command options (:cluster, :user, :default_node)

  • global_options (Hash)

    global CLI options (includes :config)

Returns:

  • (Integer)

    exit code (0 for success)

Raises:



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
100
101
102
# File 'lib/pvectl/commands/config/set_context.rb', line 64

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

  existing_context = service.context(context_name)
  action = existing_context ? "modified" : "created"

  # Use existing values if not provided
  cluster = options[:cluster] || existing_context&.cluster_ref
  user = options[:user] || existing_context&.user_ref
  default_node = options[:"default-node"] || options[:default_node] || existing_context&.default_node

  # Validate required fields for new contexts
  if cluster.nil? || user.nil?
    $stderr.puts "Error: --cluster and --user are required for new contexts"
    return ExitCodes::USAGE_ERROR
  end

  # Validate cluster and user exist
  validate_cluster_exists!(service, cluster)
  validate_user_exists!(service, user)

  service.set_context(
    name: context_name,
    cluster: cluster,
    user: user,
    default_node: default_node
  )

  puts "Context \"#{context_name}\" #{action}."
  0
rescue Pvectl::Config::ClusterNotFoundError => e
  $stderr.puts "Error: #{e.message}"
  ExitCodes::CONFIG_ERROR
rescue Pvectl::Config::UserNotFoundError => e
  $stderr.puts "Error: #{e.message}"
  ExitCodes::CONFIG_ERROR
end

.register_subcommand(parent) ⇒ void

This method returns an undefined value.

Registers the set-context subcommand.

Parameters:

  • parent (GLI::Command)

    parent config command



20
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_context.rb', line 20

def self.register_subcommand(parent)
  parent.desc "Create or modify a context"
  parent.long_desc <<~HELP
    Create a new context or modify an existing one. A context links
    a cluster definition with user credentials.

    EXAMPLES
      Create a new context:
        $ pvectl config set-context prod --cluster=production --user=admin-prod

      Set a default node:
        $ pvectl config set-context prod --default-node=pve1
  HELP
  parent.command :"set-context" do |set_ctx|
    set_ctx.arg_name "CONTEXT_NAME"

    set_ctx.desc "Cluster name"
    set_ctx.flag [:cluster]

    set_ctx.desc "User name"
    set_ctx.flag [:user]

    set_ctx.desc "Default node"
    set_ctx.flag [:"default-node"]

    set_ctx.action do |global_options, options, args|
      if args.empty?
        $stderr.puts "Error: context 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

.validate_cluster_exists!(service, cluster_name) ⇒ Object

Validates that the cluster exists in the configuration.

Parameters:

  • service (Config::Service)

    configuration service

  • cluster_name (String)

    cluster name to validate

Raises:



109
110
111
112
113
114
115
116
117
# File 'lib/pvectl/commands/config/set_context.rb', line 109

def self.validate_cluster_exists!(service, cluster_name)
  clusters = service.raw_config["clusters"] || []
  found = clusters.any? { |c| c["name"] == cluster_name }
  unless found
    available = clusters.map { |c| c["name"] }.join(", ")
    raise Pvectl::Config::ClusterNotFoundError,
          "Cluster '#{cluster_name}' not found in configuration. Available: #{available}"
  end
end

.validate_user_exists!(service, user_name) ⇒ Object

Validates that the user exists in the configuration.

Parameters:

  • service (Config::Service)

    configuration service

  • user_name (String)

    user name to validate

Raises:



124
125
126
127
128
129
130
131
132
# File 'lib/pvectl/commands/config/set_context.rb', line 124

def self.validate_user_exists!(service, user_name)
  users = service.raw_config["users"] || []
  found = users.any? { |u| u["name"] == user_name }
  unless found
    available = users.map { |u| u["name"] }.join(", ")
    raise Pvectl::Config::UserNotFoundError,
          "User '#{user_name}' not found in configuration. Available: #{available}"
  end
end