From 43b7dae699e5ff5db856d1fd56758155c4391e5f Mon Sep 17 00:00:00 2001 From: musa-asad Date: Fri, 13 Dec 2024 17:26:55 -0500 Subject: [PATCH] Remove redundant logic. --- util/awsservice/constant.go | 39 +++++++++++++------------------------ 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/util/awsservice/constant.go b/util/awsservice/constant.go index e9cd80edb..f9a3fd015 100644 --- a/util/awsservice/constant.go +++ b/util/awsservice/constant.go @@ -58,12 +58,23 @@ var ( func init() { ctx = context.Background() - var err error - awsCfg, err := config.LoadDefaultConfig(ctx, config.WithRegion("us-west-2")) + + err := ConfigureAWSClients("us-west-2") + if err != nil { + fmt.Println("There was an error trying to configure the AWS clients: ", err) + } +} + +// ConfigureAWSClients configures the AWS clients using a set region. +func ConfigureAWSClients(region string) error { + mu.Lock() + defer mu.Unlock() + + awsCfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region)) if err != nil { // handle error fmt.Println("There was an error trying to load default config: ", err) - return + return err } fmt.Println("This is the aws region: ", awsCfg.Region) @@ -78,28 +89,6 @@ func init() { S3Client = s3.NewFromConfig(awsCfg) CloudformationClient = cloudformation.NewFromConfig(awsCfg) XrayClient = xray.NewFromConfig(awsCfg) -} - -// ReconfigureAWSClients reconfigures the AWS clients using a new region. -func ReconfigureAWSClients(region string) error { - mu.Lock() - defer mu.Unlock() - - newCfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region)) - if err != nil { - return err - } - - Ec2Client = ec2.NewFromConfig(newCfg) - EcsClient = ecs.NewFromConfig(newCfg) - SsmClient = ssm.NewFromConfig(newCfg) - ImdsClient = imds.NewFromConfig(newCfg) - CwmClient = cloudwatch.NewFromConfig(newCfg) - CwlClient = cloudwatchlogs.NewFromConfig(newCfg) - DynamodbClient = dynamodb.NewFromConfig(newCfg) - S3Client = s3.NewFromConfig(newCfg) - CloudformationClient = cloudformation.NewFromConfig(newCfg) - XrayClient = xray.NewFromConfig(newCfg) return nil }