Using named variables in combination with prefixMatch and pathPrefixRewrite

67 views Asked by At

I want to setup a URL map that points to a single backend service api-backend for these routes:

GET    https://api.domain.com/:version/products
POST   https://api.domain.com/:version/products
GET    https://api.domain.com/:version/products/:id
POST   https://api.domain.com/:version/products/:id
DELETE https://api.domain.com/:version/products/:id

The path to these routes on backend service needs to be prefixed with /api. I'd like to use named variables to pass the api version as described here.

The issue I'm running into, is that I need to define 2 route rules for all routes to work:

  • one for the endpoints without :id parameter
  • one for the endpoints with :id parameter

The following URL map works but has 2 route rules to support all endpoints:

defaultService: projects/my-project/global/backendServices/api-backend
name: api-matcher
routeRules:
- description: Product
  matchRules:
  - pathTemplateMatch: /{version=*}/products/{params=**}
  priority: 0
  service: projects/my-project/global/backendServices/api-backend
  routeAction:
    urlRewrite:
      pathTemplateRewrite: /api/{version}/products/{params}
- description: Products
  matchRules:
  - pathTemplateMatch: /{version=*}/products
  priority: 1
  service: projects/my-project/global/backendServices/api-backend
  routeAction:
    urlRewrite:
      pathTemplateRewrite: /api/{version}/products

I'd like to combine these in 1 route rule, but it seems that additional URL parameters are not passed unless I create 2 route rules and explitely pass {params} for the endpoints containing :id.

I've tried working with prefixMatch instead of pathTemplateMatch and pathPrefixRewrite instead of pathTemplateRewrite. That works fine if I do not use a named variable for :version, but instead pin it to a fixed version (e.g. v1):

defaultService: projects/my-project/global/backendServices/api-backend
name: api-matcher
routeRules:
- description: Products
  matchRules:
  - prefixMatch: /v1/products
  priority: 0
  service: projects/my-project/global/backendServices/api-backend
  routeAction:
    urlRewrite:
      pathPrefixRewrite: /api/v1/products

However, when I replace v1 with a named variable version, the endpoints are not working anymore:

defaultService: projects/my-project/global/backendServices/api-backend
name: api-matcher
routeRules:
- description: Products
  matchRules:
  - prefixMatch: /{version=*}/products
  priority: 0
  service: projects/my-project/global/backendServices/api-backend
  routeAction:
    urlRewrite:
      pathPrefixRewrite: /api/{version}/products

I'm looking for advice on how to use prefixMatch and pathPrefixRewrite in combination with named variables, but the documentation does not describe if that's possible - and I'm not sure what the difference is with pathTemplateMatch. Any suggestions are welcome.

Alternatively, I'd be happy to achieve the same with pathTemplateMatch and pathTemplateRewrite, as long as I don't have to create multiple route rules for each version, or for each endpoint.

0

There are 0 answers