Skip to content

Commit

Permalink
Added Modules generation (cycloidio#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
xescugc authored Feb 15, 2021
1 parent 65208ce commit 938f428
Show file tree
Hide file tree
Showing 15 changed files with 846 additions and 148 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## [Unreleased]

### Added

- Ability to create Modules directly when importing
([Issue #141](https://github.com/cycloidio/terracognita/issues/141))

## [0.6.0] _2020-12-22_

### Added
Expand Down
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,59 @@ clean: Removes binary and/or docker image

[![asciicast](https://asciinema.org/a/330055.svg)](https://asciinema.org/a/330055)

### Modules

Terracognita can generate Terraform Modules directly when importing. To enable this feature you'll need to use the `--module {module/path/name}` and then on that specific path is where the module will be generated. The path has to be directory or a none existent path (it'll be created), the content of the path will be deleted (after user confirmation) so we can have a clean import.

The output structure will look like (having `--module test`) this where each file aggregates the resources from the same "category":

```
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
```

By default all the attributes will be changed for variables, those variables will then be on the `module-{name}/variables.tf` and exposed on the `module.tf` like so:

```hcl
module "test" {
# aws_instance_front_instance_type = "t2.small"
[...]
source = "module-test"
}
```

If you want to change this behavior, as for big infrastructures this will create a lot of variables, you can use the `--module-varibles path/to/file` and the file will have the list of attributes that you want to actually be used as variables, it can be in JSON or YAML:

```json
{
"aws_instance": [
"instance_type",
"cpu_threads_per_core",
"cpu_core_count"
]
}
```

```yaml
aws_instance:
- instance_type
- cpu_threads_per_core
- cpu_core_count
```
### Docker
You can use directly [the image built](https://hub.docker.com/r/cycloid/terracognita), or you can build your own.
Expand Down
14 changes: 11 additions & 3 deletions cmd/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ var (
Use: "aws",
Short: "Terracognita reads from AWS and generates hcl resources and/or terraform state",
Long: "Terracognita reads from AWS and generates hcl resources and/or terraform state",
PreRun: func(cmd *cobra.Command, args []string) {
preRunEOutput(cmd, args)
PreRunE: func(cmd *cobra.Command, args []string) error {
err := preRunEOutput(cmd, args)
if err != nil {
return err
}

viper.BindPFlag("aws-access-key", cmd.Flags().Lookup("aws-access-key"))
viper.BindPFlag("aws-secret-access-key", cmd.Flags().Lookup("aws-secret-access-key"))
Expand All @@ -47,6 +50,8 @@ var (
viper.RegisterAlias("secret-key", "aws-secret-access-key")
viper.RegisterAlias("session-token", "aws-session-token")
viper.RegisterAlias("region", "aws-default-region")

return nil
},
PostRunE: postRunEOutput,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -85,7 +90,10 @@ var (
}

var hclW, stateW writer.Writer
options := &writer.Options{Interpolate: viper.GetBool("interpolate")}
options, err := getWriterOptions()
if err != nil {
return err
}

if hclOut != nil {
logger.Log("msg", "initializing HCL writer")
Expand Down
14 changes: 11 additions & 3 deletions cmd/azurerm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ var (
Use: "azurerm",
Short: "Terracognita reads from Azure and generates hcl resources and/or terraform state",
Long: "Terracognita reads from Azure and generates hcl resources and/or terraform state",
PreRun: func(cmd *cobra.Command, args []string) {
preRunEOutput(cmd, args)
PreRunE: func(cmd *cobra.Command, args []string) error {
err := preRunEOutput(cmd, args)
if err != nil {
return err
}
viper.BindPFlag("client-id", cmd.Flags().Lookup("client-id"))
viper.BindPFlag("client-secret", cmd.Flags().Lookup("client-secret"))
viper.BindPFlag("environment", cmd.Flags().Lookup("environment"))
viper.BindPFlag("resource-group-name", cmd.Flags().Lookup("resource-group-name"))
viper.BindPFlag("subscription-id", cmd.Flags().Lookup("subscription-id"))
viper.BindPFlag("tenant-id", cmd.Flags().Lookup("tenant-id"))

return nil
},
PostRunE: postRunEOutput,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -65,7 +70,10 @@ var (
}

var hclW, stateW writer.Writer
options := &writer.Options{Interpolate: viper.GetBool("interpolate")}
options, err := getWriterOptions()
if err != nil {
return err
}

if hclOut != nil {
logger.Log("msg", "initializing HCL writer")
Expand Down
14 changes: 11 additions & 3 deletions cmd/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ var (
Use: "google",
Short: "Terracognita reads from GCP and generates hcl resources and/or terraform state",
Long: "Terracognita reads from GCP and generates hcl resources and/or terraform state",
PreRun: func(cmd *cobra.Command, args []string) {
preRunEOutput(cmd, args)
PreRunE: func(cmd *cobra.Command, args []string) error {
err := preRunEOutput(cmd, args)
if err != nil {
return err
}
viper.BindPFlag("credentials", cmd.Flags().Lookup("credentials"))
viper.BindPFlag("project", cmd.Flags().Lookup("project"))
viper.BindPFlag("region", cmd.Flags().Lookup("region"))
viper.BindPFlag("labels", cmd.Flags().Lookup("labels"))
viper.BindPFlag("max-results", cmd.Flags().Lookup("max-results"))

return nil
},
PostRunE: postRunEOutput,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -73,7 +78,10 @@ var (
}

var hclW, stateW writer.Writer
options := &writer.Options{Interpolate: viper.GetBool("interpolate")}
options, err := getWriterOptions()
if err != nil {
return err
}

if hclOut != nil {
logger.Log("msg", "initializing HCL writer")
Expand Down
Loading

0 comments on commit 938f428

Please sign in to comment.