Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
atmos terraform generate backends
command (#200)
* SAdd `atmos terraform generate backends` command * SAdd `atmos terraform generate backends` command * SAdd `atmos terraform generate backends` command
- Loading branch information
Showing
14 changed files
with
263 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cmd | ||
|
||
import ( | ||
e "github.com/cloudposse/atmos/internal/exec" | ||
u "github.com/cloudposse/atmos/pkg/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// terraformGenerateBackendsCmd generates backend configs for all terraform components | ||
var terraformGenerateBackendsCmd = &cobra.Command{ | ||
Use: "backends", | ||
Short: "Execute 'terraform generate backends' command", | ||
Long: `This command generates backend configs for all terraform components`, | ||
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := e.ExecuteTerraformGenerateBackendsCmd(cmd, args) | ||
if err != nil { | ||
u.PrintErrorToStdErrorAndExit(err) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
terraformGenerateBackendsCmd.DisableFlagParsing = false | ||
|
||
terraformGenerateBackendsCmd.PersistentFlags().String("format", "hcl", "Output format.\n"+ | ||
"Supported formats: hcl, json ('hcl' is default).\n"+ | ||
"atmos terraform generate backends --format=hcl/json") | ||
|
||
terraformGenerateCmd.AddCommand(terraformGenerateBackendsCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package exec | ||
|
||
import ( | ||
"fmt" | ||
c "github.com/cloudposse/atmos/pkg/config" | ||
u "github.com/cloudposse/atmos/pkg/utils" | ||
"github.com/spf13/cobra" | ||
"path" | ||
"path/filepath" | ||
) | ||
|
||
// ExecuteTerraformGenerateBackendsCmd executes `terraform generate backends` command | ||
func ExecuteTerraformGenerateBackendsCmd(cmd *cobra.Command, args []string) error { | ||
flags := cmd.Flags() | ||
|
||
format, err := flags.GetString("format") | ||
if err != nil { | ||
return err | ||
} | ||
if format != "" && format != "json" && format != "hcl" { | ||
return fmt.Errorf("invalid '--format' argument '%s'. Valid values are 'json' and 'hcl", format) | ||
} | ||
if format == "" { | ||
format = "hcl" | ||
} | ||
|
||
return ExecuteTerraformGenerateBackends(format) | ||
} | ||
|
||
// ExecuteTerraformGenerateBackends generates backend configs for all terraform components | ||
func ExecuteTerraformGenerateBackends(format string) error { | ||
var configAndStacksInfo c.ConfigAndStacksInfo | ||
stacksMap, err := FindStacksMap(configAndStacksInfo, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println() | ||
|
||
var ok bool | ||
var componentsSection map[string]any | ||
var terraformSection map[string]any | ||
var componentSection map[string]any | ||
var backendSection map[any]any | ||
var backendType string | ||
processedTerraformComponents := map[string]any{} | ||
|
||
for _, stackSection := range stacksMap { | ||
if componentsSection, ok = stackSection.(map[any]any)["components"].(map[string]any); !ok { | ||
continue | ||
} | ||
|
||
if terraformSection, ok = componentsSection["terraform"].(map[string]any); !ok { | ||
continue | ||
} | ||
|
||
for componentName, compSection := range terraformSection { | ||
if componentSection, ok = compSection.(map[string]any); !ok { | ||
continue | ||
} | ||
|
||
// Find terraform component. | ||
// If `component` attribute is present, it's the terraform component. | ||
// Otherwise, the YAML component name is the terraform component. | ||
terraformComponent := componentName | ||
if componentAttribute, ok := componentSection["component"].(string); ok { | ||
terraformComponent = componentAttribute | ||
} | ||
|
||
// If the terraform component has been already processed, continue | ||
if u.MapKeyExists(processedTerraformComponents, terraformComponent) { | ||
continue | ||
} | ||
|
||
processedTerraformComponents[terraformComponent] = terraformComponent | ||
|
||
// Component backend | ||
if backendSection, ok = componentSection["backend"].(map[any]any); !ok { | ||
continue | ||
} | ||
|
||
// Backend type | ||
if backendType, ok = componentSection["backend_type"].(string); !ok { | ||
continue | ||
} | ||
|
||
// Component metadata | ||
metadataSection := map[any]any{} | ||
if metadataSection, ok = componentSection["metadata"].(map[any]any); ok { | ||
if componentType, ok := metadataSection["type"].(string); ok { | ||
// Don't process abstract components | ||
if componentType == "abstract" { | ||
continue | ||
} | ||
} | ||
} | ||
|
||
// Absolute path to the terraform component | ||
backendFilePath := path.Join( | ||
c.Config.BasePath, | ||
c.Config.Components.Terraform.BasePath, | ||
terraformComponent, | ||
"backend.tf", | ||
) | ||
|
||
if format == "json" { | ||
backendFilePath = backendFilePath + ".json" | ||
} | ||
|
||
backendFileAbsolutePath, err := filepath.Abs(backendFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Write the backend config to the file | ||
u.PrintMessage(fmt.Sprintf("Writing backend config for the terraform component '%s' to file '%s'", terraformComponent, backendFilePath)) | ||
|
||
if format == "json" { | ||
componentBackendConfig := generateComponentBackendConfig(backendType, backendSection) | ||
err = u.WriteToFileAsJSON(backendFileAbsolutePath, componentBackendConfig, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
} else if format == "hcl" { | ||
err = u.WriteTerraformBackendConfigToFileAsHcl(backendFileAbsolutePath, backendType, backendSection) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
return fmt.Errorf("invalid '--format' argument '%s'. Valid values are 'hcl' (default) and 'json", format) | ||
} | ||
} | ||
} | ||
|
||
fmt.Println() | ||
|
||
return nil | ||
} |
Oops, something went wrong.