Skip to content

Commit

Permalink
populate global variable for storage domain, to use in validation fun…
Browse files Browse the repository at this point in the history
…ctions
  • Loading branch information
manicminer committed Mar 13, 2024
1 parent a7b8108 commit 3d4058d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/kubernetesconfiguration/2022-11-01/fluxconfiguration"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/containers/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/utils"
Expand Down Expand Up @@ -238,7 +239,7 @@ func (r ArcKubernetesFluxConfigurationResource) Arguments() map[string]*pluginsd
"container_id": {
Type: pluginsdk.TypeString,
Required: 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.StorageContainerDataPlaneID,
},

"account_key": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/kubernetesconfiguration/2022-11-01/fluxconfiguration"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/containers/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/utils"
Expand Down Expand Up @@ -243,7 +244,7 @@ func (r KubernetesFluxConfigurationResource) Arguments() map[string]*pluginsdk.S
"container_id": {
Type: pluginsdk.TypeString,
Required: 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.StorageContainerDataPlaneID,
},

"account_key": {
Expand Down
6 changes: 6 additions & 0 deletions internal/services/storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

// StorageDomainSuffix is used by validation functions
var StorageDomainSuffix *string

type Client struct {
SubscriptionId string

Expand All @@ -43,6 +46,9 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
return nil, fmt.Errorf("determining domain suffix for storage in environment: %s", o.Environment.Name)
}

// Set global variable for post-configure validation
StorageDomainSuffix = storageSuffix

accountsClient := storage.NewAccountsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&accountsClient.Client, o.ResourceManagerAuthorizer)

Expand Down
22 changes: 22 additions & 0 deletions internal/services/storage/validate/storage_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ 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/blob/containers"
)

func StorageContainerName(v interface{}, k string) (warnings []string, errors []error) {
Expand All @@ -26,3 +30,21 @@ func StorageContainerName(v interface{}, k string) (warnings []string, errors []
}
return warnings, errors
}

func StorageContainerDataPlaneID(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 := containers.ParseContainerID(v, *client.StorageDomainSuffix); err != nil {
errors = append(errors, err)
}

return
}

0 comments on commit 3d4058d

Please sign in to comment.