name: Web Tier serving EFS
services:
- type: cloudsoft-exercise-2-1-web-tier-terraform
  id: cloudsoft-web-efs-exercise-web-tier-tf
  name: Web Tier (TF)
  iconUrl: classpath://alb.png

  brooklyn.config:
    tf.configuration.url: classpath://web-tier-serving-efs-tf.zip

    tf_var.name: $brooklyn:config("demo_name")
    tf_var.ami: $brooklyn:config("ami")
    tf_var.ami_user: $brooklyn:config("ami_user")
    tf_var.vpc: $brooklyn:config("vpc")
    tf_var.subnets: $brooklyn:config("subnets")
    tf_var.efs_dns: $brooklyn:config("efs_dns")
    tf_var.extra_security_groups: $brooklyn:config("web_extra_security_groups")

    shell.env:
      AWS_ACCESS_KEY_ID:           $brooklyn:external("exercise-secrets", "aws-access-key-id")
      AWS_SECRET_ACCESS_KEY:       $brooklyn:external("exercise-secrets", "aws-secret-access-key")

    tf.extra.templates.contents:
      backend.tf: |
        terraform {
          backend "s3" {
            bucket = "${config.bucket_name}"
            key    = "${config.demo_name}/${name}"
            region = "eu-west-1"
          }
        }

  brooklyn.initializers:
    - type: terraform-drift-compliance-check
      brooklyn.config:
        terraform.resources-drift.enabled: true

  brooklyn.parameters:
    - name: demo_name
      default: cloudsoft-web-efs-exercise
      constraints:
        - regex: '[A-Za-z0-9_-]+'
      pinned: true
    - name: bucket_name
      constraints:
        - required
        - regex: '[A-Za-z0-9_-]+'

    - name: ami
      description: "AMI to use, eg ami-0e0669c0b39d3a79f for CentOS 8 in eu-west-1; or ami-09e310d4361a3b13a for CentOS 9 (default)"
      reconfigurable: true
      default: ami-09e310d4361a3b13a
    - name: ami_user
      description: "User that should log in to the AMI, typically centos or ec2-user (centos 9)"
      reconfigurable: true
      default: ec2-user

    - name: vpc
      description: VPC where web-tier should be created, with access to EFS mount
      constraint:
        - required
    - name: subnets
      description: Subnets where web-tier should be created, with access to EFS mount
      type: list<string>
      constraint:
        - required

    - name: efs_dns
      description: DNS name of the EFS mount
      constraint:
        - required

    - name: web_extra_security_groups
      description: "Extra security groups to apply for the web tier"
      type: list<string>
      default: []

  brooklyn.enrichers:
    - type: org.apache.brooklyn.enricher.stock.Transformer
      brooklyn.config:
        enricher.sourceSensor: $brooklyn:sensor("tf.output.alb_dns_name")
        enricher.targetSensor: $brooklyn:sensor("main.uri")
        enricher.targetValue:
          $brooklyn:formatString:
            - "http://%s/"
            - $brooklyn:attributeWhenReady("tf.output.alb_dns_name")

    - type: org.apache.brooklyn.policy.enricher.HttpLatencyDetector
      brooklyn.config:
        latencyDetector.urlSensor: main.uri
        latencyDetector.period:    2s
        latencyDetector.rollup:    10s
        latencyDetector.requireServiceUp:  true


brooklyn.enrichers:
  - type: org.apache.brooklyn.enricher.stock.Propagator
    brooklyn.config:
      producer: $brooklyn:entity("cloudsoft-web-efs-exercise-web-tier-tf")
      propagating:
        - main.uri