Class: ThreeScaleToolbox::Commands::ImportCommand::ImportCsvSubcommand

Inherits:
Cri::CommandRunner
  • Object
show all
Includes:
ThreeScaleToolbox::Command
Defined in:
lib/3scale_toolbox/commands/import_command/import_csv.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



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

def self.command
  Cri::Command.define do
    name        'csv'
    usage       'csv [opts] -d <dst> -f <file>'
    summary     'import csv file'
    description 'Create new services, metrics, methods and mapping rules from CSV formatted file'

    option  :d, :destination, '3scale target instance. Url or remote name', argument: :required
    option  :f, 'file', 'CSV formatted file', argument: :required

    runner ImportCsvSubcommand
  end
end

Instance Method Details

#auth_app_key_according_service(service) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/3scale_toolbox/commands/import_command/import_csv.rb', line 21

def auth_app_key_according_service(service)
  case service['backend_version']
  when '1'
    'user_key'
  when '2'
    'app_id'
  when 'oauth'
    'oauth'
  end
end

#import_csv(destination, file_path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/3scale_toolbox/commands/import_command/import_csv.rb', line 32

def import_csv(destination, file_path)
  client = threescale_client(destination)

  data     = CSV.read file_path
  headings = data.shift
  services = {}
  stats    = { services: 0, metrics: 0, methods: 0, mapping_rules: 0 }

  # prepare services data
  data.each do |row|
    service_name = row[headings.find_index('service_name')]
    item         = {}

    services[service_name] ||= {}
    services[service_name][:items] ||= []

    (headings - ['service_name']).each do |heading|
      item[heading] = row[headings.find_index(heading)]
    end

    services[service_name][:items].push item
  end

  services.keys.each do |service_name|
    # create service
    service = client.create_service name: service_name

    if service['errors'].nil?
      stats[:services] += 1
      puts "Service #{service_name} has been created."
    else
      abort "Service has not been saved. Errors: #{service['errors']}"
    end

    # find hits metric (default)
    hits_metric = client.list_metrics(service['id']).find do |metric|
      metric['system_name'] == 'hits'
    end

    services[service_name][:items].each do |item|
      metric, method = {}

      case item['type']
        # create a metric
      when 'metric'
        metric = client.create_metric(service['id'], {
                                        system_name:   item['endpoint_system_name'],
                                        friendly_name: item['endpoint_name'],
                                        unit:          'unit'
                                      })

        if metric['errors'].nil?
          stats[:metrics] += 1
          puts "Metric #{item['endpoint_name']} has been created."
        else
          puts "Metric has not been saved. Errors: #{metric['errors']}"
        end
        # create a method
      when 'method'
        method = client.create_method(service['id'], hits_metric['id'], {
                                        system_name:   item['endpoint_system_name'],
                                        friendly_name: item['endpoint_name'],
                                        unit:          'unit'
                                      })

        if method['errors'].nil?
          stats[:methods] += 1
          puts "Method #{item['endpoint_name']} has been created."
        else
          puts "Method has not been saved. Errors: #{method['errors']}"
        end
      end

      # create a mapping rule
      if (metric_id = metric['id'] || method['id'])
        mapping_rule = client.create_mapping_rule(service['id'], {
                                                    metric_id:          metric_id,
                                                    pattern:            item['endpoint_path'],
                                                    http_method:        item['endpoint_http_method'],
                                                    metric_system_name: item['endpoint_system_name'],
                                                    auth_app_key:       auth_app_key_according_service(service),
                                                    delta:              1
                                                  })

        if mapping_rule['errors'].nil?
          stats[:mapping_rules] += 1
          puts "Mapping rule #{item['endpoint_system_name']} has been created."
        else
          puts "Mapping rule has not been saved. Errors: #{mapping_rule['errors']}"
        end
      end
    end
  end

  puts "#{services.keys.count} services in CSV file"
  puts "#{stats[:services]} services have been created"
  puts "#{stats[:metrics]} metrics have been created"
  puts "#{stats[:methods]} methods have beeen created"
  puts "#{stats[:mapping_rules]} mapping rules have been created"
end

#runObject



133
134
135
136
137
138
# File 'lib/3scale_toolbox/commands/import_command/import_csv.rb', line 133

def run
  destination = fetch_required_option(:destination)
  file_path = fetch_required_option(:file)

  import_csv(destination, file_path)
end