Sha256: e3c7038c792707fb48450f5ea2218b788e2aa55d7f67684043746ac6eb9f50f3

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

module ThreeScaleToolbox
  module Commands
    module PoliciesCommand
      class ImportSubcommand < Cri::CommandRunner
        include ThreeScaleToolbox::Command
        include ThreeScaleToolbox::ResourceReader

        def self.command
          Cri::Command.define do
            name        'import'
            usage       'import [opts] <remote> <product>'
            summary     'import product policy chain'
            description 'import product policy chain'

            option      :f, :file, 'Read from file', argument: :required
            option      :u, :url, 'Read from url', argument: :required
            param       :remote
            param       :service_ref

            runner ImportSubcommand
          end
        end

        def run
          res = product.update_policies('policies_config' => policies)
          if res.is_a?(Hash) && (errors = res['errors'])
            raise ThreeScaleToolbox::Error, "Product policies have not been imported. #{errors}"
          end
          if res.is_a?(Array) && (error_item = res.find { |i| i.key?('errors') })
            raise ThreeScaleToolbox::Error, "Product policies have not been imported. #{error_item['errors']}"
          end
        end

        private

        def remote
          @remote ||= threescale_client(arguments[:remote])
        end

        def service_ref
          arguments[:service_ref]
        end

        def product
          @product ||= find_product
        end

        def find_product
          Entities::Service.find(remote: remote,
                                 ref: service_ref).tap do |svc|
            raise ThreeScaleToolbox::Error, "Product #{service_ref} does not exist" if svc.nil?
          end
        end

        def policies
          @policies ||= load_resource(options[:file] || options[:url] || '-', verify_ssl)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
3scale_toolbox-1.0.1 lib/3scale_toolbox/commands/policies_command/import_command.rb
3scale_toolbox-1.0.0 lib/3scale_toolbox/commands/policies_command/import_command.rb
3scale_toolbox-0.20.0 lib/3scale_toolbox/commands/policies_command/import_command.rb