How azure/aztfexport works
Main Components
• CLI/User Interface Layer: This repository has an interactive “config” command interface (internal/ui and related subpackages) that handles user inputs and shows progress, e.g., via spinner or progress bars.
• Configuration Management: This is handled in packages such as internal/config and pkg/config where either interactive or noninteractive configuration is set up, including reading and writing to the configuration file at $HOME/.aztfexport/config.json.
• Azure Resource Discovery and Client: The client subpackage (internal/client, internal/ui/aztfexportclient) is dedicated to interfacing with Azure services, retrieving resource identifiers, and working with external Azure authentication (e.g., auth_oidc.go).
• Mapping and Meta Processing: The “meta” packages (internal/meta and pkg/meta) along with resource mapping (internal/resmap and internal/resourceset) handle the translation of Azure resource IDs into Terraform resource types and generate metadata required for import.
• Terraform Integration: The tool interacts with Terraform by running commands like “terraform import” and then using external tools (tfadd) to generate valid HCL code. This can be depicted as an integration point after the Azure resource data is processed.
• Telemetry and Logging: With packages such as pkg/telemetry and internal/log, telemetry data (including the “installation_id” used to track configuration changes) and log outputs are managed.
• Testing and Validation: The extensive testing directory (internal/test with subdirectories for cases, queries, resource group tests, etc.) ensures that the resource extraction and Terraform state import work correctly.
• Packaging and Deployment: The presence of Docker, GitHub workflows, azure-pipelines, and packaging scripts (in the scripts folder) indicates how the app is containerized, built, and released through CI/CD pipelines.
Relationships and Data Flow
• User triggers the tool through a command-line interface which routes commands to the UI layer.
• The UI and configuration components read the necessary settings (like telemetry and installation IDs) from the config files.
• The client component then interacts with Azure, retrieving the list of resources to be managed.
• The meta and resource mapping components then take over to correlate the Azure resource IDs with a Terraform resource type (leveraging external libraries like aztft and tfadd).
• Once mapping is done, the tool issues “terraform import” commands to establish the resource state and then generates the corresponding HCL configuration.
• Telemetry and logging modules collect runtime data and log outputs during processing.
• Testing and internal utilities validate the processes, ensuring consistency between the remote state and generated configuration.
Architectural Patterns and Design Principles
• Modularity: The project clearly separates core functionalities into discrete internal packages (e.g., config, client, meta, ui) increasing maintainability.
• Separation of Concerns: Different layers (UI, business logic, integrations) are kept in separate packages.
• Extensibility: Creating and updating different resource types is simplified through the meta and resource mapping layers.
• CI/CD Integration: The inclusion of Docker and GitHub workflow configuration highlights an emphasis on automated testing and deployments.
Technologies and Frameworks Highlighted
• Go: The entire codebase is written in Go and uses its module system (go.mod) for dependency management.
• Terraform: The tool both calls Terraform and generates Terraform configurations.
• Azure Integration: The tool leverages external tools for identifying and managing Azure resources.
• External Tools: It interacts with “aztft” and “tfadd” – libraries designed for identifying correct Terraform resource types and generating HCL configurations.
• At the top level, place “User/CLI” as the start point.
• A split from CLI into “Configuration Management” and “Command Dispatcher.”
• Next, direct flow to “Resource Mapping & Meta Processing” which invokes external libraries (“aztft” and “tfadd”).
• Side connections should indicate “Telemetry / Logging” collecting data from each step.