Skip to content

Commit

Permalink
feat: add method Validate for RouteService (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
programmer04 authored Aug 29, 2023
1 parent 2049fa9 commit 88b24ff
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 32 deletions.
6 changes: 5 additions & 1 deletion .ci/setup_kong.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ set -e

source $(dirname "$0")/_common.sh

KONG_IMAGE=${KONG_IMAGE_REPO:-kong}:${KONG_IMAGE_TAG:-3.3}
KONG_IMAGE=${KONG_IMAGE_REPO:-kong}:${KONG_IMAGE_TAG:-3.4}
NETWORK_NAME=kong-test

KONG_ROUTER_FLAVOR=${KONG_ROUTER_FLAVOR:-'traditional_compatible'}

PG_CONTAINER_NAME=pg
DATABASE_USER=kong
DATABASE_NAME=kong
Expand All @@ -32,6 +34,7 @@ function deploy_kong_postgres()
-e "KONG_ADMIN_GUI_AUTH=basic-auth" \
-e "KONG_ENFORCE_RBAC=on" \
-e "KONG_PORTAL=on" \
-e "KONG_ROUTER_FLAVOR=${KONG_ROUTER_FLAVOR}" \
-p 8000:8000 \
-p 8443:8443 \
-p 127.0.0.1:8001:8001 \
Expand All @@ -53,6 +56,7 @@ function deploy_kong_dbless()
-e "KONG_ADMIN_GUI_AUTH=basic-auth" \
-e "KONG_ENFORCE_RBAC=on" \
-e "KONG_PORTAL=on" \
-e "KONG_ROUTER_FLAVOR=${KONG_ROUTER_FLAVOR}" \
-p 8000:8000 \
-p 8443:8443 \
-p 127.0.0.1:8001:8001 \
Expand Down
5 changes: 4 additions & 1 deletion .ci/setup_kong_ee.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ set -e

source $(dirname "$0")/_common.sh

KONG_IMAGE=${KONG_IMAGE_REPO:-kong/kong-gateway}:${KONG_IMAGE_TAG:-3.3}
KONG_IMAGE=${KONG_IMAGE_REPO:-kong/kong-gateway}:${KONG_IMAGE_TAG:-3.4}
NETWORK_NAME=kong-test

KONG_ROUTER_FLAVOR=${KONG_ROUTER_FLAVOR:-'traditional_compatible'}

PG_CONTAINER_NAME=pg
DATABASE_USER=kong
DATABASE_NAME=kong
Expand Down Expand Up @@ -44,6 +46,7 @@ function deploy_kong_ee()
-e "KONG_ENFORCE_RBAC=on" \
-e "KONG_PORTAL=on" \
-e "KONG_ADMIN_GUI_SESSION_CONF={}" \
-e "KONG_ROUTER_FLAVOR=${KONG_ROUTER_FLAVOR}" \
-p 8000:8000 \
-p 8443:8443 \
-p 8001:8001 \
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/integration-test-enterprise-nightly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ jobs:
fi
test-enterprise:
strategy:
matrix:
router_flavor:
- 'traditional_compatible'
- 'expressions'
continue-on-error: true
needs:
- secret-available
if: needs.secret-available.outputs.ok
env:
KONG_ROUTER_FLAVOR: ${{ matrix.router_flavor }}
KONG_ADMIN_TOKEN: kong
KONG_IMAGE_REPO: "kong/kong-gateway-internal"
KONG_IMAGE_TAG: "master-alpine"
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/integration-test-enterprise.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ jobs:
if: needs.secret-available.outputs.ok
strategy:
matrix:
# Skip explicitly to avoid spawning many unnecessary jobs,
# since expressions router is supported for Kong >= 3.0.
# Option router_flavor is ignored for Kong < 3.0.
exclude:
- kong_version: '2.2'
router_flavor: 'expressions'
- kong_version: '2.3'
router_flavor: 'expressions'
- kong_version: '2.4'
router_flavor: 'expressions'
- kong_version: '2.5'
router_flavor: 'expressions'
- kong_version: '2.6'
router_flavor: 'expressions'
- kong_version: '2.7'
router_flavor: 'expressions'
- kong_version: '2.8'
router_flavor: 'expressions'
router_flavor:
- 'traditional_compatible'
- 'expressions'
kong_version:
- '2.2'
- '2.3'
Expand All @@ -49,7 +70,9 @@ jobs:
- '3.1'
- '3.2'
- '3.3'
- '3.4'
env:
KONG_ROUTER_FLAVOR: ${{ matrix.router_flavor }}
KONG_IMAGE_TAG: ${{ matrix.kong_version }}
KONG_ANONYMOUS_REPORTS: "off"
KONG_ADMIN_TOKEN: kong
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/integration-test-nightly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ jobs:
dbmode:
- 'dbless'
- 'postgres'
router_flavor:
- 'traditional_compatible'
- 'expressions'
env:
KONG_ROUTER_FLAVOR: ${{ matrix.router_flavor }}
KONG_IMAGE_REPO: "kong/kong"
KONG_IMAGE_TAG: "master-alpine"
KONG_ANONYMOUS_REPORTS: "off"
Expand All @@ -45,4 +49,3 @@ jobs:
name: codecov-nightly
flags: nightly,integration,community
fail_ci_if_error: true

53 changes: 40 additions & 13 deletions .github/workflows/integration-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,50 @@ jobs:
test:
strategy:
matrix:
kong_version:
- '2.1'
- '2.2'
- '2.3'
- '2.4'
- '2.5'
- '2.6'
- '2.7'
- '2.8'
- '3.0'
- '3.1'
- '3.2'
- '3.3'
# Skip explicitly to avoid spawning many unnecessary jobs,
# since expressions router is supported for Kong >= 3.0.
# Option router_flavor is ignored for Kong < 3.0.
exclude:
- kong_version: '2.1'
router_flavor: 'expressions'
- kong_version: '2.2'
router_flavor: 'expressions'
- kong_version: '2.2'
router_flavor: 'expressions'
- kong_version: '2.3'
router_flavor: 'expressions'
- kong_version: '2.4'
router_flavor: 'expressions'
- kong_version: '2.5'
router_flavor: 'expressions'
- kong_version: '2.6'
router_flavor: 'expressions'
- kong_version: '2.7'
router_flavor: 'expressions'
- kong_version: '2.8'
router_flavor: 'expressions'
dbmode:
- 'dbless'
- 'postgres'
router_flavor:
- 'traditional_compatible'
- 'expressions'
kong_version:
- '2.1'
- '2.2'
- '2.3'
- '2.4'
- '2.5'
- '2.6'
- '2.7'
- '2.8'
- '3.0'
- '3.1'
- '3.2'
- '3.3'
- '3.4'
env:
KONG_ROUTER_FLAVOR: ${{ matrix.router_flavor }}
KONG_IMAGE_TAG: ${{ matrix.kong_version }}
KONG_ANONYMOUS_REPORTS: "off"
runs-on: ubuntu-latest
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Table of Contents

- [v0.47.0](#v0470)
- [v0.46.0](#v0460)
- [v0.45.0](#v0450)
- [v0.44.0](#v0440)
Expand Down Expand Up @@ -60,6 +61,13 @@

## Unreleased

## [v0.47.0]

> Release date: 2023/08/29
- Added method `Validate` to `RouteService`
[#368](https://github.com/Kong/go-kong/pull/368)

## [v0.46.0]

> Release date: 2023/07/17
Expand Down
2 changes: 1 addition & 1 deletion kong/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func TestBaseRootURL(t *testing.T) {

func TestReloadDeclarativeRawConfig(t *testing.T) {
RunWhenDBMode(t, "off")

SkipWhenKongRouterFlavor(t, Expressions)
tests := []struct {
name string
config Configuration
Expand Down
4 changes: 3 additions & 1 deletion kong/plugin_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestPluginsServiceValidation(T *testing.T) {

func TestPluginsService(T *testing.T) {
RunWhenDBMode(T, "postgres")
SkipWhenKongRouterFlavor(T, Expressions)

assert := assert.New(T)
require := require.New(T)
Expand Down Expand Up @@ -403,6 +404,7 @@ func TestPluginListEndpoint(T *testing.T) {

func TestPluginListAllForEntityEndpoint(T *testing.T) {
RunWhenDBMode(T, "postgres")
SkipWhenKongRouterFlavor(T, Expressions)

assert := assert.New(T)
require := require.New(T)
Expand Down Expand Up @@ -474,7 +476,7 @@ func TestPluginListAllForEntityEndpoint(T *testing.T) {
},
}

// create fixturs
// create fixtures
for i := 0; i < len(plugins); i++ {
schema, err := client.Plugins.GetFullSchema(defaultCtx, plugins[i].Name)
assert.NoError(err)
Expand Down
20 changes: 20 additions & 0 deletions kong/route_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package kong
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)

// AbstractRouteService handles routes in Kong.
Expand All @@ -24,6 +26,8 @@ type AbstractRouteService interface {
ListAll(ctx context.Context) ([]*Route, error)
// ListForService fetches a list of Routes in Kong associated with a service.
ListForService(ctx context.Context, serviceNameOrID *string, opt *ListOpt) ([]*Route, *ListOpt, error)
// Validate validates a Route against its schema (checks validity of provided regex too).
Validate(ctx context.Context, route *Route) (bool, string, error)
}

// RouteService handles routes in Kong.
Expand Down Expand Up @@ -208,3 +212,19 @@ func (s *RouteService) ListForService(ctx context.Context,

return routes, next, nil
}

// Validate validates a Route against its schema (checks validity of provided regex too).
func (s *RouteService) Validate(ctx context.Context, route *Route) (bool, string, error) {
req, err := s.client.NewRequest(http.MethodPost, "/schemas/routes/validate", nil, &route)
if err != nil {
return false, "", err
}
if _, err := s.client.Do(ctx, req, nil); err != nil {
apiErr := &APIError{}
if ok := errors.As(err, &apiErr); !ok || apiErr.Code() != http.StatusBadRequest {
return false, "", err
}
return false, apiErr.message, nil
}
return true, "", nil
}
Loading

0 comments on commit 88b24ff

Please sign in to comment.