class ThreeScaleToolbox::Commands::PlansCommand::Apply::ApplySubcommand

Public Class Methods

command() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 33
def self.command
  Cri::Command.define do
    name        'apply'
    usage       'apply [opts] <remote> <service> <plan>'
    summary     'Update application plan'
    description 'Update (create if it does not exist) application plan'

    option      :n, :name, 'Plan name', argument: :required
    flag        nil, :default, 'Make default application plan'
    flag        nil, :disabled, 'Disables all methods and metrics in this application plan'
    flag        nil, :enabled, 'Enable application plan'
    flag        :p, :publish, 'Publish application plan'
    flag        nil, :hide, 'Hide application plan'
    option      nil, 'approval-required', 'Applications require approval. true or false', argument: :required, transform: ThreeScaleToolbox::Helper::BooleanTransformer.new
    option      nil, 'cost-per-month', 'Cost per month', argument: :required, transform: method(:Float)
    option      nil, 'setup-fee', 'Setup fee', argument: :required, transform: method(:Float)
    option      nil, 'trial-period-days', 'Trial period days', argument: :required, transform: method(:Integer)
    ThreeScaleToolbox::CLI.output_flag(self)

    param       :remote
    param       :service_ref
    param       :plan_ref

    runner ApplySubcommand
  end
end

Public Instance Methods

run() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 60
def run
  validate_option_params
  plan = Entities::ApplicationPlan.find(service: service, ref: plan_ref)
  if plan.nil?
    plan = Entities::ApplicationPlan.create(service: service,
                                            plan_attrs: create_plan_attrs)
  else
    plan.update(new_plan_attrs) unless new_plan_attrs.empty?
  end

  plan.make_default if option_default
  plan.disable if option_disabled
  plan.enable if option_enabled

  printer.print_record plan.attrs
end

Private Instance Methods

create_plan_attrs() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 87
def create_plan_attrs
  new_attrs = plan_basic_attrs.merge('name' => plan_ref) { |_key, oldval, _newval| oldval }
  new_attrs.tap do |params|
    params['system_name'] = plan_ref
    params['state'] = 'published' if option_publish
  end
end
find_service() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 136
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
new_plan_attrs() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 95
def new_plan_attrs
  plan_basic_attrs.clone.tap do |params|
    params['state'] = 'published' if option_publish
    params['state'] = 'hidden' if option_hide
  end
end
option_default() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 112
def option_default
  options.fetch(:default, false)
end
option_disabled() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 120
def option_disabled
  options.fetch(:disabled, false)
end
option_enabled() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 116
def option_enabled
  options.fetch(:enabled, false)
end
option_hide() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 128
def option_hide
  options.fetch(:hide, false)
end
option_publish() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 124
def option_publish
  options.fetch(:publish, false)
end
plan_basic_attrs() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 102
def plan_basic_attrs
  {
    'name' => options[:name],
    'approval_required' => options[:'approval-required'],
    'cost_per_month' => options[:'cost-per-month'],
    'setup_fee' => options[:'setup-fee'],
    'trial_period_days' => options[:'trial-period-days']
  }.compact
end
plan_ref() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 151
def plan_ref
  arguments[:plan_ref]
end
printer() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 155
def printer
  # keep backwards compatibility
  options.fetch(:output,
                CustomPrinter.new(default: option_default, disabled: option_disabled,
                                  enabled: option_enabled, publish: option_publish,
                                  hide: option_hide))
end
remote() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 143
def remote
  @remote ||= threescale_client(arguments[:remote])
end
service() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 132
def service
  @service ||= find_service
end
service_ref() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 147
def service_ref
  arguments[:service_ref]
end
validate_option_params() click to toggle source
# File lib/3scale_toolbox/commands/plans_command/apply_command.rb, line 79
def validate_option_params
  raise ThreeScaleToolbox::Error, '--disabled and --enabled are mutually exclusive' \
    if option_enabled && option_disabled

  raise ThreeScaleToolbox::Error, '--publish and --hide are mutually exclusive' \
    if option_publish && option_hide
end