Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending templates with properties in catalog #1203

Closed
nandorholozsnyak opened this issue Jan 17, 2022 · 2 comments
Closed

Extending templates with properties in catalog #1203

nandorholozsnyak opened this issue Jan 17, 2022 · 2 comments
Labels
ideas Some idea/suggestion around jbang behavior/feature set

Comments

@nandorholozsnyak
Copy link
Contributor

nandorholozsnyak commented Jan 17, 2022

Is your feature request related to a problem? Please describe.
It is not a problem in my opinion, an idea, addition to the JBang toolset.

Describe the solution you'd like
In the current implementation of JBang the templates in the catalog are not describing, listing the available properties related to the templates and the description field is probably not enough if a JBang template is having a lot of property.
My proposal for this problem would be the following:
In the jbang-catalog.json under the templates key each element could have the following new property: properties.

So for example, lets take a look on this template file:

{#if tf-providers}
terraform {
  required_version = ">= 0.14.0"
  required_providers {
    aws        = {
      source  = "hashicorp/aws"
      version = "{tf-provider.aws.version ?: '3.71.0'}"
    }
    archive    = {
      source  = "hashicorp/archive"
      version = "{tf-provider.archive.version ?: '2.2.0'}"
    }
    null = {
      source = "hashicorp/null"
      version = "{tf-provider.null.version ?: '3.1.0'}"
    }
  }
}
provider "aws" {
  region  = "{tf-provider.aws.region ?: 'eu-central-1'}"
}
{/if}

resource "aws_iam_role" "iam_for_lambda_{baseName}" {
  name = "iam_for_lambda_{baseName}"

  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Effect": "Allow",
      "Sid": ""
    }
  ]
}
EOF
}

resource "null_resource" "jar_file_{baseName}" {
  triggers = {
    file-hash = fileexists("{baseName}.java") ? filebase64sha256("{baseName}.java") : base64sha256("0")
  }
  provisioner "local-exec" {
    command = "sh generate-jar.sh {baseName}"
  }
}

resource "aws_lambda_function" "function_{baseName}" {
  filename         = "{baseName}.jar"
  source_code_hash = fileexists("{baseName}.jar") ? filebase64sha256("{baseName}.jar") : base64sha256("0")
  function_name    = "{baseName}"
  role             = aws_iam_role.iam_for_lambda_{baseName}.arn
  handler          = "io.quarkus.amazon.lambda.runtime.QuarkusStreamHandler::handleRequest"

  depends_on = [null_resource.jar_file_{baseName}]

  runtime = "java11"
  memory_size = 256

}

{#if native-function}

resource "null_resource" "native_image_{baseName}" {
  triggers = {
    file-hash = fileexists("{baseName}.java") ? filebase64sha256("{baseName}.java") : base64sha256("0")
  }
  provisioner "local-exec" {
    command = "sh generate-native-image.sh {baseName}"
  }
}

data "archive_file" "native_function_{baseName}" {
  output_path = "{baseName}-function.zip"
  type        = "zip"
  source_file = "bootstrap"
}

resource "aws_lambda_function" "function_{baseName}_native" {
  filename         = "{baseName}-function.zip"
  source_code_hash = fileexists("{baseName}-function.zip") ? filebase64sha256("{baseName}-function.zip") : base64sha256("0")
  function_name    = "{baseName}"
  role             = aws_iam_role.iam_for_lambda_{baseName}.arn
  handler          = "not-needed"

  depends_on = [
    null_resource.native_image_{baseName}
  ]

  runtime     = "provided"
  memory_size = 128

}
{/if}

It has the following properties that can be parameterised when you it is being used during the initialization of a new script:

  • tf-providers
  • tf-provider.aws.version
  • tf-provider.archive.version
  • tf-provider.null.version
  • tf-provider.aws.region
  • native-function

If the maintainer would like to make sure that their jbang-catalog.json is the most well documented then a new section could help them.

How about this format in the jbang-catalog.json:

...
"templates": {
    "q-aws-lambda-tf": {
      "file-refs": {
        "{filename}": "aws/aws-lambda.java.qute",
        "generate-jar.sh": "aws/generate-jar.sh",
        "generate-native-image.sh": "aws/generate-native-image.sh",
        "application.properties": "aws/application.properties",
        "lambda-aws.tf": "aws/lambda-aws.tf.qute"
      },
      "description": "Quarkus AWS Lambda template with Terraform template. Use the -Dnative-function flag to have native image based Terraform resources....",
      "properties": {
        "tf-providers": {
          "description": "If enabled extra Terraform related providers will be generated",
          "defaultValue": false
        },
        "tf-provider.aws.version": {
          "description": "Version of the AWS Terraform provider",
          "defaultValue": "3.71.0"
        },
        "tf-provider.archive.version": {
          "description": "Version of the Archive Terraform provider",
          "defaultValue": "2.2.0"
        },
        "tf-provider.null.version": {
          "description": "Version of the NULL Terraform provider",
          "defaultValue": "3.1.0"
        },
        "tf-provider.aws.region": {
          "description": "AWS Region",
          "defaultValue": "eu-central-1"
        },
        "native-function": {
          "description": "Generate native executable based lambda function or not",
          "defaultValue": false
        }
      }
    },
...

If this new addition would get applied and implemented then with the "Catalog documentation" generator this feature could be a really nice way to show off what your JBang catalog repository is capable: #1202

And I think the JBang IDEA plugin could relay on it to make sure when you are initialising a script from a template you can change its properties.
https://github.com/jbangdev/jbang-idea

@nandorholozsnyak nandorholozsnyak added the ideas Some idea/suggestion around jbang behavior/feature set label Jan 17, 2022
@maxandersen
Copy link
Collaborator

I like the idea. Just important the format doesn't break existing jbang versions. Which should work - just something to check :)

@quintesse
Copy link
Contributor

Implemented by #1205, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ideas Some idea/suggestion around jbang behavior/feature set
Projects
None yet
Development

No branches or pull requests

3 participants