Skip to content

Commit

Permalink
feat: use the public_dns as default name for APIs. fix Kong#181
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed May 7, 2015
1 parent 3bd7f75 commit 4bfc718
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion kong/dao/cassandra/apis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local PluginsConfigurations = require "kong.dao.cassandra.plugins_configurations

local SCHEMA = {
id = { type = constants.DATABASE_TYPES.ID },
name = { type = "string", required = true, unique = true, queryable = true },
name = { type = "string", unique = true, queryable = true, default = function(api_t) return api_t.public_dns end },
public_dns = { type = "string", required = true, unique = true, queryable = true,
regex = "(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])" },
target_url = { type = "string", required = true },
Expand Down
2 changes: 1 addition & 1 deletion kong/dao/schemas.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function _M.validate(t, schema, is_update)
-- Set default value for the field if given
if t[column] == nil and v.default ~= nil then
if type(v.default) == "function" then
t[column] = v.default()
t[column] = v.default(t)
else
t[column] = v.default
end
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/admin_api/admin_api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local ENDPOINTS = {
update_fields = {
public_dns = "newapi.mockbin.com"
},
error_message = '{"public_dns":"public_dns is required","name":"name is required","target_url":"target_url is required"}\n'
error_message = '{"public_dns":"public_dns is required","target_url":"target_url is required"}\n'
},
{
collection = "consumers",
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/dao/cassandra_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ describe("Cassandra DAO", function()
assert.truthy(api.created_at)
end)

it("should use the public_dns as the name if none is specified", function()
local api, err = dao_factory.apis:insert {
public_dns = "test.com",
target_url = "http://mockbin.com"
}
assert.falsy(err)
assert.truthy(api.name)
assert.are.same("test.com", api.name)
end)

it("should not insert an invalid api", function()
-- Nil
local api, err = dao_factory.apis:insert()
Expand Down
7 changes: 5 additions & 2 deletions spec/unit/validations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local schemas = require "kong.dao.schemas"
local constants = require "kong.constants"
local validate = schemas.validate

describe("Validation #schema", function()
describe("Validation", function()

it("should return the right alias", function()
assert.are.same("number", schemas.get_type("number"))
Expand All @@ -23,7 +23,10 @@ describe("Validation #schema", function()
date = { default = 123456, immutable = true },
allowed = { enum = { "hello", "world" }},
boolean_val = { type = "boolean" },
default = { default = function() return "default" end },
default = { default = function(t)
assert.truthy(t)
return "default"
end },
custom = { func = function(v, t)
if v then
if t.default == "test_custom_func" then
Expand Down

0 comments on commit 4bfc718

Please sign in to comment.