module KongSchema::Resource::Target

Public Instance Methods

all(*) click to toggle source
# File lib/kong_schema/resource/target.rb, line 20
def all(*)
  Kong::Upstream.all.map(&:targets).flatten
end
changed?(record, directive) click to toggle source
# File lib/kong_schema/resource/target.rb, line 36
def changed?(record, directive)
  (
    record.target != directive['target'] ||
    record.weight != directive.fetch('weight', 100) ||
    record.upstream.name != directive['upstream_id']
  )
end
creatable?(*) click to toggle source
# File lib/kong_schema/resource/target.rb, line 32
def creatable?(*)
  true
end
create(attributes) click to toggle source
# File lib/kong_schema/resource/target.rb, line 24
def create(attributes)
  with_upstream(attributes) do |upstream|
    Adapter.for(Kong::Target).create(
      attributes.merge('upstream_id' => upstream.id)
    )
  end
end
delete(target) click to toggle source
# File lib/kong_schema/resource/target.rb, line 49
def delete(target)
  Adapter.for(Kong::Target).delete(target)
end
identify(record) click to toggle source
# File lib/kong_schema/resource/target.rb, line 11
def identify(record)
  case record
  when Kong::Target
    [record.upstream.name, record.target].to_json
  when Hash
    [record['upstream_id'], record['target']].to_json
  end
end
update(record, partial_attributes) click to toggle source
# File lib/kong_schema/resource/target.rb, line 44
def update(record, partial_attributes)
  delete(record)
  create(partial_attributes)
end

Private Instance Methods

with_upstream(params) { |upstream| ... } click to toggle source
# File lib/kong_schema/resource/target.rb, line 55
def with_upstream(params)
  upstream = Kong::Upstream.find_by_name(params.fetch('upstream_id', ''))

  fail "Can not add a target without an upstream!" if upstream.nil?

  yield upstream
end