From b25cc6927674e389967f3c42571de53a092067b9 Mon Sep 17 00:00:00 2001 From: anon Date: Mon, 3 Feb 2025 23:25:42 +0100 Subject: [PATCH] mv felh assignment --- Misc./felh/.github/workflows/terraform.yml | 91 ++++++++++++++++++++++ Misc./felh/.notgitignore | 4 + Misc./felh/main.tf | 46 +++++++++++ Misc./felh/modules/data/main.tf | 7 ++ Misc./felh/modules/data/outputs.tf | 4 + Misc./felh/modules/data/variables.tf | 8 ++ Misc./felh/modules/files/main.tf | 14 ++++ Misc./felh/modules/files/outputs.tf | 8 ++ Misc./felh/modules/files/variables.tf | 18 +++++ Misc./felh/modules/read/outputs.tf | 14 ++++ Misc./felh/modules/read/variables.tf | 5 ++ Misc./felh/modules/write/outputs.tf | 9 +++ Misc./felh/modules/write/variables.tf | 5 ++ Misc./felh/terraform.tfvars | 5 ++ 14 files changed, 238 insertions(+) create mode 100644 Misc./felh/.github/workflows/terraform.yml create mode 100644 Misc./felh/.notgitignore create mode 100644 Misc./felh/main.tf create mode 100644 Misc./felh/modules/data/main.tf create mode 100644 Misc./felh/modules/data/outputs.tf create mode 100644 Misc./felh/modules/data/variables.tf create mode 100644 Misc./felh/modules/files/main.tf create mode 100644 Misc./felh/modules/files/outputs.tf create mode 100644 Misc./felh/modules/files/variables.tf create mode 100644 Misc./felh/modules/read/outputs.tf create mode 100644 Misc./felh/modules/read/variables.tf create mode 100644 Misc./felh/modules/write/outputs.tf create mode 100644 Misc./felh/modules/write/variables.tf create mode 100644 Misc./felh/terraform.tfvars diff --git a/Misc./felh/.github/workflows/terraform.yml b/Misc./felh/.github/workflows/terraform.yml new file mode 100644 index 0000000..d421b45 --- /dev/null +++ b/Misc./felh/.github/workflows/terraform.yml @@ -0,0 +1,91 @@ +# This workflow installs the latest version of Terraform CLI and configures the Terraform CLI configuration file +# with an API token for Terraform Cloud (app.terraform.io). On pull request events, this workflow will run +# `terraform init`, `terraform fmt`, and `terraform plan` (speculative plan via Terraform Cloud). On push events +# to the "main" branch, `terraform apply` will be executed. +# +# Documentation for `hashicorp/setup-terraform` is located here: https://github.com/hashicorp/setup-terraform +# +# To use this workflow, you will need to complete the following setup steps. +# +# 1. Create a `main.tf` file in the root of this repository with the `remote` backend and one or more resources defined. +# Example `main.tf`: +# # The configuration for the `remote` backend. +# terraform { +# backend "remote" { +# # The name of your Terraform Cloud organization. +# organization = "example-organization" +# +# # The name of the Terraform Cloud workspace to store Terraform state files in. +# workspaces { +# name = "example-workspace" +# } +# } +# } +# +# # An example resource that does nothing. +# resource "null_resource" "example" { +# triggers = { +# value = "A example resource that does nothing!" +# } +# } +# +# +# 2. Generate a Terraform Cloud user API token and store it as a GitHub secret (e.g. TF_API_TOKEN) on this repository. +# Documentation: +# - https://www.terraform.io/docs/cloud/users-teams-organizations/api-tokens.html +# - https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets +# +# 3. Reference the GitHub secret in step using the `hashicorp/setup-terraform` GitHub Action. +# Example: +# - name: Setup Terraform +# uses: hashicorp/setup-terraform@v1 +# with: +# cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} + +name: 'Terraform' + +on: + push: + branches: [ "main" ] + pull_request: + +permissions: + contents: read + +jobs: + terraform: + name: 'Terraform' + runs-on: ubuntu-latest + environment: production + + # Use the Bash shell regardless whether the GitHub Actions runner is ubuntu-latest, macos-latest, or windows-latest + defaults: + run: + shell: bash + + steps: + # Checkout the repository to the GitHub Actions runner + - name: Checkout + uses: actions/checkout@v3 + + # Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token + - name: Setup Terraform + uses: hashicorp/setup-terraform@v1 + with: + cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} + + # Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc. + - name: Terraform Init + run: terraform init + + + # Generates an execution plan for Terraform + - name: Terraform Plan + run: terraform plan -input=false + + # On push to "main", build or change infrastructure according to Terraform configuration files + # Note: It is recommended to set up a required "strict" status check in your repository for "Terraform Cloud". See the documentation on "strict" required status checks for more information: https://help.github.com/en/github/administering-a-repository/types-of-required-status-checks + - name: Terraform Apply + #if: github.ref == 'refs/heads/"main"' && github.event_name == 'push' + if: github.ref == 'refs/heads/main' && github.event_name == 'push' + run: terraform apply -auto-approve -input=false diff --git a/Misc./felh/.notgitignore b/Misc./felh/.notgitignore new file mode 100644 index 0000000..f2843f1 --- /dev/null +++ b/Misc./felh/.notgitignore @@ -0,0 +1,4 @@ +modules/files/*.txt +terraform.tfstate* +.terraform/ +.terraform.lock.hcl diff --git a/Misc./felh/main.tf b/Misc./felh/main.tf new file mode 100644 index 0000000..2634007 --- /dev/null +++ b/Misc./felh/main.tf @@ -0,0 +1,46 @@ +module "files" { + source = "./modules/files" + file_count = 5 +} + +module "read" { + source = "./modules/read" + input_value = module.files.file_names +} + +variable "answer_1" {} +variable "answer_2" {} +variable "answer_3" {} +variable "answer_4" {} +variable "answer_5" {} +module "write" { + source = "./modules/write" + answer_1 = var.answer_1 + answer_2 = var.answer_2 + answer_3 = var.answer_3 + answer_4 = var.answer_4 + answer_5 = var.answer_5 +} + + + +output "read_output" { + #value = module.read.processed_input + value = module.read.prefixed_outputs +} + +output "answers" { + value = module.write.all_variables +} + +module "data" { + source = "./modules/data" + depends_on = [module.files] + file_count = module.files.file_count + path = "${path.root}/modules/files/file1.txt" +} + +output "file_id" { + description = "The ID of the file" + value = module.data.file_id +} diff --git a/Misc./felh/modules/data/main.tf b/Misc./felh/modules/data/main.tf new file mode 100644 index 0000000..82bb528 --- /dev/null +++ b/Misc./felh/modules/data/main.tf @@ -0,0 +1,7 @@ +locals { + file_number = 1 +} + +data "local_file" "file_data" { + filename = var.path +} diff --git a/Misc./felh/modules/data/outputs.tf b/Misc./felh/modules/data/outputs.tf new file mode 100644 index 0000000..0ce96ad --- /dev/null +++ b/Misc./felh/modules/data/outputs.tf @@ -0,0 +1,4 @@ +output "file_id" { + description = "The ID of the file" + value = data.local_file.file_data.id +} diff --git a/Misc./felh/modules/data/variables.tf b/Misc./felh/modules/data/variables.tf new file mode 100644 index 0000000..b7893af --- /dev/null +++ b/Misc./felh/modules/data/variables.tf @@ -0,0 +1,8 @@ +variable "file_count" { + description = "The count of generated files" + type = number +} + +variable "path" { + type = string +} diff --git a/Misc./felh/modules/files/main.tf b/Misc./felh/modules/files/main.tf new file mode 100644 index 0000000..aff9e30 --- /dev/null +++ b/Misc./felh/modules/files/main.tf @@ -0,0 +1,14 @@ + +resource "null_resource" "generate_files" { + count = var.file_count + + triggers = { + timestamp = timestamp() + } + + provisioner "local-exec" { + command = <<-EOT + echo "Content of file ${path.module}${count.index}${var.file_extension}" > ${path.module}/${var.file_prefix}${count.index}${var.file_extension} + EOT + } +} diff --git a/Misc./felh/modules/files/outputs.tf b/Misc./felh/modules/files/outputs.tf new file mode 100644 index 0000000..cd3fb9d --- /dev/null +++ b/Misc./felh/modules/files/outputs.tf @@ -0,0 +1,8 @@ +output "file_names" { + description = "The names of the generated files" + value = [for i in range(var.file_count) : "${var.file_prefix}${i}${var.file_extension}"] +} + +output "file_count" { + value = var.file_count +} diff --git a/Misc./felh/modules/files/variables.tf b/Misc./felh/modules/files/variables.tf new file mode 100644 index 0000000..b4bef0e --- /dev/null +++ b/Misc./felh/modules/files/variables.tf @@ -0,0 +1,18 @@ + +variable "file_count" { + description = "The number of files to be created" + type = number + default = 1 +} + +variable "file_prefix" { + description = "The prefix for the filenames" + type = string + default = "file" +} + +variable "file_extension" { + description = "The extension for the filenames" + type = string + default = ".txt" +} diff --git a/Misc./felh/modules/read/outputs.tf b/Misc./felh/modules/read/outputs.tf new file mode 100644 index 0000000..673ee80 --- /dev/null +++ b/Misc./felh/modules/read/outputs.tf @@ -0,0 +1,14 @@ +locals { + output_prefixes = { + for key, value in var.input_value : "prefixed_${key}" => value + } +} + +output "prefixed_outputs" { + value = local.output_prefixes +} + +#output "processed_input" { +# description = "The input received from the files module with a prefix of 'read-'" +# value = "read-${var.input_value}" +#} diff --git a/Misc./felh/modules/read/variables.tf b/Misc./felh/modules/read/variables.tf new file mode 100644 index 0000000..6bf1674 --- /dev/null +++ b/Misc./felh/modules/read/variables.tf @@ -0,0 +1,5 @@ +variable "input_value" { + description = "The input value received from the files module" + type = list(any) + default = [] +} diff --git a/Misc./felh/modules/write/outputs.tf b/Misc./felh/modules/write/outputs.tf new file mode 100644 index 0000000..6af0b44 --- /dev/null +++ b/Misc./felh/modules/write/outputs.tf @@ -0,0 +1,9 @@ +output "all_variables" { + value = { + answer_1 = var.answer_1 + answer_2 = var.answer_2 + answer_3 = var.answer_3 + answer_4 = var.answer_4 + answer_5 = var.answer_5 + } +} diff --git a/Misc./felh/modules/write/variables.tf b/Misc./felh/modules/write/variables.tf new file mode 100644 index 0000000..ca4b724 --- /dev/null +++ b/Misc./felh/modules/write/variables.tf @@ -0,0 +1,5 @@ +variable "answer_1" {} +variable "answer_2" {} +variable "answer_3" {} +variable "answer_4" {} +variable "answer_5" {} diff --git a/Misc./felh/terraform.tfvars b/Misc./felh/terraform.tfvars new file mode 100644 index 0000000..f8b0d17 --- /dev/null +++ b/Misc./felh/terraform.tfvars @@ -0,0 +1,5 @@ +answer_1 = "state" +answer_2 = "tuple" +answer_3 = "while" +answer_4 = "using meta-argument" +answer_5 = "using -var flag"