class ThreeScaleToolbox::Entities::BackendUsage

BackendUsage represents Product - Backend mapping entry

Constants

CREATE_PARAMS
UPDATE_PARAMS

Attributes

id[R]
product[R]
remote[R]

Public Class Methods

create(product:, attrs:) click to toggle source
# File lib/3scale_toolbox/entities/backend_usage.rb, line 14
def create(product:, attrs:)
  resp = product.remote.create_backend_usage(
    product.id,
    Helper.filter_params(CREATE_PARAMS, attrs)
  )
  if (errors = resp['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend usage has not been created',
                                                    errors)
  end

  new(id: resp.fetch('id'), product: product, attrs: resp)
end
find_by_path(product:, path:) click to toggle source
# File lib/3scale_toolbox/entities/backend_usage.rb, line 27
def find_by_path(product:, path:)
  resp = product.remote.list_backend_usages product.id
  if resp.respond_to?(:has_key?) && (errors = resp['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend usage list error', errors)
  end

  backend_usage_attrs = resp.find { |bus| bus['path'] == path }
  return if backend_usage_attrs.nil?

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

Public Instance Methods

attrs() click to toggle source
# File lib/3scale_toolbox/entities/backend_usage.rb, line 49
def attrs
  @attrs ||= fetch_attrs
end
backend() click to toggle source
# File lib/3scale_toolbox/entities/backend_usage.rb, line 87
def backend
  Backend.new(id: backend_id, remote: remote)
end
backend_id() click to toggle source
# File lib/3scale_toolbox/entities/backend_usage.rb, line 57
def backend_id
  # 3scale API returns 'backend_id'
  # 3scale API only accepts 'backend_api_id' as params on create endpoint
  # good job
  attrs['backend_id']
end
delete() click to toggle source
# File lib/3scale_toolbox/entities/backend_usage.rb, line 83
def delete
  remote.delete_backend_usage product.id, id
end
path() click to toggle source
# File lib/3scale_toolbox/entities/backend_usage.rb, line 53
def path
  attrs['path']
end
update(usage_attrs) click to toggle source
# File lib/3scale_toolbox/entities/backend_usage.rb, line 64
def update(usage_attrs)
  new_attrs = remote.update_backend_usage(
    product.id, id,
    Helper.filter_params(UPDATE_PARAMS, usage_attrs)
  )
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend usage not been updated', errors)
  end

  if new_attrs['service_id'] != product.id
    raise ThreeScaleToolbox::Error, 'Backend usage product updated'
  end

  # update current attrs
  @attrs = new_attrs

  new_attrs
end

Private Instance Methods

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

  resp = remote.backend_usage product.id, id
  if (errors = resp['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Product backend usage not read', errors)
  end

  resp
end