Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support import of _prefix arguments #33852

Merged
merged 116 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
116 commits
Select commit Hold shift + click to select a range
c72a7d1
r/aws_s3_bucket: Validate 'bucket_prefix' is imported correctly.
ewbankkit Oct 7, 2023
eaf3a4d
Acceptance test output:
ewbankkit Oct 7, 2023
e0b6531
r/aws_cloudfront_public_key: Tidy up resource Create.
ewbankkit Oct 7, 2023
276782e
r/aws_cloudfront_public_key: Tidy up resource Update.
ewbankkit Oct 7, 2023
d67a437
r/aws_cloudfront_public_key: Tidy up resource Delete.
ewbankkit Oct 7, 2023
ec04649
r/aws_cloudfront_public_key: Tidy up resource Read.
ewbankkit Oct 7, 2023
88cfeb4
r/aws_cloudfront_public_key: Tidy up acceptance tests.
ewbankkit Oct 7, 2023
75165e3
Acceptance test output:
ewbankkit Oct 7, 2023
fbcd68f
Add 'NameGenerator'.
ewbankkit Oct 9, 2023
99efd29
Add 'TestName' and 'TestNameWithSuffix'.
ewbankkit Oct 9, 2023
d14abaa
Remove 'create.NameWithSuffix'.
ewbankkit Oct 9, 2023
b9c3f95
r/aws_sns_topic: Add 'topicName'.
ewbankkit Oct 9, 2023
909cd85
r/aws_sqs_topic: Add 'queueName'.
ewbankkit Oct 9, 2023
ed77070
Add 'TestNameWithDefaultPrefix'.
ewbankkit Oct 9, 2023
2b978f0
Remove 'Resource name generation with suffix' section from Contributo…
ewbankkit Oct 9, 2023
a6e77ca
r/aws_cloudfront_public_key: Support import of `name_prefix` argument.
ewbankkit Oct 9, 2023
9a726b5
Correct CHANGELOG entry file name.
ewbankkit Oct 9, 2023
196035a
Add 'acctest.CheckResourceAttrNameGeneratedWithPrefix'.
ewbankkit Oct 9, 2023
8aacbd0
Merge branch 'main' into f-importing_prefix
ewbankkit Oct 13, 2023
18feaff
Merge branch 'main' into f-importing_prefix
ewbankkit Oct 13, 2023
fa03f89
r/aws_docdb_cluster_parameter_group: Tidy up Create and Update.
ewbankkit Oct 13, 2023
182e0c4
r/aws_docdb_cluster_parameter_group: Tidy up Read and Delete.
ewbankkit Oct 13, 2023
4befb58
Acceptance test output:
ewbankkit Oct 13, 2023
7dde38f
r/aws_docdb_cluster_parameter_group: Tidy up sweeper.
ewbankkit Oct 13, 2023
274bd74
r/aws_docdb_subnet_group: Alphabetize attributes.
ewbankkit Oct 14, 2023
87ec452
r/aws_docdb_subnet_group: Tidy up Create.
ewbankkit Oct 14, 2023
fef91a7
r/aws_docdb_subnet_group: Tidy up Delete.
ewbankkit Oct 14, 2023
84c02d6
r/aws_docdb_subnet_group: Tidy up Update.
ewbankkit Oct 14, 2023
742207a
r/aws_docdb_subnet_group: Tidy up Read.
ewbankkit Oct 14, 2023
74bd386
r/aws_docdb_subnet_group: Tidy up acceptance tests.
ewbankkit Oct 14, 2023
bdab86a
r/aws_docdb_subnet_group: Tidy sweeper.
ewbankkit Oct 14, 2023
c67058b
Acceptance test output:
ewbankkit Oct 14, 2023
59d6de2
r/aws_docdb_subnet_group: Support import of `name_prefix` argument.
ewbankkit Oct 14, 2023
86e366c
Acceptance test output:
ewbankkit Oct 14, 2023
39e8e0e
r/aws_docdb_cluster_parameter_group: Support import of `name_prefix` …
ewbankkit Oct 14, 2023
19b25c9
Acceptance test output:
ewbankkit Oct 14, 2023
631a22d
r/aws_docdb_cluster: Support import of `cluster_identifier_prefix` ar…
ewbankkit Oct 15, 2023
6063fa0
Acceptance test output:
ewbankkit Oct 15, 2023
f6aedfc
r/aws_docdb_cluster_instance: Support import of `identifier_prefix` a…
ewbankkit Oct 15, 2023
7239e2f
Acceptance test output:
ewbankkit Oct 16, 2023
e15883b
r/aws_elb: Support import of `name_prefix` argument.
ewbankkit Oct 16, 2023
a3515db
Acceptance test output:
ewbankkit Oct 16, 2023
654ddb1
r/aws_lb: Support import of `name_prefix` argument.
ewbankkit Oct 16, 2023
5a70377
Acceptance test output:
ewbankkit Oct 16, 2023
8b6e318
r/aws_lb_target_group: Support import of `name_prefix` argument.
ewbankkit Oct 16, 2023
1d56b04
Acceptance test output:
ewbankkit Oct 16, 2023
cb79a57
r/aws_emr_security_configuration: Alphabetize attributes.
ewbankkit Oct 16, 2023
98dd632
r/aws_emr_security_configuration: Support import of `name_prefix` arg…
ewbankkit Oct 16, 2023
2ab983f
r/aws_emr_security_configuration: Additional acceptance tests.
ewbankkit Oct 16, 2023
c201d5f
Acceptance test output:
ewbankkit Oct 16, 2023
c9b66c5
r/aws_iam_group_policy: Tidy up Create.
ewbankkit Oct 16, 2023
dc04d71
r/aws_iam_group_policy: Tidy up Delete.
ewbankkit Oct 16, 2023
9f37fe1
Add 'tfresource.RetryUntilEqual'.
ewbankkit Oct 16, 2023
9fdf215
r/aws_iam_group_policy: Tidy up Read.
ewbankkit Oct 16, 2023
3ae6e85
r/aws_iam_group_policy: Support import of `name_prefix` argument.
ewbankkit Oct 16, 2023
9c21d43
r/aws_iam_role_policy: Tidy up Create.
ewbankkit Oct 16, 2023
1acdfa4
r/aws_iam_role_policy: Tidy up Delete.
ewbankkit Oct 16, 2023
b3bd259
r/aws_iam_role_policy: Tidy up Read.
ewbankkit Oct 16, 2023
377814d
r/aws_iam_role_policy: Support import of `name_prefix` argument.
ewbankkit Oct 16, 2023
d575407
Acceptance test output:
ewbankkit Oct 16, 2023
b5170b2
r/aws_iam_user_policy: Tidy up Create.
ewbankkit Oct 16, 2023
caa70a4
r/aws_iam_user_policy: Tidy up Delete.
ewbankkit Oct 16, 2023
29e15d7
r/aws_iam_user_policy: Tidy up Read.
ewbankkit Oct 16, 2023
c745d71
r/aws_iam_user_policy: Support import of `name_prefix` argument.
ewbankkit Oct 16, 2023
80f158d
Acceptance test output:
ewbankkit Oct 16, 2023
9f65ab5
r/aws_lightsail_key_pair: Alphabetize attributes.
ewbankkit Oct 17, 2023
3bf81e0
r/aws_lightsail_key_pair: Set 'name_prefix' in Read.
ewbankkit Oct 17, 2023
b101825
Acceptance test output:
ewbankkit Oct 17, 2023
98f3490
Merge branch 'main' into f-importing_prefix
ewbankkit Oct 17, 2023
804b843
r/aws_neptune_cluster: Support import of `cluster_identifier_prefix` …
ewbankkit Oct 17, 2023
f0adad2
Acceptance test output:
ewbankkit Oct 17, 2023
2471466
r/aws_neptune_cluster_instance: Support import of `identifier_prefix`…
ewbankkit Oct 17, 2023
25b4ee9
Acceptance test output:
ewbankkit Oct 17, 2023
b13095a
r/aws_neptune_cluster_parameter_group: Support import of `name_prefix…
ewbankkit Oct 17, 2023
78f42b8
Acceptance test output:
ewbankkit Oct 17, 2023
755cff7
r/aws_neptune_event_subscription: Alphabetize attributes.
ewbankkit Oct 17, 2023
680926e
r/aws_neptune_event_subscription: Tidy up Read.
ewbankkit Oct 17, 2023
da3ee85
r/aws_neptune_event_subscription: Tidy up Delete.
ewbankkit Oct 17, 2023
6e38eef
r/aws_neptune_event_subscription: Tidy up Create.
ewbankkit Oct 17, 2023
1c383ac
r/aws_neptune_event_subscription: Tidy up Update.
ewbankkit Oct 17, 2023
88e3411
r/aws_neptune_event_subscription: Support import of `name_prefix` arg…
ewbankkit Oct 17, 2023
9e7efa1
Fix golangci-lint 'nolintlint'.
ewbankkit Oct 17, 2023
42c560a
r/aws_neptune_event_subscription: Fix acceptance tests.
ewbankkit Oct 17, 2023
f0154e0
Acceptance test output:
ewbankkit Oct 18, 2023
613bbb3
r/aws_pinpoint_app: Alphabetize attributes.
ewbankkit Oct 18, 2023
233b771
r/aws_pinpoint_app: Tidy up Create.
ewbankkit Oct 18, 2023
d7cfaa6
r/aws_pinpoint_app: Tidy up Update.
ewbankkit Oct 18, 2023
6d821e3
r/aws_pinpoint_app: Tidy up Read.
ewbankkit Oct 18, 2023
77d3153
r/aws_pinpoint_app: Support import of `name_prefix` argument.
ewbankkit Oct 18, 2023
1d684ee
Acceptance test output:
ewbankkit Oct 18, 2023
58c699f
r/aws_db_option_group: Alphabetize attributes.
ewbankkit Oct 18, 2023
f90be26
r/aws_db_option_group: Tidy up Create.
ewbankkit Oct 18, 2023
0a1b97e
r/aws_db_option_group: Tidy up Delete.
ewbankkit Oct 18, 2023
7aa087b
r/aws_db_option_group: Tidy up Update.
ewbankkit Oct 18, 2023
57311af
r/aws_db_option_group: Tidy up Read.
ewbankkit Oct 18, 2023
ed875b2
r/aws_db_option_group: Support import of `name_prefix` argument.
ewbankkit Oct 18, 2023
5328e2c
Acceptance test output:
ewbankkit Oct 18, 2023
fdeacd7
r/aws_rds_cluster: Support import of `cluster_identifier_prefix` argu…
ewbankkit Oct 18, 2023
4bd6672
Acceptance test output:
ewbankkit Oct 18, 2023
1cf41df
r/aws_rds_cluster_instance: Support import of `identifier_prefix` arg…
ewbankkit Oct 18, 2023
85b10de
Acceptance test output:
ewbankkit Oct 18, 2023
e4fcbf9
Fixup 'TestAccIAMOpenIDConnectProvider_tags'.
ewbankkit Oct 18, 2023
510c74e
Fixup 'TestAccLogsGroup_namePrefix'.
ewbankkit Oct 18, 2023
4edd137
Fixup 'TestAccIAMPolicy_namePrefix'.
ewbankkit Oct 18, 2023
242fc17
r/aws_signer_signing_profile: Alphabetize attributes.
ewbankkit Oct 18, 2023
c4860f2
r/aws_signer_signing_profile: Tidy up Read.
ewbankkit Oct 18, 2023
e7015a9
r/aws_signer_signing_profile: Tidy up Delete.
ewbankkit Oct 18, 2023
f89a571
r/aws_signer_signing_profile: Tidy up Read.
ewbankkit Oct 18, 2023
41ddc22
r/aws_signer_signing_profile: Support import of `name_prefix` argument.
ewbankkit Oct 18, 2023
1351a13
Acceptance test output:
ewbankkit Oct 18, 2023
3fec82d
r/aws_signer_signing_profile_permission: Alphabetize attributes.
ewbankkit Oct 18, 2023
af1ca14
r/aws_signer_signing_profile_permission: Tidy up Create and Read.
ewbankkit Oct 18, 2023
e55c62f
r/aws_signer_signing_profile_permission: Tidy up Delete.
ewbankkit Oct 18, 2023
8298959
r/aws_signer_signing_profile_permission: Support import of `statement…
ewbankkit Oct 18, 2023
30a14b5
Acceptance test output:
ewbankkit Oct 18, 2023
5632e9e
r/aws_signer_signing_profile_permission: Add `signer:SignPayload` as …
ewbankkit Oct 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions .changelog/33852.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
```release-note:enhancement
resource/aws_cloudfront_public_key: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_docdb_cluster: Support import of `cluster_identifier_prefix` argument
```

```release-note:enhancement
resource/aws_docdb_cluster_instance: Support import of `identifier_prefix` argument
```

```release-note:enhancement
resource/aws_docdb_cluster_parameter_group: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_docdb_subnet_group: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_elb: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_alb: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_lb: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_alb_target_group: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_lb_target_group: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_emr_security_configuration: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_iam_group_policy: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_iam_role_policy: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_iam_user_policy: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_neptune_cluster: Support import of `cluster_identifier_prefix` argument
```

```release-note:enhancement
resource/aws_neptune_cluster_instance: Support import of `identifier_prefix` argument
```

```release-note:enhancement
resource/aws_neptune_cluster_parameter_group: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_neptune_event_subscription: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_pinpoint_app: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_db_option_group: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_rds_cluster: Support import of `cluster_identifier_prefix` argument
```

```release-note:enhancement
resource/aws_rds_cluster_instance: Support import of `identifier_prefix` argument
```

```release-note:enhancement
resource/aws_signer_signing_profile: Support import of `name_prefix` argument
```

```release-note:enhancement
resource/aws_signer_signing_profile_permission: Support import of `statement_id_prefix` argument
```

```release-note:enhancement
resource/aws_signer_signing_profile_permission: Add `signer:SignPayload` as a valid `action` value
```
18 changes: 0 additions & 18 deletions docs/resource-name-generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,3 @@ resource "aws_service_thing" "test" {
```markdown
* `name` - (Optional) Name of the thing. If omitted, Terraform will assign a random, unique name. Conflicts with `name_prefix`.
```

## Resource name generation with suffix

Some generated resource names require a fixed suffix (for example Amazon SNS FIFO topic names must end in `.fifo`).
In these cases use `create.NameWithSuffix()` in the resource `Create` function and `create.NamePrefixFromNameWithSuffix()` in the resource `Read` function, e.g.

```go
name := create.NameWithSuffix(d.Get("name").(string), d.Get("name_prefix").(string), ".fifo")
```

and

```go
d.Set("name", resp.Name)
d.Set("name_prefix", create.NamePrefixFromNameWithSuffix(aws.StringValue(resp.Name), ".fifo"))
```

There are also functions `acctest.CheckResourceAttrNameWithSuffixGenerated` and `acctest.CheckResourceAttrNameWithSuffixFromPrefix` for use in tests.
7 changes: 7 additions & 0 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,13 @@ func CheckResourceAttrNameGenerated(resourceName string, attributeName string) r
return CheckResourceAttrNameWithSuffixGenerated(resourceName, attributeName, "")
}

// CheckResourceAttrNameGeneratedWithPrefix verifies that the state attribute value matches name automatically generated with prefix
func CheckResourceAttrNameGeneratedWithPrefix(resourceName string, attributeName string, prefix string) resource.TestCheckFunc {
return func(s *terraform.State) error {
return resource.TestMatchResourceAttr(resourceName, attributeName, resourceUniqueIDPrefixPlusAdditionalSuffixRegexp(prefix, ""))(s)
}
}

// CheckResourceAttrNameWithSuffixGenerated verifies that the state attribute value matches name with suffix automatically generated without prefix
func CheckResourceAttrNameWithSuffixGenerated(resourceName string, attributeName string, suffix string) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down
84 changes: 69 additions & 15 deletions internal/create/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,7 @@ import (

// Name returns in order the name if non-empty, a prefix generated name if non-empty, or fully generated name prefixed with terraform-
func Name(name string, namePrefix string) string {
return NameWithSuffix(name, namePrefix, "")
}

// NameWithSuffix returns in order the name if non-empty, a prefix generated name if non-empty, or fully generated name prefixed with "terraform-".
// In the latter two cases, any suffix is appended to the generated name
func NameWithSuffix(name string, namePrefix string, nameSuffix string) string {
if name != "" {
return name
}

if namePrefix != "" {
return id.PrefixedUniqueId(namePrefix) + nameSuffix
}

return id.UniqueId() + nameSuffix
return NewNameGenerator(WithConfiguredName(name), WithConfiguredPrefix(namePrefix)).Generate()
}

// hasResourceUniqueIDPlusAdditionalSuffix returns true if the string has the built-in unique ID suffix plus an additional suffix
Expand Down Expand Up @@ -62,3 +48,71 @@ func NamePrefixFromNameWithSuffix(name, nameSuffix string) *string {

return &namePrefix
}

type nameGenerator struct {
configuredName string
configuredPrefix string
defaultPrefix string
suffix string
}

// nameGeneratorOptionsFunc is a type alias for a name generator functional option.
type NameGeneratorOptionsFunc func(*nameGenerator)

// WithConfiguredName is a helper function to construct functional options
// that set a name generator's configured name value.
// An empty ("") configured name inidicates that no name was configured.
func WithConfiguredName(name string) NameGeneratorOptionsFunc {
return func(g *nameGenerator) {
g.configuredName = name
}
}

// WithConfiguredPrefix is a helper function to construct functional options
// that set a name generator's configured prefix value.
// An empty ("") configured prefix inidicates that no prefix was configured.
func WithConfiguredPrefix(prefix string) NameGeneratorOptionsFunc {
return func(g *nameGenerator) {
g.configuredPrefix = prefix
}
}

// WithDefaultPrefix is a helper function to construct functional options
// that set a name generator's default prefix value.
func WithDefaultPrefix(prefix string) NameGeneratorOptionsFunc {
return func(g *nameGenerator) {
g.defaultPrefix = prefix
}
}

// WithSuffix is a helper function to construct functional options
// that set a name generator's suffix value.
func WithSuffix(suffix string) NameGeneratorOptionsFunc {
return func(g *nameGenerator) {
g.suffix = suffix
}
}

// NewNameGenerator returns a new name generator from the specified varidaic list of functional options.
func NewNameGenerator(optFns ...NameGeneratorOptionsFunc) *nameGenerator {
g := &nameGenerator{defaultPrefix: id.UniqueIdPrefix}

for _, optFn := range optFns {
optFn(g)
}

return g
}

// Generate generates a new name.
func (g *nameGenerator) Generate() string {
if g.configuredName != "" {
return g.configuredName
}

prefix := g.defaultPrefix
if g.configuredPrefix != "" {
prefix = g.configuredPrefix
}
return id.PrefixedUniqueId(prefix) + g.suffix
}
Loading
Loading