3scale_toolbox 0.9.0 → 0.10.0

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 (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -23
  3. data/lib/3scale_toolbox/base_command.rb +0 -1
  4. data/lib/3scale_toolbox/commands.rb +4 -0
  5. data/lib/3scale_toolbox/commands/copy_command/copy_service.rb +1 -1
  6. data/lib/3scale_toolbox/commands/import_command/openapi/create_activedocs_step.rb +2 -2
  7. data/lib/3scale_toolbox/commands/import_command/openapi/create_method_step.rb +21 -15
  8. data/lib/3scale_toolbox/commands/import_command/openapi/create_service_step.rb +10 -13
  9. data/lib/3scale_toolbox/commands/methods_command.rb +28 -0
  10. data/lib/3scale_toolbox/commands/methods_command/apply_command.rb +101 -0
  11. data/lib/3scale_toolbox/commands/methods_command/create_command.rb +73 -0
  12. data/lib/3scale_toolbox/commands/methods_command/delete_command.rb +67 -0
  13. data/lib/3scale_toolbox/commands/methods_command/list_command.rb +64 -0
  14. data/lib/3scale_toolbox/commands/metrics_command.rb +28 -0
  15. data/lib/3scale_toolbox/commands/metrics_command/apply_command.rb +102 -0
  16. data/lib/3scale_toolbox/commands/metrics_command/create_command.rb +77 -0
  17. data/lib/3scale_toolbox/commands/metrics_command/delete_command.rb +66 -0
  18. data/lib/3scale_toolbox/commands/metrics_command/list_command.rb +70 -0
  19. data/lib/3scale_toolbox/commands/plans_command/create_command.rb +1 -1
  20. data/lib/3scale_toolbox/commands/plans_command/export/read_app_plan_step.rb +1 -1
  21. data/lib/3scale_toolbox/commands/plans_command/export/step.rb +5 -1
  22. data/lib/3scale_toolbox/commands/plans_command/import/import_plan_metrics_step.rb +7 -13
  23. data/lib/3scale_toolbox/commands/plans_command/import/step.rb +2 -8
  24. data/lib/3scale_toolbox/commands/plans_command/show_command.rb +1 -1
  25. data/lib/3scale_toolbox/commands/remote_command/remote_add.rb +1 -1
  26. data/lib/3scale_toolbox/entities.rb +2 -0
  27. data/lib/3scale_toolbox/entities/application_plan.rb +47 -14
  28. data/lib/3scale_toolbox/entities/method.rb +80 -0
  29. data/lib/3scale_toolbox/entities/metric.rb +113 -0
  30. data/lib/3scale_toolbox/entities/service.rb +112 -38
  31. data/lib/3scale_toolbox/error.rb +13 -0
  32. data/lib/3scale_toolbox/tasks/copy_activedocs_task.rb +1 -1
  33. data/lib/3scale_toolbox/tasks/copy_methods_task.rb +11 -8
  34. data/lib/3scale_toolbox/tasks/copy_metrics_task.rb +1 -1
  35. data/lib/3scale_toolbox/tasks/copy_service_proxy_task.rb +1 -1
  36. data/lib/3scale_toolbox/tasks/delete_activedocs_task.rb +1 -1
  37. data/lib/3scale_toolbox/tasks/update_service_settings_task.rb +3 -3
  38. data/lib/3scale_toolbox/version.rb +1 -1
  39. metadata +16 -4
@@ -33,22 +33,16 @@ module ThreeScaleToolbox
33
33
  end
34
34
 
35
35
  def create_metric(metric_attrs)
36
- metric = service.create_metric(metric_attrs)
37
- if (errors = metric['errors'])
38
- raise ThreeScaleToolbox::Error, "Metric has not been created. #{errors}"
39
- end
40
-
41
- puts "Created metric: #{metric['system_name']}"
36
+ metric = ThreeScaleToolbox::Entities::Metric.create(service: service,
37
+ attrs: metric_attrs)
38
+ puts "Created metric: #{metric.attrs['system_name']}"
42
39
  end
43
40
 
44
41
  def create_method(method_attrs)
45
- method = service.create_method(service_hits['id'], method_attrs)
46
- if (errors = method['errors'])
47
- raise ThreeScaleToolbox::Error, "Method has not been created. #{errors}" \
48
-
49
- end
50
-
51
- puts "Created method: #{method_attrs['system_name']}"
42
+ method = ThreeScaleToolbox::Entities::Method.create(service: service,
43
+ parent_id: service_hits['id'],
44
+ attrs: method_attrs)
45
+ puts "Created method: #{method.attrs['system_name']}"
52
46
  end
53
47
  end
54
48
  end
@@ -74,11 +74,11 @@ module ThreeScaleToolbox
74
74
  end
75
75
 
76
76
  def service_hits
77
- context[:service_hits] ||= find_service_hits
77
+ context[:service_hits] ||= service.hits
78
78
  end
79
79
 
80
80
  def service_methods
81
- context[:service_methods] ||= service.methods
81
+ context[:service_methods] ||= service.methods(service_hits['id'])
82
82
  end
83
83
 
84
84
  def invalidate_service_methods
@@ -116,12 +116,6 @@ module ThreeScaleToolbox
116
116
  raise ThreeScaleToolbox::Error, "Application plan #{plan_system_name} does not exist" if p.nil?
117
117
  end
118
118
  end
119
-
120
- def find_service_hits
121
- find_metric_by_system_name('hits').tap do |hits_metric|
122
- raise ThreeScaleToolbox::Error, 'missing hits metric' if hits_metric.nil?
123
- end
124
- end
125
119
  end
126
120
  end
127
121
  end
@@ -43,7 +43,7 @@ module ThreeScaleToolbox
43
43
  end
44
44
 
45
45
  def plan_attrs
46
- @plan_attrs ||= plan.show
46
+ @plan_attrs ||= plan.attrs
47
47
  end
48
48
 
49
49
  def plan
@@ -31,7 +31,7 @@ module ThreeScaleToolbox
31
31
  # parsing url before trying to create client
32
32
  # raises Invalid URL when syntax is incorrect
33
33
  ThreeScaleToolbox::Helper.parse_uri(remote_url_str)
34
- threescale_client(remote_url_str).list_services
34
+ threescale_client(remote_url_str).list_accounts
35
35
  rescue ThreeScale::API::HttpClient::ForbiddenError
36
36
  raise ThreeScaleToolbox::Error, 'remote not valid'
37
37
  end
@@ -1,2 +1,4 @@
1
1
  require '3scale_toolbox/entities/service'
2
2
  require '3scale_toolbox/entities/application_plan'
3
+ require '3scale_toolbox/entities/metric'
4
+ require '3scale_toolbox/entities/method'
@@ -5,15 +5,15 @@ module ThreeScaleToolbox
5
5
  def create(service:, plan_attrs:)
6
6
  plan = service.remote.create_application_plan service.id, build_plan_attrs(plan_attrs)
7
7
  if (errors = plan['errors'])
8
- raise ThreeScaleToolbox::Error, "Application plan has not been saved. Errors: #{errors}"
8
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Application plan has not been created', errors)
9
9
  end
10
10
 
11
- new(id: plan.fetch('id'), service: service)
11
+ new(id: plan.fetch('id'), service: service, attrs: plan)
12
12
  end
13
13
 
14
14
  # ref can be system_name or service_id
15
15
  def find(service:, ref:)
16
- new(id: ref, service: service).tap(&:show)
16
+ new(id: ref, service: service).tap(&:attrs)
17
17
  rescue ThreeScale::API::HttpClient::NotFoundError
18
18
  find_by_system_name(service: service, system_name: ref)
19
19
  end
@@ -22,7 +22,7 @@ module ThreeScaleToolbox
22
22
  plan = service.plans.find { |p| p['system_name'] == system_name }
23
23
  return if plan.nil?
24
24
 
25
- new(id: plan.fetch('id'), service: service)
25
+ new(id: plan.fetch('id'), service: service, attrs: plan)
26
26
  end
27
27
 
28
28
  def build_plan_attrs(source_attrs)
@@ -30,32 +30,42 @@ module ThreeScaleToolbox
30
30
  source_attrs.clone.tap do |new_plan_attrs|
31
31
  # plans are created by default in hidden state
32
32
  # If published is required, 'state_event' attr has to be added
33
- new_plan_attrs['state_event'] = 'publish' if new_plan_attrs['state'] == 'published'
34
- new_plan_attrs['state_event'] = 'hide' if new_plan_attrs['state'] == 'hidden'
33
+ state = new_plan_attrs.delete('state')
34
+ new_plan_attrs['state_event'] = 'publish' if state == 'published'
35
+ new_plan_attrs['state_event'] = 'hide' if state == 'hidden'
35
36
  end
36
37
  end
37
38
  end
38
39
 
39
40
  attr_reader :id, :service, :remote
40
41
 
41
- def initialize(id:, service:)
42
+ def initialize(id:, service:, attrs: nil)
42
43
  @id = id
43
44
  @service = service
44
45
  @remote = service.remote
46
+ @attrs = attrs
45
47
  end
46
48
 
47
- def show
48
- remote.show_application_plan service.id, id
49
+ def attrs
50
+ @attrs ||= read_plan_attrs
49
51
  end
50
52
 
51
53
  def update(plan_attrs)
52
- remote.update_application_plan(service.id, id, self.class.build_plan_attrs(plan_attrs))
54
+ new_attrs = remote.update_application_plan(service.id, id,
55
+ self.class.build_plan_attrs(plan_attrs))
56
+ if (errors = new_attrs['errors'])
57
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Application plan has not been updated', errors)
58
+ end
59
+
60
+ @attrs = new_attrs
61
+
62
+ new_attrs
53
63
  end
54
64
 
55
65
  def make_default
56
66
  plan = remote.application_plan_as_default service.id, id
57
67
  if (errors = plan['errors'])
58
- raise ThreeScaleToolbox::Error, "Application plan has not been set to default. Errors: #{errors}"
68
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Application plan has not been set to default', errors)
59
69
  end
60
70
 
61
71
  plan
@@ -91,13 +101,27 @@ module ThreeScaleToolbox
91
101
  end
92
102
 
93
103
  def limits
94
- remote.list_application_plan_limits id
104
+ plan_limits = remote.list_application_plan_limits id
105
+ if plan_limits.respond_to?(:has_key?) && (errors = plan_limits['errors'])
106
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Limits per application plan not read', errors)
107
+ end
108
+
109
+ plan_limits
110
+ end
111
+
112
+ def metric_limits(metric_id)
113
+ # remote.list_metric_limits(plan_id, metric_id) returns all limits for a given metric,
114
+ # without filtering by app plan
115
+ # Already reported. https://issues.jboss.org/browse/THREESCALE-2486
116
+ # Meanwhile, the strategy will be to get all metrics from a given plan
117
+ # and filter by metric_id
118
+ limits.select { |limit| limit['metric_id'] == metric_id }
95
119
  end
96
120
 
97
121
  def create_limit(metric_id, limit_attrs)
98
122
  limit = remote.create_application_plan_limit id, metric_id, limit_attrs
99
123
  if (errors = limit['errors'])
100
- raise ThreeScaleToolbox::Error, "Limit has not been created. Errors: #{errors}"
124
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Limit has not been created', errors)
101
125
  end
102
126
 
103
127
  limit
@@ -106,7 +130,7 @@ module ThreeScaleToolbox
106
130
  def update_limit(metric_id, limit_id, limit_attrs)
107
131
  limit = remote.update_application_plan_limit id, metric_id, limit_id, limit_attrs
108
132
  if (errors = limit['errors'])
109
- raise ThreeScaleToolbox::Error, "Limit #{limit_id} has not been updated. Errors: #{errors}"
133
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Limit not updated', errors)
110
134
  end
111
135
 
112
136
  limit
@@ -138,6 +162,15 @@ module ThreeScaleToolbox
138
162
 
139
163
  private
140
164
 
165
+ def read_plan_attrs
166
+ plan_attrs = remote.show_application_plan service.id, id
167
+ if (errors = plan_attrs['errors'])
168
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Application plan not read', errors)
169
+ end
170
+
171
+ plan_attrs
172
+ end
173
+
141
174
  def eternity_zero_limits
142
175
  limits.select { |limit| zero_eternity_limit_attrs < limit }
143
176
  end
@@ -0,0 +1,80 @@
1
+ module ThreeScaleToolbox
2
+ module Entities
3
+ class Method
4
+ class << self
5
+ def create(service:, parent_id:, attrs:)
6
+ method = service.remote.create_method service.id, parent_id, attrs
7
+ if (errors = method['errors'])
8
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Method has not been created', errors)
9
+
10
+ end
11
+
12
+ new(id: method.fetch('id'), parent_id: parent_id, service: service, attrs: method)
13
+ end
14
+
15
+ # ref can be system_name or method_id
16
+ def find(service:, parent_id:, ref:)
17
+ new(id: ref, parent_id: parent_id, service: service).tap(&:attrs)
18
+ rescue ThreeScale::API::HttpClient::NotFoundError
19
+ find_by_system_name(service: service, parent_id: parent_id, system_name: ref)
20
+ end
21
+
22
+ def find_by_system_name(service:, parent_id:, system_name:)
23
+ method = service.methods(parent_id).find { |m| m['system_name'] == system_name }
24
+ return if method.nil?
25
+
26
+ new(id: method.fetch('id'), parent_id: parent_id, service: service, attrs: method)
27
+ end
28
+ end
29
+
30
+ attr_reader :id, :parent_id, :service, :remote
31
+
32
+ def initialize(id:, parent_id:, service:, attrs: nil)
33
+ @id = id
34
+ @service = service
35
+ @parent_id = parent_id
36
+ @remote = service.remote
37
+ @attrs = attrs
38
+ end
39
+
40
+ def attrs
41
+ @attrs ||= method_attrs
42
+ end
43
+
44
+ def disable
45
+ Metric.new(id: id, service: service).disable
46
+ end
47
+
48
+ def enable
49
+ Metric.new(id: id, service: service).enable
50
+ end
51
+
52
+ def update(m_attrs)
53
+ new_attrs = remote.update_method(service.id, parent_id, id, m_attrs)
54
+ if (errors = new_attrs['errors'])
55
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Method has not been updated', errors)
56
+ end
57
+
58
+ # update current attrs
59
+ @attrs = new_attrs
60
+
61
+ new_attrs
62
+ end
63
+
64
+ def delete
65
+ remote.delete_method service.id, parent_id, id
66
+ end
67
+
68
+ private
69
+
70
+ def method_attrs
71
+ method = remote.show_method service.id, parent_id, id
72
+ if (errors = method['errors'])
73
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Method not read', errors)
74
+ end
75
+
76
+ method
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,113 @@
1
+ module ThreeScaleToolbox
2
+ module Entities
3
+ class Metric
4
+ class << self
5
+ def create(service:, attrs:)
6
+ metric = service.remote.create_metric service.id, attrs
7
+ if (errors = metric['errors'])
8
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Metric has not been created', errors)
9
+ end
10
+
11
+ new(id: metric.fetch('id'), service: service, attrs: metric)
12
+ end
13
+
14
+ # ref can be system_name or metric_id
15
+ def find(service:, ref:)
16
+ new(id: ref, service: service).tap(&:attrs)
17
+ rescue ThreeScale::API::HttpClient::NotFoundError
18
+ find_by_system_name(service: service, system_name: ref)
19
+ end
20
+
21
+ def find_by_system_name(service:, system_name:)
22
+ metric = service.metrics.find { |m| m['system_name'] == system_name }
23
+ return if metric.nil?
24
+
25
+ new(id: metric.fetch('id'), service: service, attrs: metric)
26
+ end
27
+ end
28
+
29
+ attr_reader :id, :service, :remote
30
+
31
+ def initialize(id:, service:, attrs: nil)
32
+ @id = id
33
+ @service = service
34
+ @remote = service.remote
35
+ @attrs = attrs
36
+ end
37
+
38
+ def attrs
39
+ @attrs ||= metric_attrs
40
+ end
41
+
42
+ def disable
43
+ # For each plan, get limits for the current metric
44
+ # if already disabled -> NOOP
45
+ # if non zero eternity limit exist, update
46
+ # if non eternity limit exist, create
47
+ service_plans.each do |plan|
48
+ eternity_limit = plan_eternity_limit(plan)
49
+ if eternity_limit.nil?
50
+ plan.create_limit(id, zero_eternity_limit_attrs)
51
+ elsif !eternity_limit.fetch('value').zero?
52
+ plan.update_limit(id, eternity_limit.fetch('id'), zero_eternity_limit_attrs)
53
+ end
54
+ end
55
+ end
56
+
57
+ def enable
58
+ service_plans.each do |plan|
59
+ limit = plan_zero_eternity_limit(plan)
60
+ plan.delete_limit(id, limit.fetch('id')) unless limit.nil?
61
+ end
62
+ end
63
+
64
+ def update(new_metric_attrs)
65
+ new_attrs = remote.update_metric(service.id, id, new_metric_attrs)
66
+ if (errors = new_attrs['errors'])
67
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Metric has not been updated', errors)
68
+ end
69
+
70
+ # update current attrs
71
+ @attrs = new_attrs
72
+
73
+ new_attrs
74
+ end
75
+
76
+ def delete
77
+ remote.delete_metric service.id, id
78
+ end
79
+
80
+ private
81
+
82
+ def metric_attrs
83
+ metric = remote.show_metric service.id, id
84
+ if (errors = metric['errors'])
85
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Metric attrs not read', errors)
86
+ end
87
+
88
+ metric
89
+ end
90
+
91
+ def plan_zero_eternity_limit(plan)
92
+ # only one limit for eternity period is allowed per (plan_id, metric_id)
93
+ plan.metric_limits(id).find { |limit| limit > zero_eternity_limit_attrs }
94
+ end
95
+
96
+ def plan_eternity_limit(plan)
97
+ # only one limit for eternity period is allowed per (plan_id, metric_id)
98
+ plan.metric_limits(id).find { |limit| limit['period'] == 'eternity' }
99
+ end
100
+
101
+ def service_plans
102
+ service.plans.map do |plan_attrs|
103
+ ThreeScaleToolbox::Entities::ApplicationPlan.new(id: plan_attrs.fetch('id'),
104
+ service: service)
105
+ end
106
+ end
107
+
108
+ def zero_eternity_limit_attrs
109
+ { 'period' => 'eternity', 'value' => 0 }
110
+ end
111
+ end
112
+ end
113
+ end
@@ -10,29 +10,35 @@ module ThreeScaleToolbox
10
10
 
11
11
  class << self
12
12
  def create(remote:, service:, system_name:)
13
- svc_obj = create_service(
13
+ svc_attrs = create_service(
14
14
  remote: remote,
15
15
  service: copy_service_params(service, system_name)
16
16
  )
17
- if (errors = svc_obj['errors'])
18
- raise ThreeScaleToolbox::Error, "Service has not been saved. Errors: #{errors}" \
17
+ if (errors = svc_attrs['errors'])
18
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service has not been created', errors)
19
19
  end
20
20
 
21
- new(id: svc_obj.fetch('id'), remote: remote)
21
+ new(id: svc_attrs.fetch('id'), remote: remote, attrs: svc_attrs)
22
22
  end
23
23
 
24
24
  # ref can be system_name or service_id
25
25
  def find(remote:, ref:)
26
- new(id: ref, remote: remote).tap(&:show_service)
26
+ new(id: ref, remote: remote).tap(&:attrs)
27
27
  rescue ThreeScale::API::HttpClient::NotFoundError
28
28
  find_by_system_name(remote: remote, system_name: ref)
29
29
  end
30
30
 
31
31
  def find_by_system_name(remote:, system_name:)
32
- service = remote.list_services.find { |svc| svc['system_name'] == system_name }
33
- return if service.nil?
32
+ service_list = remote.list_services
34
33
 
35
- new(id: service.fetch('id'), remote: remote)
34
+ if service_list.respond_to?(:has_key?) && (errors = service_list['errors'])
35
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service list not read', errors)
36
+ end
37
+
38
+ service_attrs = service_list.find { |svc| svc['system_name'] == system_name }
39
+ return if service_attrs.nil?
40
+
41
+ new(id: service_attrs.fetch('id'), remote: remote, attrs: service_attrs)
36
42
  end
37
43
 
38
44
  private
@@ -62,25 +68,42 @@ module ThreeScaleToolbox
62
68
 
63
69
  attr_reader :id, :remote
64
70
 
65
- def initialize(id:, remote:)
71
+ def initialize(id:, remote:, attrs: nil)
66
72
  @id = id
67
73
  @remote = remote
74
+ @attrs = attrs
68
75
  end
69
76
 
70
- def show_service
71
- remote.show_service id
77
+ def attrs
78
+ @attrs ||= service_attrs
72
79
  end
73
80
 
74
81
  def update_proxy(proxy)
75
- remote.update_proxy id, proxy
82
+ new_proxy_attrs = remote.update_proxy id, proxy
83
+
84
+ if (errors = new_proxy_attrs['errors'])
85
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service proxy not updated', errors)
86
+ end
87
+
88
+ new_proxy_attrs
76
89
  end
77
90
 
78
- def show_proxy
79
- remote.show_proxy id
91
+ def proxy
92
+ proxy_attrs = remote.show_proxy id
93
+ if (errors = proxy_attrs['errors'])
94
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service proxy not read', errors)
95
+ end
96
+
97
+ proxy_attrs
80
98
  end
81
99
 
82
100
  def metrics
83
- remote.list_metrics id
101
+ service_metrics = remote.list_metrics id
102
+ if service_metrics.respond_to?(:has_key?) && (errors = service_metrics['errors'])
103
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service metrics not read', errors)
104
+ end
105
+
106
+ service_metrics
84
107
  end
85
108
 
86
109
  def hits
@@ -92,20 +115,22 @@ module ThreeScaleToolbox
92
115
  hits_metric
93
116
  end
94
117
 
95
- def methods
96
- remote.list_methods id, hits['id']
97
- end
98
-
99
- def create_metric(metric)
100
- remote.create_metric id, metric
101
- end
118
+ def methods(parent_metric_id)
119
+ service_methods = remote.list_methods id, parent_metric_id
120
+ if service_methods.respond_to?(:has_key?) && (errors = service_methods['errors'])
121
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service methods not read', errors)
122
+ end
102
123
 
103
- def create_method(parent_metric_id, method)
104
- remote.create_method id, parent_metric_id, method
124
+ service_methods
105
125
  end
106
126
 
107
127
  def plans
108
- remote.list_service_application_plans id
128
+ service_plans = remote.list_service_application_plans id
129
+ if service_plans.respond_to?(:has_key?) && (errors = service_plans['errors'])
130
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service plans not read', errors)
131
+ end
132
+
133
+ service_plans
109
134
  end
110
135
 
111
136
  def mapping_rules
@@ -120,11 +145,19 @@ module ThreeScaleToolbox
120
145
  remote.create_mapping_rule id, mapping_rule
121
146
  end
122
147
 
123
- def update_service(params)
124
- remote.update_service(id, params)
148
+ def update(svc_attrs)
149
+ new_attrs = remote.update_service id, svc_attrs
150
+ if (errors = new_attrs['errors'])
151
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service not updated', errors)
152
+ end
153
+
154
+ # update current attrs
155
+ @attrs = new_attrs
156
+
157
+ new_attrs
125
158
  end
126
159
 
127
- def delete_service
160
+ def delete
128
161
  remote.delete_service id
129
162
  end
130
163
 
@@ -136,31 +169,72 @@ module ThreeScaleToolbox
136
169
  remote.update_policies(id, params)
137
170
  end
138
171
 
139
- def list_activedocs
140
- remote.list_activedocs.select do |activedoc|
172
+ def activedocs
173
+ tenant_activedocs = remote.list_activedocs
174
+
175
+ if tenant_activedocs.respond_to?(:has_key?) && (errors = tenant_activedocs['errors'])
176
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service activedocs not read', errors)
177
+ end
178
+
179
+ tenant_activedocs.select do |activedoc|
141
180
  # service_id is optional attr. It would return nil and would not match
142
181
  # activedocs endpoints return service_id as integers
143
182
  activedoc['service_id'] == id.to_i
144
183
  end
145
184
  end
146
185
 
147
- def show_oidc
148
- remote.show_oidc id
186
+ def oidc
187
+ service_oidc = remote.show_oidc id
188
+
189
+ if (errors = service_oidc['errors'])
190
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service oicdc not read', errors)
191
+ end
192
+
193
+ service_oidc
149
194
  end
150
195
 
151
196
  def update_oidc(oidc_settings)
152
- remote.update_oidc(id, oidc_settings)
197
+ new_oidc = remote.update_oidc(id, oidc_settings)
198
+
199
+ if (errors = new_oidc['errors'])
200
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service oicdc not updated', errors)
201
+ end
202
+
203
+ new_oidc
153
204
  end
154
205
 
155
206
  def features
156
- remote.list_service_features id
207
+ service_features = remote.list_service_features id
208
+
209
+ if service_features.respond_to?(:has_key?) && (errors = service_features['errors'])
210
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service features not read', errors)
211
+ end
212
+
213
+ service_features
157
214
  end
158
215
 
159
- def create_feature(attrs)
216
+ def create_feature(feature_attrs)
160
217
  # Workaround until issue is fixed: https://github.com/3scale/porta/issues/774
161
- attrs['scope'] = 'ApplicationPlan' if attrs['scope'] == 'application_plan'
162
- attrs['scope'] = 'ServicePlan' if attrs['scope'] == 'service_plan'
163
- remote.create_service_feature id, attrs
218
+ feature_attrs['scope'] = 'ApplicationPlan' if feature_attrs['scope'] == 'application_plan'
219
+ feature_attrs['scope'] = 'ServicePlan' if feature_attrs['scope'] == 'service_plan'
220
+ new_feature = remote.create_service_feature id, feature_attrs
221
+
222
+ if (errors = new_feature['errors'])
223
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service feature not created', errors)
224
+ end
225
+
226
+ new_feature
227
+ end
228
+
229
+ private
230
+
231
+ def service_attrs
232
+ svc = remote.show_service id
233
+ if (errors = svc['errors'])
234
+ raise ThreeScaleToolbox::ThreeScaleApiError.new('Service attrs not read', errors)
235
+ end
236
+
237
+ svc
164
238
  end
165
239
  end
166
240
  end