class ThreeScaleToolbox::Commands::ProductCommand::ImportSubcommand

Public Class Methods

command() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 8
def self.command
  Cri::Command.define do
    name        'import'
    usage       'import [opts] <remote>'
    summary     'Import product from serialized format'
    description 'This command deserializes one product and associated backends'

    option      :f, :file, 'Read from file instead of stdin', argument: :required
    ThreeScaleToolbox::CLI.output_flag(self)
    param       :remote

    runner ImportSubcommand
  end
end

Public Instance Methods

run() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 23
def run
  validate_artifacts_resource!

  product_list.each do |product|
    context = {
        target_remote: remote,
        source_remote: crd_remote,
        source_service_ref: product.system_name,
        delete_mapping_rules: true,
        logger: Logger.new(File::NULL)
    }

    Commands::ProductCommand::CopySubcommand.workflow(context)

    report[product.system_name] = context.fetch(:report)
  end

  printer.print_collection report
end

Private Instance Methods

artifacts_resource() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 108
def artifacts_resource
  @artifacts_resource ||= load_resource(options[:file] || '-')
end
artifacts_resource_items() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 86
def artifacts_resource_items
  artifacts_resource.fetch('items') do
    raise ThreeScaleToolbox::Error, 'Invalid content. items not found'
  end
end
backend_list() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 55
def backend_list
  @backend_list ||= backend_resources.map do |backend_cr|
    CRD::BackendParser.new backend_cr
  end
end
backend_resources() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 100
def backend_resources
  artifacts_resource_items.select do |item|
    item.respond_to?(:has_key?) &&
      item.fetch('apiVersion', '').include?('capabilities.3scale.net') &&
      item['kind'] == 'Backend'
  end
end
crd_remote() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 45
def crd_remote
  @crd_remote ||= CRD::Remote.new(product_list, backend_list)
end
printer() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 120
def printer
  options.fetch(:output, CLI::JsonPrinter.new)
end
product_list() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 49
def product_list
  @product_list ||= product_resources.map do |product_cr|
    CRD::ProductParser.new product_cr
  end
end
product_resources() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 92
def product_resources
  artifacts_resource_items.select do |item|
    item.respond_to?(:has_key?) &&
      item.fetch('apiVersion', '').include?('capabilities.3scale.net') &&
      item['kind'] == 'Product'
  end
end
remote() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 116
def remote
  @remote ||= threescale_client(arguments[:remote])
end
report() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 112
def report
  @report ||= {}
end
validate_api_version!() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 70
def validate_api_version!
  artifacts_resource.fetch('apiVersion') do
    raise ThreeScaleToolbox::Error, 'Invalid content. apiVersion not found'
  end

  raise ThreeScaleToolbox::Error, 'Invalid content. apiVersion wrong value ' unless artifacts_resource.fetch('apiVersion') == 'v1'
end
validate_artifacts_resource!() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 61
def validate_artifacts_resource!
  # TODO: Add openapiV3 validation
  # https://github.com/3scale/3scale-operator/blob/3scale-2.10.0-CR2/deploy/crds/capabilities.3scale.net_backends_crd.yaml
  # https://github.com/3scale/3scale-operator/blob/3scale-2.10.0-CR2/deploy/crds/capabilities.3scale.net_products_crd.yaml
  validate_api_version!

  validate_kind!
end
validate_kind!() click to toggle source
# File lib/3scale_toolbox/commands/product_command/import_command.rb, line 78
def validate_kind!
  artifacts_resource.fetch('kind') do
    raise ThreeScaleToolbox::Error, 'Invalid content. kind not found'
  end

  raise ThreeScaleToolbox::Error, 'Invalid content. kind wrong value ' unless artifacts_resource.fetch('kind') == 'List'
end