name: Terraform Provision

on:
  push:
    branches:
      - main
    paths:
      - opdracht-3/**/*.tf
      - opdracht-3/**/*.tfvars
      - opdracht-3/**/*.tftpl
  workflow_dispatch:

jobs:
  terraform:
    name: Terraform Apply
    runs-on: skylab
    defaults:
      run:
        working-directory: ./opdracht-3

    env:
      TF_VAR_esxi_hostname: ${{ secrets.ESXI_HOSTNAME }}
      TF_VAR_esxi_username: ${{ secrets.ESXI_USERNAME }}
      TF_VAR_esxi_password: ${{ secrets.ESXI_PASSWORD }}
      TF_VAR_skylab_ssh_public_key: ${{ secrets.SKYLAB_SSH_PUBLIC_KEY }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Terraform
        uses: hashicorp/setup-terraform@v3

      - name: Install ovftool
        run: |
          wget https://github.com/rgl/ovftool-binaries/raw/main/archive/VMware-ovftool-4.6.3-24031167-lin.x86_64.zip
          unzip VMware-ovftool-4.6.3-24031167-lin.x86_64.zip
          mv ovftool vmware-ovftool
          mv vmware-ovftool /usr/bin/
          chmod +x /usr/bin/vmware-ovftool/ovftool*
          ln -s /usr/bin/vmware-ovftool/ovftool /usr/bin/ovftool

      - name: Terraform Init
        run: terraform init

      - name: Terraform Format
        run: terraform fmt -check

      - name: Terraform Validate
        run: terraform validate

      - name: Terraform Plan
        run: terraform plan

      - name: Terraform Apply
        run: terraform apply -auto-approve
