Skip to content

Commit

Permalink
fix(schema) convert shorthands to shorthand_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm authored and bungle committed Sep 22, 2020
1 parent d016ade commit 4ce5620
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 52 deletions.
53 changes: 28 additions & 25 deletions kong/db/schema/entities/services.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,37 @@ return {
then_match = { eq = null }}},
},

shorthands = {
{ url = function(sugar_url)
local parsed_url = url.parse(tostring(sugar_url))
if not parsed_url then
return
end
shorthand_fields = {
{ url = {
type = "string",
func = function(sugar_url)
local parsed_url = url.parse(tostring(sugar_url))
if not parsed_url then
return
end

local port = tonumber(parsed_url.port)
local port = tonumber(parsed_url.port)

local prot
if port == 80 then
prot = "http"
elseif port == 443 then
prot = "https"
end
local prot
if port == 80 then
prot = "http"
elseif port == 443 then
prot = "https"
end

local protocol = parsed_url.scheme or prot or default_protocol
local protocol = parsed_url.scheme or prot or default_protocol

return {
protocol = protocol,
host = parsed_url.host or null,
port = port or
parsed_url.port or
(protocol == "http" and 80) or
(protocol == "https" and 443) or
default_port,
path = parsed_url.path or null,
}
end },
return {
protocol = protocol,
host = parsed_url.host or null,
port = port or
parsed_url.port or
(protocol == "http" and 80) or
(protocol == "https" and 443) or
default_port,
path = parsed_url.path or null,
}
end
}, },
}
}
16 changes: 10 additions & 6 deletions kong/db/schema/entities/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,10 @@ local r = {
-- This is a hack to preserve backwards compatibility with regard to the
-- behavior of the hash_on field, and have it take place both in the Admin API
-- and via declarative configuration.
shorthands = {
{ algorithm = function(value)
shorthand_fields = {
{ algorithm = {
type = "string",
func = function(value)
if value == "least-connections" then
return {
algorithm = value,
Expand All @@ -245,11 +247,13 @@ local r = {
algorithm = value,
}
end
end
},
end,
}, },
-- Then, if hash_on is set to some non-null value, adjust the algorithm
-- field accordingly.
{ hash_on = function(value)
{ hash_on = {
type = "string",
func = function(value)
if value == null then
return {
hash_on = "none"
Expand All @@ -266,7 +270,7 @@ local r = {
}
end
end
},
}, },
},
}

Expand Down
22 changes: 15 additions & 7 deletions kong/plugins/acl/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ return {
{ deny = { type = "array", elements = { type = "string" }, }, },
{ hide_groups_header = { type = "boolean", default = false }, },
},
shorthands = {
shorthand_fields = {
-- deprecated forms, to be removed in Kong 3.0
{ blacklist = function(value)
return { deny = value }
end },
{ whitelist = function(value)
return { allow = value }
end },
{ blacklist = {
type = "array",
elements = { type = "string", is_regex = true },
func = function(value)
return { deny = value }
end,
}, },
{ whitelist = {
type = "array",
elements = { type = "string", is_regex = true },
func = function(value)
return { allow = value }
end,
}, },
},
}
}
Expand Down
22 changes: 15 additions & 7 deletions kong/plugins/bot-detection/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,22 @@ return {
default = {},
}, },
},
shorthands = {
shorthand_fields = {
-- deprecated forms, to be removed in Kong 3.0
{ blacklist = function(value)
return { deny = value }
end },
{ whitelist = function(value)
return { allow = value }
end },
{ blacklist = {
type = "array",
elements = { type = "string", is_regex = true },
func = function(value)
return { deny = value }
end,
}, },
{ whitelist = {
type = "array",
elements = { type = "string", is_regex = true },
func = function(value)
return { allow = value }
end,
}, },
},
}, },
},
Expand Down
22 changes: 15 additions & 7 deletions kong/plugins/ip-restriction/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ return {
{ allow = { type = "array", elements = typedefs.ip_or_cidr, }, },
{ deny = { type = "array", elements = typedefs.ip_or_cidr, }, },
},
shorthands = {
shorthand_fields = {
-- deprecated forms, to be removed in Kong 3.0
{ blacklist = function(value)
return { deny = value }
end },
{ whitelist = function(value)
return { allow = value }
end },
{ blacklist = {
type = "array",
elements = { type = "string", is_regex = true },
func = function(value)
return { deny = value }
end,
}, },
{ whitelist = {
type = "array",
elements = { type = "string", is_regex = true },
func = function(value)
return { allow = value }
end,
}, },
},
},
},
Expand Down

0 comments on commit 4ce5620

Please sign in to comment.