Class: ThreeScaleToolbox::Entities::Service

Inherits:
Object
  • Object
show all
Includes:
CRD::ProductSerializer
Defined in:
lib/3scale_toolbox/entities/service.rb

Constant Summary collapse

VALID_PARAMS =
%w[
  name backend_version deployment_option description
  system_name support_email tech_support_email admin_support_email
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CRD::ProductSerializer

#appkey_authentication_to_cr, #authentication_to_cr, #cr_name, #deployment_to_cr, #gateway_response_to_cr, #hosted_deployment_to_cr, #oidc_authentication_to_cr, #oidc_flow_to_cr, #security_to_cr, #self_managed_deployment_to_cr, #to_cr, #userkey_authentication_to_cr

Constructor Details

#initialize(id:, remote:, attrs: nil) ⇒ Service

Returns a new instance of Service.



95
96
97
98
99
# File 'lib/3scale_toolbox/entities/service.rb', line 95

def initialize(id:, remote:, attrs: nil)
  @id = id.to_i
  @remote = remote
  @attrs = attrs
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



93
94
95
# File 'lib/3scale_toolbox/entities/service.rb', line 93

def id
  @id
end

#remoteObject (readonly)

Returns the value of attribute remote.



93
94
95
# File 'lib/3scale_toolbox/entities/service.rb', line 93

def remote
  @remote
end

Class Method Details

.create(remote:, service_params:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/3scale_toolbox/entities/service.rb', line 13

def create(remote:, service_params:)
  svc_attrs = create_service(
    remote: remote,
    service: filtered_service_params(service_params)
  )
  if (errors = svc_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Service has not been created', errors)
  end

  new(id: svc_attrs.fetch('id'), remote: remote, attrs: svc_attrs)
end

.find(remote:, ref:) ⇒ Object

ref can be system_name or service_id



26
27
28
29
30
# File 'lib/3scale_toolbox/entities/service.rb', line 26

def find(remote:, ref:)
  new(id: ref, remote: remote).tap(&:attrs)
rescue ThreeScaleToolbox::InvalidIdError, ThreeScale::API::HttpClient::NotFoundError
  find_by_system_name(remote: remote, system_name: ref)
end

.find_by_system_name(remote:, system_name:) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/3scale_toolbox/entities/service.rb', line 32

def find_by_system_name(remote:, system_name:)
  attrs = list_services(remote: remote).find do |svc|
    svc['system_name'] == system_name
  end
  return if attrs.nil?

  new(id: attrs.fetch('id'), remote: remote, attrs: attrs)
end

Instance Method Details

#==(other) ⇒ Object



365
366
367
# File 'lib/3scale_toolbox/entities/service.rb', line 365

def ==(other)
  remote.http_client.endpoint == other.remote.http_client.endpoint && id == other.id
end

#activedocsObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/3scale_toolbox/entities/service.rb', line 237

def activedocs
  tenant_activedocs = remote.list_activedocs

  if tenant_activedocs.respond_to?(:has_key?) && (errors = tenant_activedocs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Service activedocs not read', errors)
  end

  service_activedocs = tenant_activedocs.select do |activedoc_attrs|
    activedoc_attrs['service_id'] == id
  end

  service_activedocs.map do |activedoc_attrs|
    Entities::ActiveDocs.new(id: activedoc_attrs.fetch('id'), remote: remote, attrs: activedoc_attrs)
  end
end

#applicationsObject



311
312
313
314
315
316
317
318
319
320
# File 'lib/3scale_toolbox/entities/service.rb', line 311

def applications
  app_attrs_list = remote.list_applications(service_id: id)
  if app_attrs_list.respond_to?(:has_key?) && (errors = app_attrs_list['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Service applications not read', errors)
  end

  app_attrs_list.map do |app_attrs|
    Entities::Application.new(id: app_attrs.fetch('id'), remote: remote, attrs: app_attrs)
  end
end

#attrsObject



101
102
103
# File 'lib/3scale_toolbox/entities/service.rb', line 101

def attrs
  @attrs ||= fetch_attrs
end

#backend_usage_listObject



322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/3scale_toolbox/entities/service.rb', line 322

def backend_usage_list
  resp = remote.list_backend_usages id
  if resp.respond_to?(:has_key?) && (errors = resp['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Product backend usage not read', errors)
  end

  resp.map do |backend_usage_attrs|
    Entities::BackendUsage.new(id: backend_usage_attrs.fetch('id'),
                               product: self,
                               attrs: backend_usage_attrs)
  end
end

#backend_versionObject



121
122
123
# File 'lib/3scale_toolbox/entities/service.rb', line 121

def backend_version
  attrs['backend_version']
end

#cached_oidcObject



263
264
265
# File 'lib/3scale_toolbox/entities/service.rb', line 263

def cached_oidc
  @cached_oidc ||= oidc
end

#cached_proxyObject



144
145
146
# File 'lib/3scale_toolbox/entities/service.rb', line 144

def cached_proxy
  @cached_proxy ||= proxy
end

#create_feature(feature_attrs) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/3scale_toolbox/entities/service.rb', line 287

def create_feature(feature_attrs)
  # Workaround until issue is fixed: https://github.com/3scale/porta/issues/774
  feature_attrs['scope'] = 'ApplicationPlan' if feature_attrs['scope'] == 'application_plan'
  feature_attrs['scope'] = 'ServicePlan' if feature_attrs['scope'] == 'service_plan'
  new_feature = remote.create_service_feature id, feature_attrs

  if (errors = new_feature['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Service feature not created', errors)
  end

  new_feature
end

#create_mapping_rule(mr_attrs) ⇒ Object



335
336
337
# File 'lib/3scale_toolbox/entities/service.rb', line 335

def create_mapping_rule(mr_attrs)
  Entities::MappingRule.create(service: self, attrs: mr_attrs)
end

#deleteObject



220
221
222
# File 'lib/3scale_toolbox/entities/service.rb', line 220

def delete
  remote.delete_service id
end

#deployment_optionObject



117
118
119
# File 'lib/3scale_toolbox/entities/service.rb', line 117

def deployment_option
  attrs['deployment_option']
end

#descriptionObject



113
114
115
# File 'lib/3scale_toolbox/entities/service.rb', line 113

def description
  attrs['description']
end

#featuresObject



277
278
279
280
281
282
283
284
285
# File 'lib/3scale_toolbox/entities/service.rb', line 277

def features
  service_features = remote.list_service_features id

  if service_features.respond_to?(:has_key?) && (errors = service_features['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Service features not read', errors)
  end

  service_features
end

#find_metric_or_method(system_name) ⇒ Object



369
370
371
# File 'lib/3scale_toolbox/entities/service.rb', line 369

def find_metric_or_method(system_name)
  (metrics + methods).find { |m| m.system_name == system_name }
end

#hitsObject



158
159
160
161
162
163
164
165
# File 'lib/3scale_toolbox/entities/service.rb', line 158

def hits
  metric_list = metrics_and_methods.map do |metric_attrs|
    Metric.new(id: metric_attrs.fetch('id'), service: self, attrs: metric_attrs)
  end
  metric_list.find { |metric| metric.system_name == 'hits' }.tap do |hits_metric|
    raise ThreeScaleToolbox::Error, 'missing hits metric' if hits_metric.nil?
  end
end

#mapping_rulesObject



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/3scale_toolbox/entities/service.rb', line 195

def mapping_rules
  mr_list = remote.list_mapping_rules id
  if mr_list.respond_to?(:has_key?) && (errors = mr_list['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Service mapping rules not read', errors)
  end

  mr_list.map do |mr_attrs|
    MappingRule.new(id: mr_attrs.fetch('id'),
                    service: self,
                    attrs: mr_attrs)
  end
end

#methodsList

Returns:

  • (List)


169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/3scale_toolbox/entities/service.rb', line 169

def methods
  method_attr_list = remote.list_methods id, hits.id
  if method_attr_list.respond_to?(:has_key?) && (errors = method_attr_list['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Service methods not read', errors)
  end

  method_attr_list.map do |method_attrs|
    Method.new(id: method_attrs.fetch('id'),
               service: self,
               attrs: method_attrs)
  end
end

#metricsList

Returns:

  • (List)


150
151
152
153
154
155
156
# File 'lib/3scale_toolbox/entities/service.rb', line 150

def metrics
  metric_attr_list = metrics_and_methods.select { |metric_attrs| metric_attrs['parent_id'].nil? }

  metric_attr_list.map do |metric_attrs|
    Metric.new(id: metric_attrs.fetch('id'), service: self, attrs: metric_attrs)
  end
end

#metrics_mapping(other) ⇒ Object

Compute matrics mapping between products, including related backend metrics as well



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/3scale_toolbox/entities/service.rb', line 349

def metrics_mapping(other)
  mapping = (metrics + methods).product(other.metrics + other.methods).select do |m_a, m_b|
    m_a.system_name == m_b.system_name
  end.map { |m_a, m_b| [m_a.id, m_b.id] }.to_h

  backend_pairs = backend_usage_list.map(&:backend).product(other.backend_usage_list.map(&:backend)).select do |b_a, b_b|
    b_a.system_name == b_b.system_name
  end

  backend_pairs.each do |b_a, b_b|
    mapping.merge!(b_a.metrics_mapping(b_b))
  end

  mapping
end

#nameObject



109
110
111
# File 'lib/3scale_toolbox/entities/service.rb', line 109

def name
  attrs['name']
end

#oidcObject



253
254
255
256
257
258
259
260
261
# File 'lib/3scale_toolbox/entities/service.rb', line 253

def oidc
  service_oidc = remote.show_oidc id

  if (errors = service_oidc['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Service oicdc not read', errors)
  end

  service_oidc
end

#plansObject



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/3scale_toolbox/entities/service.rb', line 182

def plans
  service_plans = remote.list_service_application_plans id
  if service_plans.respond_to?(:has_key?) && (errors = service_plans['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Service plans not read', errors)
  end

  service_plans.map do |plan_attrs|
    ApplicationPlan.new(id: plan_attrs.fetch('id'),
                        service: self,
                        attrs: plan_attrs)
  end
end

#policiesObject



224
225
226
227
228
229
230
231
# File 'lib/3scale_toolbox/entities/service.rb', line 224

def policies
  policy_chain = remote.show_policies id
  if policy_chain.respond_to?(:has_key?) && (errors = policy_chain['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Service policies not read', errors)
  end

  policy_chain
end

#proxyObject



135
136
137
138
139
140
141
142
# File 'lib/3scale_toolbox/entities/service.rb', line 135

def proxy
  proxy_attrs = remote.show_proxy id
  if (errors = proxy_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Service proxy not read', errors)
  end

  proxy_attrs
end

#proxy_configs(environment) ⇒ Object



300
301
302
303
304
305
306
307
308
309
# File 'lib/3scale_toolbox/entities/service.rb', line 300

def proxy_configs(environment)
  proxy_configs_attrs = remote.proxy_config_list(id, environment)
  if proxy_configs_attrs.respond_to?(:has_key?) && (errors = proxy_configs_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('ProxyConfigs not read', errors)
  end

  proxy_configs_attrs.map do |proxy_config_attrs|
    Entities::ProxyConfig.new(environment: environment, service: self, version: proxy_config_attrs.fetch("version"), attrs: proxy_config_attrs)
  end
end

#proxy_deployObject



339
340
341
342
343
344
345
346
# File 'lib/3scale_toolbox/entities/service.rb', line 339

def proxy_deploy
  proxy_attrs = remote.proxy_deploy id
  if proxy_attrs.respond_to?(:has_key?) && (errors = proxy_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Proxy configuration not deployed', errors)
  end

  proxy_attrs
end

#system_nameObject



105
106
107
# File 'lib/3scale_toolbox/entities/service.rb', line 105

def system_name
  attrs['system_name']
end

#update(svc_attrs) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/3scale_toolbox/entities/service.rb', line 208

def update(svc_attrs)
  new_attrs = safe_update(svc_attrs)
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Service not updated', errors)
  end

  # update current attrs
  @attrs = new_attrs

  new_attrs
end

#update_oidc(oidc_settings) ⇒ Object



267
268
269
270
271
272
273
274
275
# File 'lib/3scale_toolbox/entities/service.rb', line 267

def update_oidc(oidc_settings)
  new_oidc = remote.update_oidc(id, oidc_settings)

  if (errors = new_oidc['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Service oicdc not updated', errors)
  end

  new_oidc
end

#update_policies(params) ⇒ Object



233
234
235
# File 'lib/3scale_toolbox/entities/service.rb', line 233

def update_policies(params)
  remote.update_policies(id, params)
end

#update_proxy(proxy) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/3scale_toolbox/entities/service.rb', line 125

def update_proxy(proxy)
  new_proxy_attrs = remote.update_proxy id, proxy

  if (errors = new_proxy_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Service proxy not updated', errors)
  end

  new_proxy_attrs
end