kotless icon indicating copy to clipboard operation
kotless copied to clipboard

Support for Path Parameters (Kotless DSL)

Open Serdnad opened this issue 4 years ago • 6 comments

Does the Kotless DSL currently support path parameters? I understand the documentation is a work in progress, so I tried using a few different common patterns e.g. /users/:id, /users/{id} just to check, and none of them seem to be routed to my endpoint.

If it's the case that it's not yet implemented, I'd be happy to give it a shot, especially if anyone could point me to the right place for it.

Thanks!

Update: After looking through the source code, it's pretty obvious this just hasn't been implemented yet. It seems like the two parts to implementing this are parsing and matching the route in the dispatcher, and then generating the right Terraform output, does that sound about right?

Also, would a PR be accepted if it only implemented this feature for the Kotless DSL?

Serdnad avatar Nov 07 '20 21:11 Serdnad

I must agree. Its important feature form me.

sitole avatar Nov 16 '20 17:11 sitole

I wonder if it is possible to use the terraform extension and add this to create the needed aws resources to support path parameters?

https://stackoverflow.com/a/39141672

Anybody out there already did this?

BlackHornet avatar Dec 02 '20 22:12 BlackHornet

Otherwise this feature would be really important for me as well

BlackHornet avatar Dec 04 '20 07:12 BlackHornet

@Serdnad @Sitole

I found a solution that works for me.

Using Ktor I was able to use the Ktor "Locations"-Feature to set up the implementation for my route with path parameters.

Luckily Kotless does not create the terraform code for any "Locations" defined route. So I could write the aws terraform resources (as mentioned in my link above) in an extension file and add it to my terraform deployment.

In my extension I added the required:

  • aws_api_gateway_integration
  • aws_api_gateway_method (add the request_parameters)
  • aws_api_gateway_resource (using the path_part: e.g. {path})
  • aws_lambda_permission

in my case this looks like that

resource "aws_api_gateway_integration" "api_pathParam_get" {
  depends_on = [aws_api_gateway_resource.api_pathParam]
  rest_api_id = aws_api_gateway_rest_api.api.id
  resource_id = aws_api_gateway_resource.api_pathParam.id
  http_method = "GET"
  integration_http_method = "POST"
  type = "AWS_PROXY"
  uri = "arn:aws:apigateway:${data.aws_region.current.name}:lambda:path/2015-03-31/functions/${aws_lambda_function.merged_0.arn}/invocations"
}

resource "aws_api_gateway_method" "api_pathParam_get" {
  depends_on = [aws_api_gateway_resource.api_pathParam]
  rest_api_id = aws_api_gateway_rest_api.api.id
  resource_id = aws_api_gateway_resource.api_pathParam.id
  http_method = "GET"
  authorization = "NONE"

  request_parameters = {
    "method.request.path.pathParam" = true
  }
}

resource "aws_api_gateway_resource" "api_pathParam" {
  depends_on = [aws_api_gateway_resource.api]
  rest_api_id = aws_api_gateway_rest_api.api.id
  parent_id = aws_api_gateway_resource.api.id
  path_part = "{pathParam}"
}

resource "aws_lambda_permission" "api_pathParam_get" {
  statement_id = "api-pathparam-get"
  action = "lambda:InvokeFunction"
  function_name = aws_lambda_function.merged_0.arn
  principal = "apigateway.amazonaws.com"
  source_arn = "arn:aws:execute-api:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${aws_api_gateway_rest_api.api.id}/*/GET/api/*"
}

notice the path placeholder in the aws_lambda_permission.source_arn

As long as Kotless does not support Ktor Locations....i'm happy

BlackHornet avatar Dec 10 '20 18:12 BlackHornet

Will need it too please

hamadycisse avatar Feb 10 '21 14:02 hamadycisse

there are some plan? I think that is very important and I intent solve replacing { } : to _ or empty

allangomes avatar Jan 05 '22 02:01 allangomes