Skip to content

Commit

Permalink
Rebased to master to conform some new changes on test
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcturusZhang committed Dec 31, 2019
1 parent 1dd4980 commit 22a999e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func resourceArmDiskEncryptionSet() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: ValidateDiskEncryptionSetName,
ValidateFunc: validateDiskEncryptionSetName,
},

"location": azure.SchemaLocation(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
)

func TestAccDataSourceAzureRMDiskEncryptionSet_basic(t *testing.T) {
dataSourceName := "data.azurerm_disk_encryption_set.test"
ri := tf.AccRandTimeInt()
rs := acctest.RandString(6)
resourceGroup := fmt.Sprintf("acctestRG-%d", ri)
vaultName := fmt.Sprintf("vault%d", ri)
keyName := fmt.Sprintf("key-%s", rs)
desName := fmt.Sprintf("acctestdes-%d", ri)
location := acceptance.Location()
data := acceptance.BuildTestData(t, "data.azurerm_disk_encryption_set", "test")
resourceGroup := fmt.Sprintf("acctestRG-%d", data.RandomInteger)
vaultName := fmt.Sprintf("vault%d", data.RandomInteger)
keyName := fmt.Sprintf("key-%s", data.RandomString)
desName := fmt.Sprintf("acctestdes-%d", data.RandomInteger)
location := data.Locations.Primary

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Expand All @@ -35,19 +31,18 @@ func TestAccDataSourceAzureRMDiskEncryptionSet_basic(t *testing.T) {
},
// This step is not negligible, without this step, the final step will fail on refresh complaining `Disk Encryption Set does not exist`
{
PreConfig: func() { enableSoftDeleteAndPurgeProtectionForKeyvault(resourceGroup, vaultName) },
Config: testAccAzureRMDiskEncryptionSet_basic(resourceGroup, location, vaultName, keyName, desName),
Config: testAccAzureRMDiskEncryptionSet_basic(resourceGroup, location, vaultName, keyName, desName),
},
{
Config: testAccDataSourceDiskEncryptionSet_basic(resourceGroup, location, vaultName, keyName, desName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "identity.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "identity.0.type", "SystemAssigned"),
resource.TestCheckResourceAttrSet(dataSourceName, "identity.0.principal_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "identity.0.tenant_id"),
resource.TestCheckResourceAttr(dataSourceName, "active_key.#", "1"),
resource.TestCheckResourceAttrSet(dataSourceName, "active_key.0.source_vault_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "active_key.0.key_url"),
resource.TestCheckResourceAttr(data.ResourceName, "identity.#", "1"),
resource.TestCheckResourceAttr(data.ResourceName, "identity.0.type", "SystemAssigned"),
resource.TestCheckResourceAttrSet(data.ResourceName, "identity.0.principal_id"),
resource.TestCheckResourceAttrSet(data.ResourceName, "identity.0.tenant_id"),
resource.TestCheckResourceAttr(data.ResourceName, "active_key.#", "1"),
resource.TestCheckResourceAttrSet(data.ResourceName, "active_key.0.source_vault_id"),
resource.TestCheckResourceAttrSet(data.ResourceName, "active_key.0.key_url"),
),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@ import (
"testing"

"github.com/Azure/azure-sdk-for-go/services/keyvault/mgmt/2018-02-14/keyvault"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func TestAccAzureRMDiskEncryptionSet_basic(t *testing.T) {
resourceName := "azurerm_disk_encryption_set.test"
ri := tf.AccRandTimeInt()
rs := acctest.RandString(6)
resourceGroup := fmt.Sprintf("acctestRG-%d", ri)
vaultName := fmt.Sprintf("vault%d", ri)
keyName := fmt.Sprintf("key-%s", rs)
desName := fmt.Sprintf("acctestdes-%d", ri)
location := acceptance.Location()
data := acceptance.BuildTestData(t, "azurerm_disk_encryption_set", "test")
resourceGroup := fmt.Sprintf("acctestRG-%d", data.RandomInteger)
vaultName := fmt.Sprintf("vault%d", data.RandomInteger)
keyName := fmt.Sprintf("key-%s", data.RandomString)
desName := fmt.Sprintf("acctestdes-%d", data.RandomInteger)
location := data.Locations.Primary

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Expand All @@ -34,22 +30,19 @@ func TestAccAzureRMDiskEncryptionSet_basic(t *testing.T) {
// TODO: After applying soft-delete and purge-protection in keyVault, this extra step can be removed.
{
Config: testAccPrepareKeyvaultAndKey(resourceGroup, location, vaultName, keyName),
Check: resource.ComposeTestCheckFunc(),
},
{
PreConfig: func() { enableSoftDeleteAndPurgeProtectionForKeyvault(resourceGroup, vaultName) },
Config: testAccAzureRMDiskEncryptionSet_basic(resourceGroup, location, vaultName, keyName, desName),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDiskEncryptionSetExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "active_key.0.source_vault_id"),
resource.TestCheckResourceAttrSet(resourceName, "active_key.0.key_url"),
enableSoftDeleteAndPurgeProtectionForKeyvault(resourceGroup, vaultName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
Config: testAccAzureRMDiskEncryptionSet_basic(resourceGroup, location, vaultName, keyName, desName),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDiskEncryptionSetExists(data.ResourceName),
resource.TestCheckResourceAttrSet(data.ResourceName, "active_key.0.source_vault_id"),
resource.TestCheckResourceAttrSet(data.ResourceName, "active_key.0.key_url"),
),
},
data.ImportStep(),
},
})
}
Expand Down
11 changes: 7 additions & 4 deletions azurerm/internal/services/compute/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,19 @@ func validateName(maxLength int) func(i interface{}, k string) (warnings []strin
}
}

func ValidateDiskEncryptionSetName(i interface{}, k string) (warnings []string, errors []error) {
func validateDiskEncryptionSetName(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
return
}

// Supported characters for the name are a-z, A-Z, 0-9 and _. The maximum name length is 80 characters.
// despite the swagger says the constraint above, but a name that contains hyphens will also pass.
if matched := regexp.MustCompile(`^[a-zA-Z0-9_-]{1,80}$`).Match([]byte(v)); !matched {
// Swagger says: Supported characters for the name are a-z, A-Z, 0-9 and _. The maximum name length is 80 characters.
// Confirmed with the service team, they gave me a regex: ^[^_\W][\w-._]{0,79}(?<![-.])$
// This means the name can contain a-z, A-Z, 0-9, underscore, dot or hyphen, and must not starts with a underscore or any other non-word characters (underscore is considered as word character)
// additionally, the name cannot end with hyphen or dot.
// Golang regex does not support "negative look ahead" (aka `?<!`), therefore I transformed this regex to the following regular expression.
if matched := regexp.MustCompile(`^[^_\W][\w-._]{0,78}\w$`).Match([]byte(v)); !matched {
errors = append(errors, fmt.Errorf("%s must be between 1 - 80 characters long, and contains only a-z, A-Z, 0-9 and _", k))
}
return
Expand Down

0 comments on commit 22a999e

Please sign in to comment.