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 end_user_registration_required
  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.



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

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.



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

def id
  @id
end

#remoteObject (readonly)

Returns the value of attribute remote.



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

def remote
  @remote
end

Class Method Details

.create(remote:, service_params:) ⇒ Object



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

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



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

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



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

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



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

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

#activedocsObject



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

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



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

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



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

def attrs
  @attrs ||= fetch_attrs
end

#backend_usage_listObject



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

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



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

def backend_version
  attrs['backend_version']
end

#cached_oidcObject



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

def cached_oidc
  @cached_oidc ||= oidc
end

#cached_proxyObject



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

def cached_proxy
  @cached_proxy ||= proxy
end

#create_feature(feature_attrs) ⇒ Object



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

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



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

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

#deleteObject



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

def delete
  remote.delete_service id
end

#deployment_optionObject



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

def deployment_option
  attrs['deployment_option']
end

#descriptionObject



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

def description
  attrs['description']
end

#featuresObject



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

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

#hitsObject



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

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



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

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)


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

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)


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

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



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

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



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

def name
  attrs['name']
end

#oidcObject



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

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



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

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



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

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



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

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



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

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



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

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



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

def system_name
  attrs['system_name']
end

#update(svc_attrs) ⇒ Object



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

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



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

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



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

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

#update_proxy(proxy) ⇒ Object



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

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