Skip to content

Commit

Permalink
Incremental checkin to ensure consistent context
Browse files Browse the repository at this point in the history
  • Loading branch information
mweagle committed Oct 9, 2021
1 parent 03ff952 commit 4dbe2e6
Show file tree
Hide file tree
Showing 39 changed files with 276 additions and 205 deletions.
2 changes: 1 addition & 1 deletion IMPORTS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
11 changes: 6 additions & 5 deletions apigateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions aws/cloudformation/impl_aws_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

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/

func platformUserName() string {
return ""
}

func platformAccountUserName(awsConfig aws.Config) (string, error) {
func platformAccountUserName(awsConfig awsv2.Config) (string, error) {
return "", nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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).
Expand All @@ -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)
}
14 changes: 7 additions & 7 deletions aws/cloudformation/resources/customResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion aws/cloudformation/resources/helloWorldResource_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resources

import (
"context"
"encoding/json"
"os"
"testing"
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions aws/cloudformation/resources/helloworldresource.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resources

import (
"context"
"encoding/json"

awsv2 "github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -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{}
Expand All @@ -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{}
Expand All @@ -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{}
Expand Down
10 changes: 5 additions & 5 deletions aws/cloudformation/resources/s3ArtifactPublisherResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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) {

Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions aws/cloudformation/resources/s3LambdaEventSourceResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions aws/cloudformation/resources/sesLambdaEventSourceResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions aws/cloudformation/resources/snsLambdaEventSourceResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 4dbe2e6

Please sign in to comment.