Skip to content

Commit

Permalink
feat(opentofu)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ant0wan committed Oct 20, 2023
1 parent 970ff41 commit 3ee3c65
Show file tree
Hide file tree
Showing 92 changed files with 252 additions and 14 deletions.
104 changes: 104 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
name: Tests

on:
push:
branches:
- main
paths:
- '**.rs'
- '**.toml'
- '.github/workflows/tests.yml'
tags:
- '*'
pull_request:
branches:
- main

permissions: {}

env:
CARGO_TERM_COLOR: always

jobs:
tf:
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: ubuntu-latest
env:
TFEXE: terraform
chdir: ./test_dir/project1/
- os: ubuntu-latest
env:
TFEXE: terraform
chdir: ./test_dir/project2/
- os: ubuntu-latest
env:
TFEXE: terraform
chdir: ./test_dir/project4/
- os: ubuntu-latest
env:
NONE: ''
chdir: ./test_dir/project1/
- os: ubuntu-latest
env:
NONE: ''
chdir: ./test_dir/project2/
- os: ubuntu-latest
env:
NONE: ''
chdir: ./test_dir/project4/
- os: ubuntu-latest
env:
TFEXE: tofu
chdir: ./test_dir/project1/
- os: ubuntu-latest
env:
TFEXE: tofu
chdir: ./test_dir/project2/
- os: ubuntu-latest
env:
TFEXE: tofu
chdir: ./test_dir/project4/
- os: macos-latest
env:
TFEXE: terraform
chdir: ./test_dir/project1/
- os: macos-latest
env:
TFEXE: terraform
chdir: ./test_dir/project2/
- os: macos-latest
env:
TFEXE: terraform
chdir: ./test_dir/project4/
- os: macos-latest
env:
NONE: ''
chdir: ./test_dir/project1/
- os: macos-latest
env:
NONE: ''
chdir: ./test_dir/project2/
- os: macos-latest
env:
NONE: ''
chdir: ./test_dir/project4/
- os: macos-latest
env:
TFEXE: tofu
chdir: ./test_dir/project1/
- os: macos-latest
env:
TFEXE: tofu
chdir: ./test_dir/project2/
- os: macos-latest
env:
TFEXE: tofu
chdir: ./test_dir/project4/
steps:
- uses: actions/checkout@v3
- uses: opentofu/setup-opentofu
- run: cargo run -- -chdir=${{ matrix.chdir }} init
env: ${{ matrix.env }}
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 19 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
use std::env;
use std::io::Result;
use std::process::{exit, Child, Command, ExitStatus};
use std::process::{Command, ExitStatus};

fn main() -> Result<()> {
let args: Vec<String> = env::args().skip(1).collect();

let mut tfswitch: Child = Command::new("tfswitch").spawn()?;
let tfswitch_exit_status: ExitStatus = tfswitch.wait()?;
if !tfswitch_exit_status.success() {
exit(tfswitch_exit_status.code().unwrap_or(1));
fn exec(val: &str, args: Vec<String>) -> Result<ExitStatus, std::io::Error> {
let mut cmd = Command::new(val);
for arg in args {
cmd.arg(arg);
}
cmd.spawn()?.wait()
}

fn main() -> Result<(), std::io::Error> {
let args: Vec<String> = env::args().skip(1).collect();
let tfexe: String = env::var("TFEXE").unwrap_or_else(|_| "tfswitch".to_string());

let mut terraform: Child = Command::new("terraform").args(args).spawn()?;
let terraform_exit_status: ExitStatus = terraform.wait()?;
if !terraform_exit_status.success() {
exit(terraform_exit_status.code().unwrap_or(1));
match tfexe.as_str() {
"tfswitch" => {
let _ = exec("tfswitch", Vec::new())?;
exec("terraform", args)?;
}
_ => {
exec(&tfexe, args)?;
}
}

Ok(())
Expand Down
Empty file.
Empty file.
Empty file.
Empty file added test_dir/project1/aws/aws.tf
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file added test_dir/project1/gcp/gcp.tf
Empty file.
Empty file.
Empty file.
Empty file.
15 changes: 15 additions & 0 deletions test_dir/project1/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
terraform {
required_version = "<=0.15.0"
}

module "gcp" {
source = "./gcp"
}

module "azure" {
source = "./azure"
}

module "aws" {
source = "./aws"
}
Empty file added test_dir/project1/outputs.tf
Empty file.
Empty file added test_dir/project1/variables.tf
Empty file.
Empty file added test_dir/project2/dev/main.tf
Empty file.
3 changes: 3 additions & 0 deletions test_dir/project2/dev/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "vpc_id" {
value = "dev module here"
}
1 change: 1 addition & 0 deletions test_dir/project2/dev/region_a/service_one.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "dev"
1 change: 1 addition & 0 deletions test_dir/project2/dev/region_a/service_three.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "dev"
1 change: 1 addition & 0 deletions test_dir/project2/dev/region_a/service_two.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "dev"
1 change: 1 addition & 0 deletions test_dir/project2/dev/region_b/service_one.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "dev"
1 change: 1 addition & 0 deletions test_dir/project2/dev/region_b/service_three.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "dev"
1 change: 1 addition & 0 deletions test_dir/project2/dev/region_b/service_two.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "dev"
18 changes: 18 additions & 0 deletions test_dir/project2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_version = "~> 1.3"
}

module "conditionally_used_dev_module" {
source = "./dev"
count = var.environment == "dev" ? 1 : 0
}

module "conditionally_used_staging_module" {
source = "./staging"
count = var.environment == "staging" ? 1 : 0
}

module "conditionally_used_production_module" {
source = "./production"
count = var.environment == "production" ? 1 : 0
}
3 changes: 3 additions & 0 deletions test_dir/project2/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "module_used" {
value = concat(module.conditionally_used_dev_module[*].vpc_id, module.conditionally_used_production_module[*].vpc_id, module.conditionally_used_staging_module[*].vpc_id)
}
Empty file.
3 changes: 3 additions & 0 deletions test_dir/project2/production/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "vpc_id" {
value = "production module here"
}
1 change: 1 addition & 0 deletions test_dir/project2/production/region_a/service_one.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "production"
1 change: 1 addition & 0 deletions test_dir/project2/production/region_a/service_three.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "production"
1 change: 1 addition & 0 deletions test_dir/project2/production/region_a/service_two.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "production"
1 change: 1 addition & 0 deletions test_dir/project2/production/region_b/service_one.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "production"
1 change: 1 addition & 0 deletions test_dir/project2/production/region_b/service_three.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "production"
1 change: 1 addition & 0 deletions test_dir/project2/production/region_b/service_two.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "production"
Empty file.
3 changes: 3 additions & 0 deletions test_dir/project2/staging/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "vpc_id" {
value = "staging module here"
}
1 change: 1 addition & 0 deletions test_dir/project2/staging/region_a/service_one.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "staging"
1 change: 1 addition & 0 deletions test_dir/project2/staging/region_a/service_three.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "staging"
1 change: 1 addition & 0 deletions test_dir/project2/staging/region_a/service_two.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "staging"
1 change: 1 addition & 0 deletions test_dir/project2/staging/region_b/service_one.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "staging"
1 change: 1 addition & 0 deletions test_dir/project2/staging/region_b/service_three.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "staging"
1 change: 1 addition & 0 deletions test_dir/project2/staging/region_b/service_two.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
environment = "staging"
3 changes: 3 additions & 0 deletions test_dir/project2/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
variable "environment" {
type = string
}
1 change: 1 addition & 0 deletions test_dir/project3/applications/backend-app/env/dev.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
clouds = ["aws", "azure", "gcp"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
clouds = ["aws", "gcp"]
1 change: 1 addition & 0 deletions test_dir/project3/applications/backend-app/env/qa.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
clouds = ["aws", "azure", "gcp"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
clouds = ["aws", "azure", "gcp"]
3 changes: 3 additions & 0 deletions test_dir/project3/applications/backend-app/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = "~> 1.2"
}
Empty file.
Empty file.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions test_dir/project3/applications/frontend-app/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = "1.4.1"
}
Empty file.
3 changes: 3 additions & 0 deletions test_dir/project3/modules/network/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = "1.2.3"
}
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions test_dir/project3/modules/spaces/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = "~> 0.15"
}
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions test_dir/project4/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
terraform {
required_version = "~>1.6"
}

3 changes: 3 additions & 0 deletions test_dir/project4/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "environment" {
value = var.environment
}
4 changes: 4 additions & 0 deletions test_dir/project4/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "environment" {
type = string
description = "Environment to deploy to."
}
4 changes: 4 additions & 0 deletions test_dir/project4/vars/asia-east1/app-one/dev.tfvars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"environment": "dev"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"environment": "production"
}

4 changes: 4 additions & 0 deletions test_dir/project4/vars/asia-east1/app-one/staging.tfvars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"environment": "staging"
}

4 changes: 4 additions & 0 deletions test_dir/project4/vars/asia-east1/app-two/dev.tfvars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"environment": "dev"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"environment": "production"
}

4 changes: 4 additions & 0 deletions test_dir/project4/vars/asia-east1/app-two/staging.tfvars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"environment": "staging"
}

4 changes: 4 additions & 0 deletions test_dir/project4/vars/us-east1/app-one/dev.tfvars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"environment": "dev"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"environment": "production"
}

3 changes: 3 additions & 0 deletions test_dir/project4/vars/us-east1/app-one/staging.tfvars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"environment": "staging"
}

0 comments on commit 3ee3c65

Please sign in to comment.