3scale_toolbox 0.19.2 → 0.19.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/lib/3scale_toolbox/commands/import_command/openapi/update_policies_step.rb +23 -10
  3. data/lib/3scale_toolbox/commands/plans_command/export_command.rb +52 -29
  4. data/lib/3scale_toolbox/commands/plans_command/import/import_backend_metrics_step.rb +37 -0
  5. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_limits_step.rb +11 -2
  6. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_metrics_step.rb +2 -2
  7. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb +12 -1
  8. data/lib/3scale_toolbox/commands/plans_command/import/step.rb +23 -8
  9. data/lib/3scale_toolbox/commands/plans_command/import/validate_plan_step.rb +126 -0
  10. data/lib/3scale_toolbox/commands/plans_command/import_command.rb +5 -1
  11. data/lib/3scale_toolbox/crds/limit_dump.rb +1 -1
  12. data/lib/3scale_toolbox/crds/pricing_rule_dump.rb +1 -1
  13. data/lib/3scale_toolbox/entities/application_plan.rb +64 -0
  14. data/lib/3scale_toolbox/entities/backend.rb +4 -0
  15. data/lib/3scale_toolbox/entities/backend_method.rb +16 -0
  16. data/lib/3scale_toolbox/entities/backend_metric.rb +16 -0
  17. data/lib/3scale_toolbox/entities/limit.rb +52 -7
  18. data/lib/3scale_toolbox/entities/method.rb +11 -0
  19. data/lib/3scale_toolbox/entities/metric.rb +12 -0
  20. data/lib/3scale_toolbox/entities/pricing_rule.rb +52 -7
  21. data/lib/3scale_toolbox/entities/service.rb +4 -0
  22. data/lib/3scale_toolbox/remote_cache.rb +42 -1
  23. data/lib/3scale_toolbox/version.rb +1 -1
  24. data/licenses.xml +4 -4
  25. metadata +4 -10
  26. data/lib/3scale_toolbox/commands/plans_command/export/read_app_plan_step.rb +0 -16
  27. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_features_step.rb +0 -16
  28. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_limits_step.rb +0 -19
  29. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_methods_step.rb +0 -47
  30. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_metrics_step.rb +0 -47
  31. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_pricing_rules_step.rb +0 -19
  32. data/lib/3scale_toolbox/commands/plans_command/export/step.rb +0 -85
  33. data/lib/3scale_toolbox/commands/plans_command/export/write_artifacts_file_step.rb +0 -84
@@ -3,6 +3,8 @@ module ThreeScaleToolbox
3
3
  class Limit
4
4
  include CRD::Limit
5
5
 
6
+ LIMITS_BLACKLIST = %w[id metric_id plan_id links created_at updated_at].freeze
7
+
6
8
  class << self
7
9
  def create(plan:, metric_id:, attrs:)
8
10
  resp_attrs = plan.remote.create_application_plan_limit plan.id, metric_id, attrs
@@ -37,7 +39,7 @@ module ThreeScaleToolbox
37
39
  end
38
40
 
39
41
  def metric_link
40
- links.find { |link| link['rel'] == 'metric' }
42
+ links.find { |link| link['rel'] == 'metric' } || {}
41
43
  end
42
44
 
43
45
  def update(new_limit_attrs)
@@ -56,15 +58,58 @@ module ThreeScaleToolbox
56
58
  remote.delete_application_plan_limit plan.id, metric_id, id
57
59
  end
58
60
 
61
+ def product_metric
62
+ plan.service.metrics.find { |m| m.id == metric_id }
63
+ end
64
+
65
+ def product_method
66
+ plan.service.methods.find { |m| m.id == metric_id }
67
+ end
68
+
69
+ def backend_metric
70
+ if (backend = backend_from_metric_link)
71
+ return backend.metrics.find { |m| m.id == metric_id }
72
+ end
73
+ end
74
+
75
+ def backend_method
76
+ if (backend = backend_from_metric_link)
77
+ return backend.methods.find { |m| m.id == metric_id }
78
+ end
79
+ end
80
+
81
+ def to_hash
82
+ extra_attrs = {}
83
+
84
+ if (metric = product_metric)
85
+ extra_attrs['metric_system_name'] = metric.system_name
86
+ elsif (method = product_method)
87
+ extra_attrs['metric_system_name'] = method.system_name
88
+ elsif (metric = backend_metric)
89
+ extra_attrs['metric_system_name'] = metric.system_name
90
+ extra_attrs['metric_backend_system_name'] = metric.backend.system_name
91
+ elsif (method = backend_method)
92
+ extra_attrs['metric_system_name'] = method.system_name
93
+ extra_attrs['metric_backend_system_name'] = method.backend.system_name
94
+ else
95
+ raise_metric_not_found
96
+ end
97
+
98
+ attrs.merge(extra_attrs).reject { |key, _| LIMITS_BLACKLIST.include? key }
99
+ end
100
+
59
101
  private
60
102
 
61
- # Used by CRD::Limit
62
- # Returns the backend hosting the metric
63
- def backend_from_metric
64
- backend_id = Helper.backend_metric_link_parser(metric_link['href'] || '')
65
- return if backend_id.nil?
103
+ def raise_metric_not_found
104
+ raise ThreeScaleToolbox::Error, "Unexpected error. Limit #{id} " \
105
+ "referencing to metric id #{metric_id} which has not been found"
106
+ end
66
107
 
67
- Backend.new(id: backend_id.to_i, remote: remote)
108
+ # Returns the backend hosting the metric
109
+ def backend_from_metric_link
110
+ if (backend_id = Helper.backend_metric_link_parser(metric_link['href'] || ''))
111
+ return Backend.new(id: backend_id.to_i, remote: remote)
112
+ end
68
113
  end
69
114
  end
70
115
  end
@@ -3,6 +3,8 @@ module ThreeScaleToolbox
3
3
  class Method
4
4
  include CRD::MethodSerializer
5
5
 
6
+ METHOD_BLACKLIST = %w[id links created_at updated_at parent_id].freeze
7
+
6
8
  class << self
7
9
  def create(service:, attrs:)
8
10
  method_attrs = service.remote.create_method service.id, service.hits.id, attrs
@@ -74,6 +76,15 @@ module ThreeScaleToolbox
74
76
  remote.delete_method service.id, hits_id, id
75
77
  end
76
78
 
79
+ # enriched_key returns a metric key that will be unique for all
80
+ # metrics/methods from products and backends
81
+ def enriched_key
82
+ "product.#{service.id}.#{id}"
83
+ end
84
+
85
+ def to_hash
86
+ attrs.reject { |key, _| METHOD_BLACKLIST.include? key }
87
+ end
77
88
 
78
89
  private
79
90
 
@@ -3,6 +3,8 @@ module ThreeScaleToolbox
3
3
  class Metric
4
4
  include CRD::MetricSerializer
5
5
 
6
+ METRIC_BLACKLIST = %w[id links created_at updated_at].freeze
7
+
6
8
  class << self
7
9
  def create(service:, attrs:)
8
10
  metric = service.remote.create_metric service.id, attrs
@@ -92,6 +94,16 @@ module ThreeScaleToolbox
92
94
  remote.delete_metric service.id, id
93
95
  end
94
96
 
97
+ # enriched_key returns a metric key that will be unique for all
98
+ # metrics from products and backends
99
+ def enriched_key
100
+ "product.#{service.id}.#{id}"
101
+ end
102
+
103
+ def to_hash
104
+ attrs.reject { |key, _| METRIC_BLACKLIST.include? key }
105
+ end
106
+
95
107
  private
96
108
 
97
109
  def metric_attrs
@@ -3,6 +3,8 @@ module ThreeScaleToolbox
3
3
  class PricingRule
4
4
  include CRD::PricingRuleSerializer
5
5
 
6
+ PRICINGRULES_BLACKLIST = %w[id metric_id plan_id links created_at updated_at].freeze
7
+
6
8
  class << self
7
9
  def create(plan:, metric_id:, attrs:)
8
10
  resp_attrs = plan.remote.create_pricingrule plan.id, metric_id, attrs
@@ -41,22 +43,65 @@ module ThreeScaleToolbox
41
43
  end
42
44
 
43
45
  def metric_link
44
- links.find { |link| link['rel'] == 'metric' }
46
+ links.find { |link| link['rel'] == 'metric' } || {}
45
47
  end
46
48
 
47
49
  def delete
48
50
  remote.delete_application_plan_pricingrule plan.id, metric_id, id
49
51
  end
50
52
 
53
+ def product_metric
54
+ plan.service.metrics.find { |m| m.id == metric_id }
55
+ end
56
+
57
+ def product_method
58
+ plan.service.methods.find { |m| m.id == metric_id }
59
+ end
60
+
61
+ def backend_metric
62
+ if (backend = backend_from_metric_link)
63
+ return backend.metrics.find { |m| m.id == metric_id }
64
+ end
65
+ end
66
+
67
+ def backend_method
68
+ if (backend = backend_from_metric_link)
69
+ return backend.methods.find { |m| m.id == metric_id }
70
+ end
71
+ end
72
+
73
+ def to_hash
74
+ extra_attrs = {}
75
+
76
+ if (metric = product_metric)
77
+ extra_attrs['metric_system_name'] = metric.system_name
78
+ elsif (method = product_method)
79
+ extra_attrs['metric_system_name'] = method.system_name
80
+ elsif (metric = backend_metric)
81
+ extra_attrs['metric_system_name'] = metric.system_name
82
+ extra_attrs['metric_backend_system_name'] = metric.backend.system_name
83
+ elsif (method = backend_method)
84
+ extra_attrs['metric_system_name'] = method.system_name
85
+ extra_attrs['metric_backend_system_name'] = method.backend.system_name
86
+ else
87
+ raise_method_not_found
88
+ end
89
+
90
+ attrs.merge(extra_attrs).reject { |key, _| PRICINGRULES_BLACKLIST.include? key }
91
+ end
92
+
51
93
  private
52
94
 
53
- # Used by CRD::PricingRule
54
- # Returns the backend hosting the metric
55
- def backend_from_metric
56
- backend_id = Helper.backend_metric_link_parser(metric_link['href'] || '')
57
- return if backend_id.nil?
95
+ def raise_method_not_found
96
+ raise ThreeScaleToolbox::Error, "Unexpected error. Pricing Rule #{id} " \
97
+ "referencing to metric id #{metric_id} which has not been found"
98
+ end
58
99
 
59
- Backend.new(id: backend_id.to_i, remote: remote)
100
+ # Returns the backend hosting the metric
101
+ def backend_from_metric_link
102
+ if (backend_id = Helper.backend_metric_link_parser(metric_link['href'] || ''))
103
+ return Backend.new(id: backend_id.to_i, remote: remote)
104
+ end
60
105
  end
61
106
  end
62
107
  end
@@ -367,6 +367,10 @@ module ThreeScaleToolbox
367
367
  remote.http_client.endpoint == other.remote.http_client.endpoint && id == other.id
368
368
  end
369
369
 
370
+ def find_metric_or_method(system_name)
371
+ (metrics + methods).find { |m| m.system_name == system_name }
372
+ end
373
+
370
374
  private
371
375
 
372
376
  def fetch_attrs
@@ -1,7 +1,10 @@
1
1
  module ThreeScaleToolbox
2
2
  class RemoteCache < BasicObject
3
3
 
4
- attr_reader :metrics_cache, :methods_cache, :backend_metrics_cache, :backend_methods_cache, :subject
4
+ attr_reader :metrics_cache, :methods_cache,
5
+ :backend_metrics_cache, :backend_methods_cache,
6
+ :backends_cache,
7
+ :subject
5
8
 
6
9
  def initialize(subject)
7
10
  @subject = subject
@@ -13,6 +16,8 @@ module ThreeScaleToolbox
13
16
  @backend_metrics_cache = {}
14
17
  # Backend methods cache data
15
18
  @backend_methods_cache = {}
19
+ # Backends cache data
20
+ @backends_cache = {}
16
21
  end
17
22
 
18
23
  def list_metrics(service_id)
@@ -136,6 +141,42 @@ module ThreeScaleToolbox
136
141
  end
137
142
  end
138
143
 
144
+ def list_backends(params = nil)
145
+ return backends_cache[params] if backends_cache.has_key? params
146
+
147
+ subject.list_backends(params).tap do |backends|
148
+ backends_cache[params] = backends unless backends.respond_to?(:has_key?) && !backends['errors'].nil?
149
+ end
150
+ end
151
+
152
+ def create_backend(attributes)
153
+ subject.create_backend(attributes).tap do |backend_attrs|
154
+ backends_cache.clear unless backend_attrs.respond_to?(:has_key?) && !backend_attrs['errors'].nil?
155
+ end
156
+ end
157
+
158
+ def update_backend(id, attributes)
159
+ subject.update_backend(id, attributes).tap do |backend_attrs|
160
+ backends_cache.clear unless backend_attrs.respond_to?(:has_key?) && !backend_attrs['errors'].nil?
161
+ end
162
+ end
163
+
164
+ def delete_backend(id)
165
+ subject.delete_backend(id).tap do |resp|
166
+ backends_cache.clear unless resp.respond_to?(:has_key?) && !resp['errors'].nil?
167
+ end
168
+ end
169
+
170
+ def backend(id)
171
+ # if exist in the cache, return it.
172
+ # But if not, it is not populated because cache is paginated and page is unknown.
173
+ if (backend_attrs = backends_cache.values.reduce([], :concat).find { |attrs| attrs['id'] == id })
174
+ return backend_attrs
175
+ end
176
+
177
+ subject.backend(id)
178
+ end
179
+
139
180
  ###
140
181
  # Generic methods
141
182
  ###
@@ -1,3 +1,3 @@
1
1
  module ThreeScaleToolbox
2
- VERSION = '0.19.2'
2
+ VERSION = '0.19.3'
3
3
  end
data/licenses.xml CHANGED
@@ -13,7 +13,7 @@
13
13
  </dependency>
14
14
  <dependency>
15
15
  <packageName>3scale_toolbox</packageName>
16
- <version>0.19.2</version>
16
+ <version>0.19.3</version>
17
17
  <licenses>
18
18
  <license>
19
19
  <name>MIT</name>
@@ -261,7 +261,7 @@
261
261
  </dependency>
262
262
  <dependency>
263
263
  <packageName>nokogiri</packageName>
264
- <version>1.12.4</version>
264
+ <version>1.12.5</version>
265
265
  <licenses>
266
266
  <license>
267
267
  <name>MIT</name>
@@ -311,7 +311,7 @@
311
311
  </dependency>
312
312
  <dependency>
313
313
  <packageName>racc</packageName>
314
- <version>1.5.2</version>
314
+ <version>1.6.0</version>
315
315
  <licenses>
316
316
  <license>
317
317
  <name>ruby</name>
@@ -509,7 +509,7 @@
509
509
  </dependency>
510
510
  <dependency>
511
511
  <packageName>zeitwerk</packageName>
512
- <version>2.4.2</version>
512
+ <version>2.5.1</version>
513
513
  <licenses>
514
514
  <license>
515
515
  <name>MIT</name>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: 3scale_toolbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.2
4
+ version: 0.19.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Soriano
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2021-09-16 00:00:00.000000000 Z
12
+ date: 2021-10-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -218,21 +218,15 @@ files:
218
218
  - lib/3scale_toolbox/commands/plans_command/apply_command.rb
219
219
  - lib/3scale_toolbox/commands/plans_command/create_command.rb
220
220
  - lib/3scale_toolbox/commands/plans_command/delete_command.rb
221
- - lib/3scale_toolbox/commands/plans_command/export/read_app_plan_step.rb
222
- - lib/3scale_toolbox/commands/plans_command/export/read_plan_features_step.rb
223
- - lib/3scale_toolbox/commands/plans_command/export/read_plan_limits_step.rb
224
- - lib/3scale_toolbox/commands/plans_command/export/read_plan_methods_step.rb
225
- - lib/3scale_toolbox/commands/plans_command/export/read_plan_metrics_step.rb
226
- - lib/3scale_toolbox/commands/plans_command/export/read_plan_pricing_rules_step.rb
227
- - lib/3scale_toolbox/commands/plans_command/export/step.rb
228
- - lib/3scale_toolbox/commands/plans_command/export/write_artifacts_file_step.rb
229
221
  - lib/3scale_toolbox/commands/plans_command/export_command.rb
230
222
  - lib/3scale_toolbox/commands/plans_command/import/create_or_update_app_plan_step.rb
223
+ - lib/3scale_toolbox/commands/plans_command/import/import_backend_metrics_step.rb
231
224
  - lib/3scale_toolbox/commands/plans_command/import/import_plan_features_step.rb
232
225
  - lib/3scale_toolbox/commands/plans_command/import/import_plan_limits_step.rb
233
226
  - lib/3scale_toolbox/commands/plans_command/import/import_plan_metrics_step.rb
234
227
  - lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb
235
228
  - lib/3scale_toolbox/commands/plans_command/import/step.rb
229
+ - lib/3scale_toolbox/commands/plans_command/import/validate_plan_step.rb
236
230
  - lib/3scale_toolbox/commands/plans_command/import_command.rb
237
231
  - lib/3scale_toolbox/commands/plans_command/list_command.rb
238
232
  - lib/3scale_toolbox/commands/plans_command/show_command.rb
@@ -1,16 +0,0 @@
1
- module ThreeScaleToolbox
2
- module Commands
3
- module PlansCommand
4
- module Export
5
- class ReadAppPlanStep
6
- include Step
7
- ##
8
- # Reads Application plan
9
- def call
10
- result[:plan] = plan.attrs
11
- end
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,16 +0,0 @@
1
- module ThreeScaleToolbox
2
- module Commands
3
- module PlansCommand
4
- module Export
5
- class ReadPlanFeaturesStep
6
- include Step
7
- ##
8
- # Reads Application Plan features
9
- def call
10
- result[:plan_features] = plan.features
11
- end
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,19 +0,0 @@
1
- module ThreeScaleToolbox
2
- module Commands
3
- module PlansCommand
4
- module Export
5
- class ReadPlanLimitsStep
6
- include Step
7
- ##
8
- # Reads Application Plan limits
9
- # add metric system_name out of metric_id
10
- def call
11
- result[:limits] = plan.limits.map do |limit|
12
- limit.attrs.merge('metric' => metric_info(limit, 'Limit'))
13
- end
14
- end
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,47 +0,0 @@
1
- module ThreeScaleToolbox
2
- module Commands
3
- module PlansCommand
4
- module Export
5
- class ReadPlanMethods
6
- include Step
7
- ##
8
- # Compute unique list of methods from limits and pricingrules
9
- def call
10
- methods = [
11
- limit_methods,
12
- pricingrule_methods
13
- ]
14
- result[:plan_methods] = methods.each_with_object({}) { |elem, acc| acc.merge!(elem) }
15
- end
16
-
17
- private
18
-
19
- def limit_methods
20
- # multiple limits can reference the same method
21
- filtered_limit_methods.each_with_object({}) do |elem, acc|
22
- # find_method should not return nil.
23
- # It is assumed that metric_id refers to existing element from previous steps
24
- acc[elem['metric_id']] = find_method(elem['metric_id']).attrs
25
- end
26
- end
27
-
28
- def filtered_limit_methods
29
- result[:limits].select { |limit| limit.dig('metric', 'type') == 'method' }
30
- end
31
-
32
- def pricingrule_methods
33
- filtered_pricing_rule_methods.each_with_object({}) do |elem, acc|
34
- # find_method should not return nil.
35
- # It is assumed that metric_id refers to existing element from previous steps
36
- acc[elem['metric_id']] = find_method(elem['metric_id']).attrs
37
- end
38
- end
39
-
40
- def filtered_pricing_rule_methods
41
- result[:pricingrules].select { |limit| limit.dig('metric', 'type') == 'method' }
42
- end
43
- end
44
- end
45
- end
46
- end
47
- end
@@ -1,47 +0,0 @@
1
- module ThreeScaleToolbox
2
- module Commands
3
- module PlansCommand
4
- module Export
5
- class ReadPlanMetrics
6
- include Step
7
- ##
8
- # Compute unique list of metrics limits and pricingrules
9
- def call
10
- all_metrics = [
11
- limit_metrics,
12
- pricingrule_metrics
13
- ]
14
- result[:plan_metrics] = all_metrics.each_with_object({}) { |elem, acc| acc.merge!(elem) }
15
- end
16
-
17
- private
18
-
19
- def limit_metrics
20
- # multiple limits can reference the same metric
21
- filtered_limit_metrics.each_with_object({}) do |elem, acc|
22
- # find_metric should not return nil.
23
- # It is assumed that metric_id refers to existing element from previous steps
24
- acc[elem['metric_id']] = find_metric(elem['metric_id']).attrs
25
- end
26
- end
27
-
28
- def filtered_limit_metrics
29
- result[:limits].select { |limit| limit.dig('metric', 'type') == 'metric' }
30
- end
31
-
32
- def pricingrule_metrics
33
- filtered_pricing_rule_metrics.each_with_object({}) do |elem, acc|
34
- # find_metric should not return nil.
35
- # It is assumed that metric_id refers to existing element from previous steps
36
- acc[elem['metric_id']] = find_metric(elem['metric_id']).attrs
37
- end
38
- end
39
-
40
- def filtered_pricing_rule_metrics
41
- result[:pricingrules].select { |limit| limit.dig('metric', 'type') == 'metric' }
42
- end
43
- end
44
- end
45
- end
46
- end
47
- end
@@ -1,19 +0,0 @@
1
- module ThreeScaleToolbox
2
- module Commands
3
- module PlansCommand
4
- module Export
5
- class ReadPlanPricingRulesStep
6
- include Step
7
- ##
8
- # Reads Application Plan pricing rules
9
- # add metric system_name out of metric_id
10
- def call
11
- result[:pricingrules] = plan.pricing_rules.map do |pr|
12
- pr.attrs.merge('metric' => metric_info(pr, 'PricingRule'), 'cost_per_unit' => pr.cost_per_unit.to_f)
13
- end
14
- end
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,85 +0,0 @@
1
- module ThreeScaleToolbox
2
- module Commands
3
- module PlansCommand
4
- module Export
5
- APP_PLANS_BLACKLIST = %w[id links default custom created_at updated_at].freeze
6
- LIMITS_BLACKLIST = %w[id metric_id links created_at updated_at].freeze
7
- PRICINGRULES_BLACKLIST = %w[id metric_id links created_at updated_at].freeze
8
- PLAN_FEATURE_BLACKLIST = %w[id links created_at updated_at].freeze
9
- METRIC_BLACKLIST = %w[id links created_at updated_at].freeze
10
-
11
- module Step
12
- attr_reader :context
13
-
14
- def initialize(context)
15
- @context = context
16
- end
17
-
18
- def service
19
- context[:service] ||= find_service
20
- end
21
-
22
- def file
23
- context[:file]
24
- end
25
-
26
- def threescale_client
27
- context[:threescale_client]
28
- end
29
-
30
- # can be id or system_name
31
- def service_system_name
32
- context[:service_system_name]
33
- end
34
-
35
- # can be id or system_name
36
- def plan_system_name
37
- context[:plan_system_name]
38
- end
39
-
40
- def result
41
- context[:result] ||= {}
42
- end
43
-
44
- def plan
45
- context[:plan] ||= find_plan
46
- end
47
-
48
- def metric_info(elem, elem_name)
49
- if (method = find_method(elem.metric_id))
50
- { 'type' => 'method', 'system_name' => method.system_name }
51
- elsif (metric = find_metric(elem.metric_id))
52
- { 'type' => 'metric', 'system_name' => metric.system_name }
53
- else
54
- raise ThreeScaleToolbox::Error, "Unexpected error. #{elem_name} #{elem.id} " \
55
- "referencing to metric id #{elem.metric_id} which has not been found"
56
- end
57
- end
58
-
59
- private
60
-
61
- def find_service
62
- Entities::Service.find(remote: threescale_client,
63
- ref: service_system_name).tap do |svc|
64
- raise ThreeScaleToolbox::Error, "Service #{service_system_name} does not exist" if svc.nil?
65
- end
66
- end
67
-
68
- def find_plan
69
- Entities::ApplicationPlan.find(service: service, ref: plan_system_name).tap do |p|
70
- raise ThreeScaleToolbox::Error, "Application plan #{plan_system_name} does not exist" if p.nil?
71
- end
72
- end
73
-
74
- def find_metric(id)
75
- service.metrics.find { |metric| metric.id == id }
76
- end
77
-
78
- def find_method(id)
79
- service.methods.find { |method| method.id == id }
80
- end
81
- end
82
- end
83
- end
84
- end
85
- end