From 6bf1011df4f4325d477747badd2236dfd3768224 Mon Sep 17 00:00:00 2001 From: Jesse Szwedko Date: Fri, 18 Dec 2015 19:56:58 +0000 Subject: [PATCH] Validate type earlier for aws_directory_service_directory Also DRY it up a little --- ...esource_aws_directory_service_directory.go | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/builtin/providers/aws/resource_aws_directory_service_directory.go b/builtin/providers/aws/resource_aws_directory_service_directory.go index 3eb3d941d..33c31957f 100644 --- a/builtin/providers/aws/resource_aws_directory_service_directory.go +++ b/builtin/providers/aws/resource_aws_directory_service_directory.go @@ -12,6 +12,11 @@ import ( "github.com/hashicorp/terraform/helper/resource" ) +var directoryCreationFuncs = map[string]func(*directoryservice.DirectoryService, *schema.ResourceData) (string, error){ + "SimpleAD": createSimpleDirectoryService, + "MicrosoftAD": createActiveDirectoryService, +} + func resourceAwsDirectoryServiceDirectory() *schema.Resource { return &schema.Resource{ Create: resourceAwsDirectoryServiceDirectoryCreate, @@ -92,6 +97,17 @@ func resourceAwsDirectoryServiceDirectory() *schema.Resource { Optional: true, Default: "SimpleAD", ForceNew: true, + ValidateFunc: func(v interface{}, k string) (ws []string, es []error) { + validTypes := []string{"SimpleAD", "MicrosoftAD"} + value := v.(string) + for validType, _ := range directoryCreationFuncs { + if validType == value { + return + } + } + es = append(es, fmt.Errorf("%q must be one of %q", k, validTypes)) + return + }, }, }, } @@ -184,19 +200,13 @@ func createActiveDirectoryService(dsconn *directoryservice.DirectoryService, d * func resourceAwsDirectoryServiceDirectoryCreate(d *schema.ResourceData, meta interface{}) error { dsconn := meta.(*AWSClient).dsconn - var ( - directoryId string - err error - ) - - switch d.Get("type").(string) { - case "SimpleAD": - directoryId, err = createSimpleDirectoryService(dsconn, d) - case "MicrosoftAD": - directoryId, err = createActiveDirectoryService(dsconn, d) - default: + creationFunc, ok := directoryCreationFuncs[d.Get("type").(string)] + if !ok { + // Shouldn't happen as this is validated above return fmt.Errorf("Unsupported directory type: %s", d.Get("type")) } + + directoryId, err := creationFunc(dsconn, d) if err != nil { return err }