module KongSchema::Resource::Plugin

Public Instance Methods

all(*) click to toggle source
# File lib/kong_schema/resource/plugin.rb, line 30
def all(*)
  Kong::Plugin.all
end
changed?(record, attributes) click to toggle source
# File lib/kong_schema/resource/plugin.rb, line 42
def changed?(record, attributes)
  current = record.attributes.keys.each_with_object({}) do |key, map|
    value = record.attributes[key]

    map[key] = case key
    when 'api_id'
      record.api.name
    else
      value
    end
  end

  Adapter.for(Kong::Plugin).changed?(current, attributes)
end
creatable?(attributes) click to toggle source
# File lib/kong_schema/resource/plugin.rb, line 38
def creatable?(attributes)
  attributes['enabled'] != false
end
create(attributes) click to toggle source
# File lib/kong_schema/resource/plugin.rb, line 34
def create(attributes)
  Adapter.for(Kong::Plugin).create(attributes)
end
delete(record) click to toggle source
# File lib/kong_schema/resource/plugin.rb, line 75
def delete(record)
  Adapter.for(Kong::Plugin).delete(record)
end
identify(record) click to toggle source
# File lib/kong_schema/resource/plugin.rb, line 13
def identify(record)
  case record
  when Kong::Plugin
    [
      record.name,
      api_bound?(record) ? record.api.name : nil,
      consumer_bound?(record) ? record.consumer_id : nil
    ]
  when Hash
    [
      record['name'],
      record['api_id'] || nil,
      record['consumer_id'] || nil
    ]
  end
end
update(record, partial_attributes) click to toggle source
# File lib/kong_schema/resource/plugin.rb, line 57
def update(record, partial_attributes)
  # a plugin may be removed implicitly by the API if its context has been
  # removed like an Api or Consumer, so we need to refresh before
  # attempting to update it
  if deleted_by_owner?(record)
    return nil
  elsif partial_attributes['enabled'] == false
    delete(record)
  else
    Adapter.for(Kong::Plugin).update(
      record,
      partial_attributes.merge(
        'api_id' => api_bound?(record) ? record.api.id : nil
      )
    )
  end
end

Private Instance Methods

api_bound?(record) click to toggle source
# File lib/kong_schema/resource/plugin.rb, line 81
def api_bound?(record)
  !blank?(record.api_id)
end
consumer_bound?(record) click to toggle source
# File lib/kong_schema/resource/plugin.rb, line 85
def consumer_bound?(record)
  !blank?(record.consumer_id)
end
deleted_by_owner?(record) click to toggle source
# File lib/kong_schema/resource/plugin.rb, line 89
def deleted_by_owner?(record)
  if api_bound?(record) || consumer_bound?(record)
    Kong::Plugin.find(record.id).nil?
  else
    false
  end
end