Skip to content

Commit

Permalink
select default region from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaysomani07 committed Feb 20, 2023
1 parent 91631a1 commit a51b81f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 3 additions & 2 deletions cmd/localstackpopulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ var lspop = &cobra.Command{
"me-south-1", "me-central-1", "sa-east-1", "us-gov-east-1", "us-gov-west-1"}

var sessions []*session.Session

for i := 0; i < 4; i++ {
sessDef, err := getSession(profile, pop.GetDefaultAWSRegion())
sessions = append(sessions, sessDef)
for i := 1; i < 4; i++ {
gofakeit.Seed(0)
sess, err := getSession(profile, regions[gofakeit.Number(0, len(regions)-1)])
if err != nil {
Expand Down
12 changes: 9 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/one2nc/cloud-lens/internal/color"
"github.com/one2nc/cloud-lens/internal/config"
"github.com/one2nc/cloud-lens/internal/view"
pop "github.com/one2nc/cloud-lens/populator"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand All @@ -28,7 +29,7 @@ var (

func init() {
rootCmd.PersistentFlags().StringVarP(&profile, "profile", "p", "default", "Read aws profile")
rootCmd.PersistentFlags().StringVarP(&region, "region", "r", "ap-south-1", "Read aws region")
rootCmd.PersistentFlags().StringVarP(&region, "region", "r", "", "Read aws region")
}

func Execute() {
Expand All @@ -53,10 +54,15 @@ func run(cmd *cobra.Command, args []string) {

//TODO profiles and regions should under aws
profiles := readAndValidateProfile()
regions := readAndValidateRegion()
if profiles[0] == "default" && len(region) == 0 {
region = pop.GetDefaultAWSRegion()
} else {
region = "ap-south-1"
}

regions := readAndValidateRegion()
//TODO Move this in the AWS folder
sess, err := config.GetSession(profiles[0], regions[0])
sess, err := config.GetSession(profiles[0], pop.GetDefaultAWSRegion())
if err != nil {
panic(fmt.Sprintf("aws session init failed -- %v", err))
}
Expand Down
13 changes: 13 additions & 0 deletions populator/populator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package pop

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -670,3 +673,13 @@ func CreateLambdaFunction(sessions []*session.Session) error {
}
return nil
}

func GetDefaultAWSRegion() string {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load AWS SDK config: %v\n", err)
os.Exit(1)
}
region := cfg.Region
return region
}

0 comments on commit a51b81f

Please sign in to comment.