Skip to content

Commit

Permalink
Merge pull request #37483 from hashicorp/td-generate-tagtests-tlscert
Browse files Browse the repository at this point in the history
Allows generating tag tests for resource types that use self-signed certificates
  • Loading branch information
gdavison authored May 15, 2024
2 parents 716e8af + b436c1f commit 8d65842
Show file tree
Hide file tree
Showing 13 changed files with 2,070 additions and 145 deletions.
3 changes: 3 additions & 0 deletions .changelog/37483.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_iam_server_certificate: Now correctly reads tags after update and on read.
```
54 changes: 42 additions & 12 deletions internal/generate/tagstests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import (
"os"
"path"
"path/filepath"
"slices"
"strconv"
"strings"
"text/template"

"github.com/YakDriver/regexache"
"github.com/hashicorp/terraform-provider-aws/internal/generate/common"
tfmaps "github.com/hashicorp/terraform-provider-aws/internal/maps"
"github.com/hashicorp/terraform-provider-aws/names"
"github.com/hashicorp/terraform-provider-aws/names/data"
)
Expand Down Expand Up @@ -116,12 +118,14 @@ func main() {
}

if resource.GenerateConfig {
additionalTfVars := tfmaps.Keys(resource.AdditionalTfVars)
slices.Sort(additionalTfVars)
testDirPath := path.Join("testdata", resource.Name)

generateTestConfig(g, testDirPath, "tags", false, configTmplFile, configTmpl)
generateTestConfig(g, testDirPath, "tags", true, configTmplFile, configTmpl)
generateTestConfig(g, testDirPath, "tagsComputed1", false, configTmplFile, configTmpl)
generateTestConfig(g, testDirPath, "tagsComputed2", false, configTmplFile, configTmpl)
generateTestConfig(g, testDirPath, "tags", false, configTmplFile, configTmpl, additionalTfVars)
generateTestConfig(g, testDirPath, "tags", true, configTmplFile, configTmpl, additionalTfVars)
generateTestConfig(g, testDirPath, "tagsComputed1", false, configTmplFile, configTmpl, additionalTfVars)
generateTestConfig(g, testDirPath, "tagsComputed2", false, configTmplFile, configTmpl, additionalTfVars)
}
}

Expand All @@ -145,6 +149,7 @@ type ResourceDatum struct {
ExistsTypeName string
FileName string
Generator string
ImportStateID string
ImportIgnore []string
Implementation implementation
Serialize bool
Expand All @@ -153,17 +158,24 @@ type ResourceDatum struct {
NoRemoveTags bool
GoImports []goImport
GenerateConfig bool
InitCodeBlocks []codeBlock
AdditionalTfVars map[string]string
}

type goImport struct {
Path string
Alias string
}

type codeBlock struct {
Code string
}

type ConfigDatum struct {
Tags string
WithDefaultTags bool
ComputedTag bool
Tags string
WithDefaultTags bool
ComputedTag bool
AdditionalTfVars []string
}

//go:embed test.go.gtpl
Expand Down Expand Up @@ -229,7 +241,8 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {

// Look first for tagging annotations.
d := ResourceDatum{
FileName: v.fileName,
FileName: v.fileName,
AdditionalTfVars: make(map[string]string),
}
tagged := false
skip := false
Expand Down Expand Up @@ -297,6 +310,9 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
d.ImportIgnore[i] = names.ConstOrQuote(val)
}
}
if attr, ok := args.Keyword["importStateId"]; ok {
d.ImportStateID = attr
}
if attr, ok := args.Keyword["name"]; ok {
d.Name = strings.ReplaceAll(attr, " ", "")
}
Expand Down Expand Up @@ -346,6 +362,19 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
d.NoRemoveTags = b
}
}
if attr, ok := args.Keyword["tlsKey"]; ok {
if b, err := strconv.ParseBool(attr); err != nil {
v.errs = append(v.errs, fmt.Errorf("invalid skipEmptyTags value: %q at %s. Should be boolean value.", attr, fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
continue
} else if b {
d.InitCodeBlocks = append(d.InitCodeBlocks, codeBlock{
Code: `privateKeyPEM := acctest.TLSRSAPrivateKeyPEM(t, 2048)
certificatePEM := acctest.TLSRSAX509SelfSignedCertificatePEM(t, privateKeyPEM, "example.com")`,
})
d.AdditionalTfVars["certificate_pem"] = "certificatePEM"
d.AdditionalTfVars["private_key_pem"] = "privateKeyPEM"
}
}
}
}
}
Expand All @@ -367,7 +396,7 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor {
return v
}

func generateTestConfig(g *common.Generator, dirPath, test string, withDefaults bool, configTmplFile, configTmpl string) {
func generateTestConfig(g *common.Generator, dirPath, test string, withDefaults bool, configTmplFile, configTmpl string, additionalTfVars []string) {
testName := test
if withDefaults {
testName += "_defaults"
Expand All @@ -391,9 +420,10 @@ func generateTestConfig(g *common.Generator, dirPath, test string, withDefaults
}

configData := ConfigDatum{
Tags: test,
WithDefaultTags: withDefaults,
ComputedTag: (test == "tagsComputed"),
Tags: test,
WithDefaultTags: withDefaults,
ComputedTag: (test == "tagsComputed"),
AdditionalTfVars: additionalTfVars,
}
if err := tf.WriteTemplateSet(tfTemplates, configData); err != nil {
g.Fatalf("error generating Terraform file %q: %s", mainPath, err)
Expand Down
Loading

0 comments on commit 8d65842

Please sign in to comment.