Search code examples
kongkong-plugin

Kong Custom Plugin Admin Interface extension : need access to config of plugin


I'm building a custom plugin and I'm extending the admin interface via api.lua

Functions have the 'self' property where i can get request information but I also need access to the plugin configuration (like handler.lua) .

How can I do that ?

Kong 2.7.0.

return {
    ["/my-route/:id"] = {
        GET = function(self)
        -- do some stuff
        end
    }    
}

Thx,

Peter

Update

schema.lua

return {
    no_consumer = true, -- this plugin will only be API-wide
    fields = {
        redis_host = { type = "string" },
        redis_port = { type = "number", default = 6379 },
        redis_password = { type = "string" },
        redis_timeout = { type = "number", default = 2000 },
        redis_database = { type = "number", default = 1 }
    }
}

Solution

  • You can iterate on each plugins to get all your instance by name

    local function each_my_plugin()
      local iter = kong.db.plugins:each()
    
      return function()
        while true do
          local plugin, err = iter()
          if err then
            return kong.response.exit(500, { message = err })
          end
          if not plugin then
            return
          end
          if plugin.name == "my plugin" then
            return plugin
          end
        end
      end
    end
    

    or by id

    kong.db.plugins:select {id = id }