version: 1
kind: stack
meta:
  name: PHP Web Server Sandbox

components:
- name: ssl-cert
  source:
    dir: components/acme-certificate
    git:
      remote: https://github.com/agilestacks/google-components.git
      subDir: acme-certificate
  depends:
  - network
- name: network
  source:
    dir: components/network
    git:
      remote: https://github.com/agilestacks/google-components.git
      subDir: network
- name: cloud-sql
  source:
    dir: components/cloud-sql
    git:
      remote: https://github.com/agilestacks/google-components.git
      subDir: cloud-sql
- name: cloud-nat
  source:
    dir: components/cloud-nat
    git:
      remote: https://github.com/agilestacks/google-components.git
      subDir: cloud-nat
- name: managed-instance-group
  source:
    dir: components/managed-instance-group
    git:
      remote: https://github.com/agilestacks/google-components.git
      subDir: managed-instance-group
- name: load-balancer
  source:
    dir: components/load-balancer
    git:
      remote: https://github.com/agilestacks/google-components.git
      subDir: load-balancer
  depends:
  - ssl-cert
- name: dns-zone-record-set
  source:
    dir: components/dns-zone-record-set
    git:
      remote: https://github.com/agilestacks/google-components.git
      subDir: dns-zone-record-set
- name: wp-loadgenerator
  source:
    dir: components/wp-loadgenerator
    git:
      remote: https://github.com/agilestacks/google-components.git
      subDir: wp-loadgenerator

extensions:
  init:
  - gcp
  - .hub/get-info
  configure:
  - gcp
  - env

requires:
- gcp

lifecycle:
  verbs:
  - deploy
  - undeploy
  order:
  - ssl-cert
  - network
  - cloud-sql
  - cloud-nat
  - managed-instance-group
  - load-balancer
  - dns-zone-record-set
  - wp-loadgenerator

parameters:
- name: hub.sandboxDir
  fromEnv: SANDBOX_DIR
- name: hub.sandboxCommit
  fromEnv: SANDBOX_COMMIT
- name: hub.userAccount
  fromEnv: USER_ACCOUNT

- name: projectId
  fromEnv: GOOGLE_PROJECT
  brief: Reference to current GCP project
- name: dns.name
  fromEnv: HUB_STACK_NAME
- name: dns.domain
  brief: |
    Well known parameter. Used as a stack identifier.
    Also used by components that creates DNS records.
  fromEnv: HUB_DOMAIN_NAME
- name: stateBucketName
  brief: |
    Name of GCS bucket where Terraform enabled components upload their state.
    It must be located in the current GCP project and the GCP service account
    which is used to provision the stack must have permissions to access it.
  fromEnv: HUB_STATE_BUCKET
- name: cloud.region
  fromEnv: GOOGLE_REGION
- name: cloud.zone
  fromEnv: GOOGLE_ZONE

- name: component.certificate.name
  value: ${dns.name}

- name: component.network
  parameters:
  - name: name
    value: ${dns.name}
  - name: autocreateSubnets
    value: false
  - name: subnetwork.cidr
    value: 10.127.0.0/20

- name: component.cloudNat
  parameters:
  - name: name
    value: ${dns.name}-cloud-nat-lb-https-redirect
  - name: network
    value: ${component.network.name}

- name: component.managedInstanceGroup
  parameters:
  - name: name
    value: ${dns.name}
  - name: service_account
    value: vo-m1
  - name: disk_type
    value: pd-standard
  - name: machine_type
    value: e2-medium
  - name: imageProject
    value: debian-cloud
  - name: image
    value: debian-10-buster-v20220310
  - name: port
    value: 80
  - name: target_size
    value: 2
  - name: subnetwork
    value: ${component.network.subnetwork}
    kind: link
  - name: natRouterName
    value: ${component.cloudNat.routerName}
    kind: link
  - name: webServer
    value: apache2
    brief: "Avaible web servers are: nginx or apache2"
  - name: dbName
    value: ${component.cloudSql.dbName}
  - name: dbUser
    value: ${component.cloudSql.dbUser}
  - name: password
    value: ${component.cloudSql.password}
  - name: dbHost
    value: ${component.cloudSql.privateIp}
    kind: link

- name: certificate
  parameters:
  - name: commonName
    value: ${dns.domain}
  - name: acme.email
    default: admin@superhub.io
    fromEnv: ACME_EMAIL
    brief: email associated with acme registration
  - name: acme.endpoint
    brief: ACME endpoint (letsencrypt production)
    value: https://acme-v02.api.letsencrypt.org/directory

- name: component.loadBalancer
  parameters:
  - name: name
    value: ${dns.name}
  - name: instanceGroup
    value: ${component.managedInstanceGroup.instanceGroup}
    kind: link
  - name: sslCertificate
    value: "${certificate.google.selfLink}"
    kind: link
  - name: backendHealthPath
    value: /readme.html

- name: component.dnsZoneRecordSet
  parameters:
  - name: name
    value: ""
  - name: type
    value: "A"
  - name: value
    value: ${component.loadBalancer.externalIp}
    kind: link
  - name: zoneName
    value: ${dns.name}

- name: component.cloudSql
  parameters:
  - name: name
    value: ${dns.name}
  - name: network
    value: ${component.network.name}
  - name: dbName
    brief: Name of database which will be created during deployment of Cloud SQL component
    fromEnv: DB_NAME
    default: sandbox
  - name: dbUser
    brief: Name of user which will be created during deployment of Cloud SQL component
    fromEnv: DB_USER
    default: dbuser
  - name: password
    brief: User's password which will be created during deployment of Cloud SQL component
    fromEnv: DB_PASSWORD
  - name: version
    default: MYSQL_5_7
    fromEnv: DB_VERSION
  - name: publicIP
    value: false
  - name: allocatedIpRangeName
    kind: link
    value: ${component.network.cloudSql.allocatedIpRangeName}

outputs:
- name: wordpress.url
  value: https://${dns.domain}
