Skip to content

Commit

Permalink
Merge pull request #39804 from hashicorp/td-resiliencehub-tags
Browse files Browse the repository at this point in the history
Tagging tests for `resiliencehub`
  • Loading branch information
gdavison authored Oct 21, 2024
2 parents 4ebff58 + 80c5d90 commit 7c4c42d
Show file tree
Hide file tree
Showing 15 changed files with 2,666 additions and 164 deletions.
6 changes: 4 additions & 2 deletions docs/resource-tagging.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,10 @@ In that case, add the annotations `@Testing(existsTakesT=true)` and `@Testing(de
The generated acceptance tests use `ImportState` steps.
In most cases, these will work as-is.
To ignore the values of certain parameters when importing, set the annotation `@Testing(importIgnore="...")` to a list of the parameter names separated by semi-colons (`;`).
To override the import ID, use the annotation `@Testing(importStateId=<var name>)` if it can be retrieved from an existing variable,
or use `@Testing(importStateIdFunc=<func name>)` to reference a function that returns a `resource.ImportStateIdFunc`.
There are multiple methods for overriding the import ID, if needed.
To use the value of an existing variable, use the annotation `@Testing(importStateId=<var name>)`.
If the identifier can be retrieved from a specific resource attribute, use the annotation `@Testing(importStateIdAttribute=<attribute name>)`.
If the identifier can be retrieved from a `resource.ImportStateIdFunc`, use the annotation `@Testing(importStateIdFunc=<func name>)`.
If the resource type does not support importing, use the annotation `@Testing(noImport=true)`.

If the tests need to be serialized, use the annotion `@Testing(serialize=true)`.
Expand Down
23 changes: 23 additions & 0 deletions internal/acctest/state_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package acctest

import (
"fmt"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

// AttrImportStateIdFunc is a resource.ImportStateIdFunc that returns the value of the specified attribute
func AttrImportStateIdFunc(resourceName, attrName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}

return rs.Primary.Attributes[attrName], nil
}
}
12 changes: 12 additions & 0 deletions internal/generate/tagstests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ type ResourceDatum struct {
Generator string
NoImport bool
ImportStateID string
importStateIDAttribute string
ImportStateIDFunc string
ImportIgnore []string
Implementation implementation
Expand Down Expand Up @@ -400,6 +401,14 @@ func (d ResourceDatum) AdditionalTfVars() map[string]string {
})
}

func (d ResourceDatum) HasImportStateIDAttribute() bool {
return d.importStateIDAttribute != ""
}

func (d ResourceDatum) ImportStateIDAttribute() string {
return namesgen.ConstOrQuote(d.importStateIDAttribute)
}

func (d ResourceDatum) OverrideIdentifier() bool {
return d.overrideIdentifierAttribute != ""
}
Expand Down Expand Up @@ -643,6 +652,9 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
if attr, ok := args.Keyword["importStateId"]; ok {
d.ImportStateID = attr
}
if attr, ok := args.Keyword["importStateIdAttribute"]; ok {
d.importStateIDAttribute = attr
}
if attr, ok := args.Keyword["importStateIdFunc"]; ok {
d.ImportStateIDFunc = attr
}
Expand Down
15 changes: 10 additions & 5 deletions internal/generate/tagstests/resource_test.go.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,28 @@ plancheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrTagsAll), know
{{ if gt (len .ImportStateID) 0 -}}
ImportStateId: {{ .ImportStateID }},
{{ end -}}
{{ if gt (len .ImportStateIDFunc) 0 -}}
{{ if .HasImportStateIDAttribute -}}
ImportStateIdFunc: acctest.AttrImportStateIdFunc(resourceName, {{ .ImportStateIDAttribute }}),
{{ else if gt (len .ImportStateIDFunc) 0 -}}
ImportStateIdFunc: {{ .ImportStateIDFunc }}(resourceName),
{{ end -}}
ImportStateVerify: true,
{{ if .HasImportStateIDAttribute -}}
ImportStateVerifyIdentifierAttribute: {{ .ImportStateIDAttribute }},
{{ end }}
{{- end }}

{{ define "ImportBody" }}
{{ template "CommonImportBody" . }}
{{ if gt (len .ImportIgnore) 0 -}}
{{ template "CommonImportBody" . -}}
{{- if gt (len .ImportIgnore) 0 -}}
ImportStateVerifyIgnore: []string{
{{ range $i, $v := .ImportIgnore }}{{ $v }},{{ end }}
},
{{- end }}
{{ end }}

{{ define "ImportBodyIgnoreKey1" }}
{{ template "CommonImportBody" . }}
{{ template "CommonImportBody" . -}}
{{ if eq .Implementation "framework" -}}
ImportStateVerifyIgnore: []string{
acctest.CtTagsKey1, // The canonical value returned by the AWS API is ""
Expand All @@ -84,7 +89,7 @@ plancheck.ExpectKnownValue(resourceName, tfjsonpath.New(names.AttrTagsAll), know
{{ end }}

{{ define "ImportBodyIgnoreResourceKey1" }}
{{ template "CommonImportBody" . }}
{{ template "CommonImportBody" . -}}
{{ if eq .Implementation "framework" -}}
ImportStateVerifyIgnore: []string{
"tags.resourcekey1", // The canonical value returned by the AWS API is ""
Expand Down
1 change: 1 addition & 0 deletions internal/service/resiliencehub/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ServiceTagsMap -KVTValues -SkipTypesImp -ListTags -ListTagsInIDElem=ResourceArn -ListTagsOutTagsElem=Tags -TagOp=TagResource -TagInIDElem=ResourceArn -UntagOp=UntagResource -UpdateTags
//go:generate go run ../../generate/servicepackage/main.go
//go:generate go run ../../generate/tagstests/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.

package resiliencehub
2 changes: 2 additions & 0 deletions internal/service/resiliencehub/resiliency_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (

// @FrameworkResource("aws_resiliencehub_resiliency_policy", name="Resiliency Policy")
// @Tags(identifierAttribute="arn")
// @Testing(existsType="github.com/aws/aws-sdk-go-v2/service/resiliencehub;resiliencehub.DescribeResiliencyPolicyOutput")
// @Testing(importStateIdAttribute="arn")
func newResourceResiliencyPolicy(_ context.Context) (resource.ResourceWithConfigure, error) {
r := &resourceResiliencyPolicy{}

Expand Down
Loading

0 comments on commit 7c4c42d

Please sign in to comment.