{"action":"created","issue":{"url":"https://api.github.com/repos/hashicorp/terraform/issues/17389","repository_url":"https://api.github.com/repos/hashicorp/terraform","labels_url":"https://api.github.com/repos/hashicorp/terraform/issues/17389/labels{/name}","comments_url":"https://api.github.com/repos/hashicorp/terraform/issues/17389/comments","events_url":"https://api.github.com/repos/hashicorp/terraform/issues/17389/events","html_url":"https://github.com/hashicorp/terraform/issues/17389","id":298487909,"node_id":"MDU6SXNzdWUyOTg0ODc5MDk=","number":17389,"title":"helper/schema: Introduce PrefixTo (for generated names)","user":{"login":"radeksimko","id":287584,"node_id":"MDQ6VXNlcjI4NzU4NA==","avatar_url":"https://avatars1.githubusercontent.com/u/287584?v=4","gravatar_id":"","url":"https://api.github.com/users/radeksimko","html_url":"https://github.com/radeksimko","followers_url":"https://api.github.com/users/radeksimko/followers","following_url":"https://api.github.com/users/radeksimko/following{/other_user}","gists_url":"https://api.github.com/users/radeksimko/gists{/gist_id}","starred_url":"https://api.github.com/users/radeksimko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/radeksimko/subscriptions","organizations_url":"https://api.github.com/users/radeksimko/orgs","repos_url":"https://api.github.com/users/radeksimko/repos","events_url":"https://api.github.com/users/radeksimko/events{/privacy}","received_events_url":"https://api.github.com/users/radeksimko/received_events","type":"User","site_admin":false},"labels":[{"id":85946959,"node_id":"MDU6TGFiZWw4NTk0Njk1OQ==","url":"https://api.github.com/repos/hashicorp/terraform/labels/enhancement","name":"enhancement","color":"d4c5f9","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-02-20T06:47:06Z","updated_at":"2018-06-02T08:01:28Z","closed_at":null,"author_association":"MEMBER","body":"It's becoming common for resources in various providers to want to generate random names for ephemeral resources and to do so by exposing field like `name_prefix`.\r\n\r\nhttps://github.com/terraform-providers/terraform-provider-aws/search?l=Go&q=name_prefix&type=&utf8=%E2%9C%93\r\n\r\nhttps://github.com/terraform-providers/terraform-provider-azurerm/pull/767\r\n\r\nThis was always possible to achieve using the [`random` provider](https://www.terraform.io/docs/providers/random/r/id.html), but `name_prefix` approach is more convenient.\r\n\r\nThis issue is therefore not about introducing any new behaviour, just about enforcing some rules and unifying the way all resources across providers approach this problem to ensure nobody has to come up with their own unique way of doing this.\r\n\r\n## Example before\r\n\r\n```go\r\n\"name\": {\r\n\tType:          schema.TypeString,\r\n\tOptional:      true,\r\n\tComputed:      true,\r\n\tForceNew:      true,\r\n\tConflictsWith: []string{\"name_prefix\"},\r\n\tValidateFunc:  validateArmStorageAccountName,\r\n},\r\n\"name_prefix\": {\r\n\tType:         schema.TypeString,\r\n\tOptional:     true,\r\n\tForceNew:     true,\r\n\tValidateFunc: validateArmStorageAccountName,\r\n},\r\n\r\n// Plus bunch of code in CRUD involving resource.PrefixedUniqueId() and resource.UniqueId()\r\n// which we intend to reduce\r\n\r\nvar storageAccountName string\r\nif v, ok := d.GetOk(\"name\"); ok {\r\n\tstorageAccountName = v.(string)\r\n} else if v, ok := d.GetOk(\"name_prefix\"); ok {\r\n\tstorageAccountName = resource.PrefixedUniqueId(v.(string))\r\n} else {\r\n\tstorageAccountName = resource.UniqueId()\r\n}\r\nstorageAccountName = storageAccountName[:23]\r\nd.Set(\"name\", storageAccountName)\r\n```\r\n\r\n## Example after\r\n\r\n```go\r\n\"name\": {\r\n\tType:          schema.TypeString,\r\n\tOptional:      true,\r\n\tComputed:      true,\r\n\tForceNew:      true,\r\n\tConflictsWith: []string{\"name_prefix\"},\r\n\tValidateFunc:  validateArmStorageAccountName,\r\n},\r\n\"name_prefix\": {\r\n\tType:         schema.TypeString,\r\n\tOptional:     true,\r\n\tForceNew:     true,\r\n\tPrefixTo:     &schema.Prefix{Field: \"name\", MaxPrefixLength: 8},\r\n\tValidateFunc: validateArmStorageAccountName,\r\n},\r\n\r\n// Reduced code\r\nstorageAccountName := d.Get(\"name\")\r\n```\r\n\r\nHaving this logic in the schema would also enable us to internally validate it before shipping and ensure that developers follow the same good practice - e.g.\r\n\r\n - `name` & `name_prefix` both have `ConflictsWith`\r\n - both fields are `Optional`\r\n - `name` is `Computed`\r\n - _(this one is a bit trickier)_ name has length restriction and it's clear how many characters do we have left for the prefix"},"comment":{"url":"https://api.github.com/repos/hashicorp/terraform/issues/comments/394068099","html_url":"https://github.com/hashicorp/terraform/issues/17389#issuecomment-394068099","issue_url":"https://api.github.com/repos/hashicorp/terraform/issues/17389","id":394068099,"node_id":"MDEyOklzc3VlQ29tbWVudDM5NDA2ODA5OQ==","user":{"login":"radeksimko","id":287584,"node_id":"MDQ6VXNlcjI4NzU4NA==","avatar_url":"https://avatars1.githubusercontent.com/u/287584?v=4","gravatar_id":"","url":"https://api.github.com/users/radeksimko","html_url":"https://github.com/radeksimko","followers_url":"https://api.github.com/users/radeksimko/followers","following_url":"https://api.github.com/users/radeksimko/following{/other_user}","gists_url":"https://api.github.com/users/radeksimko/gists{/gist_id}","starred_url":"https://api.github.com/users/radeksimko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/radeksimko/subscriptions","organizations_url":"https://api.github.com/users/radeksimko/orgs","repos_url":"https://api.github.com/users/radeksimko/repos","events_url":"https://api.github.com/users/radeksimko/events{/privacy}","received_events_url":"https://api.github.com/users/radeksimko/received_events","type":"User","site_admin":false},"created_at":"2018-06-02T08:01:28Z","updated_at":"2018-06-02T08:01:28Z","author_association":"MEMBER","body":"@katbyte I'm not against the idea, it is in fact a solution I've been advocating for in the past, but I can also see how a special argument and implementation on provider/resource side would be valuable.\r\nLimitations on length, character set etc. usually differ per resource and field and the user is left with (sometimes uneasy) task of finding these limitations themselves to avoid apply-time failures.\r\n\r\nAnother solution - although perhaps long shot - is to implement some sort of heuristic plan-time validation for fields with interpolation functions.\r\n\r\ni.e. if we knew that `prefixed_id(\"prefix\", 10)` will generate 16 characters and then have standard `ValidateFunc`, we could fail the validation at plan time."}}
{"actor":{"display_login":"radeksimko"}}