### Build and Install Terracognita from Source Source: https://github.com/cycloidio/terracognita/blob/master/README.md This snippet outlines the steps to clone the Terracognita repository, navigate into it, and build/install the application from its latest source code using Go Modules (requires Go 1.17+). ```shell git clone https://github.com/cycloidio/terracognita cd terracognita make install ``` -------------------------------- ### Example Terracognita Docker Run for AWS Import Source: https://github.com/cycloidio/terracognita/blob/master/README.md A comprehensive example demonstrating how to run Terracognita via Docker to import AWS resources. It includes setting necessary AWS environment variables and mounting a local directory for output. ```bash export AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXXXX export AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx export AWS_DEFAULT_REGION=xx-yyyy-0 docker run \ -v "${PWD}"/outputs:/app/outputs \ cycloid/terracognita aws \ --hcl app/outputs/resources.tf ``` -------------------------------- ### Clone Terracognita Git Repository Fork Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md Instructions to clone your forked Terracognita repository to your local machine, allowing you to start contributing to the project. ```shell $ git clone https://github.com//terracognita.git $ cd terracognita ``` -------------------------------- ### Build Terracognita Project from Go Sources Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md Command to build the Terracognita executable from its Go source code. This requires Go (versions 1.12 or 1.13) to be installed and configured on your machine. ```shell $ make build ``` -------------------------------- ### Install Terracognita Binary on Linux Source: https://github.com/cycloidio/terracognita/blob/master/README.md This snippet demonstrates how to download, extract, and install the latest Terracognita binary for Linux AMD64 systems by moving it to a system-wide executable path. ```shell curl -L https://github.com/cycloidio/terracognita/releases/latest/download/terracognita-linux-amd64.tar.gz -o terracognita-linux-amd64.tar.gz tar -xf terracognita-linux-amd64.tar.gz chmod u+x terracognita-linux-amd64 sudo mv terracognita-linux-amd64 /usr/local/bin/terracognita ``` -------------------------------- ### Understanding Google Cloud Compute API for Instances Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This section outlines the process of identifying and utilizing the Google Cloud Compute API for listing instances. It guides on finding the relevant service (`InstancesService`), its `List` method, and the subsequent methods available on the `*InstancesListCall` object to define resource functions. ```APIDOC API Service: compute/v1 Service Struct: Services Target Service: InstancesService List Method: List() -> *InstancesListCall Return Object Methods: Methods available on *InstancesListCall (e.g., Project, Zone, Filter) Online Documentation: https://cloud.google.com/compute/docs/reference/rest/v1/instances/list Template Parameters: - Resource: resource name in singular PascalCase (e.g., "Instance") - API: the API name (default "compute") - Region/Zone: if the list method requires one of these parameters ``` -------------------------------- ### Install Terracognita via Homebrew on macOS Source: https://github.com/cycloidio/terracognita/blob/master/README.md This snippet provides the command to install Terracognita on macOS using the Homebrew package manager. ```sh brew install terracognita ``` -------------------------------- ### Define AWS Resource Function in Go Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This snippet shows how to define a new AWS resource function within the `aws/cmd/functions.go` file. It specifies the `Entity`, `Prefix`, and `Service` for the AWS SDK call, along with inline documentation. This definition is crucial for generating the `Get` methods in `aws/reader/reader.go`. ```shell $ vim aws/cmd/functions.go ``` ```go Function{ Entity: "DBParameterGroups", Prefix: "Describe", Service: "rds", Documentation: ` // GetDBParameterGroups returns all DB parameterGroups based on the input given. // Returned values are commented in the interface doc comment block. `, } ``` -------------------------------- ### Configuring Selective Terraform Module Variables Source: https://github.com/cycloidio/terracognita/blob/master/README.md Examples of JSON and YAML configuration files used with the `--module-variables` flag. This allows users to specify a subset of attributes that should be converted into variables, preventing the generation of excessive variables for large infrastructures. ```json { "aws_instance": [ "instance_type", "cpu_threads_per_core", "cpu_core_count" ] } ``` ```yaml aws_instance: - instance_type - cpu_threads_per_core - cpu_core_count ``` -------------------------------- ### Build and Run Terracognita Binary Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This snippet provides the commands to build the `terracognita` binary after making changes and then execute it to list detected resources, demonstrating the successful integration of new resource types. ```Shell $ make generate && make build $ ./terracognita google resources ``` -------------------------------- ### TerraCognita CLI Integration for Providers Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md Outlines the necessary command-line interface (CLI) subcommands that must be added within the `cmd/` package for new providers. This includes a subcommand for initializing the provider and another for listing all supported resources. ```APIDOC CLI Subcommand: provider Location: cmd/ package Purpose: Initializes the specific provider and calls the main `provider.Import()` function with all required parameters. CLI Subcommand: resources Location: cmd/ package Purpose: Lists all resources supported by the provider within TerraCognita. ``` -------------------------------- ### Configure Resource Generation in generate.go Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This snippet shows how to add a new resource, such as "Instance", to the `functions` slice in `google/cmd/generate.go` to enable automatic generation of its List method. The `Zone: true` parameter indicates that the resource requires a zone. ```Shell $ vim google/cmd/generate.go ``` ```Go var functions = []Function{ ... Function{Resource: "Instance", Zone: true}, } ``` -------------------------------- ### Format Go Code with goimports Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md Recommendation to use 'goimports' for formatting and organizing Go code and imports. Running this command ensures your code adheres to the project's formatting standards, which are checked by CI. ```shell $ goimports -w ``` -------------------------------- ### Implement computeInstance for Terraform Code Generation Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This Go function `computeInstance` is responsible for listing instances using the `ListInstances` reader method, iterating through them, and converting each into a `provider.Resource` object. This function is then used to generate the corresponding Terraform configuration. ```Go func computeInstance(ctx context.Context, g *google, resourceType string, filters *filter.Filter) ([]provider.Resource, error) { f := initializeFilter(filters) instancesList, err := g.gcpr.ListInstances(ctx, f) if err != nil { return nil, errors.Wrap(err, "unable to list instances from reader") } resources := make([]provider.Resource, 0) for z, instances := range instancesList { for _, instance := range instances { r := provider.NewResource(fmt.Sprintf("%s/%s/%s", g.Project(), z, instance.Name), resourceType, g) resources = append(resources, r) } } return resources, nil } ``` -------------------------------- ### Generate and Test Project Files Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This snippet shows the commands to regenerate necessary project files, such as `google/reader_generated.go` and `google/resourcetype_enumer.go`, and then run tests to ensure the changes are valid. ```Shell $ make generate $ make test ``` -------------------------------- ### Display Terracognita Command Line Help Source: https://github.com/cycloidio/terracognita/blob/master/README.md This snippet demonstrates how to access the general help documentation for Terracognita and how to retrieve specific help for a particular Terraform provider. ```bash terracognita --help terracognita [TERRAFORM_PROVIDER] --help ``` -------------------------------- ### TerraCognita Provider Initialization Function Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md Describes the `NewProvider` function, which is essential for initializing a specific provider's client (SDK) and its configuration. All necessary information for initialization must be passed explicitly via function parameters, disallowing environment variables. ```APIDOC Function: NewProvider(parameters: object) Purpose: Initializes the specific provider client (SDK) and configuration, allowing Terraform to read remote information and load schemas. Parameters: parameters: An object containing all required information to initialize the provider client and configuration. Constraints: All information must be sent via parameters; no environment variables are to be used. ``` -------------------------------- ### Go: Map ResourceType to Middleware Function Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This Go code snippet demonstrates how to map a `ResourceType` (e.g., `ComputeInstance`) to its corresponding middleware function (e.g., `computeInstances`) within the `resources` map. This mapping is essential for associating a component with the specific logic responsible for fetching its resources from the cloud provider. ```Go var ( resources = map[ResourceType]rtFn{ ... ComputeInstance: computeInstances, } ) ``` -------------------------------- ### Generate and Test Terracognita Project Files Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md After modifying the Go source files, these shell commands are used to regenerate necessary project files, such as `aws/reader/reader.go` and `aws/resourcetype_enumer.go`, and to run the project's tests to ensure all changes are correctly integrated and functional. ```shell $ make generate $ make test ``` -------------------------------- ### Run Terracognita Docker Image with Help Flag Source: https://github.com/cycloidio/terracognita/blob/master/README.md Command to execute the Terracognita Docker image and display its help message. This is useful for understanding available commands and options. ```bash docker run cycloid/terracognita -h ``` -------------------------------- ### update.sh Script Overview Source: https://github.com/cycloidio/terracognita/blob/master/scripts/terraform-provider-update/README.md An overview of the `update.sh` script, its purpose, and how it uses parameters and environment variables. ```Shell Script: update.sh Purpose: Helps update Terraform and Terraform providers used by Terracognita. Parameters: - First parameter: `provider` (the provider to update) Environment Variables: - `TAG`: Used to import a specific version. ``` -------------------------------- ### Implement Go Function for Azure Virtual Machine Terraform Code Generation Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This Go function, `virtualMachines`, demonstrates how to retrieve a list of virtual machines from the Azure reader and convert them into Terracognita `provider.Resource` objects. It handles error wrapping and prepares resources for Terraform code generation. ```Go func virtualMachines(ctx context.Context, a *azurerm, resourceType string, tags []tag.Tag) ([]provider.Resource, error) { virtualMachines, err := a.azurer.ListVirtualMachines(ctx) if err != nil { return nil, errors.Wrap(err, "unable to list virtual machines from reader") } resources := make([]provider.Resource, 0) for _, virtualMachine := range virtualMachines { r := provider.NewResource(*virtualMachine.ID, resourceType, a) resources = append(resources, r) } return resources, nil } ``` -------------------------------- ### TerraCognita Resource Import Mechanism Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md Explains the core logic for how resources are imported within TerraCognita, focusing on the `provider.Provider.Resources` method and the requirements for Terraform resource IDs. It also addresses special cases like resources generating others and the dynamic addition of `Import` functions. ```APIDOC Method: provider.Provider.Resources() Purpose: Returns all instances of Terraform resources that the specific provider has and can import. Details: Information must be fetched from the provider API using the SDK. For each instance, the Terraform resource ID must be built and returned. This ID is then used by Terraform to read data and load the resource Schema. Concept: Terraform Resource ID Requirement: The ID must be in the format expected by Terraform's import function. This can sometimes involve multiple elements. Concept: Dynamic Import Function Addition Purpose: Allows resources that do not natively support `terraform import` to be imported by dynamically adding the necessary `Import` function. Constraint: If a resource does not have an `Import` function (native or dynamically added), it will not be imported. ``` -------------------------------- ### Define New Resource Type in resources.go Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This snippet demonstrates how to declare a new `ResourceType` constant, such as `ComputeInstance`, and associate it with a corresponding function in the `resources` map. This mapping is crucial for `terracognita` to recognize and process the new resource. ```Shell $ vim azurerm/resources.go ``` ```Go const ( ... ComputeInstance ResourceType = iota ) ... var ( resources = map[ResourceType]rtFn{ ... ComputeInstance: computeInstance, } ) ``` -------------------------------- ### Basic Terracognita Command Line Usage Source: https://github.com/cycloidio/terracognita/blob/master/README.md This snippet illustrates the fundamental command structure for using Terracognita, specifying a Terraform provider and optional flags for output format (HCL/module, TFState) or resource filtering. ```bash terracognita [TERRAFORM_PROVIDER] [--flags] ``` -------------------------------- ### Initialize Terraform Configuration Source: https://github.com/cycloidio/terracognita/blob/master/README.md Command to initialize a Terraform working directory. This step is crucial after Terracognita generates Terraform configuration files, as it downloads necessary providers and modules. ```bash terraform init ``` -------------------------------- ### Generate and Test Terracognita Azure Resource Files Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md These shell commands are used to regenerate necessary Terracognita files, such as `azurerm/reader_generated.go` and `azurerm/resourcetype_enumer.go`, after adding new Azure resource definitions. Running tests ensures the new configurations are working correctly. ```Shell $ make generate $ make test ``` -------------------------------- ### Build Terracognita Docker Image Source: https://github.com/cycloidio/terracognita/blob/master/README.md Command to build a local Docker image for Terracognita. This allows users to create a custom image if they prefer not to use the official pre-built image. ```bash make dbuild ``` -------------------------------- ### Configure Azure API and Function Definitions in Go Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This snippet demonstrates how to configure the `azureAPIs` and `functions` slices in Go to define new Azure API versions and resource functions within Terracognita. It specifies the API service, version, resource name, and whether it's resource group-dependent. ```Shell $ vim aws/cmd/generate.go ``` ```Go var azureAPIs = []AzureAPI{ ... AzureAPI{API: "compute", APIVersion: "2019-07-01"}, } var functions = []Function{ ... Function{Resource: "VirtualMachine", API: "compute", ResourceGroup: true}, } ``` -------------------------------- ### Implement Go Function for Terraform Code Generation Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This Go function, `dbParameterGroups`, is responsible for fetching AWS DB parameter groups using the generated `GetDBParameterGroups` method. It then iterates through the retrieved items, initializes them as `provider.Resource` objects, and appends them to a list for Terraform code generation. Error handling is included for robust operation. ```go func dbParameterGroups(ctx context.Context, a *aws, resourceType string, filters *filter.Filter) ([]provider.Resource, error) { dbParameterGroups, err := a.awsr.GetDBParameterGroups(ctx, nil) if err != nil { return nil, err } resources := make([]provider.Resource, 0) for _, i := range dbParameterGroups.DBParameterGroups { r, err := initializeResource(a, *i.DBParameterGroupName, resourceType) if err != nil { return nil, err } resources = append(resources, r) } return resources, nil } ``` -------------------------------- ### Define Azure Resource Type and Mapping in Go Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This snippet illustrates how to define a new `ResourceType` constant and map it to its corresponding function within the `resources` map in Go. This is crucial for Terracognita to recognize and process new Azure resource types. ```Shell $ vim azurerm/resources.go ``` ```Go const ( ... VirtualMachine ResourceType = iota ) ... var ( resources = map[ResourceType]rtFn{ ... VirtualMachine: virtualMachines, } ) ``` -------------------------------- ### Update Terraform Google Provider Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This command updates the `terraform-provider-google` only when an update is necessary for the Google provider. ```shell PROVIDER=google make update-terraform-provider ``` -------------------------------- ### Add Upstream Remote for Terracognita Repository Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md Command to add the original 'cycloidio/terracognita' repository as an upstream remote. This is crucial for staying updated with the main project's changes. ```shell $ git remote add upstream https://github.com/cycloidio/terracognita.git ``` -------------------------------- ### TerraCognita Provider Interface Definition Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md Defines the `provider.Provider` interface that all new providers must implement for integration into the TerraCognita codebase. This interface outlines the core methods required for managing resources and interacting with the Terraform ecosystem. ```APIDOC Interface: provider.Provider Purpose: Defines the contract for all TerraCognita providers, enabling integration with the codebase. Methods: Resources(): Purpose: Returns all instances of resources managed by this provider. Return Type: List of resource instances. Details: This method is responsible for fetching information from the provider's API using its SDK and constructing the appropriate Terraform resource IDs for each instance. The caller of this function handles loading the resource Schema. ``` -------------------------------- ### Search Terracognita Packages on Arch Linux AUR Source: https://github.com/cycloidio/terracognita/blob/master/README.md This snippet shows how to use `yay` to search for Terracognita packages in the Arch User Repository (AUR), listing both the stable and git versions available. ```shell yay -Ss terracognita aur/terracognita 1:0.3.0-1 (+0 0.00%) Reads from existing Cloud Providers (reverse Terraform) and generates your infrastructure as code on Terraform configuration aur/terracognita-git 1:v0.3.0.r27.gdfc5a99-1 (+0 0.00%) Reads from existing Cloud Providers (reverse Terraform) and generates your infrastructure as code on Terraform configuration ``` -------------------------------- ### Plan Terraform Configuration with AWS Variables Source: https://github.com/cycloidio/terracognita/blob/master/README.md Command to generate an execution plan for the Terraform configuration. It demonstrates how to pass AWS credentials as variables to the `terraform plan` command for validation. ```bash terraform plan -var access_key=$AWS_ACCESS_KEY_ID -var secret_key=$AWS_SECRET_ACCESS_KEY -var region=$AWS_DEFAULT_REGION ``` -------------------------------- ### Update Terraform Azure Provider for Terracognita Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This shell command is used to update the `terraform-provider-azurerm` within the Terracognita project. This process is typically handled by Cycloid due to the use of a forked repository. ```Shell PROVIDER=azurerm make update-terraform-provider ``` -------------------------------- ### Go: Generate ResourceType Enum for Provider Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This Go `go:generate` directive is used to automatically generate Go code for the `ResourceType` enum. It configures the generation to add a provider-specific prefix (e.g., `google_`), transform names to snake_case, and include line comments, ensuring component names exactly match Terraform documentation. ```Go //go:generate enumer -type ResourceType -addprefix google_ -transform snake -linecomment ``` -------------------------------- ### Update Terraform Binary Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This command updates the core Terraform binary itself, which might be required if the Terraform version needs to be updated for HCL generation or Terraform State management. ```shell PROVIDER=terraform make update-terraform-provider ``` -------------------------------- ### Terracognita Terraform Module Output Structure Source: https://github.com/cycloidio/terracognita/blob/master/README.md Illustrates the typical directory and file structure generated by Terracognita when using the `--module` flag. Each file within the `module-{name}` directory aggregates resources from the same category, providing a clean and organized Terraform module. ```text test/ ├── module-test │ ├── autoscaling.tf │ ├── cloud_front.tf │ ├── cloud_watch.tf │ ├── ec2.tf │ ├── elastic_load_balancing_v2_alb_nlb.tf │ ├── iam.tf │ ├── rds.tf │ ├── route53_resolver.tf │ ├── route53.tf │ ├── s3.tf │ ├── ses.tf │ └── variables.tf └── module.tf ``` -------------------------------- ### Update Local Git Fork with Upstream Changes Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md Commands to frequently update your local fork with the latest changes from the upstream repository, ensuring your development branch is synchronized. ```shell $ git fetch upstream --prune $ git rebase upstream/master ``` -------------------------------- ### update_terracognita Actions Source: https://github.com/cycloidio/terracognita/blob/master/scripts/terraform-provider-update/README.md Detailed steps performed by the `update_terracognita` function within the `update.sh` script. This process updates Terracognita's Go modules, README, and provider-specific versions after a provider update. ```APIDOC update_terracognita: - Update Terracognita Go mod with the latest - Update README.md - Apply specific `terracognita_fix_*` script (if needed) - Update the specific version on the provider package ``` -------------------------------- ### Map AWS Resource Type in Go Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This section demonstrates how to add a new resource type constant and map it to its corresponding Go function in `aws/resources.go`. The resource type naming convention follows Terraform's `aws_resource_type` but without the `aws` prefix and converted to CamelCase, ending with an 's'. ```shell $ vim aws/resources.go ``` ```go const ( ... DBParameterGroup ) ... var ( resources = map[ResourceType]rtFn{ DBParameterGroup: dbParameterGroups, ... } ) ``` -------------------------------- ### update_terraform_provider Actions Source: https://github.com/cycloidio/terracognita/blob/master/scripts/terraform-provider-update/README.md Detailed steps performed by the `update_terraform_provider` function within the `update.sh` script. This process ensures the Cycloid Provider fork is updated from the official upstream, branched, modified, and pushed. ```APIDOC update_terraform_provider: - Git clone the Cycloid Provider fork repository - Update the code from official remote upstream - Create a cycloid branch matching the release version - Move code out of internal directory - Change the go import to match the new path - Apply specific `code_fix_*` script (if needed) - Git push a new commit/tags ``` -------------------------------- ### Update Terraform AWS Provider in Terracognita Fork Source: https://github.com/cycloidio/terracognita/blob/master/CONTRIBUTING.md This command is used to update the `terraform-provider-aws` within Cycloid's specific fork. It's important to note that this operation is restricted to Cycloid personnel due to the use of a custom fork. ```shell PROVIDER=aws make update-terraform-provider ``` -------------------------------- ### Exposing Variables in Terracognita Generated Terraform Modules Source: https://github.com/cycloidio/terracognita/blob/master/README.md Shows how attributes are converted into variables by default and exposed in the `module.tf` file. These variables are defined in `module-{name}/variables.tf` and can be overridden when calling the module. ```hcl module "test" { # aws_instance_front_instance_type = "t2.small" [...] source = "module-test" } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.