-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add configuration for controlling the autonaming behavior #1831
Changes from all commits
178c614
6b83a8d
6a7ba6c
40a8d56
7fad16d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,23 +4,30 @@ package autonaming | |
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
"github.com/pulumi/pulumi-aws-native/provider/pkg/metadata" | ||
"github.com/pulumi/pulumi/sdk/v3/go/common/resource" | ||
) | ||
|
||
type AutoNamingConfig struct { | ||
AutoTrim bool `json:"autoTrim"` | ||
RandomSuffixMinLength int `json:"randomSuffixMinLength"` | ||
} | ||
|
||
func ApplyAutoNaming( | ||
spec *metadata.AutoNamingSpec, | ||
urn resource.URN, | ||
randomSeed []byte, | ||
olds, | ||
news resource.PropertyMap, | ||
config *AutoNamingConfig, | ||
) error { | ||
if spec == nil { | ||
return nil | ||
} | ||
// Auto-name fields if not already specified | ||
val, err := getDefaultName(randomSeed, urn, spec, olds, news) | ||
val, err := getDefaultName(randomSeed, urn, spec, olds, news, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -39,6 +46,7 @@ func getDefaultName( | |
autoNamingSpec *metadata.AutoNamingSpec, | ||
olds, | ||
news resource.PropertyMap, | ||
config *AutoNamingConfig, | ||
) (resource.PropertyValue, error) { | ||
sdkName := autoNamingSpec.SdkName | ||
|
||
|
@@ -58,10 +66,23 @@ func getDefaultName( | |
return resource.PropertyValue{}, err | ||
} | ||
|
||
var autoTrim bool | ||
// resource.NewUniqueName does not allow for a random suffix shorter than 1. | ||
randomSuffixMinLength := 1 | ||
if config != nil { | ||
autoTrim = config.AutoTrim | ||
if config.RandomSuffixMinLength != 0 { | ||
randomSuffixMinLength = config.RandomSuffixMinLength | ||
} | ||
} | ||
|
||
// Generate random name that fits the length constraints. | ||
name := urn.Name() | ||
prefix := name + "-" | ||
randLength := 7 | ||
randLength := 7 // Default value | ||
if randomSuffixMinLength > randLength { | ||
randLength = randomSuffixMinLength | ||
} | ||
if len(prefix)+namingTrivia.Length()+randLength < autoNamingSpec.MinLength { | ||
randLength = autoNamingSpec.MinLength - len(prefix) - namingTrivia.Length() | ||
} | ||
|
@@ -70,6 +91,19 @@ func getDefaultName( | |
if autoNamingSpec.MaxLength > 0 { | ||
left := autoNamingSpec.MaxLength - len(prefix) - namingTrivia.Length() | ||
|
||
if left <= 0 && autoTrim { | ||
autoTrimMaxLength := autoNamingSpec.MaxLength - namingTrivia.Length() - randomSuffixMinLength | ||
if autoTrimMaxLength <= 0 { | ||
return resource.PropertyValue{}, fmt.Errorf("failed to auto-generate value for %[1]q."+ | ||
" Prefix: %[2]q is too large to fix max length constraint of %[3]d"+ | ||
" with required suffix length %[4]d. Please provide a value for %[1]q", | ||
sdkName, prefix, autoNamingSpec.MaxLength, randomSuffixMinLength) | ||
} | ||
prefix = trimName(prefix, autoTrimMaxLength) | ||
randLength = randomSuffixMinLength | ||
left = randomSuffixMinLength | ||
} | ||
|
||
if left <= 0 { | ||
if namingTrivia.Length() > 0 { | ||
return resource.PropertyValue{}, fmt.Errorf("failed to auto-generate value for %[1]q."+ | ||
|
@@ -101,3 +135,25 @@ func getDefaultName( | |
|
||
return resource.NewStringProperty(random), nil | ||
} | ||
|
||
// trimName will trim the prefix to fit within the max length constraint. | ||
// It will cut out part of the middle, keeping the beginning and the end of the string. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is the right thing to do given the current constraints. But it could also be rather surprising to users. For example, I often split my resource names with hyphens. This could generate some rather odd names. But given that pulumi/pulumi#17592 is not ready yet, I think this is a good choice. |
||
// This is so that really long generated names can still be unique. For example, if the | ||
// user creates a resource name by appending the parent onto the child, you could end up | ||
// with names like: | ||
// - "topParent-middleParent-bottonParent-child1" | ||
// - "topParent-middleParent-bottonParent-child2" | ||
// | ||
// If the max length is 30, the trimmed generated name for both would be something like | ||
// "topParent-middleParent-bottonP" and you would not be able to tell them apart. | ||
// | ||
// By trimming from the middle you would end up with something like this, preserving | ||
// the uniqueness of the generated names: | ||
// - "topParent-middlonParent-child1" | ||
// - "topParent-middlonParent-child2" | ||
func trimName(name string, maxLength int) string { | ||
floorHalf := math.Floor(float64(maxLength) / 2) | ||
half := int(floorHalf) | ||
left := maxLength - half | ||
return name[0:half] + name[len(name)-left:] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So surprised to see Fortran-style 1-based indices here but I think this checks out. This is right.