Skip to content

Commit

Permalink
fix: Add AWS Account ID to fogg init interview (#837)
Browse files Browse the repository at this point in the history
* FIX: Add init prompt, command line switch, provider parameter for global AWS Account ID

---------

Co-authored-by: David Spadea <[email protected]>
  • Loading branch information
dspadea and David Spadea authored Apr 20, 2023
1 parent 88d3808 commit 1f66cd3
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/exp_examine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func init() {
expCmd.AddCommand(examineCmd)
}

//TODO:(EC) Create a flag for path to walk
// TODO:(EC) Create a flag for path to walk
var examineCmd = &cobra.Command{
Use: "examine",
Short: "Detects terraform module updates",
Expand Down
10 changes: 10 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func init() {
initCmd.Flags().String("table", "", "Use this to pass the infra dynamo table name via CLI.")
initCmd.Flags().String("profile", "", "Use this to pass the aws auth profile via CLI.")
initCmd.Flags().String("owner", "", "Use this to pass the owner name via CLI.")
initCmd.Flags().String("aws-account-id", "", "Use this to pass the primary AWS Account ID via CLI.")
rootCmd.AddCommand(initCmd)
}

Expand Down Expand Up @@ -103,6 +104,15 @@ func userPrompt(cmd *cobra.Command) (*FoggProject, error) {
}
foggProject.Owner = &owner

awsAccountID, err := cmd.Flags().GetString("aws-account-id")
if err != nil {
return nil, err
}
if awsAccountID == "" {
awsAccountID = prompt.StringRequired("AWS Account ID?")
}
foggProject.AwsAccountID = &awsAccountID

return foggProject, nil
}

Expand Down
10 changes: 7 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"encoding/json"
"io/ioutil"
"path/filepath"

Expand All @@ -23,7 +24,9 @@ func defaultEnabled(a bool) *bool {
}

// InitConfig initializes the config file using user input
func InitConfig(project, region, bucket, table, awsProfile, owner *string, awsProviderVersion string) *v2.Config {
func InitConfig(project, region, bucket, table, awsProfile, owner, awsAccountID *string, awsProviderVersion string) *v2.Config {
accountID := json.Number(*awsAccountID)

return &v2.Config{
Defaults: v2.Defaults{
Common: v2.Common{
Expand All @@ -37,8 +40,9 @@ func InitConfig(project, region, bucket, table, awsProfile, owner *string, awsPr
Project: project,
Providers: &v2.Providers{
AWS: &v2.AWSProvider{
Profile: awsProfile,
Region: region,
AccountID: &accountID,
Profile: awsProfile,
Region: region,
CommonProvider: v2.CommonProvider{
Enabled: defaultEnabled(true),
Version: &awsProviderVersion,
Expand Down
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func getPtr(val string) *string {

func TestInitConfig(t *testing.T) {
r := require.New(t)
c := InitConfig(getPtr("proj"), getPtr("reg"), getPtr("buck"), getPtr("table"), getPtr("prof"), getPtr("[email protected]"), "0.99.0")
c := InitConfig(getPtr("proj"), getPtr("reg"), getPtr("buck"), getPtr("table"), getPtr("prof"), getPtr("[email protected]"), getPtr("123456789"), "0.99.0")
r.Equal("prof", *c.Defaults.Common.Backend.Profile)
r.Equal("prof", *c.Defaults.Providers.AWS.Profile)
r.Equal("reg", *c.Defaults.Providers.AWS.Region)
Expand Down
4 changes: 2 additions & 2 deletions exp/examine/examine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"github.com/spf13/afero"
)

//Examine loads local modules and compares them to their latest version to see differences
//TODO: Comparison between local and latest
// Examine loads local modules and compares them to their latest version to see differences
// TODO: Comparison between local and latest
func Examine(fs afero.Fs, path string) error {
//Collect local modules to be updated
module, err := GetLocalModules(fs, path)
Expand Down
4 changes: 2 additions & 2 deletions exp/examine/latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/spf13/afero"
)

//LatestModuleVersions retrieves the latest version of the provided modules
// LatestModuleVersions retrieves the latest version of the provided modules
func LatestModuleVersions(fs afero.Fs, module *tfconfig.Module) ([]ModuleWrapper, error) {
var latestModules []ModuleWrapper
var moduleWrapper ModuleWrapper
Expand Down Expand Up @@ -38,7 +38,7 @@ func LatestModuleVersions(fs afero.Fs, module *tfconfig.Module) ([]ModuleWrapper
return latestModules, nil
}

//createGitURL retrieves the latest release version and creates an HTTP accessible link
// createGitURL retrieves the latest release version and creates an HTTP accessible link
func createGitURL(moduleCall *tfconfig.ModuleCall) (string, error) {
splitString := strings.Split(moduleCall.Source, "/")
owner, repo := splitString[1], splitString[2]
Expand Down
4 changes: 2 additions & 2 deletions exp/examine/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

//**Local refers to any files located within your local file system**

//GetLocalModules retrieves all terraform modules within a given directory
//TODO:(EC) Define local and global modules OR rename the values
// GetLocalModules retrieves all terraform modules within a given directory
// TODO:(EC) Define local and global modules OR rename the values
func GetLocalModules(fs afero.Fs, dir string) (*tfconfig.Module, error) {
_, err := os.Stat(dir)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion exp/examine/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require"
)

//TODO: Move fs to versioning.go
// TODO: Move fs to versioning.go
func TestGetLocalModules(t *testing.T) {
r := require.New(t)
pwd, err := os.Getwd()
Expand Down
4 changes: 2 additions & 2 deletions exp/examine/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/afero"
)

//TODO:(EC) Add a RegistryModule field
// TODO:(EC) Add a RegistryModule field
type ModuleWrapper struct {
moduleSource string
version string
Expand Down Expand Up @@ -62,7 +62,7 @@ type Submodule struct {
const githubURL = "github.com"
const tagPattern = "ref="

//GetFromGithub Retrieves modules that are available through github
// GetFromGithub Retrieves modules that are available through github
func GetFromGithub(fs afero.Fs, repo string) (*tfconfig.Module, error) {
//FIXME: (EC) Create temporary directory, when tests fail directory stays
//TODO: Make directory name more general
Expand Down
3 changes: 2 additions & 1 deletion init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
const AWSProviderVersion = "4.34.0"

type FoggProject struct {
Project, Region, Bucket, Table, Profile, Owner *string
Project, Region, Bucket, Table, Profile, Owner, AwsAccountID *string
}

// Init reads user console input and generates a fogg.yml file
Expand All @@ -20,6 +20,7 @@ func Init(fs afero.Fs, foggProject *FoggProject) error {
foggProject.Table,
foggProject.Profile,
foggProject.Owner,
foggProject.AwsAccountID,
AWSProviderVersion,
)
e := config.Write(fs, "fogg.yml")
Expand Down
4 changes: 3 additions & 1 deletion init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ func TestInit(t *testing.T) {
table := "acme"
profile := "acme-auth"
owner := "[email protected]"
awsAccountID := "123456789"

fs, _, err := util.TestFs()
r.NoError(err)

conf := config.InitConfig(&project, &region, &bucket, &table, &profile, &owner, AWSProviderVersion)
conf := config.InitConfig(&project, &region, &bucket, &table, &profile, &owner, &awsAccountID, AWSProviderVersion)
r.NotNil(conf)
r.Equal(config.DefaultFoggVersion, conf.Version)

Expand Down
4 changes: 2 additions & 2 deletions migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"github.com/spf13/afero"
)

//Migration Defines a fogg migration and the actions that it can perform
// Migration Defines a fogg migration and the actions that it can perform
type Migration interface {
Description() string //Describes the migration taking
Guard(afero.Fs, string) (bool, error) //Returns true if migration is runnable, otherwise error
Migrate(afero.Fs, string) (string, error) //Returns path to config and err
Prompt() bool //Returns whether the user would like to run the migration
}

//RunMigrations cycles through a list of migrations and applies them if necessary
// RunMigrations cycles through a list of migrations and applies them if necessary
func RunMigrations(fs afero.Fs, configFile string, forceApply bool) error {
migrations := []Migration{}

Expand Down

0 comments on commit 1f66cd3

Please sign in to comment.