Class: ThreeScaleToolbox::Commands::MetricsCommand::Apply::ApplySubcommand

Inherits:
Cri::CommandRunner
  • Object
show all
Includes:
ThreeScaleToolbox::Command
Defined in:
lib/3scale_toolbox/commands/metrics_command/apply_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ThreeScaleToolbox::Command

#config, #config_file, #exit_with_message, #fetch_required_option, included, #remotes, #threescale_client, #verbose, #verify_ssl

Class Method Details

.commandObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/3scale_toolbox/commands/metrics_command/apply_command.rb', line 26

def self.command
  Cri::Command.define do
    name        'apply'
    usage       'apply [opts] <remote> <service> <metric>'
    summary     'Update metric'
    description 'Update (create if it does not exist) metric'

    option      :n, :name, 'Metric name', argument: :required
    flag        nil, :disabled, 'Disables this metric in all application plans'
    flag        nil, :enabled, 'Enables this metric in all application plans'
    option      nil, :unit, 'Metric unit. Default hit', argument: :required
    option      nil, :description, 'Metric description', argument: :required
    ThreeScaleToolbox::CLI.output_flag(self)

    param       :remote
    param       :service_ref
    param       :metric_ref

    runner ApplySubcommand
  end
end

Instance Method Details

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/3scale_toolbox/commands/metrics_command/apply_command.rb', line 48

def run
  validate_option_params
  metric = Entities::Metric.find(service: service, ref: metric_ref)
  if metric.nil?
    metric = Entities::Metric.create(service: service,
                                     attrs: create_metric_attrs)
  else
    metric.update(metric_attrs) unless metric_attrs.empty?
  end

  metric.disable if option_disabled
  metric.enable if option_enabled

  printer.print_record metric.attrs
end