Skip to content

Commit

Permalink
azurerm_postgresql_flexible_server_virtual_endpoint - validate with P…
Browse files Browse the repository at this point in the history
…ostgresqlFlexibleServerVirtualEndpointIDInsensitively
  • Loading branch information
neil-yechenwei committed Jan 6, 2025
1 parent 76b7ef2 commit 7af13d3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/postgres/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)
Expand All @@ -40,7 +41,7 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) ResourceType() string {
}

func (r PostgresqlFlexibleServerVirtualEndpointResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return virtualendpoints.ValidateVirtualEndpointID
return validate.PostgresqlFlexibleServerVirtualEndpointIDInsensitively
}

func (r PostgresqlFlexibleServerVirtualEndpointResource) Attributes() map[string]*pluginsdk.Schema {
Expand Down Expand Up @@ -134,7 +135,7 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Read() sdk.ResourceFunc

state := PostgresqlFlexibleServerVirtualEndpointModel{}

id, err := virtualendpoints.ParseVirtualEndpointID(metadata.ResourceData.Id())
id, err := virtualendpoints.ParseVirtualEndpointIDInsensitively(metadata.ResourceData.Id())
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package validate

import (
"fmt"

"github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints"
)

func PostgresqlFlexibleServerVirtualEndpointIDInsensitively(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 _, err := virtualendpoints.ParseVirtualEndpointIDInsensitively(v); err != nil {
errors = append(errors, err)
}

return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package validate

import "testing"

func TestPostgresqlFlexibleServerVirtualEndpointID(t *testing.T) {
cases := []struct {
Input string
Valid bool
}{
{
Input: "",
Valid: false,
},
{
Input: "/",
Valid: false,
},
{
Input: "/subscriptions/",
Valid: false,
},
{
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Valid: false,
},
{
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Valid: false,
},
{
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DBforPostgreSQL/",
Valid: false,
},
{
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DBforPostgreSQL/flexibleServers/",
Valid: false,
},
{
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DBforPostgreSQL/flexibleServers/server1",
Valid: false,
},
{
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DBforPostgreSQL/flexibleServers/server1/virtualEndpoints/endpoint1",
Valid: true,
},
{
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DBforPostgreSQL/flexibleServers/server1/virtualendpoints/endpoint1",
Valid: true,
},
{
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.DBFORPOSTGRESQL/FLEXIBLESERVERS/SERVER1/VIRTUALENDPOINTS/ENDPOINT1",
Valid: true,
},
}
for _, tc := range cases {
t.Logf("[DEBUG] Testing Value %s", tc.Input)
_, errors := PostgresqlFlexibleServerVirtualEndpointIDInsensitively(tc.Input, "test")
valid := len(errors) == 0

if tc.Valid != valid {
t.Fatalf("Expected %t but got %t", tc.Valid, valid)
}
}
}

0 comments on commit 7af13d3

Please sign in to comment.