Skip to content

Commit

Permalink
Relax validation of ID attribute
Browse files Browse the repository at this point in the history
Fixes #607
  • Loading branch information
paultyng committed Oct 13, 2020
1 parent 8dec490 commit 8042c25
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions helper/schema/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ var ReservedResourceFields = []string{
"connection",
"count",
"depends_on",
"id",
"lifecycle",
"provider",
"provisioner",
Expand Down Expand Up @@ -649,6 +648,14 @@ func (r *Resource) InternalValidate(topSchemaMap schemaMap, writable bool) error
}
}

if f, ok := tsm["id"]; ok {
// if there is an explicit ID, validate it...
err := validateResourceID(f)
if err != nil {
return err
}
}

for k, f := range tsm {
if isReservedResourceFieldName(k, f) {
return fmt.Errorf("%s is a reserved field name", k)
Expand Down Expand Up @@ -717,18 +724,30 @@ func isReservedDataSourceFieldName(name string) bool {
return false
}

func isReservedResourceFieldName(name string, s *Schema) bool {
// Allow phasing out "id"
// See https://github.com/terraform-providers/terraform-provider-aws/pull/1626#issuecomment-328881415
if name == "id" && s.Deprecated != "" {
return false
func validateResourceID(s *Schema) error {
if s.Type != TypeString {
return fmt.Errorf(`the "id" attribute must be of TypeString`)
}

if s.Required {
return fmt.Errorf(`the "id" attribute cannot be marked Required`)
}

// ID should at least be computed. If unspecified it will be set to Computed and Optional,
// but Optional is unnecessary if undesired.
if s.Computed != true {
return fmt.Errorf(`the "id" attribute must be marked Computed`)
}
return nil
}

func isReservedResourceFieldName(name string, s *Schema) bool {
for _, reservedName := range ReservedResourceFields {
if name == reservedName {
return true
}
}

return false
}

Expand Down

0 comments on commit 8042c25

Please sign in to comment.