class ThreeScaleToolbox::Commands::ApplicationCommand::Apply::ApplySubcommand

Public Class Methods

command() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 26
          def self.command
            Cri::Command.define do
              name        'apply'
              usage       'apply [opts] <remote> <application>'
              summary     'update (or create) application'
              description <<-HEREDOC
              Update (create if it does not exist) application'
              \n Application param allows:
              \n * User_key (API key)
              \n * App_id (from app_id/app_key pair) or Client ID (for OAuth and OpenID Connect authentication modes)
              \n * Application internal id
              HEREDOC

              option      nil, 'user-key', 'User Key (API Key) of the application to be created.', argument: :required
              option      nil, 'application-key', 'App Key(s) or Client Secret (for OAuth and OpenID Connect authentication modes) of the application to be created. Only used when application does not exist.' , argument: :required
              option      nil, :description, 'Application description', argument: :required
              option      nil, :name, 'Application name', argument: :required
              option      nil, :account, 'Application\'s account. Required when creating', argument: :required
              option      nil, :service, 'Application\'s service. Required when creating', argument: :required
              option      nil, :plan, 'Application\'s plan. Required when creating', argument: :required
              option      nil, :'redirect-url', 'OpenID Connect redirect url', argument: :required
              flag        nil, :resume, 'Resume a suspended application'
              flag        nil, :suspend, 'Suspends an application (changes the state to suspended)'
              ThreeScaleToolbox::CLI.output_flag(self)

              param       :remote
              param       :application

              runner ApplySubcommand
            end
          end

Public Instance Methods

run() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 58
def run
  validate_option_params

  application = Entities::Application.find(remote: remote, service_id: service_id,
                                           ref: application_ref)
  if application.nil?
    validate_creation_option_params
    application = Entities::Application.create(remote: remote,
                                               account_id: account.id,
                                               plan_id: plan.id,
                                               app_attrs: create_app_attrs)
  else
    application.update(app_attrs) unless app_attrs.empty?
  end

  application.resume if option_resume
  application.suspend if option_suspend

  printer.print_record application.attrs
end

Private Instance Methods

account() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 123
def account
  @account ||= find_account
end
app_attrs() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 112
def app_attrs
  # This apply command does not update App Key (or Client Secret).
  # Hence, not included.
  {
    'name' => option_name,
    'description' => description,
    'user_key' => option_user_key,
    'redirect_url' => option_redirect_url,
  }.compact
end
application_ref() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 200
def application_ref
  arguments[:application]
end
create_app_attrs() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 101
def create_app_attrs
  {
    'name' => option_name,
    'description' => description,
    'user_key' => application_ref,
    'application_id' => application_ref,
    'application_key' => option_app_key,
    'redirect_url' => option_redirect_url,
  }.compact
end
description() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 168
def description
  options[:description] || option_name
end
find_account() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 127
def find_account
  Entities::Account.find(remote: remote,
                         ref: option_account).tap do |acc|
    raise ThreeScaleToolbox::Error, "Account #{option_account} does not exist" if acc.nil?
  end
end
find_plan() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 154
def find_plan
  Entities::ApplicationPlan.find(service: service, ref: option_plan).tap do |pl|
    raise ThreeScaleToolbox::Error, "Application plan #{option_plan} does not exist" if pl.nil?
  end
end
find_service() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 146
def find_service
  return Entities::Service.find(remote: remote, ref: option_service) unless option_service.nil?
end
option_account() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 180
def option_account
  options[:account]
end
option_app_key() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 176
def option_app_key
  options[:'application-key']
end
option_name() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 164
def option_name
  options[:name]
end
option_plan() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 188
def option_plan
  options[:plan]
end
option_redirect_url() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 204
def option_redirect_url
  options[:'redirect-url']
end
option_resume() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 192
def option_resume
  options.fetch(:resume, false)
end
option_service() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 184
def option_service
  options[:service]
end
option_suspend() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 196
def option_suspend
  options.fetch(:suspend, false)
end
option_user_key() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 172
def option_user_key
  options[:'user-key']
end
plan() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 150
def plan
  @plan ||= find_plan
end
printer() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 208
def printer
  # keep backwards compatibility
  options.fetch(:output, CustomPrinter.new(resume: option_resume, suspend: option_suspend))
end
remote() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 160
def remote
  @remote ||= threescale_client(arguments[:remote])
end
service() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 140
def service
  return @service if defined? @service

  @service = find_service
end
service_id() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 134
def service_id
  return if service.nil?

  service.id
end
validate_creation_option_params() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 86
def validate_creation_option_params
  raise ThreeScaleToolbox::Error, "Application #{application_ref} does not exist." \
    '--account is required to create' if option_account.nil?
  raise ThreeScaleToolbox::Error, "Application #{application_ref} does not exist." \
    '--service is required to create' if option_service.nil?
  raise ThreeScaleToolbox::Error, "Service #{option_service} does not exist" if service.nil?

  raise ThreeScaleToolbox::Error, "Application #{application_ref} does not exist." \
    '--plan is required to create' if option_plan.nil?
  raise ThreeScaleToolbox::Error, "Application #{application_ref} does not exist." \
    '--name is required to create' if option_name.nil?
  raise ThreeScaleToolbox::Error, "Application #{application_ref} does not exist." \
    '--user-key option forbidden' unless option_user_key.nil?
end
validate_option_params() click to toggle source
# File lib/3scale_toolbox/commands/application_command/apply_command.rb, line 81
def validate_option_params
  raise ThreeScaleToolbox::Error, '--resume and --suspend are mutually exclusive' \
    if option_resume && option_suspend
end