Module: Pvectl::Commands::CreateResourceCommand Abstract

Included in:
CreateContainer, CreateVm
Defined in:
lib/pvectl/commands/create_resource_command.rb,
sig/pvectl/commands/create_resource_command.rbs

Overview

This module is abstract.

Include this module and implement template methods.

Shared functionality for resource creation commands.

Template method pattern: provides common create workflow (interactive detection, confirmation, dry-run, config loading) while specialization classes define resource-specific hooks.

Examples:

Specialization

class CreateVm
  include CreateResourceCommand
  private
  def resource_label = "VM"
  def resource_id_label = "VMID"
  # ...
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ void

This method returns an undefined value.

Hook called when module is included.

Parameters:

  • the class including this module



39
40
41
# File 'lib/pvectl/commands/create_resource_command.rb', line 39

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#build_create_service(connection, task_repo) ⇒ Object

Returns create service instance.

Parameters:

  • API connection

  • task repository

Returns:

  • create service instance

Raises:



97
98
99
# File 'lib/pvectl/commands/create_resource_command.rb', line 97

def build_create_service(connection, task_repo)
  raise NotImplementedError, "#{self.class} must implement #build_create_service"
end

#build_params_from_flagsHash

Returns parameters built from CLI flags.

Returns:

  • parameters built from CLI flags

Raises:



85
86
87
# File 'lib/pvectl/commands/create_resource_command.rb', line 85

def build_params_from_flags
  raise NotImplementedError, "#{self.class} must implement #build_params_from_flags"
end

#build_wizardObject

Returns wizard instance for interactive mode.

Returns:

  • wizard instance for interactive mode

Raises:



90
91
92
# File 'lib/pvectl/commands/create_resource_command.rb', line 90

def build_wizard
  raise NotImplementedError, "#{self.class} must implement #build_wizard"
end

#display_resource_summary(params) ⇒ void

This method returns an undefined value.

Parameters:

  • creation parameters

Raises:



109
110
111
# File 'lib/pvectl/commands/create_resource_command.rb', line 109

def display_resource_summary(params)
  raise NotImplementedError, "#{self.class} must implement #display_resource_summary"
end

#display_summary(params) ⇒ void

This method returns an undefined value.

Displays the creation summary.

Parameters:

  • creation parameters



193
194
195
196
197
198
199
200
201
202
# File 'lib/pvectl/commands/create_resource_command.rb', line 193

def display_summary(params)
  $stdout.puts ""
  $stdout.puts "  Create #{resource_label} - Summary"
  $stdout.puts "  #{'─' * 40}"
  $stdout.puts "  #{resource_id_label}:#{' ' * (10 - resource_id_label.length)}#{params[:vmid] || params[:ctid] || '(auto)'}"
  display_resource_summary(params)
  $stdout.puts "  #{'─' * 40}"
  $stdout.puts "  (dry-run mode -- no #{resource_label} will be created)" if @options[:"dry-run"]
  $stdout.puts ""
end

#display_summary_and_confirm(params) ⇒ Symbol?

Displays a creation summary and prompts for confirmation.

Parameters:

  • creation parameters

Returns:

  • :cancelled if user declines, nil otherwise



178
179
180
181
182
183
184
185
186
187
# File 'lib/pvectl/commands/create_resource_command.rb', line 178

def display_summary_and_confirm(params)
  display_summary(params)

  return nil if @options[:yes]

  $stdout.print "Create this #{resource_label}? [y/N] "
  $stdout.flush
  answer = $stdin.gets&.strip&.downcase
  answer == "y" ? nil : :cancelled
end

#executeInteger

Executes the create command.

Returns:

  • exit code



57
58
59
60
61
62
63
# File 'lib/pvectl/commands/create_resource_command.rb', line 57

def execute
  if interactive_mode?
    perform_interactive
  else
    perform_create
  end
end

#initialize(args, options, global_options) ⇒ CreateResourceCommand

Initializes a create command.

Parameters:

  • command arguments

  • command options

  • global CLI options

Returns:

  • a new instance of CreateResourceCommand



48
49
50
51
52
# File 'lib/pvectl/commands/create_resource_command.rb', line 48

def initialize(args, options, global_options)
  @args = args
  @options = options
  @global_options = global_options
end

#interactive_mode?Boolean

Determines whether to use interactive mode.

Returns:

  • true if interactive mode should be used



118
119
120
121
122
123
# File 'lib/pvectl/commands/create_resource_command.rb', line 118

def interactive_mode?
  return true if @options[:interactive]
  return false if @options[:"no-interactive"]

  $stdin.tty? && required_params_missing?
end

#load_configvoid

This method returns an undefined value.

Loads configuration from file or environment.



217
218
219
220
221
# File 'lib/pvectl/commands/create_resource_command.rb', line 217

def load_config
  service = Pvectl::Config::Service.new
  service.load(config: @global_options[:config])
  @config = service.current_config
end

#output_result(result) ⇒ void

This method returns an undefined value.

Parameters:

  • operation result

Raises:



103
104
105
# File 'lib/pvectl/commands/create_resource_command.rb', line 103

def output_result(result)
  raise NotImplementedError, "#{self.class} must implement #output_result"
end

#perform_createInteger

Validates flags and performs flag-based creation.

Returns:

  • exit code



140
141
142
143
144
145
# File 'lib/pvectl/commands/create_resource_command.rb', line 140

def perform_create
  params = build_params_from_flags
  perform_create_with_params(params)
rescue ArgumentError => e
  usage_error(e.message)
end

#perform_create_with_params(params) ⇒ Integer

Creates a resource with the given parameters.

Parameters:

  • creation parameters

Returns:

  • exit code



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/pvectl/commands/create_resource_command.rb', line 151

def perform_create_with_params(params)
  return ExitCodes::SUCCESS if display_summary_and_confirm(params) == :cancelled
  return ExitCodes::SUCCESS if @options[:"dry-run"]

  load_config
  connection = Pvectl::Connection.new(@config)
  task_repo = Pvectl::Repositories::Task.new(connection)

  service = build_create_service(connection, task_repo)
  result = service.execute(**params)
  output_result(result)
  result.failed? ? ExitCodes::GENERAL_ERROR : ExitCodes::SUCCESS
rescue Pvectl::Config::ConfigNotFoundError,
       Pvectl::Config::InvalidConfigError,
       Pvectl::Config::ContextNotFoundError,
       Pvectl::Config::ClusterNotFoundError,
       Pvectl::Config::UserNotFoundError
  raise
rescue StandardError => e
  $stderr.puts "Error: #{e.message}"
  ExitCodes::GENERAL_ERROR
end

#perform_interactiveInteger

Runs the interactive wizard flow.

Returns:

  • exit code



128
129
130
131
132
133
134
135
# File 'lib/pvectl/commands/create_resource_command.rb', line 128

def perform_interactive
  wizard = build_wizard
  wizard_params = wizard.run
  return ExitCodes::SUCCESS unless wizard_params

  @options[:start] = wizard_params.delete(:start) if wizard_params.key?(:start)
  perform_create_with_params(wizard_params)
end

#required_params_missing?Boolean

Returns true if required params for non-interactive mode are missing.

Returns:

  • true if required params for non-interactive mode are missing

Raises:



80
81
82
# File 'lib/pvectl/commands/create_resource_command.rb', line 80

def required_params_missing?
  raise NotImplementedError, "#{self.class} must implement #required_params_missing?"
end

#resolve_default_nodeString?

Resolves the default node from configuration.

Returns:

  • default node name or nil



207
208
209
210
211
212
# File 'lib/pvectl/commands/create_resource_command.rb', line 207

def resolve_default_node
  load_config unless @config
  @config&.default_node
rescue StandardError
  nil
end

#resource_id_labelString

Returns human label for resource ID ("VMID" or "CTID").

Returns:

  • human label for resource ID ("VMID" or "CTID")

Raises:



75
76
77
# File 'lib/pvectl/commands/create_resource_command.rb', line 75

def resource_id_label
  raise NotImplementedError, "#{self.class} must implement #resource_id_label"
end

#resource_labelString

Returns human label for resource ("VM" or "container").

Returns:

  • human label for resource ("VM" or "container")

Raises:



70
71
72
# File 'lib/pvectl/commands/create_resource_command.rb', line 70

def resource_label
  raise NotImplementedError, "#{self.class} must implement #resource_label"
end

#service_optionsHash

Builds service options from command options.

Returns:

  • service options



226
227
228
229
230
231
232
# File 'lib/pvectl/commands/create_resource_command.rb', line 226

def service_options
  opts = {}
  opts[:timeout] = @options[:timeout] if @options[:timeout]
  opts[:async] = true if @options[:async]
  opts[:start] = true if @options[:start]
  opts
end

#usage_error(message) ⇒ Integer

Outputs usage error and returns exit code.

Parameters:

  • error message

Returns:

  • exit code



238
239
240
241
# File 'lib/pvectl/commands/create_resource_command.rb', line 238

def usage_error(message)
  $stderr.puts "Error: #{message}"
  ExitCodes::USAGE_ERROR
end