3scale_toolbox 0.15.0 → 0.18.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. checksums.yaml +4 -4
  2. data/3scale_toolbox.gemspec +2 -2
  3. data/README.md +15 -9
  4. data/lib/3scale_toolbox.rb +3 -1
  5. data/lib/3scale_toolbox/3scale_client_factory.rb +3 -4
  6. data/lib/3scale_toolbox/cli.rb +4 -0
  7. data/lib/3scale_toolbox/cli/custom_table_printer.rb +32 -0
  8. data/lib/3scale_toolbox/cli/error_handler.rb +17 -14
  9. data/lib/3scale_toolbox/cli/json_printer.rb +13 -0
  10. data/lib/3scale_toolbox/cli/output_flag.rb +20 -0
  11. data/lib/3scale_toolbox/cli/yaml_printer.rb +13 -0
  12. data/lib/3scale_toolbox/commands.rb +7 -1
  13. data/lib/3scale_toolbox/commands/activedocs_command/apply_command.rb +33 -10
  14. data/lib/3scale_toolbox/commands/activedocs_command/create_command.rb +22 -7
  15. data/lib/3scale_toolbox/commands/activedocs_command/list_command.rb +10 -17
  16. data/lib/3scale_toolbox/commands/application_command/apply_command.rb +27 -4
  17. data/lib/3scale_toolbox/commands/application_command/create_command.rb +16 -1
  18. data/lib/3scale_toolbox/commands/application_command/list_command.rb +10 -13
  19. data/lib/3scale_toolbox/commands/application_command/show_command.rb +8 -14
  20. data/lib/3scale_toolbox/commands/backend_command.rb +22 -0
  21. data/lib/3scale_toolbox/commands/backend_command/copy_command.rb +65 -0
  22. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_mapping_rules_task.rb +36 -0
  23. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_methods_task.rb +35 -0
  24. data/lib/3scale_toolbox/commands/backend_command/copy_command/copy_metrics_task.rb +30 -0
  25. data/lib/3scale_toolbox/commands/backend_command/copy_command/create_or_update_target_backend_task.rb +46 -0
  26. data/lib/3scale_toolbox/commands/backend_command/copy_command/task.rb +67 -0
  27. data/lib/3scale_toolbox/commands/copy_command.rb +2 -2
  28. data/lib/3scale_toolbox/commands/copy_command/service_command.rb +40 -0
  29. data/lib/3scale_toolbox/commands/import_command/issuer_type_transformer.rb +16 -0
  30. data/lib/3scale_toolbox/commands/import_command/openapi.rb +6 -2
  31. data/lib/3scale_toolbox/commands/import_command/openapi/create_mapping_rule_step.rb +2 -1
  32. data/lib/3scale_toolbox/commands/import_command/openapi/create_method_step.rb +5 -14
  33. data/lib/3scale_toolbox/commands/import_command/openapi/step.rb +4 -0
  34. data/lib/3scale_toolbox/commands/import_command/openapi/update_service_proxy_step.rb +1 -0
  35. data/lib/3scale_toolbox/commands/methods_command/apply_command.rb +28 -8
  36. data/lib/3scale_toolbox/commands/methods_command/create_command.rb +23 -3
  37. data/lib/3scale_toolbox/commands/methods_command/delete_command.rb +1 -1
  38. data/lib/3scale_toolbox/commands/methods_command/list_command.rb +7 -13
  39. data/lib/3scale_toolbox/commands/metrics_command/apply_command.rb +26 -4
  40. data/lib/3scale_toolbox/commands/metrics_command/create_command.rb +23 -1
  41. data/lib/3scale_toolbox/commands/metrics_command/list_command.rb +7 -12
  42. data/lib/3scale_toolbox/commands/plans_command/apply_command.rb +36 -7
  43. data/lib/3scale_toolbox/commands/plans_command/create_command.rb +23 -1
  44. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_limits_step.rb +1 -1
  45. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_methods_step.rb +2 -2
  46. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_metrics_step.rb +2 -2
  47. data/lib/3scale_toolbox/commands/plans_command/export/read_plan_pricing_rules_step.rb +1 -2
  48. data/lib/3scale_toolbox/commands/plans_command/export/step.rb +8 -20
  49. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_limits_step.rb +12 -14
  50. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_metrics_step.rb +6 -13
  51. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_pricing_rules_step.rb +12 -20
  52. data/lib/3scale_toolbox/commands/plans_command/import/step.rb +2 -22
  53. data/lib/3scale_toolbox/commands/plans_command/list_command.rb +8 -13
  54. data/lib/3scale_toolbox/commands/plans_command/show_command.rb +7 -15
  55. data/lib/3scale_toolbox/commands/policies_command.rb +24 -0
  56. data/lib/3scale_toolbox/commands/policies_command/export_command.rb +98 -0
  57. data/lib/3scale_toolbox/commands/policies_command/import_command.rb +61 -0
  58. data/lib/3scale_toolbox/commands/product_command.rb +26 -0
  59. data/lib/3scale_toolbox/commands/product_command/copy_command.rb +82 -0
  60. data/lib/3scale_toolbox/commands/product_command/copy_command/copy_backends_task.rb +88 -0
  61. data/lib/3scale_toolbox/commands/product_command/copy_command/delete_target_backend_usages_task.rb +48 -0
  62. data/lib/3scale_toolbox/commands/product_command/export_command.rb +81 -0
  63. data/lib/3scale_toolbox/commands/product_command/import_command.rb +125 -0
  64. data/lib/3scale_toolbox/commands/proxy_config_command.rb +5 -0
  65. data/lib/3scale_toolbox/commands/proxy_config_command/deploy_command.rb +54 -0
  66. data/lib/3scale_toolbox/commands/proxy_config_command/export_command.rb +74 -0
  67. data/lib/3scale_toolbox/commands/proxy_config_command/helper.rb +15 -0
  68. data/lib/3scale_toolbox/commands/proxy_config_command/list_command.rb +13 -29
  69. data/lib/3scale_toolbox/commands/proxy_config_command/show_command.rb +20 -23
  70. data/lib/3scale_toolbox/commands/service_command.rb +7 -5
  71. data/lib/3scale_toolbox/commands/service_command/apply_command.rb +69 -58
  72. data/lib/3scale_toolbox/commands/service_command/copy_command.rb +95 -0
  73. data/lib/3scale_toolbox/commands/service_command/copy_command/bump_proxy_version_task.rb +36 -0
  74. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_activedocs_task.rb +49 -0
  75. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_app_plans_task.rb +35 -0
  76. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_limits_task.rb +38 -0
  77. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_mapping_rules_task.rb +35 -0
  78. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_methods_task.rb +37 -0
  79. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_metrics_task.rb +37 -0
  80. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_policies_task.rb +17 -0
  81. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_pricingrules_task.rb +41 -0
  82. data/lib/3scale_toolbox/commands/service_command/copy_command/copy_service_proxy_task.rb +32 -0
  83. data/lib/3scale_toolbox/commands/service_command/copy_command/create_or_update_service_task.rb +49 -0
  84. data/lib/3scale_toolbox/commands/service_command/copy_command/destroy_mapping_rules_task.rb +38 -0
  85. data/lib/3scale_toolbox/commands/service_command/copy_command/task.rb +85 -0
  86. data/lib/3scale_toolbox/commands/service_command/create_command.rb +58 -44
  87. data/lib/3scale_toolbox/commands/service_command/delete_command.rb +31 -33
  88. data/lib/3scale_toolbox/commands/service_command/list_command.rb +24 -34
  89. data/lib/3scale_toolbox/commands/service_command/show_command.rb +39 -44
  90. data/lib/3scale_toolbox/commands/update_command.rb +3 -3
  91. data/lib/3scale_toolbox/commands/update_command/{update_service.rb → service_command.rb} +22 -18
  92. data/lib/3scale_toolbox/commands/update_command/service_command/copy_service_settings_task.rb +35 -0
  93. data/lib/3scale_toolbox/commands/update_command/service_command/delete_activedocs_task.rb +24 -0
  94. data/lib/3scale_toolbox/crds.rb +16 -0
  95. data/lib/3scale_toolbox/crds/application_plan_dump.rb +19 -0
  96. data/lib/3scale_toolbox/crds/backend_dump.rb +39 -0
  97. data/lib/3scale_toolbox/crds/backend_mapping_rule_dump.rb +26 -0
  98. data/lib/3scale_toolbox/crds/backend_method_dump.rb +12 -0
  99. data/lib/3scale_toolbox/crds/backend_metric_dump.rb +13 -0
  100. data/lib/3scale_toolbox/crds/backend_parser.rb +55 -0
  101. data/lib/3scale_toolbox/crds/backend_usage_dump.rb +11 -0
  102. data/lib/3scale_toolbox/crds/limit_dump.rb +37 -0
  103. data/lib/3scale_toolbox/crds/mapping_rule_dump.rb +26 -0
  104. data/lib/3scale_toolbox/crds/method_dump.rb +12 -0
  105. data/lib/3scale_toolbox/crds/metric_dump.rb +13 -0
  106. data/lib/3scale_toolbox/crds/pricing_rule_dump.rb +38 -0
  107. data/lib/3scale_toolbox/crds/product_deployment_parser.rb +329 -0
  108. data/lib/3scale_toolbox/crds/product_dump.rb +157 -0
  109. data/lib/3scale_toolbox/crds/product_parser.rb +114 -0
  110. data/lib/3scale_toolbox/crds/remote.rb +682 -0
  111. data/lib/3scale_toolbox/entities.rb +8 -0
  112. data/lib/3scale_toolbox/entities/activedocs.rb +12 -0
  113. data/lib/3scale_toolbox/entities/application_plan.rb +74 -39
  114. data/lib/3scale_toolbox/entities/backend.rb +187 -0
  115. data/lib/3scale_toolbox/entities/backend_mapping_rule.rb +102 -0
  116. data/lib/3scale_toolbox/entities/backend_method.rb +99 -0
  117. data/lib/3scale_toolbox/entities/backend_metric.rb +98 -0
  118. data/lib/3scale_toolbox/entities/backend_usage.rb +105 -0
  119. data/lib/3scale_toolbox/entities/limit.rb +71 -0
  120. data/lib/3scale_toolbox/entities/mapping_rule.rb +90 -0
  121. data/lib/3scale_toolbox/entities/method.rb +33 -19
  122. data/lib/3scale_toolbox/entities/metric.rb +29 -18
  123. data/lib/3scale_toolbox/entities/pricing_rule.rb +63 -0
  124. data/lib/3scale_toolbox/entities/proxy_config.rb +0 -1
  125. data/lib/3scale_toolbox/entities/service.rb +166 -48
  126. data/lib/3scale_toolbox/error.rb +53 -0
  127. data/lib/3scale_toolbox/helper.rb +17 -0
  128. data/lib/3scale_toolbox/openapi/oas3.rb +1 -1
  129. data/lib/3scale_toolbox/proxy_logger.rb +5 -1
  130. data/lib/3scale_toolbox/remote_cache.rb +157 -0
  131. data/lib/3scale_toolbox/remotes.rb +2 -2
  132. data/lib/3scale_toolbox/version.rb +1 -1
  133. data/licenses.xml +113 -45
  134. metadata +75 -26
  135. data/lib/3scale_toolbox/commands/copy_command/copy_service.rb +0 -144
  136. data/lib/3scale_toolbox/tasks.rb +0 -15
  137. data/lib/3scale_toolbox/tasks/bump_proxy_version_task.rb +0 -32
  138. data/lib/3scale_toolbox/tasks/copy_activedocs_task.rb +0 -42
  139. data/lib/3scale_toolbox/tasks/copy_app_plans_task.rb +0 -31
  140. data/lib/3scale_toolbox/tasks/copy_limits_task.rb +0 -36
  141. data/lib/3scale_toolbox/tasks/copy_mapping_rules_task.rb +0 -32
  142. data/lib/3scale_toolbox/tasks/copy_methods_task.rb +0 -36
  143. data/lib/3scale_toolbox/tasks/copy_metrics_task.rb +0 -33
  144. data/lib/3scale_toolbox/tasks/copy_policies_task.rb +0 -13
  145. data/lib/3scale_toolbox/tasks/copy_pricingrules_task.rb +0 -41
  146. data/lib/3scale_toolbox/tasks/copy_service_proxy_task.rb +0 -13
  147. data/lib/3scale_toolbox/tasks/copy_service_settings_task.rb +0 -38
  148. data/lib/3scale_toolbox/tasks/copy_task.rb +0 -66
  149. data/lib/3scale_toolbox/tasks/delete_activedocs_task.rb +0 -22
  150. data/lib/3scale_toolbox/tasks/destroy_mapping_rules_task.rb +0 -22
  151. data/lib/3scale_toolbox/tasks/helper_task.rb +0 -25
@@ -0,0 +1,99 @@
1
+ module ThreeScaleToolbox
2
+ module Entities
3
+ class BackendMethod
4
+ include CRD::BackendMethodSerializer
5
+
6
+ VALID_PARAMS = %w[friendly_name system_name description].freeze
7
+ public_constant :VALID_PARAMS
8
+
9
+ class << self
10
+ def create(backend:, attrs:)
11
+ method = backend.remote.create_backend_method(backend.id, backend.hits.id,
12
+ Helper.filter_params(VALID_PARAMS, attrs))
13
+ if (errors = method['errors'])
14
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend Method has not been created',
15
+ errors)
16
+ end
17
+
18
+ new(id: method.fetch('id'), backend: backend, attrs: method)
19
+ end
20
+
21
+ # ref can be system_name or method_id
22
+ def find(backend:, ref:)
23
+ new(id: ref, backend: backend).tap(&:attrs)
24
+ rescue ThreeScaleToolbox::InvalidIdError, ThreeScale::API::HttpClient::NotFoundError
25
+ find_by_system_name(backend: backend, system_name: ref)
26
+ end
27
+
28
+ def find_by_system_name(backend:, system_name:)
29
+ backend.methods.find { |m| m.system_name == system_name }
30
+ end
31
+ end
32
+
33
+ attr_reader :id, :backend, :remote
34
+
35
+ def initialize(id:, backend:, attrs: nil)
36
+ @id = id.to_i
37
+ @backend = backend
38
+ @remote = backend.remote
39
+ @attrs = process_attrs(attrs)
40
+ end
41
+
42
+ def attrs
43
+ @attrs ||= process_attrs(method_attrs)
44
+ end
45
+
46
+ def system_name
47
+ attrs['system_name']
48
+ end
49
+
50
+ def friendly_name
51
+ attrs['friendly_name']
52
+ end
53
+
54
+ def description
55
+ attrs['description']
56
+ end
57
+
58
+ def update(m_attrs)
59
+ new_attrs = remote.update_backend_method(backend.id, hits_id, id,
60
+ Helper.filter_params(VALID_PARAMS, m_attrs))
61
+ if (errors = new_attrs['errors'])
62
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend Method has not been updated',
63
+ errors)
64
+ end
65
+
66
+ # update current attrs
67
+ @attrs = process_attrs(new_attrs)
68
+ end
69
+
70
+ def delete
71
+ remote.delete_backend_method backend.id, hits_id, id
72
+ end
73
+
74
+ private
75
+
76
+ def hits_id
77
+ backend.hits.id
78
+ end
79
+
80
+ def process_attrs(metric_attrs)
81
+ return if metric_attrs.nil?
82
+
83
+ # system_name: my_metric_02.45498 -> system_name: my_metric_02
84
+ metric_attrs.merge('system_name' => metric_attrs.fetch('system_name', '').partition('.').first)
85
+ end
86
+
87
+ def method_attrs
88
+ raise ThreeScaleToolbox::InvalidIdError if id.zero?
89
+
90
+ method = remote.backend_method backend.id, hits_id, id
91
+ if (errors = method['errors'])
92
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend method not read', errors)
93
+ end
94
+
95
+ method
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,98 @@
1
+ module ThreeScaleToolbox
2
+ module Entities
3
+ class BackendMetric
4
+ include CRD::BackendMetricSerializer
5
+
6
+ VALID_PARAMS = %w[friendly_name system_name unit description].freeze
7
+ public_constant :VALID_PARAMS
8
+
9
+ class << self
10
+ def create(backend:, attrs:)
11
+ metric = backend.remote.create_backend_metric(backend.id,
12
+ Helper.filter_params(VALID_PARAMS, attrs))
13
+ if (errors = metric['errors'])
14
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend metric has not been created',
15
+ errors)
16
+ end
17
+
18
+ new(id: metric.fetch('id'), backend: backend, attrs: metric)
19
+ end
20
+
21
+ # ref can be system_name or metric_id
22
+ def find(backend:, ref:)
23
+ new(id: ref, backend: backend).tap(&:attrs)
24
+ rescue ThreeScaleToolbox::InvalidIdError, ThreeScale::API::HttpClient::NotFoundError
25
+ find_by_system_name(backend: backend, system_name: ref)
26
+ end
27
+
28
+ def find_by_system_name(backend:, system_name:)
29
+ backend.metrics.find { |m| m.system_name == system_name }
30
+ end
31
+ end
32
+
33
+ attr_reader :id, :backend, :remote
34
+
35
+ def initialize(id:, backend:, attrs: nil)
36
+ @id = id.to_i
37
+ @backend = backend
38
+ @remote = backend.remote
39
+ @attrs = process_attrs(attrs)
40
+ end
41
+
42
+ def attrs
43
+ @attrs ||= process_attrs(metric_attrs)
44
+ end
45
+
46
+ def system_name
47
+ attrs['system_name']
48
+ end
49
+
50
+ def friendly_name
51
+ attrs['friendly_name']
52
+ end
53
+
54
+ def unit
55
+ attrs['unit']
56
+ end
57
+
58
+ def description
59
+ attrs['description']
60
+ end
61
+
62
+ def update(m_attrs)
63
+ new_attrs = remote.update_backend_metric(backend.id, id,
64
+ Helper.filter_params(VALID_PARAMS, m_attrs))
65
+ if (errors = new_attrs['errors'])
66
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend metric has not been updated', errors)
67
+ end
68
+
69
+ # update current attrs
70
+ @attrs = process_attrs(new_attrs)
71
+ end
72
+
73
+ def delete
74
+ remote.delete_backend_metric backend.id, id
75
+ end
76
+
77
+ private
78
+
79
+ def process_attrs(metric_attrs)
80
+ return if metric_attrs.nil?
81
+
82
+ # system_name: my_metric_02.45498 -> system_name: my_metric_02
83
+ metric_attrs.merge('system_name' => metric_attrs.fetch('system_name', '').partition('.').first)
84
+ end
85
+
86
+ def metric_attrs
87
+ raise ThreeScaleToolbox::InvalidIdError if id.zero?
88
+
89
+ metric = remote.backend_metric backend.id, id
90
+ if (errors = metric['errors'])
91
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend metric not read', errors)
92
+ end
93
+
94
+ metric
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,105 @@
1
+ module ThreeScaleToolbox
2
+ module Entities
3
+ ##
4
+ # BackendUsage represents Product - Backend mapping entry
5
+ class BackendUsage
6
+ include CRD::BackendUsageSerializer
7
+
8
+ CREATE_PARAMS = %w[path backend_api_id].freeze
9
+ public_constant :CREATE_PARAMS
10
+ UPDATE_PARAMS = %w[path].freeze
11
+ public_constant :UPDATE_PARAMS
12
+
13
+ class << self
14
+ def create(product:, attrs:)
15
+ resp = product.remote.create_backend_usage(
16
+ product.id,
17
+ Helper.filter_params(CREATE_PARAMS, attrs)
18
+ )
19
+ if (errors = resp['errors'])
20
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend usage has not been created',
21
+ errors)
22
+ end
23
+
24
+ new(id: resp.fetch('id'), product: product, attrs: resp)
25
+ end
26
+
27
+ def find_by_path(product:, path:)
28
+ resp = product.remote.list_backend_usages product.id
29
+ if resp.respond_to?(:has_key?) && (errors = resp['errors'])
30
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend usage list error', errors)
31
+ end
32
+
33
+ backend_usage_attrs = resp.find { |bus| bus['path'] == path }
34
+ return if backend_usage_attrs.nil?
35
+
36
+ new(id: backend_usage_attrs.fetch('id'), product: product, attrs: backend_usage_attrs)
37
+ end
38
+ end
39
+
40
+ attr_reader :id, :product, :remote
41
+
42
+ def initialize(id:, product:, attrs: nil)
43
+ @id = id.to_i
44
+ @product = product
45
+ @remote = product.remote
46
+ @attrs = attrs
47
+ end
48
+
49
+ def attrs
50
+ @attrs ||= fetch_attrs
51
+ end
52
+
53
+ def path
54
+ attrs['path']
55
+ end
56
+
57
+ def backend_id
58
+ # 3scale API returns 'backend_id'
59
+ # 3scale API only accepts 'backend_api_id' as params on create endpoint
60
+ # good job
61
+ attrs['backend_id']
62
+ end
63
+
64
+ def update(usage_attrs)
65
+ new_attrs = remote.update_backend_usage(
66
+ product.id, id,
67
+ Helper.filter_params(UPDATE_PARAMS, usage_attrs)
68
+ )
69
+ if (errors = new_attrs['errors'])
70
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend usage not been updated', errors)
71
+ end
72
+
73
+ if new_attrs['service_id'] != product.id
74
+ raise ThreeScaleToolbox::Error, 'Backend usage product updated'
75
+ end
76
+
77
+ # update current attrs
78
+ @attrs = new_attrs
79
+
80
+ new_attrs
81
+ end
82
+
83
+ def delete
84
+ remote.delete_backend_usage product.id, id
85
+ end
86
+
87
+ def backend
88
+ Backend.new(id: backend_id, remote: remote)
89
+ end
90
+
91
+ private
92
+
93
+ def fetch_attrs
94
+ raise ThreeScaleToolbox::InvalidIdError if id.zero?
95
+
96
+ resp = remote.backend_usage product.id, id
97
+ if (errors = resp['errors'])
98
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Product backend usage not read', errors)
99
+ end
100
+
101
+ resp
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,71 @@
1
+ module ThreeScaleToolbox
2
+ module Entities
3
+ class Limit
4
+ include CRD::Limit
5
+
6
+ class << self
7
+ def create(plan:, metric_id:, attrs:)
8
+ resp_attrs = plan.remote.create_application_plan_limit plan.id, metric_id, attrs
9
+ if (errors = resp_attrs['errors'])
10
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Limit has not been created', errors)
11
+ end
12
+
13
+ new(id: resp_attrs.fetch('id'), plan: plan, metric_id: metric_id, attrs: resp_attrs)
14
+ end
15
+ end
16
+
17
+ attr_reader :id, :plan, :remote, :attrs, :metric_id
18
+
19
+ def initialize(id:, plan:, metric_id:, attrs:)
20
+ @id = id.to_i
21
+ @plan = plan
22
+ @remote = plan.remote
23
+ @metric_id = metric_id
24
+ @attrs = attrs
25
+ end
26
+
27
+ def period
28
+ attrs['period']
29
+ end
30
+
31
+ def value
32
+ attrs['value']
33
+ end
34
+
35
+ def links
36
+ attrs['links'] || []
37
+ end
38
+
39
+ def metric_link
40
+ links.find { |link| link['rel'] == 'metric' }
41
+ end
42
+
43
+ def update(new_limit_attrs)
44
+ new_attrs = remote.update_application_plan_limit(plan.id, metric_id, id, new_limit_attrs)
45
+ if (errors = new_attrs['errors'])
46
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Limit has not been updated', errors)
47
+ end
48
+
49
+ # update current attrs
50
+ @attrs = new_attrs
51
+
52
+ new_attrs
53
+ end
54
+
55
+ def delete
56
+ remote.delete_application_plan_limit plan.id, metric_id, id
57
+ end
58
+
59
+ private
60
+
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?
66
+
67
+ Backend.new(id: backend_id.to_i, remote: remote)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,90 @@
1
+ module ThreeScaleToolbox
2
+ module Entities
3
+ class MappingRule
4
+ include CRD::MappingRuleSerializer
5
+
6
+ VALID_PARAMS = %w[metric_id pattern http_method delta position last].freeze
7
+ public_constant :VALID_PARAMS
8
+
9
+ class << self
10
+ def create(service:, attrs:)
11
+ mapping_rule = service.remote.create_mapping_rule(
12
+ service.id,
13
+ Helper.filter_params(VALID_PARAMS, attrs)
14
+ )
15
+ if (errors = mapping_rule['errors'])
16
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('mapping rule has not been created',
17
+ errors)
18
+ end
19
+
20
+ new(id: mapping_rule.fetch('id'), service: service, attrs: mapping_rule)
21
+ end
22
+ end
23
+
24
+ attr_reader :id, :service, :remote
25
+
26
+ def initialize(id:, service:, attrs: nil)
27
+ @id = id.to_i
28
+ @service = service
29
+ @remote = service.remote
30
+ @attrs = attrs
31
+ end
32
+
33
+ def attrs
34
+ @attrs ||= mapping_rule_attrs
35
+ end
36
+
37
+ def http_method
38
+ attrs['http_method']
39
+ end
40
+
41
+ def pattern
42
+ attrs['pattern']
43
+ end
44
+
45
+ def delta
46
+ attrs['delta']
47
+ end
48
+
49
+ def last
50
+ attrs['last']
51
+ end
52
+
53
+ def metric_id
54
+ attrs['metric_id']
55
+ end
56
+
57
+ def update(mr_attrs)
58
+ new_attrs = remote.update_mapping_rule(
59
+ service.id, id,
60
+ Helper.filter_params(VALID_PARAMS, mr_attrs)
61
+ )
62
+ if (errors = new_attrs['errors'])
63
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service mapping rule has not been updated', errors)
64
+ end
65
+
66
+ # update current attrs
67
+ @attrs = new_attrs
68
+
69
+ new_attrs
70
+ end
71
+
72
+ def delete
73
+ remote.delete_mapping_rule service.id, id
74
+ end
75
+
76
+ private
77
+
78
+ def mapping_rule_attrs
79
+ raise ThreeScaleToolbox::InvalidIdError if id.zero?
80
+
81
+ mapping_rule = remote.show_mapping_rule service.id, id
82
+ if (errors = mapping_rule['errors'])
83
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service mapping rule not read', errors)
84
+ end
85
+
86
+ mapping_rule
87
+ end
88
+ end
89
+ end
90
+ end