class ThreeScaleToolbox::Entities::Backend

Constants

VALID_PARAMS

Attributes

id[R]
remote[R]

Public Class Methods

create(remote:, attrs:) click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 10
def create(remote:, attrs:)
  b_attrs = remote.create_backend Helper.filter_params(VALID_PARAMS, attrs)
  if (errors = b_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend has not been created', errors)
  end

  new(id: b_attrs.fetch('id'), remote: remote, attrs: b_attrs)
end
find(remote:, ref:) click to toggle source

ref can be system_name or backend_id

# File lib/3scale_toolbox/entities/backend.rb, line 20
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:) click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 26
def find_by_system_name(remote:, system_name:)
  attrs = list_backends(remote: remote).find do |backend|
    backend['system_name'] == system_name
  end
  return if attrs.nil?

  new(id: attrs.fetch('id'), remote: remote, attrs: attrs)
end
new(id:, remote:, attrs: nil) click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 70
def initialize(id:, remote:, attrs: nil)
  @id = id.to_i
  @remote = remote
  @attrs = attrs
end

Private Class Methods

backends_enum(remote:) click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 41
def backends_enum(remote:)
  Enumerator.new do |yielder|
    page = 1
    loop do
      list = remote.list_backends(
        page: page,
        per_page: ThreeScale::API::MAX_BACKENDS_PER_PAGE
      )

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

      break if list.nil?

      yielder << list

      # The API response does not tell how many pages there are available
      # If one page is not fully filled, it means that it is the last page.
      break if list.length < ThreeScale::API::MAX_BACKENDS_PER_PAGE

      page += 1
    end
  end
end
list_backends(remote:) click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 37
def list_backends(remote:)
  backends_enum(remote: remote).reduce([], :concat)
end

Public Instance Methods

==(other) click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 160
def ==(other)
  remote.http_client.endpoint == other.remote.http_client.endpoint && id == other.id
end
attrs() click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 76
def attrs
  @attrs ||= fetch_backend_attrs
end
delete() click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 149
def delete
  remote.delete_backend id
end
description() click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 84
def description
  attrs['description']
end
hits() click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 104
def hits
  metric_list = metrics_and_methods.map do |metric_attrs|
    BackendMetric.new(id: metric_attrs.fetch('id'), backend: 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_rules() click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 126
def mapping_rules
  m_r = remote.list_backend_mapping_rules id
  if m_r.respond_to?(:has_key?) && (errors = m_r['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend mapping rules not read', errors)
  end

  m_r.map do |mr_attrs|
    BackendMappingRule.new(id: mr_attrs.fetch('id'), backend: self, attrs: mr_attrs)
  end
end
methods() click to toggle source

@api public @return [List]

# File lib/3scale_toolbox/entities/backend.rb, line 115
def methods
  method_attr_list = remote.list_backend_methods id, hits.id
  if method_attr_list.respond_to?(:has_key?) && (errors = method_attr_list['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend methods not read', errors)
  end

  method_attr_list.map do |method_attrs|
    BackendMethod.new(id: method_attrs.fetch('id'), backend: self, attrs: method_attrs)
  end
end
metrics() click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 96
def metrics
  metric_attr_list = metrics_and_methods.select { |metric_attrs| metric_attrs['parent_id'].nil? }

  metric_attr_list.map do |metric_attrs|
    BackendMetric.new(id: metric_attrs.fetch('id'), backend: self, attrs: metric_attrs)
  end
end
metrics_mapping(other) click to toggle source

Compute matrics mapping

# File lib/3scale_toolbox/entities/backend.rb, line 154
def metrics_mapping(other)
  (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
end
name() click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 88
def name
  attrs['name']
end
private_endpoint() click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 92
def private_endpoint
  attrs['private_endpoint']
end
system_name() click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 80
def system_name
  attrs['system_name']
end
update(b_attrs) click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 137
def update(b_attrs)
  new_attrs = remote.update_backend id, Helper.filter_params(VALID_PARAMS, b_attrs)
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend not updated', errors)
  end

  # update current attrs
  @attrs = new_attrs

  new_attrs
end

Private Instance Methods

fetch_backend_attrs() click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 175
def fetch_backend_attrs
  raise ThreeScaleToolbox::InvalidIdError if id.zero?

  backend = remote.backend id
  if (errors = backend['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend attrs not read', errors)
  end

  backend
end
metrics_and_methods() click to toggle source
# File lib/3scale_toolbox/entities/backend.rb, line 166
def metrics_and_methods
  m_m = remote.list_backend_metrics id
  if m_m.respond_to?(:has_key?) && (errors = m_m['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend metrics not read', errors)
  end

  m_m
end