Skip to content

Commit

Permalink
improve plan-time validation using storage doamin global variable hack
Browse files Browse the repository at this point in the history
  • Loading branch information
manicminer committed Mar 18, 2024
1 parent 7470f9e commit 9c096a5
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/client"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/helpers"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/validate"
storageValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand Down Expand Up @@ -58,7 +59,7 @@ func resourceStorageShareDirectory() *pluginsdk.Resource {
Computed: true, // TODO: remove computed in v4.0
ForceNew: true,
ConflictsWith: []string{"share_name", "storage_account_name"},
ValidateFunc: validation.IsURLWithPath, // note: storage domain suffix cannot be determined at validation time, so just make sure it's a well-formed URL
ValidateFunc: storageValidate.StorageShareDataPlaneID,
},

"share_name": {
Expand Down
2 changes: 1 addition & 1 deletion internal/services/storage/storage_share_file_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func resourceStorageShareFile() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.IsURLWithPath, // note: storage domain suffix cannot be determined at validation time, so just make sure it's a well-formed URL
ValidateFunc: storageValidate.StorageShareDataPlaneID,
},

"path": {
Expand Down
3 changes: 2 additions & 1 deletion internal/services/storage/storage_table_entity_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/client"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/helpers"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/validate"
storageValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand Down Expand Up @@ -49,7 +50,7 @@ func resourceStorageTableEntity() *pluginsdk.Resource {
Optional: true, // TODO: make required and forcenew in v4.0
Computed: true, // TODO: remove computed in v4.0
ConflictsWith: []string{"table_name", "storage_account_name"},
ValidateFunc: validation.IsURLWithPath, // note: storage domain suffix cannot be determined at validation time, so just make sure it's a well-formed URL
ValidateFunc: storageValidate.StorageTableDataPlaneID,
},

"table_name": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,30 @@ package validate
import (
"fmt"
"regexp"

"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/client"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/tombuildsstuff/giovanni/storage/2023-11-03/file/shares"
)

func StorageShareDataPlaneID(input interface{}, key string) (warnings []string, errors []error) {
v, ok := input.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected %q to be a string", key))
return
}

if client.StorageDomainSuffix == nil {
return validation.IsURLWithPath(input, key)
}

if _, err := shares.ParseShareID(v, *client.StorageDomainSuffix); err != nil {
errors = append(errors, err)
}

return
}

func StorageShareName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,30 @@ package validate
import (
"fmt"
"regexp"

"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/client"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/tombuildsstuff/giovanni/storage/2023-11-03/table/tables"
)

func StorageTableDataPlaneID(input interface{}, key string) (warnings []string, errors []error) {
v, ok := input.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected %q to be a string", key))
return
}

if client.StorageDomainSuffix == nil {
return validation.IsURLWithPath(input, key)
}

if _, err := tables.ParseTableID(v, *client.StorageDomainSuffix); err != nil {
errors = append(errors, err)
}

return
}

func StorageTableName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)
if value == "table" {
Expand Down

0 comments on commit 9c096a5

Please sign in to comment.