Skip to content
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

Adding validation to azurerm_key_vault's name #270

Merged
merged 2 commits into from
Aug 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 nice

}

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