|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | +) |
| 9 | + |
| 10 | +const exampleOrg = "example-org" |
| 11 | + |
| 12 | +// writeExampleFile is a helper function to write a file with given content and log the result |
| 13 | +func writeExampleFile(targetPath, content, fileType string) { |
| 14 | + // Create parent directories if they don't exist |
| 15 | + if err := os.MkdirAll(filepath.Dir(targetPath), 0755); err != nil { |
| 16 | + log.Fatalf("Failed to create directory for %s: %v", fileType, err) |
| 17 | + } |
| 18 | + |
| 19 | + // Write the file |
| 20 | + if err := os.WriteFile(targetPath, []byte(content), 0644); err != nil { |
| 21 | + log.Fatalf("Failed to create %s file: %v", fileType, err) |
| 22 | + } |
| 23 | + |
| 24 | + fmt.Printf("Created %s file: %s\n", fileType, targetPath) |
| 25 | +} |
| 26 | + |
| 27 | +// CreateExampleStructure creates an example organization structure with sample configuration files |
| 28 | +func CreateExampleStructure(targetDir string, useToasterDB bool) string { |
| 29 | + var exampleCmd string |
| 30 | + |
| 31 | + // Create simple manifest file for demo tofi |
| 32 | + manifestContent := `{ |
| 33 | + "dimensions": ["account", "datacenter"] |
| 34 | +}` |
| 35 | + manifestPath := filepath.Join(targetDir, "examples", "tofies", exampleOrg, "demo", "tofi_manifest.json") |
| 36 | + writeExampleFile(manifestPath, manifestContent, "example tofi manifest") |
| 37 | + |
| 38 | + // Create output.tf file |
| 39 | + outputContent := `output "account_region_from_inv" { |
| 40 | + value = var.tofugu_account_data.region |
| 41 | +} |
| 42 | +
|
| 43 | +output "account_id_from_inv" { |
| 44 | + value = var.tofugu_account_data.account_id |
| 45 | +} |
| 46 | +
|
| 47 | +output "datacenter_name_from_inv" { |
| 48 | + value = var.tofugu_datacenter_name |
| 49 | +} |
| 50 | + |
| 51 | +output "datacenter_vpc_cidr_from_inv" { |
| 52 | + value = var.tofugu_datacenter_data.vpc_cidr |
| 53 | +} |
| 54 | + |
| 55 | +output "datacenter_az_count_from_inv" { |
| 56 | + value = var.tofugu_datacenter_data.az_count |
| 57 | +}` |
| 58 | + outputPath := filepath.Join(targetDir, "examples", "tofies", exampleOrg, "demo", "output.tf") |
| 59 | + writeExampleFile(outputPath, outputContent, "example output.tf") |
| 60 | + |
| 61 | + if !useToasterDB { |
| 62 | + |
| 63 | + // Create a simple example account.json file |
| 64 | + exampleAccountContent := `{ |
| 65 | + "account_id": "123456789012", |
| 66 | + "region": "us-east-1" |
| 67 | +}` |
| 68 | + accountPath := filepath.Join(targetDir, "examples", "inventory", exampleOrg, "account", "dev.json") |
| 69 | + writeExampleFile(accountPath, exampleAccountContent, "example account") |
| 70 | + |
| 71 | + // Create example datacenter.json file |
| 72 | + exampleDevDatacenterContent := `{ |
| 73 | + "vpc_cidr": "10.0.0.0/16", |
| 74 | + "az_count": 1 |
| 75 | +}` |
| 76 | + datacenterDevPath := filepath.Join(targetDir, "examples", "inventory", exampleOrg, "datacenter", "dev.json") |
| 77 | + writeExampleFile(datacenterDevPath, exampleDevDatacenterContent, "example dev datacenter") |
| 78 | + |
| 79 | + // Create example datacenter.json file |
| 80 | + exampleProdDatacenterContent := `{ |
| 81 | + "vpc_cidr": "10.2.0.0/16", |
| 82 | + "az_count": 3 |
| 83 | +}` |
| 84 | + datacenterProdPath := filepath.Join(targetDir, "examples", "inventory", exampleOrg, "datacenter", "prod.json") |
| 85 | + writeExampleFile(datacenterProdPath, exampleProdDatacenterContent, "example prod datacenter") |
| 86 | + } else { |
| 87 | + exampleCmd = " export toasterurl=https://662cab7c5e116819738b01fe:supertoaster@toaster.altuhov.su\n" |
| 88 | + } |
| 89 | + |
| 90 | + exampleCmd = exampleCmd + |
| 91 | + " tofugu cook -o " + exampleOrg + " -d account:dev -d datacenter:dev -t demo -- init\n" + |
| 92 | + " tofugu cook -o " + exampleOrg + " -d account:dev -d datacenter:dev -t demo -- plan\n" + |
| 93 | + "Notice output from opentofu and now execute with prod datacenter:\n" + |
| 94 | + " tofugu cook -o " + exampleOrg + " -d account:dev -d datacenter:prod -t demo -- init\n" + |
| 95 | + " tofugu cook -o " + exampleOrg + " -d account:dev -d datacenter:prod -t demo -- plan\n" + |
| 96 | + "Notice different output from opentofu based on different inventory data but same Terraform code!\n" |
| 97 | + |
| 98 | + return exampleCmd |
| 99 | +} |
| 100 | + |
| 101 | +// GetTofuguConfigContent returns the content for the .tofugu configuration file |
| 102 | +func CreateExampleTofuguConfigFile(tofuguConfigPath string) { |
| 103 | + content := `defaults: |
| 104 | + tofies_path: examples/tofies |
| 105 | +# shared_modules_path: examples/tofies/shared-modules |
| 106 | + inventory_path: examples/inventory |
| 107 | + cmd_to_exec: tofu |
| 108 | +# backend: |
| 109 | +# bucket: my-tfstates |
| 110 | +# key: $tofugu_state_path |
| 111 | +# region: us-east-1 |
| 112 | +# Add additional organization-specific configurations as needed: |
| 113 | +# example-org: |
| 114 | +# backend: |
| 115 | +# bucket: example-org-tfstates |
| 116 | +# prefix: $tofugu_state_path |
| 117 | +` |
| 118 | + writeExampleFile(tofuguConfigPath, content, ".tofugu configuration") |
| 119 | +} |
0 commit comments