apiVersion: scaffolder.backstage.io/v1beta3
kind: Template
metadata:
  name: terraform-project-template
  title: Create a Terraform project for AWS
  description: Create a starter terraform project for provisioning AWS resources, including compute, database, and data type options
  tags:
    - terraform
    - aws
    - graviton
spec:
  owner: cloud-team
  type: infrastructure

  parameters:
    - title: Provide information about the terraform project
      required:
        - projectName
        - instanceType
        - databaseType
        - securityGroupType
        - dataType
        - location
      properties:
        projectName:
          title: Project Name
          type: string
          description: The name of the terraform project
          maxLength: 8
          pattern: '^([a-zA-Z][a-zA-Z0-9]*)(-[a-zA-Z0-9]+)*$'
          ui:autofocus: true
          ui:help: 'Must be alphanumeric, 1-8 characters, starting with a letter'
        instanceType:
          title: Compute Instance Type (AWS Graviton)
          type: string
          enum:
            - t4g.micro
            - t4g.small
            - t4g.medium
          description: Select the Graviton instance type
        databaseType:
          title: Database Type
          type: string
          enum:
            - db.t4g.micro
            - db.t4g.small
            - db.t4g.medium
            - db.t4g.large
          description: Choose the database size based on instance type
        securityGroupType:
          title: Security Group Type
          type: string
          enum:
            - internal
            - public
          description: Configure security group for internal or public access
        dataType:
          title: Data Type
          type: string
          enum:
            - customer_data
            - employee_data
            - public_data
          enumNames:
            - customer data
            - employee data
            - public data
          description: Select the type of data being processed
        location:
          title: AWS Region
          type: string
          enum:
            - frankfurt
            - virginia
            - zurich
          description: Choose the AWS region for your infrastructure.
          ui:options:
            conditional:
              dataType: public_data
              allowedValues:
                - frankfurt
                - virginia
                - zurich
              defaultValues: zurich

  dependencies:
    dataType:
      allOf:
        - if:
            properties:
              dataType:
                const: other_data
          then:
            properties:
              location:
                enum:
                  - frankfurt
                  - virginia
                  - zurich
        - if:
            properties:
              dataType:
                const: customer_data
          then:
            properties:
              location:
                enum:
                  - zurich

  steps:
    - id: generateTerraform
      name: Generating Terraform Configuration
      action: fetch:template
      input:
        url: ./terraform-skeleton
        values:
          projectName: ${{ parameters.projectName }}
          instanceType: ${{ parameters.instanceType }}
          databaseType: ${{ parameters.databaseType }}
          securityGroupType: ${{ parameters.securityGroupType }}
          dataType: ${{ parameters.dataType }}
          location: ${{ parameters.location }}

    - id: publish
      name: Publishing Terraform Project to GitLab
      action: publish:gitlab
      input:
        repoUrl: gitlab.com?owner=my-org&repo=${{ parameters.projectName }}
        description: Terraform project for ${{ parameters.projectName }}
        defaultBranch: main

    - id: register
      name: Registering the Catalog Info Component
      action: catalog:register
      input:
        repoContentsUrl: ${{ steps.publish.output.repoContentsUrl }}
        catalogInfoPath: /catalog-info.yaml

  output:
    links:
      - title: Open the Terraform Repository in GitLab
        url: ${{ steps.publish.output.remoteUrl }}
      - title: Open the Catalog Info Component
        icon: catalog
        entityRef: ${{ steps.register.output.entityRef }}
