Skip to content

Commit

Permalink
Merge pull request #270 from terraform-providers/key-vault
Browse files Browse the repository at this point in the history
Adding validation to `azurerm_key_vault`'s name
  • Loading branch information
tombuildsstuff authored Aug 24, 2017
2 parents 46e978a + 2f745dc commit 08a20cd
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 13 deletions.
19 changes: 15 additions & 4 deletions azurerm/resource_arm_key_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azurerm
import (
"fmt"
"log"
"regexp"

"github.com/Azure/azure-sdk-for-go/arm/keyvault"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -27,9 +28,10 @@ func resourceArmKeyVault() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateKeyVaultName,
},

"location": locationSchema(),
Expand Down Expand Up @@ -207,7 +209,7 @@ func resourceArmKeyVaultRead(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure KeyVault %s: %s", name, err)
return fmt.Errorf("Error making Read request on Azure KeyVault %s: %+v", name, err)
}

d.Set("name", resp.Name)
Expand Down Expand Up @@ -322,3 +324,12 @@ func flattenKeyVaultAccessPolicies(policies *[]keyvault.AccessPolicyEntry) []int

return result
}

func validateKeyVaultName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if matched := regexp.MustCompile(`^[a-zA-Z0-9-]{3,24}$`).Match([]byte(value)); !matched {
errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters and dashes and must be between 3-24 chars", k))
}

return
}
73 changes: 64 additions & 9 deletions azurerm/resource_arm_key_vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,71 @@ package azurerm

import (
"fmt"
"net/http"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAzureRMKeyVault_name(t *testing.T) {
cases := []struct {
Input string
ExpectError bool
}{
{
Input: "",
ExpectError: true,
},
{
Input: "hi",
ExpectError: true,
},
{
Input: "hello",
ExpectError: false,
},
{
Input: "hello-world",
ExpectError: false,
},
{
Input: "hello-world-21",
ExpectError: false,
},
{
Input: "hello_world_21",
ExpectError: true,
},
{
Input: "Hello-World",
ExpectError: false,
},
{
Input: "20202020",
ExpectError: false,
},
{
Input: "ABC123!@£",
ExpectError: true,
},
{
Input: "abcdefghijklmnopqrstuvwxyz",
ExpectError: true,
},
}

for _, tc := range cases {
_, errors := validateKeyVaultName(tc.Input, "")

hasError := len(errors) > 0

if tc.ExpectError && !hasError {
t.Fatalf("Expected the Key Vault Name to trigger a validation error for '%s'", tc.Input)
}
}
}

func TestAccAzureRMKeyVault_basic(t *testing.T) {
ri := acctest.RandInt()
config := testAccAzureRMKeyVault_basic(ri, testLocation())
Expand Down Expand Up @@ -77,15 +134,13 @@ func testCheckAzureRMKeyVaultDestroy(s *terraform.State) error {

resp, err := client.Get(resourceGroup, name)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
if responseWasNotFound(resp.Response) {
return nil
}
return err
}

if resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("Key Vault still exists:\n%#v", resp.Properties)
}
return fmt.Errorf("Key Vault still exists:\n%#v", resp.Properties)
}

return nil
Expand All @@ -109,11 +164,11 @@ func testCheckAzureRMKeyVaultExists(name string) resource.TestCheckFunc {

resp, err := client.Get(resourceGroup, vaultName)
if err != nil {
return fmt.Errorf("Bad: Get on keyVaultClient: %+v", err)
}
if responseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Vault %q (resource group: %q) does not exist", vaultName, resourceGroup)
}

if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("Bad: Vault %q (resource group: %q) does not exist", vaultName, resourceGroup)
return fmt.Errorf("Bad: Get on keyVaultClient: %+v", err)
}

return nil
Expand Down

0 comments on commit 08a20cd

Please sign in to comment.