-
Notifications
You must be signed in to change notification settings - Fork 84
/
main.go
executable file
·115 lines (94 loc) · 2.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Package main sets the handler for the Trigger Reset AWS Lambda
// Function
package main
import (
"context"
"encoding/json"
"log"
"github.com/Optum/dce/pkg/account"
"github.com/Optum/dce/pkg/config"
"github.com/Optum/dce/pkg/errors"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/codebuild"
"github.com/aws/aws-sdk-go/service/codebuild/codebuildiface"
)
type configuration struct {
Debug string `env:"DEBUG" envDefault:"false"`
BuildName string `env:"RESET_BUILD_NAME" envDefault:"ResetCodeBuild"`
}
var (
services *config.ServiceBuilder
// Settings - the configuration settings for the controller
settings *configuration
)
func init() {
cfgBldr := &config.ConfigurationBuilder{}
settings = &configuration{}
if err := cfgBldr.Unmarshal(settings); err != nil {
log.Fatalf("Could not load configuration: %s", err.Error())
}
// load up the values into the various settings...
err := cfgBldr.WithEnv("AWS_CURRENT_REGION", "AWS_CURRENT_REGION", "us-east-1").Build()
if err != nil {
log.Printf("Error: %+v", err)
}
svcBldr := &config.ServiceBuilder{Config: cfgBldr}
_, err = svcBldr.
// DCE services...
WithCodeBuild().
Build()
if err != nil {
panic(err)
}
services = svcBldr
}
func handler(ctx context.Context, sqsEvent events.SQSEvent) error {
var codeBuildSvc codebuildiface.CodeBuildAPI
if err := services.Config.GetService(&codeBuildSvc); err != nil {
panic(err)
}
for _, message := range sqsEvent.Records {
err := processMessage(codeBuildSvc, message)
if err != nil {
return err
}
}
return nil
}
func processMessage(codeBuildSvc codebuildiface.CodeBuildAPI, event events.SQSMessage) error {
acct := account.Account{}
if err := json.Unmarshal([]byte(event.Body), &acct); err != nil {
return errors.NewInternalServer("unexpected error unmarshaling sqs message", err)
}
log.Printf("Start Account: %s\nMessage ID: %s\n", *acct.ID, event.MessageId)
buildEnvironmentVars := []*codebuild.EnvironmentVariable{
{
Name: aws.String("RESET_ACCOUNT"),
Value: acct.ID,
},
{
Name: aws.String("RESET_ACCOUNT_ADMIN_ROLE_NAME"),
Value: acct.AdminRoleArn.IAMResourceName(),
},
{
Name: aws.String("RESET_ACCOUNT_PRINCIPAL_ROLE_NAME"),
Value: acct.PrincipalRoleArn.IAMResourceName(),
},
}
// Trigger Code Pipeline
log.Printf("Triggering Reset Build %s for Account %s\n", settings.BuildName, *acct.ID)
_, err := codeBuildSvc.StartBuild(&codebuild.StartBuildInput{
EnvironmentVariablesOverride: buildEnvironmentVars,
ProjectName: aws.String(settings.BuildName),
})
if err != nil {
return errors.NewInternalServer("unexpected error starting code build", err)
}
return nil
}
// Start the Lambda Handler
func main() {
lambda.Start(handler)
}