Class: ThreeScaleToolbox::Commands::PolicyRegistryCommand::Copy::CopySubcommand

Inherits:
Cri::CommandRunner
  • Object
show all
Includes:
ThreeScaleToolbox::Command
Defined in:
lib/3scale_toolbox/commands/policy_registry_command/copy_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ThreeScaleToolbox::Command

#config, #config_file, #exit_with_message, #fetch_required_option, included, #remotes, #threescale_client, #verbose, #verify_ssl

Class Method Details

.commandObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/3scale_toolbox/commands/policy_registry_command/copy_command.rb', line 8

def self.command
  Cri::Command.define do
    name        'copy'
    usage       'copy [opts] <source_remote> <target_remote>'
    summary     'Copy policy registry'
    description 'Copy policy registry'

    param       :source_remote
    param       :target_remote

    runner CopySubcommand
  end
end

Instance Method Details

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/3scale_toolbox/commands/policy_registry_command/copy_command.rb', line 22

def run
  source_policies = source_remote.list_policy_registry
  if source_policies.respond_to?(:has_key?) && (errors = source_policies['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Could not list source policy registry', errors)
  end

  target_policies = target_remote.list_policy_registry
  if target_policies.respond_to?(:has_key?) && (errors = target_policies['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Could not list target policy registry', errors)
  end

  # Create missing
  missing = missing_policies(source_policies, target_policies)
  missing.each do |policy|
    new_policy_registry = target_remote.create_policy_registry(policy)
    if (errors = new_policy_registry['errors'])
      raise ThreeScaleToolbox::ThreeScaleApiError.new('Could not create target policy registry', errors)
    end
  end

  # Update those matching
  matching = matching_policies(source_policies, target_policies)
  matching.each do |policy|
    updated_policy = target_remote.update_policy_registry(
      "#{policy['name']}-#{policy['version']}", policy
    )
    if (errors = updated_policy['errors'])
      raise ThreeScaleToolbox::ThreeScaleApiError.new('Could not update target policy registry', errors)
    end
  end

  puts "Created #{missing.size} missing policies on target tenant"
  puts "Updated #{matching.size} matching policies on target tenant"
end