Skip to content

Commit

Permalink
Merge pull request #35174 from aristosvo/secretsmanager/aws-sdk-go-v2
Browse files Browse the repository at this point in the history
`aws_secretsmanager_*` - migrate to `aws-sdk-go-v2`
  • Loading branch information
ewbankkit authored Jan 12, 2024
2 parents 407a883 + 26da4d3 commit 85b0843
Show file tree
Hide file tree
Showing 47 changed files with 1,883 additions and 980 deletions.
3 changes: 3 additions & 0 deletions .changelog/35105.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_secretsmanager_secret_rotation: Add `rotate_immediately` argument
```
7 changes: 7 additions & 0 deletions .changelog/35117.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
data-source/aws_secretsmanager_secret: Add `created_date` and `last_changed_date` attributes
```

```release-note:enhancement
data-source/aws_secretsmanager_secret_version: Add `created_date` attribute
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.48.0
github.com/aws/aws-sdk-go-v2/service/s3control v1.41.8
github.com/aws/aws-sdk-go-v2/service/scheduler v1.6.6
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.26.1
github.com/aws/aws-sdk-go-v2/service/securityhub v1.44.2
github.com/aws/aws-sdk-go-v2/service/securitylake v1.10.7
github.com/aws/aws-sdk-go-v2/service/servicecatalogappregistry v1.24.8
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ github.com/aws/aws-sdk-go-v2/service/s3control v1.41.8 h1:sNRLDR2mSZuu+BU6mHbpsV
github.com/aws/aws-sdk-go-v2/service/s3control v1.41.8/go.mod h1:fxV+LYjoXZKrMMYSp+UMmgJK/oNxnogfYh12ZcrdbxU=
github.com/aws/aws-sdk-go-v2/service/scheduler v1.6.6 h1:UGSUCgzcayABoswjfZPPC7KzQ42jFnbd+7YtbiSK+mw=
github.com/aws/aws-sdk-go-v2/service/scheduler v1.6.6/go.mod h1:ZVDwUL35K1x24YFqlUVjFgN1dpHVcfDqrYVa3PKWZlo=
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.26.1 h1:Sn3MAV9YeACCULaxNWWYFH1a6G4wYFwBn3/TA5MwE2Q=
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.26.1/go.mod h1:qutL00aW8GSo2D0I6UEOqMvRS3ZyuBrOC1BLe5D2jPc=
github.com/aws/aws-sdk-go-v2/service/securityhub v1.44.2 h1:uzdslJwui029KDFFmB6a9pzhCDuRVqqdjUlbqKVmNrk=
github.com/aws/aws-sdk-go-v2/service/securityhub v1.44.2/go.mod h1:/bd0JTnfysvNRGN27JGDeCco/KMMXOuZaI4wtQ7li38=
github.com/aws/aws-sdk-go-v2/service/securitylake v1.10.7 h1:rBGyFiX7l7+g/dMkkfoTrzQjjFEnotURc5kFdWrW8DA=
Expand Down
6 changes: 3 additions & 3 deletions internal/conns/awsclient_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ var sliceServiceNames = []string{
"rds",
"resourcegroupstaggingapi",
"route53resolver",
"secretsmanager",
}

type TemplateData struct {
Expand Down
23 changes: 0 additions & 23 deletions internal/generate/namevaluesfilters/service_filters_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions internal/generate/namevaluesfiltersv2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# namevaluesfiltersv2

The `namevaluesfiltersv2` package is designed to provide a consistent interface for handling AWS resource filtering for AWS SDK for Go v2.

This package implements a single `NameValuesFilters` type, which covers all filter handling logic, such as merging filters, via functions on the single type. The underlying implementation is compatible with Go operations such as `len()`.

Full documentation for this package can be found on [GoDoc](https://godoc.org/github.com/hashicorp/terraform-provider-aws/internal/generate/namevaluesfiltersv2).

Many AWS Go SDK v2 services that support resource filtering have their service-specific Go type conversion functions to and from `NameValuesFilters` code generated. Converting from `NameValuesFilters` to AWS Go SDK v2 types is done via `{SERVICE}Filters()` functions on the type. For more information about this code generation, see the [`generators/servicefilters` README](generators/servicefilters/README.md).

Any filtering functions that cannot be generated should be hand implemented in a service-specific source file and follow the format of similar generated code wherever possible. The first line of the source file should be `// +build !generate`. This prevents the file's inclusion during the code generation phase.

## Code Structure

```text
internal/generate/namevaluesfiltersv2
├── generators
│ └── servicefilters (generates service_filters_gen.go)
├── name_values_filters_test.go (unit tests for core logic)
├── name_values_filters.go (core logic)
├── service_generation_customizations.go (shared AWS Go SDK service customizations for generators)
├── service_filters_gen.go (generated AWS Go SDK service conversion functions)
└── <service name>_filters.go (any service-specific functions that cannot be generated)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# servicefilters

This package contains a code generator to consistently handle the various AWS Go SDK service implementations for converting service filter types to/from `NameValuesFilters`. Not all AWS Go SDK services that support filters are generated in this manner.

To run this code generator, execute `go generate ./...` from the root of the repository. The general workflow for the generator is:

- Generate Go file contents via template from local variables and functions
- Go format file contents
- Write file contents to `service_filters_gen.go` file

## Example Output

```go
// DocDBFilters returns docdb service filters.
func (filters NameValuesFilters) DocDBFilters() []*docdb.Filter {
m := filters.Map()

if len(m) == 0 {
return nil
}

result := make([]*docdb.Filter, 0, len(m))

for k, v := range m {
filter := &docdb.Filter{
Name: aws.String(k),
Values: aws.StringSlice(v),
}

result = append(result, filter)
}

return result
}
```

## Implementing a New Generated Service

- In `main.go`: Add service name, e.g. `docdb`, to one of the implementation handlers
- Use `sliceServiceNames` if the AWS Go SDK service implements a specific Go type such as `Filter`
- Run `go generate ./...` (or `make gen`) from the root of the repository to regenerate the code
- Run `go test ./...` (or `make test`) from the root of the repository to ensure the generated code compiles
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//go:build generate
// +build generate

package main

import (
"bytes"
"go/format"
"log"
"os"
"sort"
"strings"
"text/template"

"github.com/hashicorp/terraform-provider-aws/internal/generate/namevaluesfiltersv2"
)

const filename = `service_filters_v2_gen.go`

// Representing types such as []*ec2.Filter, []*rds.Filter, ...
var sliceServiceNames = []string{
"secretsmanager",
}

type TemplateData struct {
SliceServiceNames []string
}

func main() {
// Always sort to reduce any potential generation churn
sort.Strings(sliceServiceNames)

templateData := TemplateData{
SliceServiceNames: sliceServiceNames,
}
templateFuncMap := template.FuncMap{
"FilterPackage": namevaluesfiltersv2.ServiceFilterPackage,
"FilterPackagePrefix": namevaluesfiltersv2.ServiceFilterPackagePrefix,
"FilterType": namevaluesfiltersv2.ServiceFilterType,
"FilterTypeNameField": namevaluesfiltersv2.ServiceFilterTypeNameField,
"FilterTypeValuesField": namevaluesfiltersv2.ServiceFilterTypeValuesField,
"Title": strings.Title,
}

tmpl, err := template.New("servicefilters").Funcs(templateFuncMap).Parse(templateBody)

if err != nil {
log.Fatalf("error parsing template: %s", err)
}

var buffer bytes.Buffer
err = tmpl.Execute(&buffer, templateData)

if err != nil {
log.Fatalf("error executing template: %s", err)
}

generatedFileContents, err := format.Source(buffer.Bytes())

if err != nil {
log.Fatalf("error formatting generated file: %s", err)
}

f, err := os.Create(filename)

if err != nil {
log.Fatalf("error creating file (%s): %s", filename, err)
}

defer f.Close()

_, err = f.Write(generatedFileContents)

if err != nil {
log.Fatalf("error writing to file (%s): %s", filename, err)
}
}

var templateBody = `
// Code generated by generators/servicefilters/main.go; DO NOT EDIT.
package namevaluesfiltersv2
import ( // nosemgrep:ci.semgrep.aws.multiple-service-imports
{{- range .SliceServiceNames }}
{{- if eq . (. | FilterPackage) }}
{{ . }}
{{- end }}
{{- end }}
)
// []*SERVICE.Filter handling
{{- range .SliceServiceNames }}
// {{ . | Title }}Filters returns {{ . }} service filters.
func (filters NameValuesFilters) {{ . | Title }}Filters() []*{{ . | FilterPackagePrefix }}.{{ . | FilterType }} {
m := filters.Map()
if len(m) == 0 {
return nil
}
result := make([]{{ . | FilterPackagePrefix }}.{{ . | FilterType }}, 0, len(m))
for k, v := range m {
filter := {{ . | FilterPackagePrefix }}.{{ . | FilterType }}{
{{ . | FilterTypeNameField }}: { . | FilterPackagePrefix }}.FilterNameStringType(k),
{{ . | FilterTypeValuesField }}: v,
}
result = append(result, filter)
}
return result
}
{{- end }}
`
Loading

0 comments on commit 85b0843

Please sign in to comment.