From 4dbe2e683ab23181c9b9541c49f908fdf6dbff11 Mon Sep 17 00:00:00 2001 From: Matt Weagle Date: Fri, 8 Oct 2021 22:32:38 -0700 Subject: [PATCH] Incremental checkin to ensure consistent context --- IMPORTS.txt | 2 +- apigateway.go | 11 +-- aws/cloudformation/impl_aws_binary.go | 6 +- ...cloudWatchLogsLambdaEventSourceResource.go | 15 ++-- .../codeCommitLambdaEventSourceResource.go | 19 +++-- .../resources/customResource.go | 14 ++-- .../resources/helloWorldResource_test.go | 4 +- .../resources/helloworldresource.go | 7 +- .../resources/s3ArtifactPublisherResource.go | 10 +-- .../resources/s3LambdaEventSourceResource.go | 6 +- .../resources/sesLambdaEventSourceResource.go | 6 +- .../resources/snsLambdaEventSourceResource.go | 6 +- .../resources/zipToS3BucketResource.go | 6 +- .../zipToS3BucketResourceTest_test.go | 7 +- aws/cloudformation/util.go | 64 +++++++++------ aws/cloudwatch/metrics_awsbinary.go | 82 ++++++++++--------- aws/session.go | 4 +- aws/step/dynamodb.go | 5 +- cmd_build.go | 23 +++--- cmd_delete.go | 4 +- cmd_delete_test.go | 3 +- cmd_describe.go | 4 +- cmd_execute_awsbinary.go | 4 +- cmd_explore.go | 16 ++-- cmd_explore_views.go | 13 +-- cmd_provision_build.go | 20 +++-- cmd_status.go | 7 +- cmd_status_test.go | 3 +- decorator/discovery.go | 10 ++- docker/docker.go | 8 +- docs_source/content/example_service/step1.md | 2 +- hook/docker_build_hook_test.go | 4 +- profile_loop_awsbinary.go | 28 ++++--- profile_loop_build.go | 20 +++-- sparta_constants.go | 6 +- sparta_main_awsbinary.go | 7 +- sparta_main_build.go | 18 ++-- test.go | 3 +- testing/provision.go | 4 +- 39 files changed, 276 insertions(+), 205 deletions(-) diff --git a/IMPORTS.txt b/IMPORTS.txt index e9368ac3c..009aba232 100644 --- a/IMPORTS.txt +++ b/IMPORTS.txt @@ -2,6 +2,7 @@ awsv2 "github.com/aws/aws-sdk-go-v2/aws" awsv2Config "github.com/aws/aws-sdk-go-v2/config" awsv2S3 "github.com/aws/aws-sdk-go-v2/service/s3" awsv2S3Types "github.com/aws/aws-sdk-go-v2/service/s3/types" +awsv2S3Manager "github.com/aws/aws-sdk-go-v2/feature/s3/manager" awsv2Lambda "github.com/aws/aws-sdk-go-v2/service/lambda" awsv2LambdaTypes "github.com/aws/aws-sdk-go-v2/service/lambda/types" awsv2IAM "github.com/aws/aws-sdk-go-v2/service/iam" @@ -21,7 +22,6 @@ awsv2SQS "github.com/aws/aws-sdk-go-v2/service/sqs" awsv2SQSTypes "github.com/aws/aws-sdk-go-v2/service/sqs/types" awsv2SNS "github.com/aws/aws-sdk-go-v2/service/sns" awsv2SNSTypes "github.com/aws/aws-sdk-go-v2/service/sns/types" -awsv2S3Manager "github.com/aws/aws-sdk-go-v2/feature/s3/manager" awsv2Dynamo "github.com/aws/aws-sdk-go-v2/service/dynamodb" awsv2DynamoTypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" awsv2DynamoAttributeValue "github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue" diff --git a/apigateway.go b/apigateway.go index 1c0965f69..b2846bbe1 100644 --- a/apigateway.go +++ b/apigateway.go @@ -202,7 +202,8 @@ func corsOptionsGatewayMethod(api *API, restAPIID string, resourceID string) *go return corsMethod } -func apiStageInfo(apiName string, +func apiStageInfo(ctx context.Context, + apiName string, stageName string, awsConfig awsv2.Config, noop bool, @@ -217,13 +218,12 @@ func apiStageInfo(apiName string, logger.Info().Msg(noopMessage("API Gateway check")) return nil, nil } - ctxStageInfo := context.Background() svc := awsv2APIG.NewFromConfig(awsConfig) restApisInput := &awsv2APIG.GetRestApisInput{ Limit: awsv2.Int32(500), } - restApisOutput, restApisOutputErr := svc.GetRestApis(ctxStageInfo, restApisInput) + restApisOutput, restApisOutputErr := svc.GetRestApis(ctx, restApisInput) if nil != restApisOutputErr { return nil, restApisOutputErr } @@ -244,7 +244,7 @@ func apiStageInfo(apiName string, stagesInput := &awsv2APIG.GetStagesInput{ RestApiId: awsv2.String(restAPIID), } - stagesOutput, stagesOutputErr := svc.GetStages(ctxStageInfo, stagesInput) + stagesOutput, stagesOutputErr := svc.GetStages(ctx, stagesInput) if nil != stagesOutputErr { return nil, stagesOutputErr } @@ -705,7 +705,8 @@ func (api *API) Marshal(serviceName string, if nil != api.stage { // Is the stack already deployed? stageName := api.stage.name - stageInfo, stageInfoErr := apiStageInfo(api.name, + stageInfo, stageInfoErr := apiStageInfo(context.Background(), + api.name, stageName, awsConfig, noop, diff --git a/aws/cloudformation/impl_aws_binary.go b/aws/cloudformation/impl_aws_binary.go index 2a2f42371..bf7ad249e 100644 --- a/aws/cloudformation/impl_aws_binary.go +++ b/aws/cloudformation/impl_aws_binary.go @@ -3,7 +3,9 @@ package cloudformation -import "github.com/aws/aws-sdk-go-v2/aws/session" +import ( + awsv2 "github.com/aws/aws-sdk-go-v2/aws" +) // https://blog.cloudflare.com/setting-go-variables-at-compile-time/ @@ -11,6 +13,6 @@ func platformUserName() string { return "" } -func platformAccountUserName(awsConfig aws.Config) (string, error) { +func platformAccountUserName(awsConfig awsv2.Config) (string, error) { return "", nil } diff --git a/aws/cloudformation/resources/cloudWatchLogsLambdaEventSourceResource.go b/aws/cloudformation/resources/cloudWatchLogsLambdaEventSourceResource.go index 82be72f3b..7b1e9ef20 100644 --- a/aws/cloudformation/resources/cloudWatchLogsLambdaEventSourceResource.go +++ b/aws/cloudformation/resources/cloudWatchLogsLambdaEventSourceResource.go @@ -52,7 +52,8 @@ func cloudWatchEventSourceProperties(event *CloudFormationLambdaEvent) (*CloudWa return &eventProperties, nil } -func (command CloudWatchLogsLambdaEventSourceResource) updateRegistration(isTargetActive bool, +func (command CloudWatchLogsLambdaEventSourceResource) updateRegistration(ctx context.Context, + isTargetActive bool, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { @@ -123,22 +124,22 @@ func (command CloudWatchLogsLambdaEventSourceResource) updateRegistration(isTarg } // Create implements the create operation -func (command CloudWatchLogsLambdaEventSourceResource) Create(awsConfig awsv2.Config, +func (command CloudWatchLogsLambdaEventSourceResource) Create(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { - return command.updateRegistration(true, awsConfig, event, logger) + return command.updateRegistration(ctx, true, awsConfig, event, logger) } // Update implements the update operation -func (command CloudWatchLogsLambdaEventSourceResource) Update(awsConfig awsv2.Config, +func (command CloudWatchLogsLambdaEventSourceResource) Update(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { - return command.updateRegistration(true, awsConfig, event, logger) + return command.updateRegistration(ctx, true, awsConfig, event, logger) } // Delete implements the delete operation -func (command CloudWatchLogsLambdaEventSourceResource) Delete(awsConfig awsv2.Config, +func (command CloudWatchLogsLambdaEventSourceResource) Delete(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { - return command.updateRegistration(false, awsConfig, event, logger) + return command.updateRegistration(ctx, false, awsConfig, event, logger) } diff --git a/aws/cloudformation/resources/codeCommitLambdaEventSourceResource.go b/aws/cloudformation/resources/codeCommitLambdaEventSourceResource.go index 3f48c6ecd..7c7ed5928 100644 --- a/aws/cloudformation/resources/codeCommitLambdaEventSourceResource.go +++ b/aws/cloudformation/resources/codeCommitLambdaEventSourceResource.go @@ -28,7 +28,8 @@ type CodeCommitLambdaEventSourceResource struct { gof.CustomResource } -func (command CodeCommitLambdaEventSourceResource) updateRegistration(isTargetActive bool, +func (command CodeCommitLambdaEventSourceResource) updateRegistration(ctx context.Context, + isTargetActive bool, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { @@ -48,7 +49,7 @@ func (command CodeCommitLambdaEventSourceResource) updateRegistration(isTargetAc ccInput := &awsv2CodeCommit.GetRepositoryTriggersInput{ RepositoryName: awsv2.String(request.RepositoryName), } - triggers, triggersErr := codeCommitSvc.GetRepositoryTriggers(context.Background(), ccInput) + triggers, triggersErr := codeCommitSvc.GetRepositoryTriggers(ctx, ccInput) if triggersErr != nil { return nil, triggersErr } @@ -104,7 +105,7 @@ func (command CodeCommitLambdaEventSourceResource) updateRegistration(isTargetAc RepositoryName: awsv2.String(request.RepositoryName), Triggers: putTriggers, } - putTriggersResp, putTriggersRespErr := codeCommitSvc.PutRepositoryTriggers(context.Background(), putTriggersInput) + putTriggersResp, putTriggersRespErr := codeCommitSvc.PutRepositoryTriggers(ctx, putTriggersInput) // Just log it... logger.Info(). Interface("Response", putTriggersResp). @@ -121,22 +122,22 @@ func (command *CodeCommitLambdaEventSourceResource) IAMPrivileges() []string { } // Create implements the custom resource create operation -func (command CodeCommitLambdaEventSourceResource) Create(awsConfig awsv2.Config, +func (command CodeCommitLambdaEventSourceResource) Create(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { - return command.updateRegistration(true, awsConfig, event, logger) + return command.updateRegistration(ctx, true, awsConfig, event, logger) } // Update implements the custom resource update operation -func (command CodeCommitLambdaEventSourceResource) Update(awsConfig awsv2.Config, +func (command CodeCommitLambdaEventSourceResource) Update(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { - return command.updateRegistration(true, awsConfig, event, logger) + return command.updateRegistration(ctx, true, awsConfig, event, logger) } // Delete implements the custom resource delete operation -func (command CodeCommitLambdaEventSourceResource) Delete(awsConfig awsv2.Config, +func (command CodeCommitLambdaEventSourceResource) Delete(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { - return command.updateRegistration(false, awsConfig, event, logger) + return command.updateRegistration(ctx, false, awsConfig, event, logger) } diff --git a/aws/cloudformation/resources/customResource.go b/aws/cloudformation/resources/customResource.go index 2d7243477..4d5f0fbca 100644 --- a/aws/cloudformation/resources/customResource.go +++ b/aws/cloudformation/resources/customResource.go @@ -164,15 +164,15 @@ func init() { // CustomResourceCommand defines operations that a CustomResource must implement. type CustomResourceCommand interface { - Create(awsConfig awsv2.Config, + Create(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) - Update(awsConfig awsv2.Config, + Update(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) - Delete(awsConfig awsv2.Config, + Delete(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) } @@ -340,7 +340,7 @@ func SendCloudFormationResponse(lambdaCtx *awsLambdaCtx.LambdaContext, return nil } -// Returns an AWS Session (https://github.com/aws/aws-sdk-go/wiki/Getting-Started-Configuration) +// Returns an AWS Config (https://github.com/aws/aws-sdk-go-v2/blob/main/config/doc.go) // object that attaches a debug level handler to all AWS requests from services // sharing the session value. func newAWSConfig(logger *zerolog.Logger) awsv2.Config { @@ -404,11 +404,11 @@ func CloudFormationLambdaCustomResourceHandler(command CustomResourceCommand, if opErr == nil && executeOperation { switch event.RequestType { case CreateOperation: - opResults, opErr = command.Create(customResourceConfig, &event, logger) + opResults, opErr = command.Create(ctx, customResourceConfig, &event, logger) case DeleteOperation: - opResults, opErr = command.Delete(customResourceConfig, &event, logger) + opResults, opErr = command.Delete(ctx, customResourceConfig, &event, logger) case UpdateOperation: - opResults, opErr = command.Update(customResourceConfig, &event, logger) + opResults, opErr = command.Update(ctx, customResourceConfig, &event, logger) } } // Notify CloudFormation of the result diff --git a/aws/cloudformation/resources/helloWorldResource_test.go b/aws/cloudformation/resources/helloWorldResource_test.go index 4dffb4ef6..8866dfcd8 100644 --- a/aws/cloudformation/resources/helloWorldResource_test.go +++ b/aws/cloudformation/resources/helloWorldResource_test.go @@ -1,6 +1,7 @@ package resources import ( + "context" "encoding/json" "os" "testing" @@ -63,7 +64,8 @@ func TestExecuteCreateHelloWorld(t *testing.T) { }) awsConfig := newAWSConfig(&logger) - createOutputs, createError := customResource1.Create(awsConfig, + createOutputs, createError := customResource1.Create(context.Background(), + awsConfig, mockHelloWorldResourceEvent(t), &logger) if nil != createError { diff --git a/aws/cloudformation/resources/helloworldresource.go b/aws/cloudformation/resources/helloworldresource.go index c24661763..1a24435a3 100644 --- a/aws/cloudformation/resources/helloworldresource.go +++ b/aws/cloudformation/resources/helloworldresource.go @@ -1,6 +1,7 @@ package resources import ( + "context" "encoding/json" awsv2 "github.com/aws/aws-sdk-go-v2/aws" @@ -27,7 +28,7 @@ func (command *HelloWorldResource) IAMPrivileges() []string { } // Create implements resource create -func (command HelloWorldResource) Create(awsConfig awsv2.Config, +func (command HelloWorldResource) Create(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { request := HelloWorldResourceRequest{} @@ -42,7 +43,7 @@ func (command HelloWorldResource) Create(awsConfig awsv2.Config, } // Update implements resource update -func (command HelloWorldResource) Update(awsConfig awsv2.Config, +func (command HelloWorldResource) Update(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { request := HelloWorldResourceRequest{} @@ -55,7 +56,7 @@ func (command HelloWorldResource) Update(awsConfig awsv2.Config, } // Delete implements resource delete -func (command HelloWorldResource) Delete(awsConfig awsv2.Config, +func (command HelloWorldResource) Delete(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { request := HelloWorldResourceRequest{} diff --git a/aws/cloudformation/resources/s3ArtifactPublisherResource.go b/aws/cloudformation/resources/s3ArtifactPublisherResource.go index 61555517a..305612d46 100644 --- a/aws/cloudformation/resources/s3ArtifactPublisherResource.go +++ b/aws/cloudformation/resources/s3ArtifactPublisherResource.go @@ -33,7 +33,7 @@ func (command *S3ArtifactPublisherResource) IAMPrivileges() []string { } // Create implements the S3 create operation -func (command S3ArtifactPublisherResource) Create(awsConfig awsv2.Config, +func (command S3ArtifactPublisherResource) Create(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { @@ -63,14 +63,14 @@ func (command S3ArtifactPublisherResource) Create(awsConfig awsv2.Config, } // Update implements the S3 update operation -func (command S3ArtifactPublisherResource) Update(awsConfig awsv2.Config, +func (command S3ArtifactPublisherResource) Update(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { - return command.Create(awsConfig, event, logger) + return command.Create(ctx, awsConfig, event, logger) } // Delete implements the S3 delete operation -func (command S3ArtifactPublisherResource) Delete(awsConfig awsv2.Config, +func (command S3ArtifactPublisherResource) Delete(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { @@ -84,7 +84,7 @@ func (command S3ArtifactPublisherResource) Delete(awsConfig awsv2.Config, Key: awsv2.String(s3ArtifactPublisherRequest.Key), } s3Svc := awsv2S3.NewFromConfig(awsConfig) - _, s3ResponseErr := s3Svc.DeleteObject(context.Background(), s3DeleteObjectParams) + _, s3ResponseErr := s3Svc.DeleteObject(ctx, s3DeleteObjectParams) if s3ResponseErr != nil { return nil, s3ResponseErr } diff --git a/aws/cloudformation/resources/s3LambdaEventSourceResource.go b/aws/cloudformation/resources/s3LambdaEventSourceResource.go index 25b63877e..2c36695de 100644 --- a/aws/cloudformation/resources/s3LambdaEventSourceResource.go +++ b/aws/cloudformation/resources/s3LambdaEventSourceResource.go @@ -104,21 +104,21 @@ func (command S3LambdaEventSourceResource) updateNotification(isTargetActive boo } // Create implements the custom resource create operation -func (command S3LambdaEventSourceResource) Create(awsConfig awsv2.Config, +func (command S3LambdaEventSourceResource) Create(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { return command.updateNotification(true, awsConfig, event, logger) } // Update implements the custom resource update operation -func (command S3LambdaEventSourceResource) Update(awsConfig awsv2.Config, +func (command S3LambdaEventSourceResource) Update(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { return command.updateNotification(true, awsConfig, event, logger) } // Delete implements the custom resource delete operation -func (command S3LambdaEventSourceResource) Delete(awsConfig awsv2.Config, +func (command S3LambdaEventSourceResource) Delete(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { return command.updateNotification(false, awsConfig, event, logger) diff --git a/aws/cloudformation/resources/sesLambdaEventSourceResource.go b/aws/cloudformation/resources/sesLambdaEventSourceResource.go index f7641aabf..4073692bd 100644 --- a/aws/cloudformation/resources/sesLambdaEventSourceResource.go +++ b/aws/cloudformation/resources/sesLambdaEventSourceResource.go @@ -169,21 +169,21 @@ func (command *SESLambdaEventSourceResource) IAMPrivileges() []string { } // Create implements the custom resource create operation -func (command SESLambdaEventSourceResource) Create(awsConfig awsv2.Config, +func (command SESLambdaEventSourceResource) Create(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { return command.updateSESRules(true, awsConfig, event, logger) } // Update implements the custom resource update operation -func (command SESLambdaEventSourceResource) Update(awsConfig awsv2.Config, +func (command SESLambdaEventSourceResource) Update(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { return command.updateSESRules(true, awsConfig, event, logger) } // Delete implements the custom resource delete operation -func (command SESLambdaEventSourceResource) Delete(awsConfig awsv2.Config, +func (command SESLambdaEventSourceResource) Delete(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { return command.updateSESRules(false, awsConfig, event, logger) diff --git a/aws/cloudformation/resources/snsLambdaEventSourceResource.go b/aws/cloudformation/resources/snsLambdaEventSourceResource.go index 19de487a0..6cf0c9c6c 100644 --- a/aws/cloudformation/resources/snsLambdaEventSourceResource.go +++ b/aws/cloudformation/resources/snsLambdaEventSourceResource.go @@ -97,21 +97,21 @@ func (command *SNSLambdaEventSourceResource) IAMPrivileges() []string { } // Create implements the custom resource create operation -func (command SNSLambdaEventSourceResource) Create(awsConfig awsv2.Config, +func (command SNSLambdaEventSourceResource) Create(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { return command.updateRegistration(true, awsConfig, event, logger) } // Update implements the custom resource update operation -func (command SNSLambdaEventSourceResource) Update(awsConfig awsv2.Config, +func (command SNSLambdaEventSourceResource) Update(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { return command.updateRegistration(true, awsConfig, event, logger) } // Delete implements the custom resource delete operation -func (command SNSLambdaEventSourceResource) Delete(awsConfig awsv2.Config, +func (command SNSLambdaEventSourceResource) Delete(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { return command.updateRegistration(false, awsConfig, event, logger) diff --git a/aws/cloudformation/resources/zipToS3BucketResource.go b/aws/cloudformation/resources/zipToS3BucketResource.go index 453ebfff7..9d3c9f536 100644 --- a/aws/cloudformation/resources/zipToS3BucketResource.go +++ b/aws/cloudformation/resources/zipToS3BucketResource.go @@ -156,21 +156,21 @@ func (command *ZipToS3BucketResource) IAMPrivileges() []string { } // Create implements the custom resource create operation -func (command ZipToS3BucketResource) Create(awsConfig awsv2.Config, +func (command ZipToS3BucketResource) Create(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { return command.unzip(awsConfig, event, logger) } // Update implements the custom resource update operation -func (command ZipToS3BucketResource) Update(awsConfig awsv2.Config, +func (command ZipToS3BucketResource) Update(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { return command.unzip(awsConfig, event, logger) } // Delete implements the custom resource delete operation -func (command ZipToS3BucketResource) Delete(awsConfig awsv2.Config, +func (command ZipToS3BucketResource) Delete(ctx context.Context, awsConfig awsv2.Config, event *CloudFormationLambdaEvent, logger *zerolog.Logger) (map[string]interface{}, error) { request := ZipToS3BucketResourceRequest{} diff --git a/aws/cloudformation/resources/zipToS3BucketResourceTest_test.go b/aws/cloudformation/resources/zipToS3BucketResourceTest_test.go index 1bdc6cae5..35bd22a3b 100644 --- a/aws/cloudformation/resources/zipToS3BucketResourceTest_test.go +++ b/aws/cloudformation/resources/zipToS3BucketResourceTest_test.go @@ -50,8 +50,10 @@ func TestUnzip(t *testing.T) { event := mockZipResourceEvent(t) // Put it + testContext := context.Background() awsConfig, _ := awsv2Config.LoadDefaultConfig(context.Background()) - createOutputs, createError := zipResource.Create(awsConfig, + createOutputs, createError := zipResource.Create(testContext, + awsConfig, event, &logger) if nil != createError { @@ -59,7 +61,8 @@ func TestUnzip(t *testing.T) { } t.Logf("TestUnzip outputs: %#v", createOutputs) - deleteOutputs, deleteError := zipResource.Delete(awsConfig, + deleteOutputs, deleteError := zipResource.Delete(testContext, + awsConfig, event, &logger) if nil != deleteError { diff --git a/aws/cloudformation/util.go b/aws/cloudformation/util.go index a7925be00..cfd3e7b64 100644 --- a/aws/cloudformation/util.go +++ b/aws/cloudformation/util.go @@ -195,7 +195,8 @@ func cloudformationPollingDelay() time.Duration { return time.Duration(3+rand.Int31n(5)) * time.Second } -func updateStackViaChangeSet(serviceName string, +func updateStackViaChangeSet(ctx context.Context, + serviceName string, cfTemplate *gof.Template, cfTemplateURL string, stackParameters map[string]string, @@ -205,7 +206,8 @@ func updateStackViaChangeSet(serviceName string, // Create a change set name... changeSetRequestName := ResourceName(fmt.Sprintf("%sChangeSet", serviceName)) - _, changesErr := CreateStackChangeSet(changeSetRequestName, + _, changesErr := CreateStackChangeSet(ctx, + changeSetRequestName, serviceName, cfTemplate, cfTemplateURL, @@ -223,7 +225,7 @@ func updateStackViaChangeSet(serviceName string, ChangeSetName: awsv2.String(changeSetRequestName), StackName: awsv2.String(serviceName), } - executeChangeSetOutput, executeChangeSetError := awsCloudFormation.ExecuteChangeSet(context.Background(), + executeChangeSetOutput, executeChangeSetError := awsCloudFormation.ExecuteChangeSet(ctx, &executeChangeSetInput) logger.Debug(). @@ -395,7 +397,8 @@ func ConvertToInlineJSONTemplateExpression(templateData io.Reader, } // StackEvents returns the slice of awsv2CF.StackEvents for the given stackID or stackName -func StackEvents(stackID string, +func StackEvents(ctx context.Context, + stackID string, eventFilterLowerBoundInclusive time.Time, awsConfig awsv2.Config) ([]awsv2CFTypes.StackEvent, error) { @@ -411,7 +414,7 @@ func StackEvents(stackID string, params.NextToken = awsv2.String(nextToken) } - resp, err := cfService.DescribeStackEvents(context.Background(), params) + resp, err := cfService.DescribeStackEvents(ctx, params) if nil != err { return nil, err } @@ -440,7 +443,8 @@ type WaitForStackOperationCompleteResult struct { // WaitForStackOperationComplete is a blocking, polling based call that // periodically fetches the stackID set of events and uses the state value // to determine if an operation is complete -func WaitForStackOperationComplete(stackID string, +func WaitForStackOperationComplete(ctx context.Context, + stackID string, pollingMessage string, awsCloudFormation *awsv2CF.Client, logger *zerolog.Logger) (*WaitForStackOperationCompleteResult, error) { @@ -485,7 +489,7 @@ func WaitForStackOperationComplete(stackID string, sleepDuration := time.Duration(11+rand.Int31n(13)) * time.Second time.Sleep(sleepDuration) - describeStacksOutput, err := awsCloudFormation.DescribeStacks(context.Background(), describeStacksInput) + describeStacksOutput, err := awsCloudFormation.DescribeStacks(ctx, describeStacksInput) if nil != err { // TODO - add retry iff we're RateExceeded due to collective access @@ -557,7 +561,8 @@ func ResourceName(prefix string, parts ...string) string { // UploadTemplate marshals the given cfTemplate and uploads it to the // supplied bucket using the given KeyName -func UploadTemplate(serviceName string, +func UploadTemplate(ctx context.Context, + serviceName string, cfTemplate *gof.Template, s3Bucket string, s3KeyName string, @@ -588,7 +593,7 @@ func UploadTemplate(serviceName string, ContentType: awsv2.String("application/json"), Body: strings.NewReader(contentBody), } - templateUploadResult, templateUploadResultErr := s3Uploader.Upload(context.Background(), uploadInput) + templateUploadResult, templateUploadResultErr := s3Uploader.Upload(ctx, uploadInput) if nil != templateUploadResultErr { return "", templateUploadResultErr } @@ -610,7 +615,7 @@ func StackExists(ctx context.Context, describeStacksInput := &awsv2CF.DescribeStacksInput{ StackName: awsv2.String(stackNameOrID), } - describeStacksOutput, err := cf.DescribeStacks(context.Background(), describeStacksInput) + describeStacksOutput, err := cf.DescribeStacks(ctx, describeStacksInput) logger.Debug(). Interface("DescribeStackOutput", describeStacksOutput). @@ -636,7 +641,8 @@ func StackExists(ctx context.Context, // CreateStackChangeSet returns the DescribeChangeSetOutput // for a given stack transformation -func CreateStackChangeSet(changeSetRequestName string, +func CreateStackChangeSet(ctx context.Context, + changeSetRequestName string, serviceName string, cfTemplate *gof.Template, templateURL string, @@ -676,7 +682,7 @@ func CreateStackChangeSet(changeSetRequestName string, } changeSetInput.Tags = awsTags } - _, changeSetError := awsCloudFormation.CreateChangeSet(context.Background(), changeSetInput) + _, changeSetError := awsCloudFormation.CreateChangeSet(ctx, changeSetInput) if nil != changeSetError { return nil, changeSetError } @@ -699,7 +705,7 @@ func CreateStackChangeSet(changeSetRequestName string, sleepDuration := cloudformationPollingDelay() time.Sleep(sleepDuration) - changeSetOutput, describeChangeSetError := awsCloudFormation.DescribeChangeSet(context.Background(), &describeChangeSetInput) + changeSetOutput, describeChangeSetError := awsCloudFormation.DescribeChangeSet(ctx, &describeChangeSetInput) if nil != describeChangeSetError { return nil, describeChangeSetError @@ -736,7 +742,8 @@ func CreateStackChangeSet(changeSetRequestName string, Msg("No changes detected for service") // Delete it... - _, deleteChangeSetResultErr := DeleteChangeSet(serviceName, + _, deleteChangeSetResultErr := DeleteChangeSet(ctx, + serviceName, changeSetRequestName, awsCloudFormation) return nil, deleteChangeSetResultErr @@ -747,7 +754,8 @@ func CreateStackChangeSet(changeSetRequestName string, // DeleteChangeSet is a utility function that attempts to delete // an existing CloudFormation change set, with a bit of retry // logic in case of EC -func DeleteChangeSet(stackName string, +func DeleteChangeSet(ctx context.Context, + stackName string, changeSetRequestName string, awsCloudFormation *awsv2CF.Client) (*awsv2CF.DeleteChangeSetOutput, error) { @@ -761,7 +769,7 @@ func DeleteChangeSet(stackName string, for { elapsedTime := time.Since(startTime) - deleteChangeSetResults, deleteChangeSetResultErr := awsCloudFormation.DeleteChangeSet(context.Background(), &deleteChangeSetInput) + deleteChangeSetResults, deleteChangeSetResultErr := awsCloudFormation.DeleteChangeSet(ctx, &deleteChangeSetInput) if nil == deleteChangeSetResultErr { return deleteChangeSetResults, nil } else if strings.Contains(deleteChangeSetResultErr.Error(), "CREATE_IN_PROGRESS") { @@ -777,9 +785,10 @@ func DeleteChangeSet(stackName string, } // ListStacks returns a slice of stacks that meet the given filter. -func ListStacks(awsConfig awsv2.Config, +func ListStacks(ctx context.Context, + awsConfig awsv2.Config, maxReturned int, - stackFilters ...string) ([]awsv2CFTypes.StackSummary, error) { + stackFilters ...awsv2CFTypes.StackStatus) ([]awsv2CFTypes.StackSummary, error) { listStackInput := &awsv2CF.ListStacksInput{ StackStatusFilter: []awsv2CFTypes.StackStatus{}, @@ -790,7 +799,7 @@ func ListStacks(awsConfig awsv2.Config, cloudformationSvc := awsv2CF.NewFromConfig(awsConfig) accumulator := []awsv2CFTypes.StackSummary{} for { - listResult, listResultErr := cloudformationSvc.ListStacks(context.Background(), listStackInput) + listResult, listResultErr := cloudformationSvc.ListStacks(ctx, listStackInput) if listResultErr != nil { return nil, listResultErr } @@ -805,7 +814,8 @@ func ListStacks(awsConfig awsv2.Config, // ConvergeStackState ensures that the serviceName converges to the template // state defined by cfTemplate. This function establishes a polling loop to determine // when the stack operation has completed. -func ConvergeStackState(serviceName string, +func ConvergeStackState(ctx context.Context, + serviceName string, cfTemplate *gof.Template, templateURL string, stackParameters map[string]string, @@ -828,13 +838,14 @@ func ConvergeStackState(serviceName string, cloudformationSvc := awsv2CF.NewFromConfig(awsConfig) // Update the tags - exists, existsErr := StackExists(context.Background(), serviceName, awsConfig, logger) + exists, existsErr := StackExists(ctx, serviceName, awsConfig, logger) if nil != existsErr { return nil, existsErr } stackID := "" if exists { - updateErr := updateStackViaChangeSet(serviceName, + updateErr := updateStackViaChangeSet(ctx, + serviceName, cfTemplate, templateURL, stackParameters, @@ -883,7 +894,7 @@ func ConvergeStackState(serviceName string, Interface("StackInput", createStackInput). Msg("Create stack input") - createStackResponse, createStackResponseErr := cloudformationSvc.CreateStack(context.Background(), createStackInput) + createStackResponse, createStackResponseErr := cloudformationSvc.CreateStack(ctx, createStackInput) if nil != createStackResponseErr { return nil, createStackResponseErr } @@ -895,7 +906,8 @@ func ConvergeStackState(serviceName string, // Wait for the operation to succeed pollingMessage := "Waiting for CloudFormation operation to complete" - convergeResult, convergeErr := WaitForStackOperationComplete(stackID, + convergeResult, convergeErr := WaitForStackOperationComplete(ctx, + stackID, pollingMessage, cloudformationSvc, logger) @@ -906,7 +918,7 @@ func ConvergeStackState(serviceName string, // or summary information resourceMetrics := make(map[string]*resourceProvisionMetrics) errorMessages := []string{} - events, err := StackEvents(stackID, startTime, awsConfig) + events, err := StackEvents(ctx, stackID, startTime, awsConfig) if nil != err { return nil, fmt.Errorf("failed to retrieve stack events: %s", err.Error()) } @@ -993,7 +1005,7 @@ func ConvergeStackState(serviceName string, logger.Info(). Str("Value", *eachOutput.OutputValue). Str("Description", *eachOutput.Description). - Msgf(" %s", eachOutput.OutputKey) + Msgf(" %s", *eachOutput.OutputKey) } } return convergeResult.stackInfo, nil diff --git a/aws/cloudwatch/metrics_awsbinary.go b/aws/cloudwatch/metrics_awsbinary.go index 50c1a8624..5689067e1 100644 --- a/aws/cloudwatch/metrics_awsbinary.go +++ b/aws/cloudwatch/metrics_awsbinary.go @@ -4,11 +4,13 @@ package cloudwatch import ( + "context" "os" "time" - "github.com/aws/aws-sdk-go-v2/aws" - awsCloudWatch "github.com/aws/aws-sdk-go-v2/service/cloudwatch" + awsv2 "github.com/aws/aws-sdk-go-v2/aws" + awsv2CW "github.com/aws/aws-sdk-go-v2/service/cloudwatch" + awsv2CWTypes "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types" sparta "github.com/mweagle/Sparta" spartaAWS "github.com/mweagle/Sparta/aws" "github.com/rs/zerolog" @@ -23,10 +25,10 @@ import ( func publishMetrics(customDimensionMap map[string]string) { currentTime := time.Now() - // https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html + // https://docs.awsv2.amazon.com/lambda/latest/dg/current-supported-versions.html functionName := os.Getenv("AWS_LAMBDA_FUNCTION_NAME") cpuMetrics, cpuMetricsErr := gopsutilCPU.Percent(0, false) - // https://docs.aws.amazon.com/lambda/latest/dg/limits.html + // https://docs.awsv2.amazon.com/lambda/latest/dg/limits.html diskMetrics, diskMetricsErr := gopsutilDisk.Usage("/tmp") uptime, uptimeErr := gopsutilHost.Uptime() loadMetrics, loadMetricsErr := gopsutilLoad.Avg() @@ -50,70 +52,70 @@ func publishMetrics(customDimensionMap map[string]string) { Msg("Metric info") } // Return the array of metricDatum for the item - metricDatum := func(name string, value float64, unit MetricUnit) []*awsCloudWatch.MetricDatum { - defaultDatum := []*awsCloudWatch.MetricDatum{{ - MetricName: aws.String(name), - Dimensions: []*awsCloudWatch.Dimension{{ - Name: aws.String("Name"), - Value: aws.String(sparta.StampedServiceName), + metricDatum := func(name string, value float64, unit awsv2CWTypes.StandardUnit) []awsv2CWTypes.MetricDatum { + defaultDatum := []awsv2CWTypes.MetricDatum{{ + MetricName: awsv2.String(name), + Dimensions: []awsv2CWTypes.Dimension{{ + Name: awsv2.String("Name"), + Value: awsv2.String(sparta.StampedServiceName), }}, - Value: aws.Float64(value), + Value: awsv2.Float64(value), Timestamp: ¤tTime, - Unit: aws.String(string(unit)), + Unit: unit, }, } if len(customDimensionMap) != 0 { - metricDimension := []*awsCloudWatch.Dimension{{ - Name: aws.String("Name"), - Value: aws.String(sparta.StampedServiceName), + metricDimension := []awsv2CWTypes.Dimension{{ + Name: awsv2.String("Name"), + Value: awsv2.String(sparta.StampedServiceName), }} for eachKey, eachValue := range customDimensionMap { - metricDimension = append(metricDimension, &awsCloudWatch.Dimension{ - Name: aws.String(eachKey), - Value: aws.String(eachValue), + metricDimension = append(metricDimension, awsv2CWTypes.Dimension{ + Name: awsv2.String(eachKey), + Value: awsv2.String(eachValue), }) } - defaultDatum = append(defaultDatum, &awsCloudWatch.MetricDatum{ - MetricName: aws.String(name), + defaultDatum = append(defaultDatum, awsv2CWTypes.MetricDatum{ + MetricName: awsv2.String(name), Dimensions: metricDimension, - Value: aws.Float64(value), + Value: awsv2.Float64(value), Timestamp: ¤tTime, - Unit: aws.String(string(unit)), + Unit: unit, }) } return defaultDatum } // Publish all the metrics... - // https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html - metricData := []*awsCloudWatch.MetricDatum{} + // https://docs.awsv2.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html + metricData := []awsv2CWTypes.MetricDatum{} // CPU? if len(cpuMetrics) == 1 { - metricData = append(metricData, metricDatum("CPUPercent", cpuMetrics[0], UnitPercent)...) + metricData = append(metricData, metricDatum("CPUPercent", cpuMetrics[0], awsv2CWTypes.StandardUnitPercent)...) } if diskMetricsErr == nil { - metricData = append(metricData, metricDatum("DiskUsedPercent", diskMetrics.UsedPercent, UnitPercent)...) + metricData = append(metricData, metricDatum("DiskUsedPercent", diskMetrics.UsedPercent, awsv2CWTypes.StandardUnitPercent)...) } if uptimeErr == nil { - metricData = append(metricData, metricDatum("Uptime", float64(uptime), UnitMilliseconds)...) + metricData = append(metricData, metricDatum("Uptime", float64(uptime), awsv2CWTypes.StandardUnitMilliseconds)...) } if loadMetricsErr == nil { - metricData = append(metricData, metricDatum("Load1", loadMetrics.Load1, UnitNone)...) - metricData = append(metricData, metricDatum("Load5", loadMetrics.Load5, UnitNone)...) - metricData = append(metricData, metricDatum("Load15", loadMetrics.Load15, UnitNone)...) + metricData = append(metricData, metricDatum("Load1", loadMetrics.Load1, awsv2CWTypes.StandardUnitNone)...) + metricData = append(metricData, metricDatum("Load5", loadMetrics.Load5, awsv2CWTypes.StandardUnitNone)...) + metricData = append(metricData, metricDatum("Load15", loadMetrics.Load15, awsv2CWTypes.StandardUnitNone)...) } if netMetricsErr == nil && len(netMetrics) == 1 { - metricData = append(metricData, metricDatum("NetBytesSent", float64(netMetrics[0].BytesSent), UnitBytes)...) - metricData = append(metricData, metricDatum("NetBytesRecv", float64(netMetrics[0].BytesRecv), UnitBytes)...) - metricData = append(metricData, metricDatum("NetErrin", float64(netMetrics[0].Errin), UnitCount)...) - metricData = append(metricData, metricDatum("NetErrout", float64(netMetrics[0].Errout), UnitCount)...) + metricData = append(metricData, metricDatum("NetBytesSent", float64(netMetrics[0].BytesSent), awsv2CWTypes.StandardUnitBytes)...) + metricData = append(metricData, metricDatum("NetBytesRecv", float64(netMetrics[0].BytesRecv), awsv2CWTypes.StandardUnitBytes)...) + metricData = append(metricData, metricDatum("NetErrin", float64(netMetrics[0].Errin), awsv2CWTypes.StandardUnitCount)...) + metricData = append(metricData, metricDatum("NetErrout", float64(netMetrics[0].Errout), awsv2CWTypes.StandardUnitCount)...) } - putMetricInput := &awsCloudWatch.PutMetricDataInput{ + putMetricInput := &awsv2CW.PutMetricDataInput{ MetricData: metricData, - Namespace: aws.String(sparta.ProperName), + Namespace: awsv2.String(sparta.ProperName), } - session := spartaAWS.NewSession(logger) - awsCloudWatchSvc := awsCloudWatch.New(session) - putMetricResponse, putMetricResponseErr := awsCloudWatchSvc.PutMetricData(putMetricInput) + awsConfig := spartaAWS.NewConfig(logger) + awsCloudWatchSvc := awsv2CW.NewFromConfig(awsConfig) + putMetricResponse, putMetricResponseErr := awsCloudWatchSvc.PutMetricData(context.Background(), putMetricInput) if putMetricResponseErr != nil { logger.Error().Err(putMetricResponseErr).Msg("Failed to submit CloudWatch Metric data") } else { @@ -123,7 +125,7 @@ func publishMetrics(customDimensionMap map[string]string) { // RegisterLambdaUtilizationMetricPublisher installs a periodic task // to publish the current system metrics to CloudWatch Metrics. See -// https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html +// https://docs.awsv2.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html // for more information. func RegisterLambdaUtilizationMetricPublisher(customDimensionMap map[string]string) { diff --git a/aws/session.go b/aws/session.go index b7554b2c6..1e2d63632 100644 --- a/aws/session.go +++ b/aws/session.go @@ -36,7 +36,7 @@ func NewConfig(logger *zerolog.Logger) awsv2.Config { return NewConfigWithLevel(awsv2.ClientLogMode(0), logger) } -// NewConfigWithLevel returns an AWS Session (https://github.com/aws/aws-sdk-go/wiki/Getting-Started-Configuration) +// NewConfigWithLevel returns an AWS Session (https://github.com/aws/aws-sdk-go-v2/blob/main/config/doc) // object that attaches a debug level handler to all AWS requests from services // sharing the session value. func NewConfigWithLevel(level awsv2.ClientLogMode, logger *zerolog.Logger) awsv2.Config { @@ -44,7 +44,7 @@ func NewConfigWithLevel(level awsv2.ClientLogMode, logger *zerolog.Logger) awsv2 return NewConfigWithConfigLevel(awsConfig, level, logger) } -// NewConfigWithConfigLevel returns an AWS Session (https://github.com/aws/aws-sdk-go/wiki/Getting-Started-Configuration) +// NewConfigWithConfigLevel returns an AWS Session (https://github.com/aws/aws-sdk-go-v2/blob/main/config/doc) // object that attaches a debug level handler to all AWS requests from services // sharing the session value. func NewConfigWithConfigLevel(awsConfig awsv2.Config, diff --git a/aws/step/dynamodb.go b/aws/step/dynamodb.go index 0602bc1d2..d544ad75e 100644 --- a/aws/step/dynamodb.go +++ b/aws/step/dynamodb.go @@ -4,7 +4,6 @@ import ( "math/rand" awsv2DynamoTypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" - "github.com/aws/aws-sdk-go/service/dynamodb" ) //////////////////////////////////////////////////////////////////////////////// @@ -72,12 +71,12 @@ func NewDynamoDBGetItemState(stateName string, // DynamoDBPutItemParameters represents params for the SNS notification // Ref: https://docs.aws.amazon.com/sns/latest/api/API_Publish.html#API_Publish_RequestParameters type DynamoDBPutItemParameters struct { - Item map[string]*dynamodb.AttributeValue + Item map[string]*awsv2DynamoTypes.AttributeValue TableName string ConditionExpression string ConsistentRead bool ExpressionAttributeNames map[string]string - ExpressionAttributeValues map[string]*dynamodb.AttributeValue + ExpressionAttributeValues map[string]*awsv2DynamoTypes.AttributeValue ReturnConsumedCapacity string // INDEXES | TOTAL | NONE ReturnItemCollectionMetrics string // SIZE | NONE ReturnValues string // NONE | ALL_OLD | UPDATED_OLD | ALL_NEW | UPDATED_NEW diff --git a/cmd_build.go b/cmd_build.go index c82d804d9..8ecaadc83 100644 --- a/cmd_build.go +++ b/cmd_build.go @@ -602,19 +602,20 @@ func (cpo *createPackageOp) buildZIPArchive(sanitizedServiceName string, return nil } -func (cpo *createPackageOp) buildDockerImage(sanitizedServiceName string, +func (cpo *createPackageOp) buildDockerImage(ctx context.Context, + sanitizedServiceName string, logger *zerolog.Logger) error { spartaECRLabelName := "io.sparta.ecr-name" imageTag := fmt.Sprintf("sparta/%s:%s", strings.ToLower(cpo.userdata.serviceName), cpo.userdata.buildID) - buildCtx := context.Background() + // Get the current account id... accountID := "123412341234" if !cpo.userdata.noop { stsService := awsv2STS.NewFromConfig(cpo.buildContext.awsConfig) - callerInfo, callerInfoErr := stsService.GetCallerIdentity(buildCtx, + callerInfo, callerInfoErr := stsService.GetCallerIdentity(ctx, &awsv2STS.GetCallerIdentityInput{}) if callerInfoErr != nil { return callerInfoErr @@ -802,9 +803,11 @@ func (cpo *createPackageOp) Invoke(ctx context.Context, logger *zerolog.Logger) // We always need to pass the service name cpo.buildContext.cfTemplate.Metadata[MetadataParamServiceName] = cpo.userdata.serviceName - logger.Info(). - Str("Dockerfile", cpo.userdata.dockerFile). - Msg("Docker build info") + if cpo.userdata.dockerFile != "" { + logger.Info(). + Str("Dockerfile", cpo.userdata.dockerFile). + Msg("Docker build info") + } ////////////////////////////////////////////////////////////////////////////// // Next up we either need to pass the ZIP archive or the ECR information @@ -819,7 +822,7 @@ func (cpo *createPackageOp) Invoke(ctx context.Context, logger *zerolog.Logger) } // Dockerbuild - dockerErr := cpo.buildDockerImage(sanitizedServiceName, logger) + dockerErr := cpo.buildDockerImage(ctx, sanitizedServiceName, logger) if dockerErr != nil { return dockerErr } @@ -1189,7 +1192,8 @@ func (cto *createTemplateOp) Invoke(ctx context.Context, logger *zerolog.Logger) // template (http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) // which creates or updates the service state. // -func Build(noop bool, +func Build(ctx context.Context, + noop bool, serviceName string, serviceDescription string, lambdaAWSInfos []*LambdaAWSInfo, @@ -1319,8 +1323,7 @@ func Build(noop bool, }) buildPipeline.Append("createTemplate", stageCreateTemplate) - pipelineContext := context.Background() - buildErr := buildPipeline.Run(pipelineContext, "Build", logger) + buildErr := buildPipeline.Run(ctx, "Build", logger) if buildErr != nil { return buildErr } diff --git a/cmd_delete.go b/cmd_delete.go index 7733c3297..dab9b7a81 100644 --- a/cmd_delete.go +++ b/cmd_delete.go @@ -16,10 +16,10 @@ import ( // Delete ensures that the provided serviceName is deleted. // Failing to delete a non-existent service is considered a success. -func Delete(serviceName string, logger *zerolog.Logger) error { +func Delete(ctx context.Context, serviceName string, logger *zerolog.Logger) error { awsConfig := spartaAWS.NewConfig(logger) awsCloudFormation := awsv2CF.NewFromConfig(awsConfig) - ctx := context.Background() + exists, err := spartaCF.StackExists(ctx, serviceName, awsConfig, logger) if nil != err { return err diff --git a/cmd_delete_test.go b/cmd_delete_test.go index e5ea2bce2..fad579690 100644 --- a/cmd_delete_test.go +++ b/cmd_delete_test.go @@ -1,6 +1,7 @@ package sparta import ( + "context" "fmt" "os" "testing" @@ -12,7 +13,7 @@ import ( func TestDelete(t *testing.T) { logger := zerolog.New(os.Stdout).With().Timestamp().Logger() serviceName := fmt.Sprintf("ServiceTesting%d", time.Now().Unix()) - deleteErr := Delete(serviceName, &logger) + deleteErr := Delete(context.Background(), serviceName, &logger) if deleteErr != nil { t.Fatalf("Failed to consider non-existent stack successfully deleted") } diff --git a/cmd_describe.go b/cmd_describe.go index 1e9265e54..a08dfe778 100644 --- a/cmd_describe.go +++ b/cmd_describe.go @@ -5,6 +5,7 @@ package sparta import ( "bytes" + "context" "encoding/json" "fmt" "io" @@ -59,7 +60,8 @@ func Describe(serviceName string, var cloudFormationTemplate bytes.Buffer multiWriter := io.MultiWriter(templateFile, &cloudFormationTemplate) - buildErr := Build(true, + buildErr := Build(context.Background(), + true, serviceName, serviceDescription, lambdaAWSInfos, diff --git a/cmd_execute_awsbinary.go b/cmd_execute_awsbinary.go index 1201bb80a..4ec430ae2 100644 --- a/cmd_execute_awsbinary.go +++ b/cmd_execute_awsbinary.go @@ -93,10 +93,10 @@ func tappedHandler(handlerSymbol interface{}, // TODO - add Context.Timeout handler to ensure orderly exit return func(ctx context.Context, msg json.RawMessage) (interface{}, error) { - awsSession := spartaAWS.NewSession(logger) + awsConfig := spartaAWS.NewConfig(logger) ctx = applyInterceptors(ctx, msg, interceptors.Begin) ctx = context.WithValue(ctx, ContextKeyLogger, logger) - ctx = context.WithValue(ctx, ContextKeyAWSSession, awsSession) + ctx = context.WithValue(ctx, ContextKeyAWSConfig, awsConfig) ctx = applyInterceptors(ctx, msg, interceptors.BeforeSetup) // Create the entry logger that has some context information diff --git a/cmd_explore.go b/cmd_explore.go index 0f3bf9428..de507b532 100644 --- a/cmd_explore.go +++ b/cmd_explore.go @@ -28,7 +28,8 @@ const ( // ExploreWithInputFilter allows the caller to provide additional filters // for the source files that will be used as inputs -func ExploreWithInputFilter(serviceName string, +func ExploreWithInputFilter(ctx context.Context, + serviceName string, serviceDescription string, lambdaAWSInfos []*LambdaAWSInfo, api APIGateway, @@ -39,8 +40,6 @@ func ExploreWithInputFilter(serviceName string, linkerFlags string, logger *zerolog.Logger) error { - exploreContext := context.Background() - // We need to setup the log output view so that we have a writer for the // log output logDataView := tview.NewTextView(). @@ -62,7 +61,7 @@ func ExploreWithInputFilter(serviceName string, input := &awsv2CF.DescribeStackResourcesInput{ StackName: awsv2.String(serviceName), } - stackResourceOutputs, stackResourceOutputsErr := cfSvc.DescribeStackResources(exploreContext, input) + stackResourceOutputs, stackResourceOutputsErr := cfSvc.DescribeStackResources(ctx, input) if stackResourceOutputsErr != nil { return stackResourceOutputsErr } @@ -85,14 +84,16 @@ func ExploreWithInputFilter(serviceName string, settingsMap, channelMap[broadcasterFunctionSelect], logger) - eventDropdown, eventFocusable := newEventInputSelector(awsConfig, + eventDropdown, eventFocusable := newEventInputSelector(ctx, + awsConfig, application, lambdaAWSInfos, settingsMap, inputExtensions, channelMap[broadcasterFunctionSelect], logger) - outputView, outputViewFocusable := newCloudWatchLogTailView(awsConfig, + outputView, outputViewFocusable := newCloudWatchLogTailView(ctx, + awsConfig, application, lambdaAWSInfos, settingsMap, @@ -166,7 +167,8 @@ func Explore(serviceName string, linkerFlags string, logger *zerolog.Logger) error { - return ExploreWithInputFilter(serviceName, + return ExploreWithInputFilter(context.Background(), + serviceName, serviceDescription, lambdaAWSInfos, api, diff --git a/cmd_explore_views.go b/cmd_explore_views.go index 22fdcb4f4..9965ac96d 100644 --- a/cmd_explore_views.go +++ b/cmd_explore_views.go @@ -17,7 +17,6 @@ import ( "sync" "time" - "github.com/aws/aws-sdk-go-v2/aws" awsv2 "github.com/aws/aws-sdk-go-v2/aws" awsv2CFTypes "github.com/aws/aws-sdk-go-v2/service/cloudformation/types" awsv2Lambda "github.com/aws/aws-sdk-go-v2/service/lambda" @@ -188,7 +187,8 @@ func newFunctionSelector(awsConfig awsv2.Config, // // Select the event to use to invoke the function // -func newEventInputSelector(awsConfig awsv2.Config, +func newEventInputSelector(ctx context.Context, + awsConfig awsv2.Config, app *tview.Application, lambdaAWSInfos []*LambdaAWSInfo, settings map[string]string, @@ -287,10 +287,10 @@ func newEventInputSelector(awsConfig awsv2.Config, // Submit it to lambda if activeFunction != "" { lambdaInput := &awsv2Lambda.InvokeInput{ - FunctionName: aws.String(activeFunction), + FunctionName: awsv2.String(activeFunction), Payload: selectedJSONData, } - invokeOutput, invokeOutputErr := lambdaSvc.Invoke(context.Background(), lambdaInput) + invokeOutput, invokeOutputErr := lambdaSvc.Invoke(ctx, lambdaInput) if invokeOutputErr != nil { logger.Error(). Err(invokeOutputErr). @@ -332,7 +332,8 @@ func newEventInputSelector(awsConfig awsv2.Config, // // Tail the cloudwatch logs for the active function // -func newCloudWatchLogTailView(awsConfig awsv2.Config, +func newCloudWatchLogTailView(ctx context.Context, + awsConfig awsv2.Config, app *tview.Application, lambdaAWSInfos []*LambdaAWSInfo, settings map[string]string, @@ -418,7 +419,7 @@ func newCloudWatchLogTailView(awsConfig awsv2.Config, // Put this as the label in the view... doneChan = make(chan bool) - messages := spartaCWLogs.TailWithContext(context.Background(), + messages := spartaCWLogs.TailWithContext(ctx, doneChan, awsConfig, logGroupName, diff --git a/cmd_provision_build.go b/cmd_provision_build.go index 7659b7765..5901ac1ea 100644 --- a/cmd_provision_build.go +++ b/cmd_provision_build.go @@ -187,7 +187,8 @@ func versionAwareS3KeyName(s3DefaultKey string, // Upload a local file to S3. Returns the full S3 URL to the file that was // uploaded. If the target bucket does not have versioning enabled, // this function will automatically make a new key to ensure uniqueness -func uploadLocalFileToS3(awsConfig awsv2.Config, +func uploadLocalFileToS3(ctx context.Context, + awsConfig awsv2.Config, localPath string, serviceName string, s3ObjectKey string, @@ -228,7 +229,7 @@ func uploadLocalFileToS3(awsConfig awsv2.Config, s3ObjectKey) } else { // Then upload it - uploadLocation, uploadURLErr := spartaS3.UploadLocalFileToS3(context.Background(), + uploadLocation, uploadURLErr := spartaS3.UploadLocalFileToS3(ctx, localPath, awsConfig, s3ObjectBucket, @@ -419,7 +420,8 @@ func (upo *uploadPackageOp) Invoke(ctx context.Context, logger *zerolog.Logger) uploadKeyPath := fmt.Sprintf("%s/%s", upo.provisionContext.serviceName, archiveBaseName) // Create the S3 key... - zipS3URL, zipS3URLErr := uploadLocalFileToS3(upo.provisionContext.awsConfig, + zipS3URL, zipS3URLErr := uploadLocalFileToS3(ctx, + upo.provisionContext.awsConfig, localPath, upo.provisionContext.serviceName, uploadKeyPath, @@ -466,7 +468,8 @@ func (upo *uploadPackageOp) Invoke(ctx context.Context, logger *zerolog.Logger) Str("Tag", ecrImageTag). Msg("Pushing local image to ECR") - pushErr := spartaDocker.PushECRTaggedImage(ecrImageTag, + pushErr := spartaDocker.PushECRTaggedImage(ctx, + ecrImageTag, upo.provisionContext.awsConfig, logger) return newTaskResult(ecrImageTag, pushErr) @@ -650,7 +653,8 @@ func (ipuo *inPlaceUpdatesOp) Invoke(ctx context.Context, logger *zerolog.Logger awsCloudFormation := awsv2CF.NewFromConfig(ipuo.provisionContext.awsConfig) changeSetRequestName := CloudFormationResourceName(fmt.Sprintf("%sInPlaceChangeSet", ipuo.provisionContext.serviceName)) - changes, changesErr := spartaCF.CreateStackChangeSet(changeSetRequestName, + changes, changesErr := spartaCF.CreateStackChangeSet(ctx, + changeSetRequestName, ipuo.provisionContext.serviceName, ipuo.provisionContext.cfTemplate, ipuo.provisionContext.s3Uploads[s3UploadCloudFormationStackKey].location, @@ -737,7 +741,8 @@ func (ipuo *inPlaceUpdatesOp) Invoke(ctx context.Context, logger *zerolog.Logger // Add the request to delete the change set... // TODO: add some retry logic in here to handle failures. deleteChangeSetTask := func() workResult { - _, deleteChangeSetResultErr := spartaCF.DeleteChangeSet(ipuo.provisionContext.serviceName, + _, deleteChangeSetResultErr := spartaCF.DeleteChangeSet(ctx, + ipuo.provisionContext.serviceName, changeSetRequestName, awsCloudFormation) return newTaskResult("", deleteChangeSetResultErr) @@ -783,7 +788,8 @@ func (cfsu *cloudformationStackUpdateOp) Invoke(ctx context.Context, logger *zer startTime := time.Now() // Regular update, go ahead with the CloudFormation changes - stack, stackErr := spartaCF.ConvergeStackState(cfsu.provisionContext.serviceName, + stack, stackErr := spartaCF.ConvergeStackState(ctx, + cfsu.provisionContext.serviceName, cfsu.provisionContext.cfTemplate, cfsu.provisionContext.s3Uploads[s3UploadCloudFormationStackKey].location, cfsu.provisionContext.stackParameterValues, diff --git a/cmd_status.go b/cmd_status.go index a36fe228a..eb12dc5f8 100644 --- a/cmd_status.go +++ b/cmd_status.go @@ -26,7 +26,8 @@ func logSectionHeader(text string, } // Status produces a status report for the given stack -func Status(serviceName string, +func Status(ctx context.Context, + serviceName string, serviceDescription string, redact bool, logger *zerolog.Logger) error { @@ -37,7 +38,7 @@ func Status(serviceName string, params := &awsv2CF.DescribeStacksInput{ StackName: aws.String(serviceName), } - describeStacksResponse, describeStacksResponseErr := cfSvc.DescribeStacks(context.Background(), params) + describeStacksResponse, describeStacksResponseErr := cfSvc.DescribeStacks(ctx, params) if describeStacksResponseErr != nil { if strings.Contains(describeStacksResponseErr.Error(), "does not exist") { @@ -60,7 +61,7 @@ func Status(serviceName string, input := &awsv2STS.GetCallerIdentityInput{} stsSvc := awsv2STS.NewFromConfig(awsConfig) - identityResponse, identityResponseErr := stsSvc.GetCallerIdentity(context.Background(), input) + identityResponse, identityResponseErr := stsSvc.GetCallerIdentity(ctx, input) if identityResponseErr != nil { return identityResponseErr } diff --git a/cmd_status_test.go b/cmd_status_test.go index df1f832de..057de78ff 100644 --- a/cmd_status_test.go +++ b/cmd_status_test.go @@ -1,6 +1,7 @@ package sparta import ( + "context" "fmt" "testing" "time" @@ -11,7 +12,7 @@ import ( func TestStatus(t *testing.T) { logger, _ := NewLogger(zerolog.InfoLevel.String()) serviceName := fmt.Sprintf("ServiceTesting%d", time.Now().Unix()) - statusErr := Status(serviceName, "Test desc", false, logger) + statusErr := Status(context.Background(), serviceName, "Test desc", false, logger) if statusErr != nil { t.Fatalf("Failed to error for non-existent stack") } diff --git a/decorator/discovery.go b/decorator/discovery.go index b69c82e72..ec21da3e5 100644 --- a/decorator/discovery.go +++ b/decorator/discovery.go @@ -128,9 +128,9 @@ func discoveryInfoFromIDs(discoveryContext context.Context, // DiscoverInstances returns the HttpInstanceSummary items that match // the given attribute map -func DiscoverInstances(attributes map[string]string, +func DiscoverInstances(ctx context.Context, attributes map[string]string, logger *zerolog.Logger) ([]awsv2ServiceDiscoveryTypes.HttpInstanceSummary, error) { - return DiscoverInstancesWithContext(context.Background(), attributes, logger) + return DiscoverInstancesWithContext(ctx, attributes, logger) } // DiscoverInstancesWithContext returns the HttpInstanceSummary items that match @@ -139,12 +139,14 @@ func DiscoverInstances(attributes map[string]string, func DiscoverInstancesWithContext(ctx context.Context, attributes map[string]string, logger *zerolog.Logger) ([]awsv2ServiceDiscoveryTypes.HttpInstanceSummary, error) { - discoveryContext := context.Background() // Get the default discovery info and translate that into name/id pairs... namespaceID := os.Getenv(EnvVarCloudMapNamespaceID) serviceID := os.Getenv(EnvVarCloudMapServiceID) - discoveryInfo, discoveryInfoErr := discoveryInfoFromIDs(discoveryContext, namespaceID, serviceID, logger) + discoveryInfo, discoveryInfoErr := discoveryInfoFromIDs(ctx, + namespaceID, + serviceID, + logger) logger.Debug(). Str("namespaceID", namespaceID). diff --git a/docker/docker.go b/docker/docker.go index 55279a295..f0dd01714 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -157,12 +157,12 @@ func BuildDockerImage(serviceName string, } // PushECRTaggedImage pushes previously tagged image to ECR -func PushECRTaggedImage(localImageTag string, +func PushECRTaggedImage(ctx context.Context, + localImageTag string, awsConfig awsv2.Config, logger *zerolog.Logger) error { ecrSvc := awsv2ECR.NewFromConfig(awsConfig) - pushContext := context.Background() // Push the image - if that fails attempt to reauthorize with the docker // client and try again @@ -173,7 +173,7 @@ func PushECRTaggedImage(localImageTag string, logger.Info(). Err(pushError). Msg("ECR push failed - reauthorizing") - ecrAuthTokenResult, ecrAuthTokenResultErr := ecrSvc.GetAuthorizationToken(pushContext, + ecrAuthTokenResult, ecrAuthTokenResultErr := ecrSvc.GetAuthorizationToken(ctx, &awsv2ECR.GetAuthorizationTokenInput{}, ) if ecrAuthTokenResultErr != nil { @@ -297,6 +297,6 @@ func PushDockerImageToECR(ctx context.Context, pushError = errors.Wrapf(pushError, "Attempting to push Docker image") } */ - pushErr := PushECRTaggedImage(ecrTagValue, awsConfig, logger) + pushErr := PushECRTaggedImage(ctx, ecrTagValue, awsConfig, logger) return ecrTagValue, pushErr } diff --git a/docs_source/content/example_service/step1.md b/docs_source/content/example_service/step1.md index 311229b75..34ec56a66 100644 --- a/docs_source/content/example_service/step1.md +++ b/docs_source/content/example_service/step1.md @@ -12,7 +12,7 @@ Please be aware that running Lambda functions may incur [costs](https://aws.amaz ## Preconditions -Sparta uses the [AWS SDK for Go](http://aws.amazon.com/sdk-for-go/) to interact with AWS APIs. Before you get started, ensure that you've properly configured the [SDK credentials](https://github.com/aws/aws-sdk-go/wiki/configuring-sdk). +Sparta uses the [AWS SDK for Go](http://aws.amazon.com/sdk-for-go/) to interact with AWS APIs. Before you get started, ensure that you've properly configured the [SDK credentials](https://github.com/aws/aws-sdk-go-v2/blob/main/config/doc). Note that you must use an AWS region that supports Lambda. Consult the [Global Infrastructure](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/) page for the most up to date release information. diff --git a/hook/docker_build_hook_test.go b/hook/docker_build_hook_test.go index ea09bff79..ea2befeb1 100644 --- a/hook/docker_build_hook_test.go +++ b/hook/docker_build_hook_test.go @@ -2,6 +2,7 @@ package hook import ( "bytes" + "context" "fmt" "io" "io/ioutil" @@ -46,7 +47,8 @@ func TestBuildUPXImage(t *testing.T) { t.Fatalf("Failed to create test logger: %s", loggerErr) } var templateWriter bytes.Buffer - err := sparta.Build(true, + err := sparta.Build(context.Background(), + true, "SampleProvision", "", nil, diff --git a/profile_loop_awsbinary.go b/profile_loop_awsbinary.go index 212fb8d98..23391768a 100644 --- a/profile_loop_awsbinary.go +++ b/profile_loop_awsbinary.go @@ -4,6 +4,7 @@ package sparta import ( + "context" "fmt" "os" "path" @@ -11,8 +12,9 @@ import ( "runtime/pprof" "time" - "github.com/aws/aws-sdk-go-v2/aws" - "github.com/aws/aws-sdk-go-v2/service/s3/s3manager" + awsv2 "github.com/aws/aws-sdk-go-v2/aws" + awsv2S3Manager "github.com/aws/aws-sdk-go-v2/feature/s3/manager" + awsv2S3 "github.com/aws/aws-sdk-go-v2/service/s3" spartaAWS "github.com/mweagle/Sparta/aws" "github.com/rs/zerolog" ) @@ -60,7 +62,8 @@ func (ur *uploadResult) Result() interface{} { return ur.uploaded } -func uploadFileTask(uploader *s3manager.Uploader, +func uploadFileTask(ctx context.Context, + uploader *awsv2S3Manager.Uploader, profileType string, uploadSlot int, localFilePath string, @@ -75,12 +78,12 @@ func uploadFileTask(uploader *s3manager.Uploader, uploadFileName := fmt.Sprintf("%d-%s", uploadSlot, path.Base(localFilePath)) keyPath := path.Join(profileSnapshotRootKeypathForType(profileType, stackName), uploadFileName) - uploadInput := &s3manager.UploadInput{ - Bucket: aws.String(profileBucket), - Key: aws.String(keyPath), + uploadInput := &awsv2S3.PutObjectInput{ + Bucket: awsv2.String(profileBucket), + Key: awsv2.String(keyPath), Body: fileReader, } - uploadOutput, uploadErr := uploader.Upload(uploadInput) + uploadOutput, uploadErr := uploader.Upload(ctx, uploadInput) return &uploadResult{ err: uploadErr, uploaded: uploadOutput != nil, @@ -104,13 +107,15 @@ func snapshotProfiles(s3BucketArchive interface{}, Msg("Publishing CPU profile") uploadSlot := nextUploadSlot() - sess := spartaAWS.NewSession(profileLogger) - uploader := s3manager.NewUploader(sess) + awsConfig := spartaAWS.NewConfig(profileLogger) + s3Client := awsv2S3.NewFromConfig(awsConfig) + uploader := awsv2S3Manager.NewUploader(s3Client) uploadTasks := make([]*workTask, 0) if cpuProfilePath != "" { uploadTasks = append(uploadTasks, - newWorkTask(uploadFileTask(uploader, + newWorkTask(uploadFileTask(context.Background(), + uploader, "cpu", uploadSlot, cpuProfilePath, @@ -129,7 +134,8 @@ func snapshotProfiles(s3BucketArchive interface{}, namedProfile.WriteTo(outputProfile, 0) outputProfile.Close() uploadTasks = append(uploadTasks, - newWorkTask(uploadFileTask(uploader, + newWorkTask(uploadFileTask(context.Background(), + uploader, eachProfileType, uploadSlot, outputProfile.Name(), diff --git a/profile_loop_build.go b/profile_loop_build.go index 26691278c..3548b255c 100644 --- a/profile_loop_build.go +++ b/profile_loop_build.go @@ -15,6 +15,7 @@ import ( awsv2 "github.com/aws/aws-sdk-go-v2/aws" awsv2S3Downloader "github.com/aws/aws-sdk-go-v2/feature/s3/manager" + awsv2CFTypes "github.com/aws/aws-sdk-go-v2/service/cloudformation/types" awsv2S3 "github.com/aws/aws-sdk-go-v2/service/s3" gof "github.com/awslabs/goformation/v5/cloudformation" @@ -142,7 +143,8 @@ func askQuestions(userStackName string, stackNameToIDMap map[string]string) (*us return &responses, nil } -func objectKeysForProfileType(profileType string, +func objectKeysForProfileType(ctx context.Context, + profileType string, stackName string, s3BucketName string, maxCount int32, @@ -163,7 +165,7 @@ func objectKeysForProfileType(profileType string, s3Svc := awsv2S3.NewFromConfig(awsConfig) for { - listItemResults, listItemResultsErr := s3Svc.ListObjects(context.Background(), + listItemResults, listItemResultsErr := s3Svc.ListObjects(ctx, listObjectInput) if listItemResultsErr != nil { return nil, errors.Wrapf(listItemResultsErr, "Attempting to list bucket: %s", s3BucketName) @@ -306,7 +308,8 @@ func syncStackProfileSnapshots(profileType string, // Ok, let's get some user information s3Svc := awsv2S3.NewFromConfig(awsConfig) downloader := awsv2S3Downloader.NewDownloader(s3Svc) - downloadKeys, downloadKeysErr := objectKeysForProfileType(profileType, + downloadKeys, downloadKeysErr := objectKeysForProfileType(context.Background(), + profileType, stackName, s3BucketName, 1024, @@ -418,7 +421,7 @@ func syncStackProfileSnapshots(profileType string, // Profile is the interactive command used to pull S3 assets locally into /tmp // and run ppro against the cached profiles -func Profile(serviceName string, +func Profile(ctx context.Context, serviceName string, serviceDescription string, s3BucketName string, httpPort int, @@ -428,9 +431,12 @@ func Profile(serviceName string, // Get the currently active stacks... // Ref: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-describing-stacks.html#w2ab2c15c15c17c11 - stackSummaries, stackSummariesErr := spartaCF.ListStacks(awsConfig, 1024, "CREATE_COMPLETE", - "UPDATE_COMPLETE", - "UPDATE_ROLLBACK_COMPLETE") + stackSummaries, stackSummariesErr := spartaCF.ListStacks(ctx, + awsConfig, + 1024, + awsv2CFTypes.StackStatusCreateComplete, + awsv2CFTypes.StackStatusUpdateComplete, + awsv2CFTypes.StackStatusUpdateRollbackComplete) if stackSummariesErr != nil { return stackSummariesErr diff --git a/sparta_constants.go b/sparta_constants.go index 4644a1348..56e2372b4 100644 --- a/sparta_constants.go +++ b/sparta_constants.go @@ -18,7 +18,7 @@ const ( ) const ( // SpartaVersion defines the current Sparta release - SpartaVersion = "2.0.0" + SpartaVersion = "3.0.0.alpha" // LambdaBinaryTag is the build tag name used when building the binary LambdaBinaryTag = "lambdabinary" ) @@ -105,9 +105,9 @@ const ( // ContextKeyLambdaResponse is the possible response that // was returned from the lambda function ContextKeyLambdaResponse - // ContextKeyAWSSession is the aws Session instance for this + // ContextKeyAWSConfig is the aws Session instance for this // request - ContextKeyAWSSession + ContextKeyAWSConfig ) const ( diff --git a/sparta_main_awsbinary.go b/sparta_main_awsbinary.go index ceae1bddd..cd989fbef 100644 --- a/sparta_main_awsbinary.go +++ b/sparta_main_awsbinary.go @@ -7,6 +7,7 @@ package sparta // in the Lambda context import ( + "context" "fmt" "io" "io/ioutil" @@ -107,7 +108,8 @@ func Delete(serviceName string, logger *zerolog.Logger) error { } // Build is not available in the AWS Lambda binary -func Build(noop bool, +func Build(ctx context.Context, + noop bool, serviceName string, serviceDescription string, lambdaAWSInfos []*LambdaAWSInfo, @@ -206,8 +208,11 @@ func platformLogSysInfo(lambdaFunc string, logger *zerolog.Logger) { "/etc/os-release", }, zerolog.DebugLevel: { + "/proc/stat", "/proc/cpuinfo", "/proc/meminfo", + "/proc/loadavg", + "/proc/diskstats", }, } diff --git a/sparta_main_build.go b/sparta_main_build.go index b97f3b27f..53630f25b 100644 --- a/sparta_main_build.go +++ b/sparta_main_build.go @@ -4,6 +4,7 @@ package sparta import ( + "context" "fmt" "log" "os" @@ -157,7 +158,8 @@ func MainEx(serviceName string, if templateFileErr != nil { return templateFileErr } - buildErr := Build(OptionsGlobal.Noop, + buildErr := Build(context.Background(), + OptionsGlobal.Noop, serviceName, serviceDescription, lambdaAWSInfos, @@ -214,7 +216,8 @@ func MainEx(serviceName string, } // TODO: Build, then Provision - buildErr := Build(OptionsGlobal.Noop, + buildErr := Build(context.Background(), + OptionsGlobal.Noop, serviceName, serviceDescription, lambdaAWSInfos, @@ -269,7 +272,7 @@ func MainEx(serviceName string, ////////////////////////////////////////////////////////////////////////////// // Delete CommandLineOptions.Delete.RunE = func(cmd *cobra.Command, args []string) error { - return Delete(serviceName, OptionsGlobal.Logger) + return Delete(context.Background(), serviceName, OptionsGlobal.Logger) } CommandLineOptions.Root.AddCommand(CommandLineOptions.Delete) @@ -339,7 +342,8 @@ func MainEx(serviceName string, return validateErr } - return ExploreWithInputFilter(serviceName, + return ExploreWithInputFilter(context.Background(), + serviceName, serviceDescription, lambdaAWSInfos, api, @@ -361,7 +365,8 @@ func MainEx(serviceName string, if nil != validateErr { return validateErr } - return Profile(serviceName, + return Profile(context.Background(), + serviceName, serviceDescription, optionsProfile.S3Bucket, optionsProfile.Port, @@ -378,7 +383,8 @@ func MainEx(serviceName string, if nil != validateErr { return validateErr } - return Status(serviceName, + return Status(context.Background(), + serviceName, serviceDescription, optionsStatus.Redact, OptionsGlobal.Logger) diff --git a/test.go b/test.go index 0b441be9e..3170ca58b 100644 --- a/test.go +++ b/test.go @@ -149,7 +149,8 @@ func testProvisionEx(t *testing.T, if fullPathErr != nil { t.Error(fullPathErr) } - err := Build(true, + err := Build(context.Background(), + true, "SampleProvision", "", lambdaAWSInfos, diff --git a/testing/provision.go b/testing/provision.go index d10417ead..9fbce184e 100644 --- a/testing/provision.go +++ b/testing/provision.go @@ -2,6 +2,7 @@ package testing import ( "bytes" + "context" "testing" sparta "github.com/mweagle/Sparta" @@ -60,7 +61,8 @@ func ProvisionEx(t *testing.T, t.Fatalf("Failed to create test logger: %s", loggerErr) } var templateWriter bytes.Buffer - err := sparta.Build(true, + err := sparta.Build(context.Background(), + true, "SampleProvision", "", lambdaAWSInfos,