class ThreeScaleToolbox::Commands::MetricsCommand::Apply::ApplySubcommand

Public Class Methods

command() click to toggle source
# 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

Public Instance Methods

run() click to toggle source
# 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

Private Instance Methods

create_metric_attrs() click to toggle source
# File lib/3scale_toolbox/commands/metrics_command/apply_command.rb, line 71
def create_metric_attrs
  metric_attrs.merge('system_name' => metric_ref,
                     'unit' => 'hit',
                     'friendly_name' => metric_ref) { |_key, oldval, _newval| oldval }
end
find_service() click to toggle source
# File lib/3scale_toolbox/commands/metrics_command/apply_command.rb, line 97
def find_service
  Entities::Service.find(remote: remote,
                         ref: service_ref).tap do |svc|
    raise ThreeScaleToolbox::Error, "Service #{service_ref} does not exist" if svc.nil?
  end
end
metric_attrs() click to toggle source
# File lib/3scale_toolbox/commands/metrics_command/apply_command.rb, line 77
def metric_attrs
  {
    'friendly_name' => options[:name],
    'unit' => options[:unit],
    'description' => options[:description]
  }.compact
end
metric_ref() click to toggle source
# File lib/3scale_toolbox/commands/metrics_command/apply_command.rb, line 112
def metric_ref
  arguments[:metric_ref]
end
option_disabled() click to toggle source
# File lib/3scale_toolbox/commands/metrics_command/apply_command.rb, line 89
def option_disabled
  options.fetch(:disabled, false)
end
option_enabled() click to toggle source
# File lib/3scale_toolbox/commands/metrics_command/apply_command.rb, line 85
def option_enabled
  options.fetch(:enabled, false)
end
printer() click to toggle source
# File lib/3scale_toolbox/commands/metrics_command/apply_command.rb, line 116
def printer
  # keep backwards compatibility
  options.fetch(:output, CustomPrinter.new(disabled: option_disabled, enabled: option_enabled))
end
remote() click to toggle source
# File lib/3scale_toolbox/commands/metrics_command/apply_command.rb, line 104
def remote
  @remote ||= threescale_client(arguments[:remote])
end
service() click to toggle source
# File lib/3scale_toolbox/commands/metrics_command/apply_command.rb, line 93
def service
  @service ||= find_service
end
service_ref() click to toggle source
# File lib/3scale_toolbox/commands/metrics_command/apply_command.rb, line 108
def service_ref
  arguments[:service_ref]
end
validate_option_params() click to toggle source
# File lib/3scale_toolbox/commands/metrics_command/apply_command.rb, line 66
def validate_option_params
  raise ThreeScaleToolbox::Error, '--disabled and --enabled are mutually exclusive' \
    if option_enabled && option_disabled
end