|
| 1 | +# iacconsole-cli AI Coding Agent Instructions |
| 2 | + |
| 3 | +This document provides essential guidance for AI agents working on the `iacconsole-cli` codebase. |
| 4 | + |
| 5 | +## Project Overview & Architecture |
| 6 | + |
| 7 | +`iacconsole-cli` is an infrastructure layers configuration orchestrator that dynamically manages OpenTofu or Terraform. It provides infrastructure configuration definitions from outside the Terraform code, using either files or an Infrastructure Layers Configuration Management Database (CMDB) called IaCConsole-DB. Its core purpose is to enable the reuse of Terraform code across multiple environments by separating configuration from the infrastructure code itself. |
| 8 | + |
| 9 | +The key concepts are: |
| 10 | +- **units**: Reusable, generic Terraform/OpenTofu code modules located in a `units_path` (e.g., `examples/units/`). Each subdirectory within an organization folder (e.g., `demo-org/vpc`) is a "unit". |
| 11 | +- **Inventory (Configuration Sources)**: Configuration data that can come from two sources: |
| 12 | + 1. **File-based**: JSON files that define the configuration for different environments (`examples/inventory/`). |
| 13 | + 2. **IaCConsole-DB**: External OpenAPI-based Configuration Management Database (CMDB). |
| 14 | +- **Dimensions**: Key-value pairs (`-d key:value`) that select specific configuration objects (from either source) to apply to a unit. |
| 15 | +- **Orchestration Flow**: `iacconsole-cli` dynamically creates a temporary directory, copies the selected `unit` code, generates `.tfvars` files from the specified dimension's inventory sources, and then executes the `tofu` or `terraform` command within that directory. |
| 16 | + |
| 17 | +The main application logic is in Go (`cmd/` and `utils/` directories), while the infrastructure code it manages is HCL (Terraform) located in the `examples/` directory. |
| 18 | + |
| 19 | +## Key Files and Directories |
| 20 | + |
| 21 | +- `main.go`: Main entry point for the CLI application. |
| 22 | +- `cmd/exec.go`: Implements the primary `exec` command, which is the main orchestration logic. |
| 23 | +- `utils/`: Contains the core Go helper functions. |
| 24 | + - `preparetemp.go`: Logic for creating the temporary execution directory. |
| 25 | + - `generatevars.go`: Logic for processing inventory files and generating Terraform variables. |
| 26 | + - `dimensions.go`: Handles the parsing and management of dimension data. |
| 27 | +- `examples/inventory/`: Contains the hierarchical configuration data (JSON files). This is the "database" for the infrastructure configurations. |
| 28 | +- `examples/units/`: Contains the reusable Terraform modules ("units"). |
| 29 | +- `examples/.iacconsolerc`: An example of the user-level configuration file (usually located at `$HOME/.iacconsolerc`). It defines paths and backend settings. |
| 30 | + |
| 31 | +## Developer Workflow |
| 32 | + |
| 33 | +The primary workflow involves running the `exec` command to prepare and execute a Terraform/OpenTofu action. |
| 34 | + |
| 35 | +**Get the `iacconsole-cli` binary:** |
| 36 | +- Download pre-built binaries for Linux and macOS from the [releases page](https://github.com/alt-dima/iacconsole-cli/releases) |
| 37 | +- Or build it yourself: |
| 38 | +```bash |
| 39 | +go build -o bin/iacconsole-cli . |
| 40 | +``` |
| 41 | + |
| 42 | +**Run a typical command:** |
| 43 | +This command initializes the `vpc` unit for the `test-account` in the `staging1` datacenter of the `demo-org`. |
| 44 | +```bash |
| 45 | +./bin/iacconsole-cli exec -o demo-org -d account:test-account -d datacenter:staging1 -u vpc -- init |
| 46 | +``` |
| 47 | +- `-o`: Organization (subfolder in `inventory` and `units`). |
| 48 | +- `-d`: Dimension (maps to a config file, e.g., `inventory/demo-org/datacenter/staging1.json`). |
| 49 | +- `-t`: unit (the Terraform code to use, e.g., `units/demo-org/vpc`). |
| 50 | +- `--`: Separator. Everything after it is passed directly to the `tofu` or `terraform` binary (e.g., `plan`, `apply`). |
| 51 | + |
| 52 | +To debug, you can prevent the temporary directory from being deleted by using the `-c=false` flag (or by not using `-c`). This allows you to inspect the generated `.tfvars` and the final state of the execution directory. |
| 53 | + |
| 54 | +## Configuration Sources |
| 55 | + |
| 56 | +`iacconsole-cli` supports two sources for infrastructure configuration ("inventory"): |
| 57 | + |
| 58 | +1. **File-based Inventory**: By default, `iacconsole-cli` reads configuration from JSON files located in the `inventory_path` specified in the `.iacconsolerc` config file. This is the primary method for local development and testing. |
| 59 | + |
| 60 | +2. **IaCConsole-DB (CMDB)**: If the `IACCONSOLE_API_URL` environment variable is set, `iacconsole-cli` will fetch configuration from this external OpenAPI-based CMDB. This allows for centralized management of configurations. |
| 61 | + - The `IACCONSOLE_API_URL` contains the API endpoint and credentials. |
| 62 | + - To get a free account and API keys, visit [https://iacconsole.com/](https://iacconsole.com/), fill in the form with your Account Name, Email, and press Create Account. |
| 63 | + - You'll receive generated credentials and a ready-to-use export command like `export IACCONSOLE_API_URL=https://6634b72292e9e996105de19e:generatedpassword@iacconsole.com`. |
| 64 | + - Full API documentation is available at [Swagger API docs](https://app.swaggerhub.com/apis-docs/altuhovsu/iacconsole-api/. |
| 65 | + - The `inventory-to-toaster.sh` script in `examples/` shows how to upload local inventory files to the database. |
| 66 | + - The CMDB can also be accessed directly from CI/CD pipelines like Jenkins: |
| 67 | + |
| 68 | +```groovy |
| 69 | +// Example Jenkins usage to fetch available dimension values |
| 70 | +import groovy.json.JsonSlurper |
| 71 | +
|
| 72 | +def toasterDimValuesRequest(String dimkey){ |
| 73 | + def accessToken = "youraccountid:yourpassword".bytes.encodeBase64().toString() |
| 74 | + def req = new URL("https://api.iacconsole.com/v1/dimension/demo-org/${dimkey}").openConnection(); |
| 75 | + req.setRequestProperty("Authorization", "Basic " + accessToken) |
| 76 | + def content = req.getInputStream().getText() |
| 77 | + json = new JsonSlurper().parseText(content) |
| 78 | + return json.Dimensions.DimValue |
| 79 | +} |
| 80 | +
|
| 81 | +println(toasterDimValuesRequest("datacenter")) // Returns [staging1, staging2] |
| 82 | +println(toasterDimValuesRequest("account")) // Returns [test-account] |
| 83 | +``` |
| 84 | + |
| 85 | +## Project-Specific Conventions |
| 86 | + |
| 87 | +- **Go Code**: The Go code is straightforward and follows standard practices. The core logic involves file system operations (`os`, `path/filepath`), JSON parsing (`encoding/json`), and command execution (`os/exec`). |
| 88 | +- **Variable Injection**: `iacconsole-cli` injects variables into the Terraform context: |
| 89 | + - For a dimension `-d datacenter:staging1`, it creates `var.iacconsolerc_datacenter_name`, `var.iacconsolerc_datacenter_data`, and `var.iacconsolerc_datacenter_defaults`. |
| 90 | + - Environment variables prefixed with `iacconsole-cli_envvar_` (e.g., `export iacconsole-cli_envvar_aws_region=us-east-1`) are available in Terraform as `var.iacconsolerc_envvar_aws_region`. |
| 91 | +- **Configuration Files**: |
| 92 | + - `unit_manifest.json` inside a unit directory can declare required dimensions. |
| 93 | + - `dim_defaults.json` inside an inventory dimension directory provides default values for all items in that dimension. |
| 94 | +- **Backend State**: Remote state configuration is managed via the `.iacconsolerc` config file. The state path (`$iacconsole-cli_state_path`) is dynamically generated based on the organization and dimensions to ensure state isolation. |
0 commit comments