Skip to content

Commit

Permalink
propagating context
Browse files Browse the repository at this point in the history
  • Loading branch information
test authored and test committed Aug 2, 2024
1 parent 53e4358 commit 3fa3bc2
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 36 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ terraform.rc

# GoReleaser build files
dist/

# Terraform lock files
.terraform.lock.hcl
20 changes: 6 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -236,31 +236,28 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -277,8 +274,6 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand All @@ -290,15 +285,14 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand All @@ -312,8 +306,6 @@ google.golang.org/grpc v1.61.1 h1:kLAiWrZs7YeDM6MumDe7m3y4aM6wacLzM1Y/wiLP9XY=
google.golang.org/grpc v1.61.1/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
1 change: 1 addition & 0 deletions provider/keys/terraform_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ const (
AssumeRole = "assume_role"
Profile = "profile"
RoleArn = "role_arn"
Timeout = "timeout"
)
125 changes: 103 additions & 22 deletions provider/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"regexp"
"strings"
"time"

"github.com/Traceableai/terraform-provider-awsapigateway/provider/keys"
"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -42,6 +43,11 @@ func AwsApiGatewayResource() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
keys.Timeout: {
Type: schema.TypeString,
Optional: true,
Default: "1m",
},
keys.Accounts: {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -71,15 +77,27 @@ func AwsApiGatewayResource() *schema.Resource {
}
}

func resourceCreateUpdate(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
logGroupNames := make([]string, 0)
accounts := d.Get(keys.Accounts).([]interface{})
func resourceCreateUpdate(
ctx context.Context,
d *schema.ResourceData,
_ interface{}) diag.Diagnostics {
mapDiagnostics := &MapDiagnostics{
diagnostics: diag.Diagnostics{},
warnDiagnostics: make(map[string][]string),
errorDiagnostics: make(map[string][]string),
}

logGroupNames := make([]string, 0)
accounts := d.Get(keys.Accounts).([]interface{})
timeoutStr := d.Get(keys.Timeout).(string)
timeout, err := time.ParseDuration(timeoutStr)
if err != nil {
mapDiagnostics.add(errorDiagnostic(err.Error()))
return mapDiagnostics.getDiagnostics()
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

for _, account := range accounts {
acc := account.(map[string]interface{})
region := acc[keys.Region].(string)
Expand All @@ -89,7 +107,7 @@ func resourceCreateUpdate(_ context.Context, d *schema.ResourceData, meta interf

ignoreAccessLogSettings := d.Get(keys.IgnoreAccessLogSettings).(bool)

cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region))
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
if err != nil {
mapDiagnostics.add(errorDiagnostic(err.Error()))
continue
Expand All @@ -104,7 +122,8 @@ func resourceCreateUpdate(_ context.Context, d *schema.ResourceData, meta interf

conn := newFromConfig(cfg)

logGroupNames = append(logGroupNames, getLogGroupNames(apiList, exclude, ignoreAccessLogSettings, conn, mapDiagnostics)...)
logGroupNames = append(logGroupNames,
getLogGroupNames(ctx, apiList, exclude, ignoreAccessLogSettings, conn, mapDiagnostics)...)
}

if d.Id() == "" {
Expand All @@ -126,7 +145,13 @@ func resourceDelete(_ context.Context, d *schema.ResourceData, _ interface{}) di
return nil
}

func getLogGroupNames(apiGateways []interface{}, exclude bool, ignoreAccessLogSettings bool, conn AwsApiGatewayProvider, mapDiagnostics *MapDiagnostics) []string {
func getLogGroupNames(
ctx context.Context,
apiGateways []interface{},
exclude bool,
ignoreAccessLogSettings bool,
conn AwsApiGatewayProvider,
mapDiagnostics *MapDiagnostics) []string {
var summary string
if !exclude && len(apiGateways) == 0 {
summary = "api_gateways cannot be empty when action is include."
Expand Down Expand Up @@ -157,19 +182,43 @@ func getLogGroupNames(apiGateways []interface{}, exclude bool, ignoreAccessLogSe
}

accessLogFormatKeysMap := make(map[string]AccessLogFormatMap)
logGroupNames := getLogGroupNamesRestApis(conn, apiAllStages, apiWithStage, exclude, ignoreAccessLogSettings, accessLogFormatKeysMap, mapDiagnostics)
apiGatewayV2LogGroupNames := getLogGroupNamesHttpApis(conn, apiAllStages, apiWithStage, exclude, ignoreAccessLogSettings, accessLogFormatKeysMap, mapDiagnostics)
logGroupNames := getLogGroupNamesRestApis(
ctx,
conn,
apiAllStages,
apiWithStage,
exclude,
ignoreAccessLogSettings,
accessLogFormatKeysMap,
mapDiagnostics)

apiGatewayV2LogGroupNames := getLogGroupNamesHttpApis(
ctx,
conn,
apiAllStages,
apiWithStage,
exclude,
ignoreAccessLogSettings,
accessLogFormatKeysMap,
mapDiagnostics)
return removeDuplicates(append(logGroupNames, apiGatewayV2LogGroupNames...))
}

func getLogGroupNamesRestApis(conn AwsApiGatewayProvider, apiAllStages []string, apiWithStage map[string][]string,
exclude bool, ignoreAccessLogSettings bool, accessLogFormatKeysMap map[string]AccessLogFormatMap, mapDiagnostics *MapDiagnostics) []string {
func getLogGroupNamesRestApis(
ctx context.Context,
conn AwsApiGatewayProvider,
apiAllStages []string,
apiWithStage map[string][]string,
exclude bool,
ignoreAccessLogSettings bool,
accessLogFormatKeysMap map[string]AccessLogFormatMap,
mapDiagnostics *MapDiagnostics) []string {
// apiStageMappingRest is a map of api id to list of api stages that need to be considered
// if the value list is empty, it means that all stages in this api should be considered
apiStageMappingRest := make(map[string][]string)
restApisPaginator := conn.getAwsGetRestApisPaginator()
for restApisPaginator.HasMorePages() {
res, err := restApisPaginator.NextPage(context.TODO())
res, err := restApisPaginator.NextPage(ctx)
if err != nil {
summary := fmt.Sprintf("Error while invoking getRestApis sdk call: %s", err.Error())
mapDiagnostics.add(errorDiagnostic(summary))
Expand All @@ -186,18 +235,32 @@ func getLogGroupNamesRestApis(conn AwsApiGatewayProvider, apiAllStages []string,
}
}
}
logGroupNames := getLogGroupNamesRestApisHelper(conn, apiStageMappingRest, exclude, ignoreAccessLogSettings, accessLogFormatKeysMap, mapDiagnostics)
logGroupNames := getLogGroupNamesRestApisHelper(
ctx,
conn,
apiStageMappingRest,
exclude,
ignoreAccessLogSettings,
accessLogFormatKeysMap,
mapDiagnostics)
return logGroupNames
}

func getLogGroupNamesHttpApis(conn AwsApiGatewayProvider, apiAllStages []string, apiWithStage map[string][]string, exclude bool,
ignoreAccessLogSettings bool, accessLogFormatKeysMap map[string]AccessLogFormatMap, mapDiagnostics *MapDiagnostics) []string {
func getLogGroupNamesHttpApis(
ctx context.Context,
conn AwsApiGatewayProvider,
apiAllStages []string,
apiWithStage map[string][]string,
exclude bool,
ignoreAccessLogSettings bool,
accessLogFormatKeysMap map[string]AccessLogFormatMap,
mapDiagnostics *MapDiagnostics) []string {
var summary string
// apiStageMappingRest is a map of api id to list of api stages that need to be considered
// if the value list is empty, it means that all stages in this api should be considered
apiStageMappingV2 := make(map[string][]string)
apiGatewayV2Client := conn.getApiGatewayV2Client()
res, err := apiGatewayV2Client.GetApis(context.TODO(), &v2.GetApisInput{})
res, err := apiGatewayV2Client.GetApis(ctx, &v2.GetApisInput{})
if err != nil {
summary = fmt.Sprintf("Error while invoking getApis sdk call: %s", err.Error())
mapDiagnostics.add(errorDiagnostic(summary))
Expand All @@ -213,17 +276,30 @@ func getLogGroupNamesHttpApis(conn AwsApiGatewayProvider, apiAllStages []string,
}
}
if !ignoreAccessLogSettings {
return getLogGroupNamesHttpApisHelper(conn, apiStageMappingV2, exclude, accessLogFormatKeysMap, mapDiagnostics)
return getLogGroupNamesHttpApisHelper(
ctx,
conn,
apiStageMappingV2,
exclude,
accessLogFormatKeysMap,
mapDiagnostics)
}
return []string{}
}

func getLogGroupNamesRestApisHelper(conn AwsApiGatewayProvider, apiStageMappingRest map[string][]string, exclude bool,
ignoreAccessLogSettings bool, accessLogFormatKeysMap map[string]AccessLogFormatMap, mapDiagnostics *MapDiagnostics) []string {
func getLogGroupNamesRestApisHelper(
ctx context.Context,
conn AwsApiGatewayProvider,
apiStageMappingRest map[string][]string,
exclude bool,
ignoreAccessLogSettings bool,
accessLogFormatKeysMap map[string]AccessLogFormatMap,
mapDiagnostics *MapDiagnostics) []string {

var logGroupNames []string
apiGatewayClient := conn.getApiGatewayClient()
for apiId, apiStages := range apiStageMappingRest {
res, err := apiGatewayClient.GetStages(context.TODO(), &v1.GetStagesInput{
res, err := apiGatewayClient.GetStages(ctx, &v1.GetStagesInput{
RestApiId: &apiId,
})
if err != nil {
Expand Down Expand Up @@ -264,12 +340,17 @@ func getLogGroupNamesRestApisHelper(conn AwsApiGatewayProvider, apiStageMappingR
return logGroupNames
}

func getLogGroupNamesHttpApisHelper(conn AwsApiGatewayProvider, apiStageMappingV2 map[string][]string, exclude bool,
accessLogFormatKeysMap map[string]AccessLogFormatMap, mapDiagnostics *MapDiagnostics) []string {
func getLogGroupNamesHttpApisHelper(
ctx context.Context,
conn AwsApiGatewayProvider,
apiStageMappingV2 map[string][]string,
exclude bool,
accessLogFormatKeysMap map[string]AccessLogFormatMap,
mapDiagnostics *MapDiagnostics) []string {
var logGroupNames []string
apiGatewayV2Client := conn.getApiGatewayV2Client()
for apiId, apiStages := range apiStageMappingV2 {
res, err := apiGatewayV2Client.GetStages(context.TODO(), &v2.GetStagesInput{
res, err := apiGatewayV2Client.GetStages(ctx, &v2.GetStagesInput{
ApiId: &apiId,
})
if err != nil {
Expand Down

0 comments on commit 3fa3bc2

Please sign in to comment.