From 09d93ded49a647a51106b5918609acc12070931c Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Tue, 16 Jul 2024 14:58:06 -0500 Subject: [PATCH 01/50] add virtual endpoint client to postgres client.go --- go.mod | 4 +- go.sum | 8 +- internal/services/postgres/client/client.go | 2 + .../virtualendpoints/README.md | 99 +++++++++++++ .../virtualendpoints/client.go | 26 ++++ .../virtualendpoints/constants.go | 48 ++++++ .../virtualendpoints/id_flexibleserver.go | 130 ++++++++++++++++ .../virtualendpoints/id_virtualendpoint.go | 139 ++++++++++++++++++ .../virtualendpoints/method_create.go | 76 ++++++++++ .../virtualendpoints/method_delete.go | 70 +++++++++ .../virtualendpoints/method_get.go | 54 +++++++ .../virtualendpoints/method_listbyserver.go | 105 +++++++++++++ .../virtualendpoints/method_update.go | 75 ++++++++++ .../model_virtualendpointresource.go | 16 ++ .../model_virtualendpointresourceforpatch.go | 8 + ...model_virtualendpointresourceproperties.go | 10 ++ .../virtualendpoints/predicates.go | 27 ++++ .../virtualendpoints/version.go | 12 ++ .../sdk/client/resourcemanager/poller_lro.go | 32 ++-- .../poller_provisioning_state.go | 10 +- vendor/modules.txt | 5 +- 21 files changed, 929 insertions(+), 27 deletions(-) create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/README.md create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/client.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/constants.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/id_flexibleserver.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/id_virtualendpoint.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_create.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_delete.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_get.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_listbyserver.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_update.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/model_virtualendpointresource.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/model_virtualendpointresourceforpatch.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/model_virtualendpointresourceproperties.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/predicates.go create mode 100644 vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/version.go diff --git a/go.mod b/go.mod index 8d07373d403e..77e11ce9b9fa 100644 --- a/go.mod +++ b/go.mod @@ -17,8 +17,8 @@ require ( github.com/google/go-cmp v0.5.9 github.com/google/uuid v1.4.0 github.com/hashicorp/go-azure-helpers v0.70.0 - github.com/hashicorp/go-azure-sdk/resource-manager v0.20240715.1100358 - github.com/hashicorp/go-azure-sdk/sdk v0.20240715.1100358 + github.com/hashicorp/go-azure-sdk/resource-manager v0.20240715.1103416 + github.com/hashicorp/go-azure-sdk/sdk v0.20240715.1103416 github.com/hashicorp/go-hclog v1.6.3 github.com/hashicorp/go-multierror v1.1.1 github.com/hashicorp/go-uuid v1.0.3 diff --git a/go.sum b/go.sum index ed0ad0d94a49..d09bb568f87e 100644 --- a/go.sum +++ b/go.sum @@ -95,10 +95,10 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-azure-helpers v0.70.0 h1:3wvA/KLT1utHkgiz6hR426/wl+61Eg144/ba5dE39k8= github.com/hashicorp/go-azure-helpers v0.70.0/go.mod h1:BmbF4JDYXK5sEmFeU5hcn8Br21uElcqLfdQxjatwQKw= -github.com/hashicorp/go-azure-sdk/resource-manager v0.20240715.1100358 h1:b7BEgaBZnIx0HvBy1lyAehJjyTzbivcL/vJEWuhZKO4= -github.com/hashicorp/go-azure-sdk/resource-manager v0.20240715.1100358/go.mod h1:MOGHJxcUV68H2eyaykr0kbmqmF6xcZ9QwlKnkjmRSyk= -github.com/hashicorp/go-azure-sdk/sdk v0.20240715.1100358 h1:4qja7tP0biOJHsCeJIQ5CGqI/OWtaSgEfStVt54f3og= -github.com/hashicorp/go-azure-sdk/sdk v0.20240715.1100358/go.mod h1:dMKF6bXrgGmy1d3pLzkmBpG2JIHgSAV2/OMSCEgyMwE= +github.com/hashicorp/go-azure-sdk/resource-manager v0.20240715.1103416 h1:TEAZyGtJoBPIxE0bif+fjLE0SurzU95pOTM0CILUnh0= +github.com/hashicorp/go-azure-sdk/resource-manager v0.20240715.1103416/go.mod h1:8rhCtgnQvMPQEhKxYJjdxAEhodA9sOT+GzVRvzJYYvk= +github.com/hashicorp/go-azure-sdk/sdk v0.20240715.1103416 h1:84S+8LdbZLDAUo1+dOr2lq13/hIzHnPxX/QokzGDKo4= +github.com/hashicorp/go-azure-sdk/sdk v0.20240715.1103416/go.mod h1:dMKF6bXrgGmy1d3pLzkmBpG2JIHgSAV2/OMSCEgyMwE= github.com/hashicorp/go-checkpoint v0.5.0 h1:MFYpPZCnQqQTE18jFwSII6eUQrD/oxMFp3mlgcqk5mU= github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuDrwkBuEQsVcpCOgg= github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= diff --git a/internal/services/postgres/client/client.go b/internal/services/postgres/client/client.go index af2e6813a56c..1035ea0468ea 100644 --- a/internal/services/postgres/client/client.go +++ b/internal/services/postgres/client/client.go @@ -21,6 +21,7 @@ import ( flexibleserverdatabases "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2022-12-01/databases" flexibleserverfirewallrules "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2022-12-01/firewallrules" flexibleservers "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/servers" + flexibleservervirtualendpoints "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints" "github.com/hashicorp/terraform-provider-azurerm/internal/common" ) @@ -40,6 +41,7 @@ type Client struct { VirtualNetworkRulesClient *virtualnetworkrules.VirtualNetworkRulesClient ServerAdministratorsClient *serveradministrators.ServerAdministratorsClient ReplicasClient *replicas.ReplicasClient + VirtualEndpointClient *flexibleservervirtualendpoints.VirtualEndpointsClient } func NewClient(o *common.ClientOptions) (*Client, error) { diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/README.md b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/README.md new file mode 100644 index 000000000000..831488b367cd --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/README.md @@ -0,0 +1,99 @@ + +## `github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints` Documentation + +The `virtualendpoints` SDK allows for interaction with the Azure Resource Manager Service `postgresql` (API Version `2023-06-01-preview`). + +This readme covers example usages, but further information on [using this SDK can be found in the project root](https://github.com/hashicorp/go-azure-sdk/tree/main/docs). + +### Import Path + +```go +import "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints" +``` + + +### Client Initialization + +```go +client := virtualendpoints.NewVirtualEndpointsClientWithBaseURI("https://management.azure.com") +client.Client.Authorizer = authorizer +``` + + +### Example Usage: `VirtualEndpointsClient.Create` + +```go +ctx := context.TODO() +id := virtualendpoints.NewVirtualEndpointID("12345678-1234-9876-4563-123456789012", "example-resource-group", "flexibleServerValue", "virtualEndpointValue") + +payload := virtualendpoints.VirtualEndpointResource{ + // ... +} + + +if err := client.CreateThenPoll(ctx, id, payload); err != nil { + // handle the error +} +``` + + +### Example Usage: `VirtualEndpointsClient.Delete` + +```go +ctx := context.TODO() +id := virtualendpoints.NewVirtualEndpointID("12345678-1234-9876-4563-123456789012", "example-resource-group", "flexibleServerValue", "virtualEndpointValue") + +if err := client.DeleteThenPoll(ctx, id); err != nil { + // handle the error +} +``` + + +### Example Usage: `VirtualEndpointsClient.Get` + +```go +ctx := context.TODO() +id := virtualendpoints.NewVirtualEndpointID("12345678-1234-9876-4563-123456789012", "example-resource-group", "flexibleServerValue", "virtualEndpointValue") + +read, err := client.Get(ctx, id) +if err != nil { + // handle the error +} +if model := read.Model; model != nil { + // do something with the model/response object +} +``` + + +### Example Usage: `VirtualEndpointsClient.ListByServer` + +```go +ctx := context.TODO() +id := virtualendpoints.NewFlexibleServerID("12345678-1234-9876-4563-123456789012", "example-resource-group", "flexibleServerValue") + +// alternatively `client.ListByServer(ctx, id)` can be used to do batched pagination +items, err := client.ListByServerComplete(ctx, id) +if err != nil { + // handle the error +} +for _, item := range items { + // do something +} +``` + + +### Example Usage: `VirtualEndpointsClient.Update` + +```go +ctx := context.TODO() +id := virtualendpoints.NewVirtualEndpointID("12345678-1234-9876-4563-123456789012", "example-resource-group", "flexibleServerValue", "virtualEndpointValue") + +payload := virtualendpoints.VirtualEndpointResourceForPatch{ + // ... +} + + +if err := client.UpdateThenPoll(ctx, id, payload); err != nil { + // handle the error +} +``` diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/client.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/client.go new file mode 100644 index 000000000000..e4f12890b9e5 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/client.go @@ -0,0 +1,26 @@ +package virtualendpoints + +import ( + "fmt" + + "github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager" + sdkEnv "github.com/hashicorp/go-azure-sdk/sdk/environments" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type VirtualEndpointsClient struct { + Client *resourcemanager.Client +} + +func NewVirtualEndpointsClientWithBaseURI(sdkApi sdkEnv.Api) (*VirtualEndpointsClient, error) { + client, err := resourcemanager.NewResourceManagerClient(sdkApi, "virtualendpoints", defaultApiVersion) + if err != nil { + return nil, fmt.Errorf("instantiating VirtualEndpointsClient: %+v", err) + } + + return &VirtualEndpointsClient{ + Client: client, + }, nil +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/constants.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/constants.go new file mode 100644 index 000000000000..751ffa8c4732 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/constants.go @@ -0,0 +1,48 @@ +package virtualendpoints + +import ( + "encoding/json" + "fmt" + "strings" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type VirtualEndpointType string + +const ( + VirtualEndpointTypeReadWrite VirtualEndpointType = "ReadWrite" +) + +func PossibleValuesForVirtualEndpointType() []string { + return []string{ + string(VirtualEndpointTypeReadWrite), + } +} + +func (s *VirtualEndpointType) UnmarshalJSON(bytes []byte) error { + var decoded string + if err := json.Unmarshal(bytes, &decoded); err != nil { + return fmt.Errorf("unmarshaling: %+v", err) + } + out, err := parseVirtualEndpointType(decoded) + if err != nil { + return fmt.Errorf("parsing %q: %+v", decoded, err) + } + *s = *out + return nil +} + +func parseVirtualEndpointType(input string) (*VirtualEndpointType, error) { + vals := map[string]VirtualEndpointType{ + "readwrite": VirtualEndpointTypeReadWrite, + } + if v, ok := vals[strings.ToLower(input)]; ok { + return &v, nil + } + + // otherwise presume it's an undefined value and best-effort it + out := VirtualEndpointType(input) + return &out, nil +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/id_flexibleserver.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/id_flexibleserver.go new file mode 100644 index 000000000000..242239daf770 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/id_flexibleserver.go @@ -0,0 +1,130 @@ +package virtualendpoints + +import ( + "fmt" + "strings" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/recaser" + "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +func init() { + recaser.RegisterResourceId(&FlexibleServerId{}) +} + +var _ resourceids.ResourceId = &FlexibleServerId{} + +// FlexibleServerId is a struct representing the Resource ID for a Flexible Server +type FlexibleServerId struct { + SubscriptionId string + ResourceGroupName string + FlexibleServerName string +} + +// NewFlexibleServerID returns a new FlexibleServerId struct +func NewFlexibleServerID(subscriptionId string, resourceGroupName string, flexibleServerName string) FlexibleServerId { + return FlexibleServerId{ + SubscriptionId: subscriptionId, + ResourceGroupName: resourceGroupName, + FlexibleServerName: flexibleServerName, + } +} + +// ParseFlexibleServerID parses 'input' into a FlexibleServerId +func ParseFlexibleServerID(input string) (*FlexibleServerId, error) { + parser := resourceids.NewParserFromResourceIdType(&FlexibleServerId{}) + parsed, err := parser.Parse(input, false) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + id := FlexibleServerId{} + if err := id.FromParseResult(*parsed); err != nil { + return nil, err + } + + return &id, nil +} + +// ParseFlexibleServerIDInsensitively parses 'input' case-insensitively into a FlexibleServerId +// note: this method should only be used for API response data and not user input +func ParseFlexibleServerIDInsensitively(input string) (*FlexibleServerId, error) { + parser := resourceids.NewParserFromResourceIdType(&FlexibleServerId{}) + parsed, err := parser.Parse(input, true) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + id := FlexibleServerId{} + if err := id.FromParseResult(*parsed); err != nil { + return nil, err + } + + return &id, nil +} + +func (id *FlexibleServerId) FromParseResult(input resourceids.ParseResult) error { + var ok bool + + if id.SubscriptionId, ok = input.Parsed["subscriptionId"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "subscriptionId", input) + } + + if id.ResourceGroupName, ok = input.Parsed["resourceGroupName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "resourceGroupName", input) + } + + if id.FlexibleServerName, ok = input.Parsed["flexibleServerName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "flexibleServerName", input) + } + + return nil +} + +// ValidateFlexibleServerID checks that 'input' can be parsed as a Flexible Server ID +func ValidateFlexibleServerID(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 := ParseFlexibleServerID(v); err != nil { + errors = append(errors, err) + } + + return +} + +// ID returns the formatted Flexible Server ID +func (id FlexibleServerId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DBforPostgreSQL/flexibleServers/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroupName, id.FlexibleServerName) +} + +// Segments returns a slice of Resource ID Segments which comprise this Flexible Server ID +func (id FlexibleServerId) Segments() []resourceids.Segment { + return []resourceids.Segment{ + resourceids.StaticSegment("staticSubscriptions", "subscriptions", "subscriptions"), + resourceids.SubscriptionIdSegment("subscriptionId", "12345678-1234-9876-4563-123456789012"), + resourceids.StaticSegment("staticResourceGroups", "resourceGroups", "resourceGroups"), + resourceids.ResourceGroupSegment("resourceGroupName", "example-resource-group"), + resourceids.StaticSegment("staticProviders", "providers", "providers"), + resourceids.ResourceProviderSegment("staticMicrosoftDBforPostgreSQL", "Microsoft.DBforPostgreSQL", "Microsoft.DBforPostgreSQL"), + resourceids.StaticSegment("staticFlexibleServers", "flexibleServers", "flexibleServers"), + resourceids.UserSpecifiedSegment("flexibleServerName", "flexibleServerValue"), + } +} + +// String returns a human-readable description of this Flexible Server ID +func (id FlexibleServerId) String() string { + components := []string{ + fmt.Sprintf("Subscription: %q", id.SubscriptionId), + fmt.Sprintf("Resource Group Name: %q", id.ResourceGroupName), + fmt.Sprintf("Flexible Server Name: %q", id.FlexibleServerName), + } + return fmt.Sprintf("Flexible Server (%s)", strings.Join(components, "\n")) +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/id_virtualendpoint.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/id_virtualendpoint.go new file mode 100644 index 000000000000..10a8e52f1c3b --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/id_virtualendpoint.go @@ -0,0 +1,139 @@ +package virtualendpoints + +import ( + "fmt" + "strings" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/recaser" + "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +func init() { + recaser.RegisterResourceId(&VirtualEndpointId{}) +} + +var _ resourceids.ResourceId = &VirtualEndpointId{} + +// VirtualEndpointId is a struct representing the Resource ID for a Virtual Endpoint +type VirtualEndpointId struct { + SubscriptionId string + ResourceGroupName string + FlexibleServerName string + VirtualEndpointName string +} + +// NewVirtualEndpointID returns a new VirtualEndpointId struct +func NewVirtualEndpointID(subscriptionId string, resourceGroupName string, flexibleServerName string, virtualEndpointName string) VirtualEndpointId { + return VirtualEndpointId{ + SubscriptionId: subscriptionId, + ResourceGroupName: resourceGroupName, + FlexibleServerName: flexibleServerName, + VirtualEndpointName: virtualEndpointName, + } +} + +// ParseVirtualEndpointID parses 'input' into a VirtualEndpointId +func ParseVirtualEndpointID(input string) (*VirtualEndpointId, error) { + parser := resourceids.NewParserFromResourceIdType(&VirtualEndpointId{}) + parsed, err := parser.Parse(input, false) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + id := VirtualEndpointId{} + if err := id.FromParseResult(*parsed); err != nil { + return nil, err + } + + return &id, nil +} + +// ParseVirtualEndpointIDInsensitively parses 'input' case-insensitively into a VirtualEndpointId +// note: this method should only be used for API response data and not user input +func ParseVirtualEndpointIDInsensitively(input string) (*VirtualEndpointId, error) { + parser := resourceids.NewParserFromResourceIdType(&VirtualEndpointId{}) + parsed, err := parser.Parse(input, true) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + id := VirtualEndpointId{} + if err := id.FromParseResult(*parsed); err != nil { + return nil, err + } + + return &id, nil +} + +func (id *VirtualEndpointId) FromParseResult(input resourceids.ParseResult) error { + var ok bool + + if id.SubscriptionId, ok = input.Parsed["subscriptionId"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "subscriptionId", input) + } + + if id.ResourceGroupName, ok = input.Parsed["resourceGroupName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "resourceGroupName", input) + } + + if id.FlexibleServerName, ok = input.Parsed["flexibleServerName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "flexibleServerName", input) + } + + if id.VirtualEndpointName, ok = input.Parsed["virtualEndpointName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "virtualEndpointName", input) + } + + return nil +} + +// ValidateVirtualEndpointID checks that 'input' can be parsed as a Virtual Endpoint ID +func ValidateVirtualEndpointID(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 := ParseVirtualEndpointID(v); err != nil { + errors = append(errors, err) + } + + return +} + +// ID returns the formatted Virtual Endpoint ID +func (id VirtualEndpointId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DBforPostgreSQL/flexibleServers/%s/virtualEndpoints/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroupName, id.FlexibleServerName, id.VirtualEndpointName) +} + +// Segments returns a slice of Resource ID Segments which comprise this Virtual Endpoint ID +func (id VirtualEndpointId) Segments() []resourceids.Segment { + return []resourceids.Segment{ + resourceids.StaticSegment("staticSubscriptions", "subscriptions", "subscriptions"), + resourceids.SubscriptionIdSegment("subscriptionId", "12345678-1234-9876-4563-123456789012"), + resourceids.StaticSegment("staticResourceGroups", "resourceGroups", "resourceGroups"), + resourceids.ResourceGroupSegment("resourceGroupName", "example-resource-group"), + resourceids.StaticSegment("staticProviders", "providers", "providers"), + resourceids.ResourceProviderSegment("staticMicrosoftDBforPostgreSQL", "Microsoft.DBforPostgreSQL", "Microsoft.DBforPostgreSQL"), + resourceids.StaticSegment("staticFlexibleServers", "flexibleServers", "flexibleServers"), + resourceids.UserSpecifiedSegment("flexibleServerName", "flexibleServerValue"), + resourceids.StaticSegment("staticVirtualEndpoints", "virtualEndpoints", "virtualEndpoints"), + resourceids.UserSpecifiedSegment("virtualEndpointName", "virtualEndpointValue"), + } +} + +// String returns a human-readable description of this Virtual Endpoint ID +func (id VirtualEndpointId) String() string { + components := []string{ + fmt.Sprintf("Subscription: %q", id.SubscriptionId), + fmt.Sprintf("Resource Group Name: %q", id.ResourceGroupName), + fmt.Sprintf("Flexible Server Name: %q", id.FlexibleServerName), + fmt.Sprintf("Virtual Endpoint Name: %q", id.VirtualEndpointName), + } + return fmt.Sprintf("Virtual Endpoint (%s)", strings.Join(components, "\n")) +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_create.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_create.go new file mode 100644 index 000000000000..e1734310b6e1 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_create.go @@ -0,0 +1,76 @@ +package virtualendpoints + +import ( + "context" + "fmt" + "net/http" + + "github.com/hashicorp/go-azure-sdk/sdk/client" + "github.com/hashicorp/go-azure-sdk/sdk/client/pollers" + "github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager" + "github.com/hashicorp/go-azure-sdk/sdk/odata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type CreateOperationResponse struct { + Poller pollers.Poller + HttpResponse *http.Response + OData *odata.OData + Model *VirtualEndpointResource +} + +// Create ... +func (c VirtualEndpointsClient) Create(ctx context.Context, id VirtualEndpointId, input VirtualEndpointResource) (result CreateOperationResponse, err error) { + opts := client.RequestOptions{ + ContentType: "application/json; charset=utf-8", + ExpectedStatusCodes: []int{ + http.StatusAccepted, + http.StatusCreated, + http.StatusOK, + }, + HttpMethod: http.MethodPut, + Path: id.ID(), + } + + req, err := c.Client.NewRequest(ctx, opts) + if err != nil { + return + } + + if err = req.Marshal(input); err != nil { + return + } + + var resp *client.Response + resp, err = req.Execute(ctx) + if resp != nil { + result.OData = resp.OData + result.HttpResponse = resp.Response + } + if err != nil { + return + } + + result.Poller, err = resourcemanager.PollerFromResponse(resp, c.Client) + if err != nil { + return + } + + return +} + +// CreateThenPoll performs Create then polls until it's completed +func (c VirtualEndpointsClient) CreateThenPoll(ctx context.Context, id VirtualEndpointId, input VirtualEndpointResource) error { + result, err := c.Create(ctx, id, input) + if err != nil { + return fmt.Errorf("performing Create: %+v", err) + } + + if err := result.Poller.PollUntilDone(ctx); err != nil { + return fmt.Errorf("polling after Create: %+v", err) + } + + return nil +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_delete.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_delete.go new file mode 100644 index 000000000000..e465e5f8fa02 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_delete.go @@ -0,0 +1,70 @@ +package virtualendpoints + +import ( + "context" + "fmt" + "net/http" + + "github.com/hashicorp/go-azure-sdk/sdk/client" + "github.com/hashicorp/go-azure-sdk/sdk/client/pollers" + "github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager" + "github.com/hashicorp/go-azure-sdk/sdk/odata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type DeleteOperationResponse struct { + Poller pollers.Poller + HttpResponse *http.Response + OData *odata.OData +} + +// Delete ... +func (c VirtualEndpointsClient) Delete(ctx context.Context, id VirtualEndpointId) (result DeleteOperationResponse, err error) { + opts := client.RequestOptions{ + ContentType: "application/json; charset=utf-8", + ExpectedStatusCodes: []int{ + http.StatusAccepted, + http.StatusNoContent, + }, + HttpMethod: http.MethodDelete, + Path: id.ID(), + } + + req, err := c.Client.NewRequest(ctx, opts) + if err != nil { + return + } + + var resp *client.Response + resp, err = req.Execute(ctx) + if resp != nil { + result.OData = resp.OData + result.HttpResponse = resp.Response + } + if err != nil { + return + } + + result.Poller, err = resourcemanager.PollerFromResponse(resp, c.Client) + if err != nil { + return + } + + return +} + +// DeleteThenPoll performs Delete then polls until it's completed +func (c VirtualEndpointsClient) DeleteThenPoll(ctx context.Context, id VirtualEndpointId) error { + result, err := c.Delete(ctx, id) + if err != nil { + return fmt.Errorf("performing Delete: %+v", err) + } + + if err := result.Poller.PollUntilDone(ctx); err != nil { + return fmt.Errorf("polling after Delete: %+v", err) + } + + return nil +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_get.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_get.go new file mode 100644 index 000000000000..3d17967feac2 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_get.go @@ -0,0 +1,54 @@ +package virtualendpoints + +import ( + "context" + "net/http" + + "github.com/hashicorp/go-azure-sdk/sdk/client" + "github.com/hashicorp/go-azure-sdk/sdk/odata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type GetOperationResponse struct { + HttpResponse *http.Response + OData *odata.OData + Model *VirtualEndpointResource +} + +// Get ... +func (c VirtualEndpointsClient) Get(ctx context.Context, id VirtualEndpointId) (result GetOperationResponse, err error) { + opts := client.RequestOptions{ + ContentType: "application/json; charset=utf-8", + ExpectedStatusCodes: []int{ + http.StatusOK, + }, + HttpMethod: http.MethodGet, + Path: id.ID(), + } + + req, err := c.Client.NewRequest(ctx, opts) + if err != nil { + return + } + + var resp *client.Response + resp, err = req.Execute(ctx) + if resp != nil { + result.OData = resp.OData + result.HttpResponse = resp.Response + } + if err != nil { + return + } + + var model VirtualEndpointResource + result.Model = &model + + if err = resp.Unmarshal(result.Model); err != nil { + return + } + + return +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_listbyserver.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_listbyserver.go new file mode 100644 index 000000000000..fc8065183062 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_listbyserver.go @@ -0,0 +1,105 @@ +package virtualendpoints + +import ( + "context" + "fmt" + "net/http" + + "github.com/hashicorp/go-azure-sdk/sdk/client" + "github.com/hashicorp/go-azure-sdk/sdk/odata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type ListByServerOperationResponse struct { + HttpResponse *http.Response + OData *odata.OData + Model *[]VirtualEndpointResource +} + +type ListByServerCompleteResult struct { + LatestHttpResponse *http.Response + Items []VirtualEndpointResource +} + +type ListByServerCustomPager struct { + NextLink *odata.Link `json:"nextLink"` +} + +func (p *ListByServerCustomPager) NextPageLink() *odata.Link { + defer func() { + p.NextLink = nil + }() + + return p.NextLink +} + +// ListByServer ... +func (c VirtualEndpointsClient) ListByServer(ctx context.Context, id FlexibleServerId) (result ListByServerOperationResponse, err error) { + opts := client.RequestOptions{ + ContentType: "application/json; charset=utf-8", + ExpectedStatusCodes: []int{ + http.StatusOK, + }, + HttpMethod: http.MethodGet, + Pager: &ListByServerCustomPager{}, + Path: fmt.Sprintf("%s/virtualEndpoints", id.ID()), + } + + req, err := c.Client.NewRequest(ctx, opts) + if err != nil { + return + } + + var resp *client.Response + resp, err = req.ExecutePaged(ctx) + if resp != nil { + result.OData = resp.OData + result.HttpResponse = resp.Response + } + if err != nil { + return + } + + var values struct { + Values *[]VirtualEndpointResource `json:"value"` + } + if err = resp.Unmarshal(&values); err != nil { + return + } + + result.Model = values.Values + + return +} + +// ListByServerComplete retrieves all the results into a single object +func (c VirtualEndpointsClient) ListByServerComplete(ctx context.Context, id FlexibleServerId) (ListByServerCompleteResult, error) { + return c.ListByServerCompleteMatchingPredicate(ctx, id, VirtualEndpointResourceOperationPredicate{}) +} + +// ListByServerCompleteMatchingPredicate retrieves all the results and then applies the predicate +func (c VirtualEndpointsClient) ListByServerCompleteMatchingPredicate(ctx context.Context, id FlexibleServerId, predicate VirtualEndpointResourceOperationPredicate) (result ListByServerCompleteResult, err error) { + items := make([]VirtualEndpointResource, 0) + + resp, err := c.ListByServer(ctx, id) + if err != nil { + result.LatestHttpResponse = resp.HttpResponse + err = fmt.Errorf("loading results: %+v", err) + return + } + if resp.Model != nil { + for _, v := range *resp.Model { + if predicate.Matches(v) { + items = append(items, v) + } + } + } + + result = ListByServerCompleteResult{ + LatestHttpResponse: resp.HttpResponse, + Items: items, + } + return +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_update.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_update.go new file mode 100644 index 000000000000..3e6a71684d73 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/method_update.go @@ -0,0 +1,75 @@ +package virtualendpoints + +import ( + "context" + "fmt" + "net/http" + + "github.com/hashicorp/go-azure-sdk/sdk/client" + "github.com/hashicorp/go-azure-sdk/sdk/client/pollers" + "github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager" + "github.com/hashicorp/go-azure-sdk/sdk/odata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type UpdateOperationResponse struct { + Poller pollers.Poller + HttpResponse *http.Response + OData *odata.OData + Model *VirtualEndpointResource +} + +// Update ... +func (c VirtualEndpointsClient) Update(ctx context.Context, id VirtualEndpointId, input VirtualEndpointResourceForPatch) (result UpdateOperationResponse, err error) { + opts := client.RequestOptions{ + ContentType: "application/json; charset=utf-8", + ExpectedStatusCodes: []int{ + http.StatusAccepted, + http.StatusOK, + }, + HttpMethod: http.MethodPatch, + Path: id.ID(), + } + + req, err := c.Client.NewRequest(ctx, opts) + if err != nil { + return + } + + if err = req.Marshal(input); err != nil { + return + } + + var resp *client.Response + resp, err = req.Execute(ctx) + if resp != nil { + result.OData = resp.OData + result.HttpResponse = resp.Response + } + if err != nil { + return + } + + result.Poller, err = resourcemanager.PollerFromResponse(resp, c.Client) + if err != nil { + return + } + + return +} + +// UpdateThenPoll performs Update then polls until it's completed +func (c VirtualEndpointsClient) UpdateThenPoll(ctx context.Context, id VirtualEndpointId, input VirtualEndpointResourceForPatch) error { + result, err := c.Update(ctx, id, input) + if err != nil { + return fmt.Errorf("performing Update: %+v", err) + } + + if err := result.Poller.PollUntilDone(ctx); err != nil { + return fmt.Errorf("polling after Update: %+v", err) + } + + return nil +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/model_virtualendpointresource.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/model_virtualendpointresource.go new file mode 100644 index 000000000000..5924aefac2e1 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/model_virtualendpointresource.go @@ -0,0 +1,16 @@ +package virtualendpoints + +import ( + "github.com/hashicorp/go-azure-helpers/resourcemanager/systemdata" +) + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type VirtualEndpointResource struct { + Id *string `json:"id,omitempty"` + Name *string `json:"name,omitempty"` + Properties *VirtualEndpointResourceProperties `json:"properties,omitempty"` + SystemData *systemdata.SystemData `json:"systemData,omitempty"` + Type *string `json:"type,omitempty"` +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/model_virtualendpointresourceforpatch.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/model_virtualendpointresourceforpatch.go new file mode 100644 index 000000000000..141027b0b639 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/model_virtualendpointresourceforpatch.go @@ -0,0 +1,8 @@ +package virtualendpoints + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type VirtualEndpointResourceForPatch struct { + Properties *VirtualEndpointResourceProperties `json:"properties,omitempty"` +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/model_virtualendpointresourceproperties.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/model_virtualendpointresourceproperties.go new file mode 100644 index 000000000000..8384fc61746e --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/model_virtualendpointresourceproperties.go @@ -0,0 +1,10 @@ +package virtualendpoints + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type VirtualEndpointResourceProperties struct { + EndpointType *VirtualEndpointType `json:"endpointType,omitempty"` + Members *[]string `json:"members,omitempty"` + VirtualEndpoints *[]string `json:"virtualEndpoints,omitempty"` +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/predicates.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/predicates.go new file mode 100644 index 000000000000..dd12a082467a --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/predicates.go @@ -0,0 +1,27 @@ +package virtualendpoints + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +type VirtualEndpointResourceOperationPredicate struct { + Id *string + Name *string + Type *string +} + +func (p VirtualEndpointResourceOperationPredicate) Matches(input VirtualEndpointResource) bool { + + if p.Id != nil && (input.Id == nil || *p.Id != *input.Id) { + return false + } + + if p.Name != nil && (input.Name == nil || *p.Name != *input.Name) { + return false + } + + if p.Type != nil && (input.Type == nil || *p.Type != *input.Type) { + return false + } + + return true +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/version.go b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/version.go new file mode 100644 index 000000000000..914c7c521ea6 --- /dev/null +++ b/vendor/github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints/version.go @@ -0,0 +1,12 @@ +package virtualendpoints + +import "fmt" + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. + +const defaultApiVersion = "2023-06-01-preview" + +func userAgent() string { + return fmt.Sprintf("hashicorp/go-azure-sdk/virtualendpoints/%s", defaultApiVersion) +} diff --git a/vendor/github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager/poller_lro.go b/vendor/github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager/poller_lro.go index da08efa21e47..0eade8da42ee 100644 --- a/vendor/github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager/poller_lro.go +++ b/vendor/github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager/poller_lro.go @@ -15,10 +15,8 @@ import ( "strings" "time" - "github.com/hashicorp/go-azure-helpers/lang/response" "github.com/hashicorp/go-azure-sdk/sdk/client" "github.com/hashicorp/go-azure-sdk/sdk/client/pollers" - "github.com/hashicorp/go-azure-sdk/sdk/odata" ) var _ pollers.PollerType = &longRunningOperationPoller{} @@ -79,14 +77,6 @@ func (p *longRunningOperationPoller) Poll(ctx context.Context) (result *pollers. return nil, fmt.Errorf("internal error: cannot poll without a pollingUrl") } - // Retry the polling operation if a 404 was returned - retryOn404 := func(resp *http.Response, _ *odata.OData) (bool, error) { - if resp != nil && response.WasStatusCode(resp, http.StatusNotFound) { - return true, nil - } - return false, nil - } - reqOpts := client.RequestOptions{ ContentType: "application/json; charset=utf-8", ExpectedStatusCodes: []int{ @@ -94,11 +84,14 @@ func (p *longRunningOperationPoller) Poll(ctx context.Context) (result *pollers. http.StatusCreated, http.StatusAccepted, http.StatusNoContent, + + // NOTE: 404 doesn't want to be a retry since we already retry on 404 + http.StatusNotFound, }, HttpMethod: http.MethodGet, OptionsObject: nil, Path: p.pollingUrl.Path, - RetryFunc: client.RequestRetryAny(append(defaultRetryFunctions, retryOn404)...), + RetryFunc: client.RequestRetryAny(defaultRetryFunctions...), } // TODO: port over the `api-version` header @@ -135,6 +128,13 @@ func (p *longRunningOperationPoller) Poll(ctx context.Context) (result *pollers. } } + // We've just created/changed this resource, so it _should_ exist but some APIs + // return a 404 initially - so we should wait for that to complete for non-Delete LROs + if result.HttpResponse.StatusCode == http.StatusNotFound { + result.Status = pollers.PollingStatusInProgress + return + } + // 202's don't necessarily return a body, so there's nothing to deserialize if result.HttpResponse.StatusCode == http.StatusAccepted { result.Status = pollers.PollingStatusInProgress @@ -213,15 +213,17 @@ type operationResult struct { // > cannot parse " 01:58:30 +0000" as "T" StartTime *string `json:"startTime"` - Properties struct { - // Some APIs (such as Storage) return the Resource Representation from the LRO API, as such we need to check provisioningState - ProvisioningState status `json:"provisioningState"` - } `json:"properties"` + Properties operationResultProperties `json:"properties"` // others return Status, so we check that too Status status `json:"status"` } +type operationResultProperties struct { + // Some APIs (such as Storage) return the Resource Representation from the LRO API, as such we need to check provisioningState + ProvisioningState status `json:"provisioningState"` +} + type status string const ( diff --git a/vendor/github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager/poller_provisioning_state.go b/vendor/github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager/poller_provisioning_state.go index 0805d872b613..f5c6f2e81d7a 100644 --- a/vendor/github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager/poller_provisioning_state.go +++ b/vendor/github.com/hashicorp/go-azure-sdk/sdk/client/resourcemanager/poller_provisioning_state.go @@ -143,15 +143,17 @@ func (p *provisioningStatePoller) Poll(ctx context.Context) (*pollers.PollResult } type provisioningStateResult struct { - Properties struct { - // Some API's (such as Storage) return the Resource Representation from the LRO API, as such we need to check provisioningState - ProvisioningState status `json:"provisioningState"` - } `json:"properties"` + Properties provisioningStateResultProperties `json:"properties"` // others return Status, so we check that too Status status `json:"status"` } +type provisioningStateResultProperties struct { + // Some API's (such as Storage) return the Resource Representation from the LRO API, as such we need to check provisioningState + ProvisioningState status `json:"provisioningState"` +} + func resourceManagerResourcePathFromUri(input string) (*string, error) { parsed, err := url.ParseRequestURI(input) if err != nil { diff --git a/vendor/modules.txt b/vendor/modules.txt index a5d4356b04cf..b0cd3d2074c8 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -152,7 +152,7 @@ github.com/hashicorp/go-azure-helpers/resourcemanager/tags github.com/hashicorp/go-azure-helpers/resourcemanager/zones github.com/hashicorp/go-azure-helpers/sender github.com/hashicorp/go-azure-helpers/storage -# github.com/hashicorp/go-azure-sdk/resource-manager v0.20240715.1100358 +# github.com/hashicorp/go-azure-sdk/resource-manager v0.20240715.1103416 ## explicit; go 1.21 github.com/hashicorp/go-azure-sdk/resource-manager/aad/2021-05-01/domainservices github.com/hashicorp/go-azure-sdk/resource-manager/aadb2c/2021-04-01-preview @@ -920,6 +920,7 @@ github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2022-12-01/adminis github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2022-12-01/databases github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2022-12-01/firewallrules github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/servers +github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints github.com/hashicorp/go-azure-sdk/resource-manager/postgresqlhsc/2022-11-08/clusters github.com/hashicorp/go-azure-sdk/resource-manager/postgresqlhsc/2022-11-08/configurations github.com/hashicorp/go-azure-sdk/resource-manager/postgresqlhsc/2022-11-08/firewallrules @@ -1121,7 +1122,7 @@ github.com/hashicorp/go-azure-sdk/resource-manager/workloads/2023-04-01/saplands github.com/hashicorp/go-azure-sdk/resource-manager/workloads/2023-04-01/saprecommendations github.com/hashicorp/go-azure-sdk/resource-manager/workloads/2023-04-01/sapsupportedsku github.com/hashicorp/go-azure-sdk/resource-manager/workloads/2023-04-01/sapvirtualinstances -# github.com/hashicorp/go-azure-sdk/sdk v0.20240715.1100358 +# github.com/hashicorp/go-azure-sdk/sdk v0.20240715.1103416 ## explicit; go 1.21 github.com/hashicorp/go-azure-sdk/sdk/auth github.com/hashicorp/go-azure-sdk/sdk/auth/autorest From 67bf096ea84307f8f1824319f73ac4d8f864948d Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Tue, 16 Jul 2024 14:59:43 -0500 Subject: [PATCH 02/50] expose virtual endpoint client --- internal/services/postgres/client/client.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/services/postgres/client/client.go b/internal/services/postgres/client/client.go index 1035ea0468ea..7de68d1e4572 100644 --- a/internal/services/postgres/client/client.go +++ b/internal/services/postgres/client/client.go @@ -135,6 +135,12 @@ func NewClient(o *common.ClientOptions) (*Client, error) { } o.Configure(flexibleServerAdministratorsClient.Client, o.Authorizers.ResourceManager) + virtualEndpointClient, err := flexibleservervirtualendpoints.NewVirtualEndpointsClientWithBaseURI(o.Environment.ResourceManager) + if err != nil { + return nil, fmt.Errorf("building FlexibleServerVirtualEndpoint client: %+v", err) + } + o.Configure(virtualEndpointClient.Client, o.Authorizers.ResourceManager) + return &Client{ ConfigurationsClient: configurationsClient, DatabasesClient: databasesClient, @@ -151,5 +157,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) { VirtualNetworkRulesClient: virtualNetworkRulesClient, ServerAdministratorsClient: serverAdministratorsClient, ReplicasClient: replicasClient, + VirtualEndpointClient: virtualEndpointClient, }, nil } From 03f55afae9eca2ec0505a3be6f48f9d82ef9cd46 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Tue, 16 Jul 2024 15:08:38 -0500 Subject: [PATCH 03/50] add resource file --- ...exible_server_virtual_endpoint_resource.go | 55 +++++++++++++++++++ internal/services/postgres/registration.go | 1 + 2 files changed, 56 insertions(+) create mode 100644 internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go new file mode 100644 index 000000000000..51df6a78568b --- /dev/null +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -0,0 +1,55 @@ +package postgres + +import ( + "time" + + "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" +) + +var postgresqlFlexibleServerVirtualEndpointResourceName = "azurerm_postgresql_flexible_server_virtual_endpoint" + +func resourcePostgresqlFlexibleServerVirtualEndpoint() *pluginsdk.Resource { + return &pluginsdk.Resource{ + Create: resourcePostgresqlFlexibleServerVirtualEndpointCreate, + Read: resourcePostgresqlFlexibleServerVirtualEndpointRead, + Update: resourcePostgresqlFlexibleServerVirtualEndpointUpdate, + Delete: resourcePostgresqlFlexibleServerVirtualEndpointDelete, + + Timeouts: &pluginsdk.ResourceTimeout{ + Create: pluginsdk.DefaultTimeout(1 * time.Hour), + Read: pluginsdk.DefaultTimeout(5 * time.Minute), + Update: pluginsdk.DefaultTimeout(1 * time.Hour), + Delete: pluginsdk.DefaultTimeout(1 * time.Hour), + }, + + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := virtualendpoints.ParseVirtualEndpointID(id) + return err + }), + + Schema: map[string]*pluginsdk.Schema{ + "name": { + Type: pluginsdk.TypeString, + ForceNew: true, + Required: true, + }, + }, + } +} + +func resourcePostgresqlFlexibleServerVirtualEndpointCreate(d *pluginsdk.ResourceData, meta interface{}) error { + return nil +} + +func resourcePostgresqlFlexibleServerVirtualEndpointRead(d *pluginsdk.ResourceData, meta interface{}) error { + return nil +} + +func resourcePostgresqlFlexibleServerVirtualEndpointUpdate(d *pluginsdk.ResourceData, meta interface{}) error { + return nil +} + +func resourcePostgresqlFlexibleServerVirtualEndpointDelete(d *pluginsdk.ResourceData, meta interface{}) error { + return nil +} diff --git a/internal/services/postgres/registration.go b/internal/services/postgres/registration.go index 1ccf42b25673..20e104f59612 100644 --- a/internal/services/postgres/registration.go +++ b/internal/services/postgres/registration.go @@ -51,5 +51,6 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource { "azurerm_postgresql_flexible_server_configuration": resourcePostgresqlFlexibleServerConfiguration(), "azurerm_postgresql_flexible_server_database": resourcePostgresqlFlexibleServerDatabase(), "azurerm_postgresql_flexible_server_active_directory_administrator": resourcePostgresqlFlexibleServerAdministrator(), + "azurerm_postgresql_flexible_server_virtual_endpoint": resourcePostgresqlFlexibleServerVirtualEndpoint(), } } From 3bbfa3017d388ad0d42003414b8198ac5837d74e Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Wed, 17 Jul 2024 10:59:20 -0500 Subject: [PATCH 04/50] build out methods --- ...exible_server_virtual_endpoint_resource.go | 133 +++++++++++++++++- 1 file changed, 128 insertions(+), 5 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 51df6a78568b..d6ed8e4d0ede 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -1,14 +1,20 @@ package postgres import ( + "fmt" + "log" "time" + "github.com/hashicorp/go-azure-helpers/lang/response" + "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/servers" "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints" + "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/locks" "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" ) -var postgresqlFlexibleServerVirtualEndpointResourceName = "azurerm_postgresql_flexible_server_virtual_endpoint" - func resourcePostgresqlFlexibleServerVirtualEndpoint() *pluginsdk.Resource { return &pluginsdk.Resource{ Create: resourcePostgresqlFlexibleServerVirtualEndpointCreate, @@ -30,19 +36,120 @@ func resourcePostgresqlFlexibleServerVirtualEndpoint() *pluginsdk.Resource { Schema: map[string]*pluginsdk.Schema{ "name": { - Type: pluginsdk.TypeString, - ForceNew: true, - Required: true, + Type: pluginsdk.TypeString, + Description: "The name of the Virtual Endpoint", + ForceNew: true, + Required: true, + ValidateFunc: virtualendpoints.ValidateVirtualEndpointID, + }, + "source_server_id": { + Type: pluginsdk.TypeString, + Description: "The Resource ID of the *Source* Postgres Flexible Server this should be associated with", + ForceNew: true, + Required: true, + ValidateFunc: servers.ValidateFlexibleServerID, + }, + "replica_server_id": { + Type: pluginsdk.TypeString, + Description: "The Resource ID of the *Source* Postgres Flexible Server this should be associated with", + ForceNew: true, + Required: true, + ValidateFunc: servers.ValidateFlexibleServerID, + }, + "type": { + Type: pluginsdk.TypeString, + Description: "The type of Virtual Endpoint", + ForceNew: true, + Required: true, + ValidateFunc: validation.StringInSlice(virtualendpoints.PossibleValuesForVirtualEndpointType(), true), }, }, } } func resourcePostgresqlFlexibleServerVirtualEndpointCreate(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).Postgres.VirtualEndpointClient + ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) + defer cancel() + + name := d.Get("name").(string) + flexibleServer := d.Get("source_server_id").(string) + replicaServer := d.Get("replica_server_id").(string) + virtualEndpointType := d.Get("type").(string) + + sourceServerId, err := servers.ParseFlexibleServerID(flexibleServer) + if err != nil { + return err + } + + replicaServerId, err := servers.ParseFlexibleServerID(replicaServer) + if err != nil { + return err + } + + id := virtualendpoints.NewVirtualEndpointID(sourceServerId.SubscriptionId, sourceServerId.ResourceGroupName, sourceServerId.FlexibleServerName, name) + + locks.ByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + + if err = client.CreateThenPoll(ctx, id, virtualendpoints.VirtualEndpointResource{ + Name: &name, + Properties: &virtualendpoints.VirtualEndpointResourceProperties{ + EndpointType: (*virtualendpoints.VirtualEndpointType)(&virtualEndpointType), + Members: &[]string{replicaServerId.FlexibleServerName}, // TODO: Can we pass multiple at once? + }, + }); err != nil { + return fmt.Errorf("creating %s: %+v", id, err) + } + + d.SetId(id.ID()) + return nil } func resourcePostgresqlFlexibleServerVirtualEndpointRead(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).Postgres.VirtualEndpointClient + ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := virtualendpoints.ParseVirtualEndpointID(d.Id()) + if err != nil { + return err + } + + resp, err := client.Get(ctx, *id) + if err != nil { + if response.WasNotFound(resp.HttpResponse) { + log.Printf("[INFO] Postgresql Flexible Server Endpoint %q does not exist - removing from state", d.Id()) + d.SetId("") + return nil + } + return fmt.Errorf("retrieving %s: %+v", id, err) + } + + if err := d.Set("name", id.FlexibleServerName); err != nil { + return fmt.Errorf("setting `name`: %+v", err) + } + + flexibleServerId, err := servers.ParseFlexibleServerID(d.Id()) + if err != nil { + return err + } + + if err := d.Set("source_server_id", flexibleServerId.ID()); err != nil { + return fmt.Errorf("setting `source_server_id`: %+v", err) + } + + if model := resp.Model; model != nil { + if err := d.Set("replica_server_id", (*resp.Model.Properties.Members)[0]); err != nil { + return fmt.Errorf("setting `replica_server_id`: %+v", err) + } //TODO: This should be more resiliant + + if err := d.Set("type", resp.Model.Type); err != nil { + return fmt.Errorf("setting `type`: %+v", err) + } + } + return nil } @@ -51,5 +158,21 @@ func resourcePostgresqlFlexibleServerVirtualEndpointUpdate(d *pluginsdk.Resource } func resourcePostgresqlFlexibleServerVirtualEndpointDelete(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).Postgres.VirtualEndpointClient + ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := virtualendpoints.ParseVirtualEndpointID(d.Id()) + if err != nil { + return err + } + + locks.ByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + + if err := client.DeleteThenPoll(ctx, *id); err != nil { + return fmt.Errorf("deleting %q: %+v", id, err) + } + return nil } From 30dcc27186d4b8f158355ec63b3b6ac83e78d546 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Wed, 17 Jul 2024 13:26:45 -0500 Subject: [PATCH 05/50] complete initial pass --- ...exible_server_virtual_endpoint_resource.go | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index d6ed8e4d0ede..b4dcd07a35dc 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -50,9 +50,9 @@ func resourcePostgresqlFlexibleServerVirtualEndpoint() *pluginsdk.Resource { ValidateFunc: servers.ValidateFlexibleServerID, }, "replica_server_id": { - Type: pluginsdk.TypeString, - Description: "The Resource ID of the *Source* Postgres Flexible Server this should be associated with", - ForceNew: true, + Type: pluginsdk.TypeString, + Description: "The Resource ID of the *Source* Postgres Flexible Server this should be associated with", + // ForceNew: true, Required: true, ValidateFunc: servers.ValidateFlexibleServerID, }, @@ -154,6 +154,35 @@ func resourcePostgresqlFlexibleServerVirtualEndpointRead(d *pluginsdk.ResourceDa } func resourcePostgresqlFlexibleServerVirtualEndpointUpdate(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).Postgres.VirtualEndpointClient + ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) + defer cancel() + + id, err := virtualendpoints.ParseVirtualEndpointID(d.Id()) + if err != nil { + return err + } + + replicaServer := d.Get("replica_server_id").(string) + virtualEndpointType := d.Get("type").(string) + + replicaServerId, err := servers.ParseFlexibleServerID(replicaServer) + if err != nil { + return err + } + + locks.ByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + + if err := client.UpdateThenPoll(ctx, *id, virtualendpoints.VirtualEndpointResourceForPatch{ + Properties: &virtualendpoints.VirtualEndpointResourceProperties{ + EndpointType: (*virtualendpoints.VirtualEndpointType)(&virtualEndpointType), + Members: &[]string{replicaServerId.FlexibleServerName}, // TODO: Can we pass multiple at once? + }, + }); err != nil { + return fmt.Errorf("updating %q: %+v", id, err) + } + return nil } From bb925606e811edd7f656cc29583212468a5c8884 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Wed, 17 Jul 2024 23:18:46 -0500 Subject: [PATCH 06/50] all behaviors implemented --- ...exible_server_virtual_endpoint_resource.go | 54 +++++++++---------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index b4dcd07a35dc..7be4dcc5fbc4 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -23,10 +23,10 @@ func resourcePostgresqlFlexibleServerVirtualEndpoint() *pluginsdk.Resource { Delete: resourcePostgresqlFlexibleServerVirtualEndpointDelete, Timeouts: &pluginsdk.ResourceTimeout{ - Create: pluginsdk.DefaultTimeout(1 * time.Hour), - Read: pluginsdk.DefaultTimeout(5 * time.Minute), - Update: pluginsdk.DefaultTimeout(1 * time.Hour), - Delete: pluginsdk.DefaultTimeout(1 * time.Hour), + Create: pluginsdk.DefaultTimeout(20 * time.Minute), + Read: pluginsdk.DefaultTimeout(20 * time.Minute), + Update: pluginsdk.DefaultTimeout(20 * time.Minute), + Delete: pluginsdk.DefaultTimeout(20 * time.Minute), }, Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { @@ -36,11 +36,10 @@ func resourcePostgresqlFlexibleServerVirtualEndpoint() *pluginsdk.Resource { Schema: map[string]*pluginsdk.Schema{ "name": { - Type: pluginsdk.TypeString, - Description: "The name of the Virtual Endpoint", - ForceNew: true, - Required: true, - ValidateFunc: virtualendpoints.ValidateVirtualEndpointID, + Type: pluginsdk.TypeString, + Description: "The name of the Virtual Endpoint", + ForceNew: true, + Required: true, }, "source_server_id": { Type: pluginsdk.TypeString, @@ -73,11 +72,11 @@ func resourcePostgresqlFlexibleServerVirtualEndpointCreate(d *pluginsdk.Resource defer cancel() name := d.Get("name").(string) - flexibleServer := d.Get("source_server_id").(string) + sourceServer := d.Get("source_server_id").(string) replicaServer := d.Get("replica_server_id").(string) virtualEndpointType := d.Get("type").(string) - sourceServerId, err := servers.ParseFlexibleServerID(flexibleServer) + sourceServerId, err := servers.ParseFlexibleServerID(sourceServer) if err != nil { return err } @@ -117,6 +116,15 @@ func resourcePostgresqlFlexibleServerVirtualEndpointRead(d *pluginsdk.ResourceDa return err } + if err := d.Set("name", id.VirtualEndpointName); err != nil { + return fmt.Errorf("setting `name`: %+v", err) + } + + flexibleServerId := servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, id.FlexibleServerName) + if err := d.Set("source_server_id", flexibleServerId.ID()); err != nil { + return fmt.Errorf("setting `source_server_id`: %+v", err) + } + resp, err := client.Get(ctx, *id) if err != nil { if response.WasNotFound(resp.HttpResponse) { @@ -127,26 +135,12 @@ func resourcePostgresqlFlexibleServerVirtualEndpointRead(d *pluginsdk.ResourceDa return fmt.Errorf("retrieving %s: %+v", id, err) } - if err := d.Set("name", id.FlexibleServerName); err != nil { - return fmt.Errorf("setting `name`: %+v", err) - } - - flexibleServerId, err := servers.ParseFlexibleServerID(d.Id()) - if err != nil { - return err - } - - if err := d.Set("source_server_id", flexibleServerId.ID()); err != nil { - return fmt.Errorf("setting `source_server_id`: %+v", err) - } - if model := resp.Model; model != nil { - if err := d.Set("replica_server_id", (*resp.Model.Properties.Members)[0]); err != nil { - return fmt.Errorf("setting `replica_server_id`: %+v", err) - } //TODO: This should be more resiliant - - if err := d.Set("type", resp.Model.Type); err != nil { - return fmt.Errorf("setting `type`: %+v", err) + if len(*resp.Model.Properties.Members) > 0 { + replicateServerId := servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[0]) + if err := d.Set("replica_server_id", replicateServerId.ID()); err != nil { + return fmt.Errorf("setting `replica_server_id`: %+v", err) + } //TODO: This should be more resiliant } } From 7c19ec83fb23cc15864152166d0c50e0879630ea Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 18 Jul 2024 10:27:59 -0500 Subject: [PATCH 07/50] fix index error --- ...sql_flexible_server_virtual_endpoint_resource.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 7be4dcc5fbc4..9ac663667442 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -26,7 +26,7 @@ func resourcePostgresqlFlexibleServerVirtualEndpoint() *pluginsdk.Resource { Create: pluginsdk.DefaultTimeout(20 * time.Minute), Read: pluginsdk.DefaultTimeout(20 * time.Minute), Update: pluginsdk.DefaultTimeout(20 * time.Minute), - Delete: pluginsdk.DefaultTimeout(20 * time.Minute), + Delete: pluginsdk.DefaultTimeout(1 * time.Hour), }, Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { @@ -49,9 +49,8 @@ func resourcePostgresqlFlexibleServerVirtualEndpoint() *pluginsdk.Resource { ValidateFunc: servers.ValidateFlexibleServerID, }, "replica_server_id": { - Type: pluginsdk.TypeString, - Description: "The Resource ID of the *Source* Postgres Flexible Server this should be associated with", - // ForceNew: true, + Type: pluginsdk.TypeString, + Description: "The Resource ID of the *Source* Postgres Flexible Server this should be associated with", Required: true, ValidateFunc: servers.ValidateFlexibleServerID, }, @@ -95,7 +94,7 @@ func resourcePostgresqlFlexibleServerVirtualEndpointCreate(d *pluginsdk.Resource Name: &name, Properties: &virtualendpoints.VirtualEndpointResourceProperties{ EndpointType: (*virtualendpoints.VirtualEndpointType)(&virtualEndpointType), - Members: &[]string{replicaServerId.FlexibleServerName}, // TODO: Can we pass multiple at once? + Members: &[]string{replicaServerId.FlexibleServerName}, }, }); err != nil { return fmt.Errorf("creating %s: %+v", id, err) @@ -136,11 +135,11 @@ func resourcePostgresqlFlexibleServerVirtualEndpointRead(d *pluginsdk.ResourceDa } if model := resp.Model; model != nil { - if len(*resp.Model.Properties.Members) > 0 { + if model.Properties != nil && model.Properties.Members != nil && len(*resp.Model.Properties.Members) > 0 { replicateServerId := servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[0]) if err := d.Set("replica_server_id", replicateServerId.ID()); err != nil { return fmt.Errorf("setting `replica_server_id`: %+v", err) - } //TODO: This should be more resiliant + } } } From 3af434c78514e1962e50524dde3a319947a50504 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 18 Jul 2024 11:35:32 -0500 Subject: [PATCH 08/50] workaround api limitation --- ...server_virtual_endpoint_resource_poller.go | 59 +++++++++++++++++++ ...exible_server_virtual_endpoint_resource.go | 37 +++++++++--- 2 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go diff --git a/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go b/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go new file mode 100644 index 000000000000..f72c9b0e4c3f --- /dev/null +++ b/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go @@ -0,0 +1,59 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package custompollers + +import ( + "context" + "fmt" + "time" + + "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints" + "github.com/hashicorp/go-azure-sdk/sdk/client" + "github.com/hashicorp/go-azure-sdk/sdk/client/pollers" +) + +var _ pollers.PollerType = &postgresFlexibleServerVirtualEndpointPoller{} + +type postgresFlexibleServerVirtualEndpointPoller struct { + client *virtualendpoints.VirtualEndpointsClient + id virtualendpoints.VirtualEndpointId +} + +// Workaround due to Azure performing a psuedo-soft delete on virtual endpoints. +// +// - The `DELETE` endpoint does not fully delete the endpont, it sets `properties` to an empty object +// +// - Subsequent `GET` operations for the endpoint will always return 200 with empty metadata, so Terraform will hang on `DELETE` +// +// - The only way to currently check for deletion is to check the `properties` property +func NewPostgresFlexibleServerVirtualEndpointDeletePoller(client *virtualendpoints.VirtualEndpointsClient, id virtualendpoints.VirtualEndpointId) *postgresFlexibleServerVirtualEndpointPoller { + return &postgresFlexibleServerVirtualEndpointPoller{ + client: client, + id: id, + } +} + +func (p postgresFlexibleServerVirtualEndpointPoller) Poll(ctx context.Context) (*pollers.PollResult, error) { + resp, err := p.client.Get(ctx, p.id) + if err != nil { + return nil, fmt.Errorf("retrieving %s: %+v", p.id, err) + } + + if model := resp.Model; model != nil && model.Properties == nil { + return &pollers.PollResult{ + HttpResponse: &client.Response{ + Response: resp.HttpResponse, + }, + PollInterval: 5 * time.Second, + Status: pollers.PollingStatusSucceeded, + }, nil + } + + return nil, pollers.PollingFailedError{ + HttpResponse: &client.Response{ + Response: resp.HttpResponse, + }, + Message: fmt.Sprintf("failed to delete flexible server virtual endpoint"), + } +} diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 9ac663667442..5db94d78e140 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" "github.com/hashicorp/terraform-provider-azurerm/internal/locks" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/postgres/custompollers" "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" @@ -26,7 +27,7 @@ func resourcePostgresqlFlexibleServerVirtualEndpoint() *pluginsdk.Resource { Create: pluginsdk.DefaultTimeout(20 * time.Minute), Read: pluginsdk.DefaultTimeout(20 * time.Minute), Update: pluginsdk.DefaultTimeout(20 * time.Minute), - Delete: pluginsdk.DefaultTimeout(1 * time.Hour), + Delete: pluginsdk.DefaultTimeout(20 * time.Minute), }, Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { @@ -90,6 +91,28 @@ func resourcePostgresqlFlexibleServerVirtualEndpointCreate(d *pluginsdk.Resource locks.ByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + // Azure doesn't completely delete Virtual Endpoints, so recreating one with the same name will fail + // This just updates the existing one if it has a conflict AND the existing entity is set to nil + // GH Issue: https://github.com/Azure/azure-rest-api-specs/issues/29898 + // A custom Delete poller is also used to work around this issue. + if resp, err := client.Get(ctx, id); err != nil { + if response.WasConflict(resp.HttpResponse) { + if resp.Model != nil && resp.Model.Properties == nil { + if err := client.UpdateThenPoll(ctx, id, virtualendpoints.VirtualEndpointResourceForPatch{ + Properties: &virtualendpoints.VirtualEndpointResourceProperties{ + EndpointType: (*virtualendpoints.VirtualEndpointType)(&virtualEndpointType), + Members: &[]string{replicaServerId.FlexibleServerName}, + }, + }); err != nil { + return fmt.Errorf("updating previously deleted virtual endpoint %q: %+v", id, err) + } + + d.SetId(id.ID()) + return nil + } + } + } + if err = client.CreateThenPoll(ctx, id, virtualendpoints.VirtualEndpointResource{ Name: &name, Properties: &virtualendpoints.VirtualEndpointResourceProperties{ @@ -135,8 +158,9 @@ func resourcePostgresqlFlexibleServerVirtualEndpointRead(d *pluginsdk.ResourceDa } if model := resp.Model; model != nil { - if model.Properties != nil && model.Properties.Members != nil && len(*resp.Model.Properties.Members) > 0 { - replicateServerId := servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[0]) + // Model.Properties.Members is a tuple => [source_server, replication_server] + if model.Properties != nil && model.Properties.Members != nil && len(*resp.Model.Properties.Members) == 2 { + replicateServerId := servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[1]) if err := d.Set("replica_server_id", replicateServerId.ID()); err != nil { return fmt.Errorf("setting `replica_server_id`: %+v", err) } @@ -170,7 +194,7 @@ func resourcePostgresqlFlexibleServerVirtualEndpointUpdate(d *pluginsdk.Resource if err := client.UpdateThenPoll(ctx, *id, virtualendpoints.VirtualEndpointResourceForPatch{ Properties: &virtualendpoints.VirtualEndpointResourceProperties{ EndpointType: (*virtualendpoints.VirtualEndpointType)(&virtualEndpointType), - Members: &[]string{replicaServerId.FlexibleServerName}, // TODO: Can we pass multiple at once? + Members: &[]string{replicaServerId.FlexibleServerName}, }, }); err != nil { return fmt.Errorf("updating %q: %+v", id, err) @@ -192,9 +216,8 @@ func resourcePostgresqlFlexibleServerVirtualEndpointDelete(d *pluginsdk.Resource locks.ByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) - if err := client.DeleteThenPoll(ctx, *id); err != nil { - return fmt.Errorf("deleting %q: %+v", id, err) - } + deletePoller := custompollers.NewPostgresFlexibleServerVirtualEndpointDeletePoller(client, *id) + deletePoller.Poll(ctx) return nil } From b55d0aeaf83a3f2ba9ef5c0f5598520dc7c50337 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 18 Jul 2024 13:49:43 -0500 Subject: [PATCH 09/50] refine behavior --- ...server_virtual_endpoint_resource_poller.go | 20 +++++++++++++++---- ...exible_server_virtual_endpoint_resource.go | 15 +++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go b/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go index f72c9b0e4c3f..077d5975f58f 100644 --- a/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go +++ b/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go @@ -22,11 +22,11 @@ type postgresFlexibleServerVirtualEndpointPoller struct { // Workaround due to Azure performing a psuedo-soft delete on virtual endpoints. // -// - The `DELETE` endpoint does not fully delete the endpont, it sets `properties` to an empty object +// - The `DELETE` endpoint does not fully delete the resource, it sets `properties.members` to nil // // - Subsequent `GET` operations for the endpoint will always return 200 with empty metadata, so Terraform will hang on `DELETE` // -// - The only way to currently check for deletion is to check the `properties` property +// - The only way to currently check for deletion is to check the `properties.members` property func NewPostgresFlexibleServerVirtualEndpointDeletePoller(client *virtualendpoints.VirtualEndpointsClient, id virtualendpoints.VirtualEndpointId) *postgresFlexibleServerVirtualEndpointPoller { return &postgresFlexibleServerVirtualEndpointPoller{ client: client, @@ -40,7 +40,19 @@ func (p postgresFlexibleServerVirtualEndpointPoller) Poll(ctx context.Context) ( return nil, fmt.Errorf("retrieving %s: %+v", p.id, err) } - if model := resp.Model; model != nil && model.Properties == nil { + model := resp.Model + + if model != nil && model.Properties != nil { + if model.Properties.Members != nil { + return &pollers.PollResult{ + HttpResponse: &client.Response{ + Response: resp.HttpResponse, + }, + PollInterval: 5 * time.Second, + Status: pollers.PollingStatusInProgress, + }, nil + } + return &pollers.PollResult{ HttpResponse: &client.Response{ Response: resp.HttpResponse, @@ -54,6 +66,6 @@ func (p postgresFlexibleServerVirtualEndpointPoller) Poll(ctx context.Context) ( HttpResponse: &client.Response{ Response: resp.HttpResponse, }, - Message: fmt.Sprintf("failed to delete flexible server virtual endpoint"), + Message: fmt.Sprintln("failed to delete flexible server virtual endpoint"), } } diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 5db94d78e140..3b4b4a42825d 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/go-azure-helpers/lang/response" "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/servers" "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints" + "github.com/hashicorp/go-azure-sdk/sdk/client/pollers" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" "github.com/hashicorp/terraform-provider-azurerm/internal/locks" "github.com/hashicorp/terraform-provider-azurerm/internal/services/postgres/custompollers" @@ -92,12 +93,12 @@ func resourcePostgresqlFlexibleServerVirtualEndpointCreate(d *pluginsdk.Resource defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) // Azure doesn't completely delete Virtual Endpoints, so recreating one with the same name will fail - // This just updates the existing one if it has a conflict AND the existing entity is set to nil + // This just updates the existing one if it has a conflict AND the existing members are set to nil // GH Issue: https://github.com/Azure/azure-rest-api-specs/issues/29898 // A custom Delete poller is also used to work around this issue. if resp, err := client.Get(ctx, id); err != nil { if response.WasConflict(resp.HttpResponse) { - if resp.Model != nil && resp.Model.Properties == nil { + if resp.Model != nil && resp.Model.Properties != nil && resp.Model.Properties.Members == nil { if err := client.UpdateThenPoll(ctx, id, virtualendpoints.VirtualEndpointResourceForPatch{ Properties: &virtualendpoints.VirtualEndpointResourceProperties{ EndpointType: (*virtualendpoints.VirtualEndpointType)(&virtualEndpointType), @@ -217,7 +218,15 @@ func resourcePostgresqlFlexibleServerVirtualEndpointDelete(d *pluginsdk.Resource defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) deletePoller := custompollers.NewPostgresFlexibleServerVirtualEndpointDeletePoller(client, *id) - deletePoller.Poll(ctx) + poller := pollers.NewPoller(deletePoller, 20*time.Second, pollers.DefaultNumberOfDroppedConnectionsToAllow) + + if _, err := client.Delete(ctx, *id); err != nil { + return fmt.Errorf("deleting %s: %+v", *id, err) + } + + if err := poller.PollUntilDone(ctx); err != nil { + return err + } return nil } From 2dbd15ba48e41686fc5b526b0d38091bec5d3d0b Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 18 Jul 2024 15:38:30 -0500 Subject: [PATCH 10/50] remove unnecessary update or create logic --- ...exible_server_virtual_endpoint_resource.go | 26 +++---------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 3b4b4a42825d..f2c113ac3249 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -92,28 +92,8 @@ func resourcePostgresqlFlexibleServerVirtualEndpointCreate(d *pluginsdk.Resource locks.ByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) - // Azure doesn't completely delete Virtual Endpoints, so recreating one with the same name will fail - // This just updates the existing one if it has a conflict AND the existing members are set to nil - // GH Issue: https://github.com/Azure/azure-rest-api-specs/issues/29898 - // A custom Delete poller is also used to work around this issue. - if resp, err := client.Get(ctx, id); err != nil { - if response.WasConflict(resp.HttpResponse) { - if resp.Model != nil && resp.Model.Properties != nil && resp.Model.Properties.Members == nil { - if err := client.UpdateThenPoll(ctx, id, virtualendpoints.VirtualEndpointResourceForPatch{ - Properties: &virtualendpoints.VirtualEndpointResourceProperties{ - EndpointType: (*virtualendpoints.VirtualEndpointType)(&virtualEndpointType), - Members: &[]string{replicaServerId.FlexibleServerName}, - }, - }); err != nil { - return fmt.Errorf("updating previously deleted virtual endpoint %q: %+v", id, err) - } - - d.SetId(id.ID()) - return nil - } - } - } - + // This API can be a bit flaky if the same named resource is created/destroyed quickly + // usually waiting a minute or two before redeploying is enough to resolve the conflict if err = client.CreateThenPoll(ctx, id, virtualendpoints.VirtualEndpointResource{ Name: &name, Properties: &virtualendpoints.VirtualEndpointResourceProperties{ @@ -218,7 +198,7 @@ func resourcePostgresqlFlexibleServerVirtualEndpointDelete(d *pluginsdk.Resource defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) deletePoller := custompollers.NewPostgresFlexibleServerVirtualEndpointDeletePoller(client, *id) - poller := pollers.NewPoller(deletePoller, 20*time.Second, pollers.DefaultNumberOfDroppedConnectionsToAllow) + poller := pollers.NewPoller(deletePoller, 5*time.Second, pollers.DefaultNumberOfDroppedConnectionsToAllow) if _, err := client.Delete(ctx, *id); err != nil { return fmt.Errorf("deleting %s: %+v", *id, err) From a2a94363075ba301b2eceb7fff0f690f45f5f2fe Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 18 Jul 2024 15:59:34 -0500 Subject: [PATCH 11/50] add test file --- ...e_server_virtual_endpoint_resource_test.go | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go new file mode 100644 index 000000000000..aa9aa276fa3f --- /dev/null +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -0,0 +1,142 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package postgres_test + +import ( + "context" + "fmt" + "testing" + + "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" + "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" + "github.com/hashicorp/terraform-provider-azurerm/utils" +) + +type PostgresqlFlexibleServerVirtualEndpointResource struct{} + +func TestAccPostgresqlFlexibleServerVirtualEndpoint_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_active_directory_administrator", "test") + r := PostgresqlFlexibleServerVirtualEndpointResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +// func TestAccPostgresqlFlexibleServerVirtualEndpoint_requiresImport(t *testing.T) { +// data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_active_directory_administrator", "test") +// r := PostgresqlFlexibleServerVirtualEndpointResource{} + +// data.ResourceTest(t, r, []acceptance.TestStep{ +// { +// Config: r.basic(data), +// Check: acceptance.ComposeTestCheckFunc( +// check.That(data.ResourceName).ExistsInAzure(r), +// ), +// }, +// { +// Config: r.requiresImport(data), +// ExpectError: acceptance.RequiresImportError("azurerm_postgresql_flexible_server_active_directory_administrator"), +// }, +// }) +// } + +// func TestAccPostgresqlFlexibleServerVirtualEndpoint_disappears(t *testing.T) { +// data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_active_directory_administrator", "test") +// r := PostgresqlFlexibleServerVirtualEndpointResource{} + +// data.ResourceTest(t, r, []acceptance.TestStep{ +// data.DisappearsStep(acceptance.DisappearsStepData{ +// Config: r.basic, +// TestResource: r, +// }), +// }) +// } + +func (r PostgresqlFlexibleServerVirtualEndpointResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { + id, err := virtualendpoints.ParseVirtualEndpointID(state.ID) + if err != nil { + return nil, err + } + + resp, err := clients.Postgres.VirtualEndpointClient.Get(ctx, *id) + if err != nil { + return nil, fmt.Errorf("reading Postgresql Virtual Endpoint (%s): %+v", id.String(), err) + } + + return utils.Bool(resp.Model != nil), nil +} + +// func (r PostgresqlFlexibleServerVirtualEndpointResource) Destroy(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { +// id, err := administrators.ParseAdministratorID(state.ID) +// if err != nil { +// return nil, err +// } + +// if _, err := client.Postgres.FlexibleServerAdministratorsClient.Delete(ctx, *id); err != nil { +// return nil, fmt.Errorf("deleting Postgresql AAD Administrator (%s): %+v", id.String(), err) +// } + +// return utils.Bool(true), nil +// } + +func (PostgresqlFlexibleServerVirtualEndpointResource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-psql-virtualendpoint-%[1]d" + location = "%[2]s" +} + +resource "azurerm_postgresql_flexible_server" "test" { + name = "acctestRG-psql-virtualendpoint-primary-%[1]d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + version = "16" + public_network_access_enabled = false + administrator_login = "psqladmin" + administrator_password = "H@Sh1CoR3!" + zone = "1" + + storage_mb = 32768 + storage_tier = "P30" + + sku_name = "GP_Standard_D2ads_v5" +} + +resource "azurerm_postgresql_flexible_server" "test_replica" { + name = "acctestRG-psql-virtualendpoint-replica-%[1]d" + resource_group_name = azurerm_postgresql_flexible_server.test.resource_group_name + location = azurerm_postgresql_flexible_server.test.location + create_mode = "Replica" + source_server_id = azurerm_postgresql_flexible_server.test.id + version = "16" + public_network_access_enabled = false + zone = "1" + storage_mb = 32768 + storage_tier = "P30" + + sku_name = "GP_Standard_D2ads_v5" +} + +resource "azurerm_postgresql_flexible_server_virtual_endpoint" "test" { + name = "acctestRG-psqlvirtualendpoint-endpoint-%[1]d" + source_server_id = azurerm_postgresql_flexible_server.test.id + replica_server_id = azurerm_postgresql_flexible_server.test_replica.id + type = "ReadWrite" +} +`, data.RandomInteger, data.Locations.Primary) +} From b3463eef301c8d81c17cfc79469362dc152a7030 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 18 Jul 2024 16:04:08 -0500 Subject: [PATCH 12/50] complete basic tests --- ...e_server_virtual_endpoint_resource_test.go | 77 +++++++++---------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index aa9aa276fa3f..1351be2ca2cc 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -19,7 +19,7 @@ import ( type PostgresqlFlexibleServerVirtualEndpointResource struct{} func TestAccPostgresqlFlexibleServerVirtualEndpoint_basic(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_active_directory_administrator", "test") + data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_virtual_endpoint", "test") r := PostgresqlFlexibleServerVirtualEndpointResource{} data.ResourceTest(t, r, []acceptance.TestStep{ @@ -33,35 +33,32 @@ func TestAccPostgresqlFlexibleServerVirtualEndpoint_basic(t *testing.T) { }) } -// func TestAccPostgresqlFlexibleServerVirtualEndpoint_requiresImport(t *testing.T) { -// data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_active_directory_administrator", "test") -// r := PostgresqlFlexibleServerVirtualEndpointResource{} - -// data.ResourceTest(t, r, []acceptance.TestStep{ -// { -// Config: r.basic(data), -// Check: acceptance.ComposeTestCheckFunc( -// check.That(data.ResourceName).ExistsInAzure(r), -// ), -// }, -// { -// Config: r.requiresImport(data), -// ExpectError: acceptance.RequiresImportError("azurerm_postgresql_flexible_server_active_directory_administrator"), -// }, -// }) -// } - -// func TestAccPostgresqlFlexibleServerVirtualEndpoint_disappears(t *testing.T) { -// data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_active_directory_administrator", "test") -// r := PostgresqlFlexibleServerVirtualEndpointResource{} - -// data.ResourceTest(t, r, []acceptance.TestStep{ -// data.DisappearsStep(acceptance.DisappearsStepData{ -// Config: r.basic, -// TestResource: r, -// }), -// }) -// } +func TestAccPostgresqlFlexibleServerVirtualEndpoint_requiresImport(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_virtual_endpoint", "test") + r := PostgresqlFlexibleServerVirtualEndpointResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccPostgresqlFlexibleServerVirtualEndpoint_disappears(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_virtual_endpoint", "test") + r := PostgresqlFlexibleServerVirtualEndpointResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + data.DisappearsStep(acceptance.DisappearsStepData{ + Config: r.basic, + TestResource: r, + }), + }) +} func (r PostgresqlFlexibleServerVirtualEndpointResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := virtualendpoints.ParseVirtualEndpointID(state.ID) @@ -77,18 +74,18 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Exists(ctx context.Cont return utils.Bool(resp.Model != nil), nil } -// func (r PostgresqlFlexibleServerVirtualEndpointResource) Destroy(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { -// id, err := administrators.ParseAdministratorID(state.ID) -// if err != nil { -// return nil, err -// } +func (r PostgresqlFlexibleServerVirtualEndpointResource) Destroy(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { + id, err := virtualendpoints.ParseVirtualEndpointID(state.ID) + if err != nil { + return nil, err + } -// if _, err := client.Postgres.FlexibleServerAdministratorsClient.Delete(ctx, *id); err != nil { -// return nil, fmt.Errorf("deleting Postgresql AAD Administrator (%s): %+v", id.String(), err) -// } + if _, err := client.Postgres.VirtualEndpointClient.Delete(ctx, *id); err != nil { + return nil, fmt.Errorf("deleting Postgresql Virtual Endpoint (%s): %+v", id.String(), err) + } -// return utils.Bool(true), nil -// } + return utils.Bool(true), nil +} func (PostgresqlFlexibleServerVirtualEndpointResource) basic(data acceptance.TestData) string { return fmt.Sprintf(` From 1865594dd72e8b5e93baa416b217a662dcef6da0 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 18 Jul 2024 16:47:25 -0500 Subject: [PATCH 13/50] fix tests --- ...e_server_virtual_endpoint_resource_test.go | 55 +++++++------------ 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index 1351be2ca2cc..8a247441b377 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -29,30 +29,7 @@ func TestAccPostgresqlFlexibleServerVirtualEndpoint_basic(t *testing.T) { check.That(data.ResourceName).ExistsInAzure(r), ), }, - data.ImportStep(), - }) -} - -func TestAccPostgresqlFlexibleServerVirtualEndpoint_requiresImport(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_virtual_endpoint", "test") - r := PostgresqlFlexibleServerVirtualEndpointResource{} - - data.ResourceTest(t, r, []acceptance.TestStep{ - { - Config: r.basic(data), - Check: acceptance.ComposeTestCheckFunc( - check.That(data.ResourceName).ExistsInAzure(r), - ), - }, - data.ImportStep(), - }) -} - -func TestAccPostgresqlFlexibleServerVirtualEndpoint_disappears(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_virtual_endpoint", "test") - r := PostgresqlFlexibleServerVirtualEndpointResource{} - - data.ResourceTest(t, r, []acceptance.TestStep{ + data.ImportStep("type"), data.DisappearsStep(acceptance.DisappearsStepData{ Config: r.basic, TestResource: r, @@ -60,6 +37,18 @@ func TestAccPostgresqlFlexibleServerVirtualEndpoint_disappears(t *testing.T) { }) } +// func TestAccPostgresqlFlexibleServerVirtualEndpoint_disappears(t *testing.T) { +// data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_virtual_endpoint", "test") +// r := PostgresqlFlexibleServerVirtualEndpointResource{} + +// data.ResourceTest(t, r, []acceptance.TestStep{ +// data.DisappearsStep(acceptance.DisappearsStepData{ +// Config: r.basic, +// TestResource: r, +// }), +// }) +// } + func (r PostgresqlFlexibleServerVirtualEndpointResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := virtualendpoints.ParseVirtualEndpointID(state.ID) if err != nil { @@ -94,12 +83,12 @@ provider "azurerm" { } resource "azurerm_resource_group" "test" { - name = "acctestRG-psql-virtualendpoint-%[1]d" + name = "acctest-psql-virtualendpoint-rg-%[1]d" location = "%[2]s" } resource "azurerm_postgresql_flexible_server" "test" { - name = "acctestRG-psql-virtualendpoint-primary-%[1]d" + name = "acctest-psql-virtualendpoint-primary-%[1]d" resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location version = "16" @@ -107,15 +96,13 @@ resource "azurerm_postgresql_flexible_server" "test" { administrator_login = "psqladmin" administrator_password = "H@Sh1CoR3!" zone = "1" - - storage_mb = 32768 - storage_tier = "P30" - - sku_name = "GP_Standard_D2ads_v5" + storage_mb = 32768 + storage_tier = "P30" + sku_name = "GP_Standard_D2ads_v5" } resource "azurerm_postgresql_flexible_server" "test_replica" { - name = "acctestRG-psql-virtualendpoint-replica-%[1]d" + name = "acctest-psql-virtualendpoint-replica-%[1]d" resource_group_name = azurerm_postgresql_flexible_server.test.resource_group_name location = azurerm_postgresql_flexible_server.test.location create_mode = "Replica" @@ -125,12 +112,10 @@ resource "azurerm_postgresql_flexible_server" "test_replica" { zone = "1" storage_mb = 32768 storage_tier = "P30" - - sku_name = "GP_Standard_D2ads_v5" } resource "azurerm_postgresql_flexible_server_virtual_endpoint" "test" { - name = "acctestRG-psqlvirtualendpoint-endpoint-%[1]d" + name = "acctest-psqlvirtualendpoint-endpoint-%[1]d" source_server_id = azurerm_postgresql_flexible_server.test.id replica_server_id = azurerm_postgresql_flexible_server.test_replica.id type = "ReadWrite" From 7bb389586fd7684cc7a3be50ada920066628e0d6 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Fri, 19 Jul 2024 09:37:06 -0500 Subject: [PATCH 14/50] first tests pass --- ...esql_flexible_server_virtual_endpoint_resource_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index 8a247441b377..0455b96405b1 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -27,13 +27,13 @@ func TestAccPostgresqlFlexibleServerVirtualEndpoint_basic(t *testing.T) { Config: r.basic(data), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("name").Exists(), + check.That(data.ResourceName).Key("source_server_id").Exists(), + check.That(data.ResourceName).Key("replica_server_id").Exists(), + check.That(data.ResourceName).Key("type").Exists(), ), }, data.ImportStep("type"), - data.DisappearsStep(acceptance.DisappearsStepData{ - Config: r.basic, - TestResource: r, - }), }) } From 8e9c97c9e8fde9298a546bdd574c6b7e319982fa Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Fri, 19 Jul 2024 13:58:23 -0500 Subject: [PATCH 15/50] complete tests --- ...exible_server_virtual_endpoint_resource.go | 22 ++++++++++++--- ...e_server_virtual_endpoint_resource_test.go | 27 +++++++++---------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index f2c113ac3249..c848c008ac7e 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -1,6 +1,7 @@ package postgres import ( + "context" "fmt" "log" "time" @@ -138,13 +139,18 @@ func resourcePostgresqlFlexibleServerVirtualEndpointRead(d *pluginsdk.ResourceDa return fmt.Errorf("retrieving %s: %+v", id, err) } - if model := resp.Model; model != nil { - // Model.Properties.Members is a tuple => [source_server, replication_server] - if model.Properties != nil && model.Properties.Members != nil && len(*resp.Model.Properties.Members) == 2 { + if model := resp.Model; model != nil && model.Properties != nil { + // Model.Properties.Members should be a tuple => [source_server, replication_server] + if resp.Model.Properties.Members != nil { replicateServerId := servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[1]) if err := d.Set("replica_server_id", replicateServerId.ID()); err != nil { return fmt.Errorf("setting `replica_server_id`: %+v", err) } + } else { + // if members list is nil, this is an endpoint that was previously deleted + log.Printf("[INFO] Postgresql Flexible Server Endpoint %q was previously deleted - removing from state", d.Id()) + d.SetId("") + return nil } } @@ -197,6 +203,15 @@ func resourcePostgresqlFlexibleServerVirtualEndpointDelete(d *pluginsdk.Resource locks.ByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + if err := DeletePostgresFlexibileServerVirtualEndpoint(ctx, client, id); err != nil { + return err + } + + return nil +} + +// exposed so we can access from tests +func DeletePostgresFlexibileServerVirtualEndpoint(ctx context.Context, client *virtualendpoints.VirtualEndpointsClient, id *virtualendpoints.VirtualEndpointId) error { deletePoller := custompollers.NewPostgresFlexibleServerVirtualEndpointDeletePoller(client, *id) poller := pollers.NewPoller(deletePoller, 5*time.Second, pollers.DefaultNumberOfDroppedConnectionsToAllow) @@ -207,6 +222,5 @@ func resourcePostgresqlFlexibleServerVirtualEndpointDelete(d *pluginsdk.Resource if err := poller.PollUntilDone(ctx); err != nil { return err } - return nil } diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index 0455b96405b1..141a37eef2d2 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/postgres" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -34,21 +35,13 @@ func TestAccPostgresqlFlexibleServerVirtualEndpoint_basic(t *testing.T) { ), }, data.ImportStep("type"), + data.DisappearsStep(acceptance.DisappearsStepData{ + Config: r.basic, + TestResource: r, + }), }) } -// func TestAccPostgresqlFlexibleServerVirtualEndpoint_disappears(t *testing.T) { -// data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_virtual_endpoint", "test") -// r := PostgresqlFlexibleServerVirtualEndpointResource{} - -// data.ResourceTest(t, r, []acceptance.TestStep{ -// data.DisappearsStep(acceptance.DisappearsStepData{ -// Config: r.basic, -// TestResource: r, -// }), -// }) -// } - func (r PostgresqlFlexibleServerVirtualEndpointResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := virtualendpoints.ParseVirtualEndpointID(state.ID) if err != nil { @@ -60,7 +53,7 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Exists(ctx context.Cont return nil, fmt.Errorf("reading Postgresql Virtual Endpoint (%s): %+v", id.String(), err) } - return utils.Bool(resp.Model != nil), nil + return utils.Bool(resp.Model != nil && resp.Model.Properties != nil && resp.Model.Properties.Members != nil), nil } func (r PostgresqlFlexibleServerVirtualEndpointResource) Destroy(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { @@ -69,7 +62,7 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Destroy(ctx context.Con return nil, err } - if _, err := client.Postgres.VirtualEndpointClient.Delete(ctx, *id); err != nil { + if err := postgres.DeletePostgresFlexibileServerVirtualEndpoint(ctx, client.Postgres.VirtualEndpointClient, id); err != nil { return nil, fmt.Errorf("deleting Postgresql Virtual Endpoint (%s): %+v", id.String(), err) } @@ -79,7 +72,11 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Destroy(ctx context.Con func (PostgresqlFlexibleServerVirtualEndpointResource) basic(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { - features {} + features { + resource_group { + prevent_deletion_if_contains_resources = false + } + } } resource "azurerm_resource_group" "test" { From 9f5f19f87cd43d97931ff815e5e836507a7cf268 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Fri, 19 Jul 2024 14:26:44 -0500 Subject: [PATCH 16/50] fix fmt --- ...postgresql_flexible_server_virtual_endpoint_resource_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index 141a37eef2d2..2bc5cf828104 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -75,7 +75,7 @@ provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false - } + } } } From 6c10c9ecbf55d15e41fa48486e77a09c3684a329 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Fri, 19 Jul 2024 14:37:21 -0500 Subject: [PATCH 17/50] add docs page --- ...ible_server_virtual_endpoint.html.markdown | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown diff --git a/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown b/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown new file mode 100644 index 000000000000..00b34ba63e4d --- /dev/null +++ b/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown @@ -0,0 +1,93 @@ +--- +subcategory: "Database" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_postgresql_flexible_server_virtual_endpoint" +description: |- + Manages a Virtual Endpoint on a PostgreSQL Flexible server +--- + +# azurerm_postgresql_flexible_server_virtual_endpoint + +Allows you to create a Virtual Endpoint associated with a Postgres Flexible Replica. + +## Example Usage + +```hcl +resource "azurerm_resource_group" "example" { + name = "example-resources" + location = "East US" +} + +resource "azurerm_postgresql_flexible_server" "example" { + name = "example" + resource_group_name = azurerm_resource_group.example.name + location = azurerm_resource_group.example.location + version = "16" + public_network_access_enabled = false + administrator_login = "psqladmin" + administrator_password = "H@Sh1CoR3!" + zone = "1" + + storage_mb = 32768 + storage_tier = "P30" + + sku_name = "GP_Standard_D2ads_v5" +} + +resource "azurerm_postgresql_flexible_server" "example_replica" { + name = "example-replica" + resource_group_name = azurerm_postgresql_flexible_server.example.resource_group_name + location = azurerm_postgresql_flexible_server.example.location + create_mode = "Replica" + source_server_id = azurerm_postgresql_flexible_server.example.id + version = "16" + public_network_access_enabled = false + zone = "1" + storage_mb = 32768 + storage_tier = "P30" + + sku_name = "GP_Standard_D2ads_v5" +} + +resource "azurerm_postgresql_flexible_server_virtual_endpoint" "example" { + name = "example-endpoint-1" + source_server_id = azurerm_postgresql_flexible_server.example.id + replica_server_id = azurerm_postgresql_flexible_server.example_replica.id + type = "ReadWrite" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the Virtual Endpoint + +* `source_server_id` - (Required) The Resource ID of the *Source* Postgres Flexible Server this should be associated with. + +* `replica_server_id` - (Required) The Resource ID of the *Replica* Postgres Flexible Server this should be associated with + +* `type` - (Required) The type of Virtual Endpoint. Currently only `ReadWrite` is supported. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the PostgreSQL Active Directory Administrator. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions: + +* `create` - (Defaults to 30 minutes) Used when creating the PostgreSQL Active Directory Administrator. +* `update` - (Defaults to 30 minutes) Used when updating the PostgreSQL Active Directory Administrator. +* `read` - (Defaults to 5 minutes) Used when retrieving the PostgreSQL Active Directory Administrator. +* `delete` - (Defaults to 30 minutes) Used when deleting the PostgreSQL Active Directory Administrator. + +## Import + +A PostgreSQL Flexible Virtual Endpoint can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_postgresql_flexible_server_virtual_endpoint.example subscriptions//resourceGroups//providers/Microsoft.DBForPostgreSql/flexibleServers//virtualendpoints/ +``` From 3a10a8449c53e6a7da3491d1f2f2062663942f44 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Fri, 19 Jul 2024 14:37:31 -0500 Subject: [PATCH 18/50] fix description --- .../postgresql_flexible_server_virtual_endpoint_resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index c848c008ac7e..ff69baad0758 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -53,7 +53,7 @@ func resourcePostgresqlFlexibleServerVirtualEndpoint() *pluginsdk.Resource { }, "replica_server_id": { Type: pluginsdk.TypeString, - Description: "The Resource ID of the *Source* Postgres Flexible Server this should be associated with", + Description: "The Resource ID of the *Replica* Postgres Flexible Server this should be associated with", Required: true, ValidateFunc: servers.ValidateFlexibleServerID, }, From 93821c1bad29e4fe8aeb5127c55b57b7d42deb3b Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Fri, 19 Jul 2024 14:38:25 -0500 Subject: [PATCH 19/50] lock region during test --- ...postgresql_flexible_server_virtual_endpoint_resource_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index 2bc5cf828104..88b2e52a9a83 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -117,5 +117,5 @@ resource "azurerm_postgresql_flexible_server_virtual_endpoint" "test" { replica_server_id = azurerm_postgresql_flexible_server.test_replica.id type = "ReadWrite" } -`, data.RandomInteger, data.Locations.Primary) +`, data.RandomInteger, "eastus") // force region due to SKU constraints } From f058493d2e52ff223c50c7815ceac337321c0dc4 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Fri, 19 Jul 2024 14:45:50 -0500 Subject: [PATCH 20/50] fix typo --- ...stgresql_flexible_server_virtual_endpoint_resource_poller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go b/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go index 077d5975f58f..9addf4472bb8 100644 --- a/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go +++ b/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go @@ -20,7 +20,7 @@ type postgresFlexibleServerVirtualEndpointPoller struct { id virtualendpoints.VirtualEndpointId } -// Workaround due to Azure performing a psuedo-soft delete on virtual endpoints. +// Workaround due to Azure performing a pseudo-soft delete on virtual endpoints. // // - The `DELETE` endpoint does not fully delete the resource, it sets `properties.members` to nil // From e4d91f7c813c354910c21cac7a20226f16639bf8 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Fri, 19 Jul 2024 14:51:07 -0500 Subject: [PATCH 21/50] update docs --- ...esql_flexible_server_virtual_endpoint.html.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown b/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown index 00b34ba63e4d..2381eae15929 100644 --- a/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown +++ b/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown @@ -73,16 +73,16 @@ The following arguments are supported: In addition to the Arguments listed above - the following Attributes are exported: -* `id` - The ID of the PostgreSQL Active Directory Administrator. +* `id` - The ID of the PostgreSQL Flexible Virtual Endpoint. ## Timeouts The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions: -* `create` - (Defaults to 30 minutes) Used when creating the PostgreSQL Active Directory Administrator. -* `update` - (Defaults to 30 minutes) Used when updating the PostgreSQL Active Directory Administrator. -* `read` - (Defaults to 5 minutes) Used when retrieving the PostgreSQL Active Directory Administrator. -* `delete` - (Defaults to 30 minutes) Used when deleting the PostgreSQL Active Directory Administrator. +* `create` - (Defaults to 30 minutes) Used when creating the PostgreSQL Flexible Virtual Endpoint. +* `update` - (Defaults to 30 minutes) Used when updating the PostgreSQL Flexible Virtual Endpoint. +* `read` - (Defaults to 5 minutes) Used when retrieving the PostgreSQL Flexible Virtual Endpoint. +* `delete` - (Defaults to 30 minutes) Used when deleting the PostgreSQL Flexible Virtual Endpoint. ## Import From 3b4fbbd1945adbdad883185dd0ff516fe020164e Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Fri, 19 Jul 2024 15:07:32 -0500 Subject: [PATCH 22/50] fix timeouts --- ...ostgresql_flexible_server_virtual_endpoint_resource.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index ff69baad0758..21cf15c91ab7 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -26,10 +26,10 @@ func resourcePostgresqlFlexibleServerVirtualEndpoint() *pluginsdk.Resource { Delete: resourcePostgresqlFlexibleServerVirtualEndpointDelete, Timeouts: &pluginsdk.ResourceTimeout{ - Create: pluginsdk.DefaultTimeout(20 * time.Minute), - Read: pluginsdk.DefaultTimeout(20 * time.Minute), - Update: pluginsdk.DefaultTimeout(20 * time.Minute), - Delete: pluginsdk.DefaultTimeout(20 * time.Minute), + Create: pluginsdk.DefaultTimeout(10 * time.Minute), + Read: pluginsdk.DefaultTimeout(5 * time.Minute), + Update: pluginsdk.DefaultTimeout(10 * time.Minute), + Delete: pluginsdk.DefaultTimeout(10 * time.Minute), }, Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { From f544df8023ee3e31de962da649494251a977d20f Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Wed, 31 Jul 2024 12:02:06 -0500 Subject: [PATCH 23/50] update to new format --- ...exible_server_virtual_endpoint_resource.go | 332 ++++++++++-------- internal/services/postgres/registration.go | 16 +- 2 files changed, 192 insertions(+), 156 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 21cf15c91ab7..4fe56c331f27 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -10,204 +10,228 @@ import ( "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/servers" "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints" "github.com/hashicorp/go-azure-sdk/sdk/client/pollers" - "github.com/hashicorp/terraform-provider-azurerm/internal/clients" "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/custompollers" "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" ) -func resourcePostgresqlFlexibleServerVirtualEndpoint() *pluginsdk.Resource { - return &pluginsdk.Resource{ - Create: resourcePostgresqlFlexibleServerVirtualEndpointCreate, - Read: resourcePostgresqlFlexibleServerVirtualEndpointRead, - Update: resourcePostgresqlFlexibleServerVirtualEndpointUpdate, - Delete: resourcePostgresqlFlexibleServerVirtualEndpointDelete, - - Timeouts: &pluginsdk.ResourceTimeout{ - Create: pluginsdk.DefaultTimeout(10 * time.Minute), - Read: pluginsdk.DefaultTimeout(5 * time.Minute), - Update: pluginsdk.DefaultTimeout(10 * time.Minute), - Delete: pluginsdk.DefaultTimeout(10 * time.Minute), - }, +type PostgresqlFlexibleServerVirtualEndpointResource struct{} - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { - _, err := virtualendpoints.ParseVirtualEndpointID(id) - return err - }), - - Schema: map[string]*pluginsdk.Schema{ - "name": { - Type: pluginsdk.TypeString, - Description: "The name of the Virtual Endpoint", - ForceNew: true, - Required: true, - }, - "source_server_id": { - Type: pluginsdk.TypeString, - Description: "The Resource ID of the *Source* Postgres Flexible Server this should be associated with", - ForceNew: true, - Required: true, - ValidateFunc: servers.ValidateFlexibleServerID, - }, - "replica_server_id": { - Type: pluginsdk.TypeString, - Description: "The Resource ID of the *Replica* Postgres Flexible Server this should be associated with", - Required: true, - ValidateFunc: servers.ValidateFlexibleServerID, - }, - "type": { - Type: pluginsdk.TypeString, - Description: "The type of Virtual Endpoint", - ForceNew: true, - Required: true, - ValidateFunc: validation.StringInSlice(virtualendpoints.PossibleValuesForVirtualEndpointType(), true), - }, - }, - } +type PostgresqlFlexibleServerVirtualEndpointModel struct { + Name string `tfschema:"name"` + SourceServerId string `tfschema:"source_server_id"` + ReplicaServerId string `tfschema:"replica_server_id"` + Type string `tfschema:"type"` } -func resourcePostgresqlFlexibleServerVirtualEndpointCreate(d *pluginsdk.ResourceData, meta interface{}) error { - client := meta.(*clients.Client).Postgres.VirtualEndpointClient - ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) - defer cancel() +var _ sdk.ResourceWithUpdate = PostgresqlFlexibleServerVirtualEndpointResource{} - name := d.Get("name").(string) - sourceServer := d.Get("source_server_id").(string) - replicaServer := d.Get("replica_server_id").(string) - virtualEndpointType := d.Get("type").(string) +func (r PostgresqlFlexibleServerVirtualEndpointResource) ModelObject() interface{} { + return &PostgresqlFlexibleServerVirtualEndpointModel{} +} - sourceServerId, err := servers.ParseFlexibleServerID(sourceServer) - if err != nil { - return err - } +func (r PostgresqlFlexibleServerVirtualEndpointResource) ResourceType() string { + return "azurerm_postgresql_flexible_server_virtual_endpoint" +} - replicaServerId, err := servers.ParseFlexibleServerID(replicaServer) - if err != nil { - return err - } +func (r PostgresqlFlexibleServerVirtualEndpointResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { + return func(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.ParseVirtualEndpointID(v); err != nil { + errors = append(errors, err) + } - id := virtualendpoints.NewVirtualEndpointID(sourceServerId.SubscriptionId, sourceServerId.ResourceGroupName, sourceServerId.FlexibleServerName, name) + return + } +} - locks.ByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) - defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) +func (r PostgresqlFlexibleServerVirtualEndpointResource) Attributes() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{} +} - // This API can be a bit flaky if the same named resource is created/destroyed quickly - // usually waiting a minute or two before redeploying is enough to resolve the conflict - if err = client.CreateThenPoll(ctx, id, virtualendpoints.VirtualEndpointResource{ - Name: &name, - Properties: &virtualendpoints.VirtualEndpointResourceProperties{ - EndpointType: (*virtualendpoints.VirtualEndpointType)(&virtualEndpointType), - Members: &[]string{replicaServerId.FlexibleServerName}, +func (r PostgresqlFlexibleServerVirtualEndpointResource) Arguments() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + "name": { + Type: pluginsdk.TypeString, + Description: "The name of the Virtual Endpoint", + ForceNew: true, + Required: true, + }, + "source_server_id": { + Type: pluginsdk.TypeString, + Description: "The Resource ID of the *Source* Postgres Flexible Server this should be associated with", + ForceNew: true, + Required: true, + ValidateFunc: servers.ValidateFlexibleServerID, + }, + "replica_server_id": { + Type: pluginsdk.TypeString, + Description: "The Resource ID of the *Replica* Postgres Flexible Server this should be associated with", + Required: true, + ValidateFunc: servers.ValidateFlexibleServerID, + }, + "type": { + Type: pluginsdk.TypeString, + Description: "The type of Virtual Endpoint", + ForceNew: true, + Required: true, + ValidateFunc: validation.StringInSlice(virtualendpoints.PossibleValuesForVirtualEndpointType(), true), }, - }); err != nil { - return fmt.Errorf("creating %s: %+v", id, err) } +} - d.SetId(id.ID()) +func (r PostgresqlFlexibleServerVirtualEndpointResource) Create() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 5 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + var virtualEndpoint PostgresqlFlexibleServerVirtualEndpointModel - return nil -} + if err := metadata.Decode(&virtualEndpoint); err != nil { + return err + } -func resourcePostgresqlFlexibleServerVirtualEndpointRead(d *pluginsdk.ResourceData, meta interface{}) error { - client := meta.(*clients.Client).Postgres.VirtualEndpointClient - ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) - defer cancel() + client := metadata.Client.Postgres.VirtualEndpointClient - id, err := virtualendpoints.ParseVirtualEndpointID(d.Id()) - if err != nil { - return err - } + sourceServerId, err := servers.ParseFlexibleServerID(virtualEndpoint.SourceServerId) + if err != nil { + return err + } - if err := d.Set("name", id.VirtualEndpointName); err != nil { - return fmt.Errorf("setting `name`: %+v", err) - } + replicaServerId, err := servers.ParseFlexibleServerID(virtualEndpoint.ReplicaServerId) + if err != nil { + return err + } - flexibleServerId := servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, id.FlexibleServerName) - if err := d.Set("source_server_id", flexibleServerId.ID()); err != nil { - return fmt.Errorf("setting `source_server_id`: %+v", err) - } + id := virtualendpoints.NewVirtualEndpointID(sourceServerId.SubscriptionId, sourceServerId.ResourceGroupName, sourceServerId.FlexibleServerName, virtualEndpoint.Name) + + locks.ByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + + // This API can be a bit flaky if the same named resource is created/destroyed quickly + // usually waiting a minute or two before redeploying is enough to resolve the conflict + if err = client.CreateThenPoll(ctx, id, virtualendpoints.VirtualEndpointResource{ + Name: &virtualEndpoint.Name, + Properties: &virtualendpoints.VirtualEndpointResourceProperties{ + EndpointType: (*virtualendpoints.VirtualEndpointType)(&virtualEndpoint.Type), + Members: &[]string{replicaServerId.FlexibleServerName}, + }, + }); err != nil { + return fmt.Errorf("creating %s: %+v", id, err) + } + + metadata.SetID(id) - resp, err := client.Get(ctx, *id) - if err != nil { - if response.WasNotFound(resp.HttpResponse) { - log.Printf("[INFO] Postgresql Flexible Server Endpoint %q does not exist - removing from state", d.Id()) - d.SetId("") return nil - } - return fmt.Errorf("retrieving %s: %+v", id, err) + }, } +} + +func (r PostgresqlFlexibleServerVirtualEndpointResource) Read() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 5 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + client := metadata.Client.Postgres.VirtualEndpointClient - if model := resp.Model; model != nil && model.Properties != nil { - // Model.Properties.Members should be a tuple => [source_server, replication_server] - if resp.Model.Properties.Members != nil { - replicateServerId := servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[1]) - if err := d.Set("replica_server_id", replicateServerId.ID()); err != nil { - return fmt.Errorf("setting `replica_server_id`: %+v", err) + id, err := virtualendpoints.ParseVirtualEndpointID(metadata.ResourceData.Id()) + if err != nil { + return err } - } else { - // if members list is nil, this is an endpoint that was previously deleted - log.Printf("[INFO] Postgresql Flexible Server Endpoint %q was previously deleted - removing from state", d.Id()) - d.SetId("") - return nil - } - } - return nil -} + resp, err := client.Get(ctx, *id) + if err != nil { + if response.WasNotFound(resp.HttpResponse) { + log.Printf("[INFO] Postgresql Flexible Server Endpoint %q does not exist - removing from state", metadata.ResourceData.Id()) + return metadata.MarkAsGone(id) + } + return fmt.Errorf("retrieving %s: %+v", id, err) + } -func resourcePostgresqlFlexibleServerVirtualEndpointUpdate(d *pluginsdk.ResourceData, meta interface{}) error { - client := meta.(*clients.Client).Postgres.VirtualEndpointClient - ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) - defer cancel() + virtualEndpoint := PostgresqlFlexibleServerVirtualEndpointModel{ + SourceServerId: virtualendpoints.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, id.FlexibleServerName).ID(), + } - id, err := virtualendpoints.ParseVirtualEndpointID(d.Id()) - if err != nil { - return err + if model := resp.Model; model != nil && model.Properties != nil { + // Model.Properties.Members should be a tuple => [source_server, replication_server] + if resp.Model.Properties.Members != nil && len(*resp.Model.Properties.Members) == 2 { + virtualEndpoint.ReplicaServerId = servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[1]).ID() + } else { + // if members list is nil, this is an endpoint that was previously deleted + log.Printf("[INFO] Postgresql Flexible Server Endpoint %q was previously deleted - removing from state", id.ID()) + return metadata.MarkAsGone(id) + } + } + + return metadata.Encode(&virtualEndpoint) + }, } +} - replicaServer := d.Get("replica_server_id").(string) - virtualEndpointType := d.Get("type").(string) +func (r PostgresqlFlexibleServerVirtualEndpointResource) Delete() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 5 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + client := metadata.Client.Postgres.VirtualEndpointClient - replicaServerId, err := servers.ParseFlexibleServerID(replicaServer) - if err != nil { - return err - } + id, err := virtualendpoints.ParseVirtualEndpointID(metadata.ResourceData.Id()) + if err != nil { + return err + } - locks.ByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) - defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + locks.ByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + + if err := DeletePostgresFlexibileServerVirtualEndpoint(ctx, client, id); err != nil { + return err + } - if err := client.UpdateThenPoll(ctx, *id, virtualendpoints.VirtualEndpointResourceForPatch{ - Properties: &virtualendpoints.VirtualEndpointResourceProperties{ - EndpointType: (*virtualendpoints.VirtualEndpointType)(&virtualEndpointType), - Members: &[]string{replicaServerId.FlexibleServerName}, + return metadata.MarkAsGone(id) // is this correct?? }, - }); err != nil { - return fmt.Errorf("updating %q: %+v", id, err) } - - return nil } -func resourcePostgresqlFlexibleServerVirtualEndpointDelete(d *pluginsdk.ResourceData, meta interface{}) error { - client := meta.(*clients.Client).Postgres.VirtualEndpointClient - ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) - defer cancel() +func (r PostgresqlFlexibleServerVirtualEndpointResource) Update() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 5 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + var virtualEndpoint PostgresqlFlexibleServerVirtualEndpointModel + client := metadata.Client.Postgres.VirtualEndpointClient - id, err := virtualendpoints.ParseVirtualEndpointID(d.Id()) - if err != nil { - return err - } + if err := metadata.Decode(&virtualEndpoint); err != nil { + return err + } - locks.ByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) - defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + id, err := virtualendpoints.ParseVirtualEndpointID(metadata.ResourceData.Id()) + if err != nil { + return err + } - if err := DeletePostgresFlexibileServerVirtualEndpoint(ctx, client, id); err != nil { - return err - } + replicaServerId, err := servers.ParseFlexibleServerID(virtualEndpoint.ReplicaServerId) + if err != nil { + return err + } - return nil + locks.ByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + defer locks.UnlockByName(id.FlexibleServerName, postgresqlFlexibleServerResourceName) + + if err := client.UpdateThenPoll(ctx, *id, virtualendpoints.VirtualEndpointResourceForPatch{ + Properties: &virtualendpoints.VirtualEndpointResourceProperties{ + EndpointType: (*virtualendpoints.VirtualEndpointType)(&virtualEndpoint.Type), + Members: &[]string{replicaServerId.FlexibleServerName}, + }, + }); err != nil { + return fmt.Errorf("updating %q: %+v", id, err) + } + + return nil + }, + } } // exposed so we can access from tests diff --git a/internal/services/postgres/registration.go b/internal/services/postgres/registration.go index 20e104f59612..cad8f4e49dcf 100644 --- a/internal/services/postgres/registration.go +++ b/internal/services/postgres/registration.go @@ -10,7 +10,10 @@ import ( type Registration struct{} -var _ sdk.UntypedServiceRegistrationWithAGitHubLabel = Registration{} +var ( + _ sdk.TypedServiceRegistrationWithAGitHubLabel = Registration{} + _ sdk.UntypedServiceRegistrationWithAGitHubLabel = Registration{} +) func (r Registration) AssociatedGitHubLabel() string { return "service/postgresql" @@ -51,6 +54,15 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource { "azurerm_postgresql_flexible_server_configuration": resourcePostgresqlFlexibleServerConfiguration(), "azurerm_postgresql_flexible_server_database": resourcePostgresqlFlexibleServerDatabase(), "azurerm_postgresql_flexible_server_active_directory_administrator": resourcePostgresqlFlexibleServerAdministrator(), - "azurerm_postgresql_flexible_server_virtual_endpoint": resourcePostgresqlFlexibleServerVirtualEndpoint(), } } + +func (r Registration) Resources() []sdk.Resource { + return []sdk.Resource{ + PostgresqlFlexibleServerVirtualEndpointResource{}, + } +} + +func (r Registration) DataSources() []sdk.DataSource { + return []sdk.DataSource{} +} From 07cb86c1a4a081fe540603e56fda1e5447143e11 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Wed, 31 Jul 2024 15:56:51 -0500 Subject: [PATCH 24/50] add service to typed services list --- internal/provider/services.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/provider/services.go b/internal/provider/services.go index 30fb0e987a0f..bbe5e5ee98dd 100644 --- a/internal/provider/services.go +++ b/internal/provider/services.go @@ -222,6 +222,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration { voiceservices.Registration{}, web.Registration{}, workloads.Registration{}, + postgres.Registration{}, } services = append(services, autoRegisteredTypedServices()...) return services From d00bc17cf5ded2809f2cfa56854034f8db5ba970 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Wed, 31 Jul 2024 16:50:44 -0500 Subject: [PATCH 25/50] add properties to state upon read --- ...resql_flexible_server_virtual_endpoint_resource.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 4fe56c331f27..9f2497b3a4ba 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -153,13 +153,18 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Read() sdk.ResourceFunc return fmt.Errorf("retrieving %s: %+v", id, err) } - virtualEndpoint := PostgresqlFlexibleServerVirtualEndpointModel{ - SourceServerId: virtualendpoints.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, id.FlexibleServerName).ID(), - } + virtualEndpoint := PostgresqlFlexibleServerVirtualEndpointModel{} if model := resp.Model; model != nil && model.Properties != nil { + virtualEndpoint.Name = *model.Name + + if resp.Model.Properties.EndpointType != nil { + virtualEndpoint.Type = string(*model.Properties.EndpointType) + } + // Model.Properties.Members should be a tuple => [source_server, replication_server] if resp.Model.Properties.Members != nil && len(*resp.Model.Properties.Members) == 2 { + virtualEndpoint.SourceServerId = servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[0]).ID() virtualEndpoint.ReplicaServerId = servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[1]).ID() } else { // if members list is nil, this is an endpoint that was previously deleted From c9b5d2ef5f41520e5b5d195735b466035764c0cc Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Wed, 31 Jul 2024 17:08:36 -0500 Subject: [PATCH 26/50] get rid of if/else nesting --- ...exible_server_virtual_endpoint_resource.go | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 9f2497b3a4ba..3f8dcff8cf83 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -153,26 +153,34 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Read() sdk.ResourceFunc return fmt.Errorf("retrieving %s: %+v", id, err) } - virtualEndpoint := PostgresqlFlexibleServerVirtualEndpointModel{} + model := resp.Model + if model == nil { + return fmt.Errorf("retrieving %s: model was nil", id) + } - if model := resp.Model; model != nil && model.Properties != nil { - virtualEndpoint.Name = *model.Name + properties := resp.Model.Properties + if properties == nil { + return fmt.Errorf("retrieving %s: properties were nil", id) + } - if resp.Model.Properties.EndpointType != nil { - virtualEndpoint.Type = string(*model.Properties.EndpointType) - } + virtualEndpoint := PostgresqlFlexibleServerVirtualEndpointModel{ + Name: *model.Name, + } - // Model.Properties.Members should be a tuple => [source_server, replication_server] - if resp.Model.Properties.Members != nil && len(*resp.Model.Properties.Members) == 2 { - virtualEndpoint.SourceServerId = servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[0]).ID() - virtualEndpoint.ReplicaServerId = servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[1]).ID() - } else { - // if members list is nil, this is an endpoint that was previously deleted - log.Printf("[INFO] Postgresql Flexible Server Endpoint %q was previously deleted - removing from state", id.ID()) - return metadata.MarkAsGone(id) - } + if properties.EndpointType != nil { + virtualEndpoint.Type = string(*model.Properties.EndpointType) + } + + if properties.Members == nil || len(*properties.Members) != 2 { + // if members list is nil, this is an endpoint that was previously deleted + log.Printf("[INFO] Postgresql Flexible Server Endpoint %q was previously deleted - removing from state", id.ID()) + return metadata.MarkAsGone(id) } + // Model.Properties.Members should be a tuple => [source_server, replication_server] + virtualEndpoint.SourceServerId = servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[0]).ID() + virtualEndpoint.ReplicaServerId = servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[1]).ID() + return metadata.Encode(&virtualEndpoint) }, } From 1689b344f6402e7d5e0b2c064427f1782ef48c33 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:08:23 -0500 Subject: [PATCH 27/50] alphabetize services lists --- internal/provider/services.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/internal/provider/services.go b/internal/provider/services.go index bbe5e5ee98dd..c4d232b31944 100644 --- a/internal/provider/services.go +++ b/internal/provider/services.go @@ -173,10 +173,9 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration { eventhub.Registration{}, fluidrelay.Registration{}, graphservices.Registration{}, - storagecache.Registration{}, hybridcompute.Registration{}, - iothub.Registration{}, iotcentral.Registration{}, + iothub.Registration{}, keyvault.Registration{}, kusto.Registration{}, labservice.Registration{}, @@ -187,42 +186,43 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration { maintenance.Registration{}, managedhsm.Registration{}, media.Registration{}, - monitor.Registration{}, mobilenetwork.Registration{}, + monitor.Registration{}, mssql.Registration{}, mssqlmanagedinstance.Registration{}, mysql.Registration{}, - network.Registration{}, netapp.Registration{}, + network.Registration{}, networkfunction.Registration{}, newrelic.Registration{}, nginx.Registration{}, + orbital.Registration{}, paloalto.Registration{}, policy.Registration{}, + postgres.Registration{}, privatednsresolver.Registration{}, recoveryservices.Registration{}, - redis.Registration{}, redhatopenshift.Registration{}, + redis.Registration{}, resource.Registration{}, + search.Registration{}, + securitycenter.Registration{}, sentinel.Registration{}, serviceconnector.Registration{}, servicefabricmanaged.Registration{}, servicenetworking.Registration{}, + signalr.Registration{}, + springcloud.Registration{}, storage.Registration{}, + storagecache.Registration{}, storagemover.Registration{}, - signalr.Registration{}, - subscription.Registration{}, - orbital.Registration{}, streamanalytics.Registration{}, - search.Registration{}, - springcloud.Registration{}, - securitycenter.Registration{}, + subscription.Registration{}, systemcentervirtualmachinemanager.Registration{}, vmware.Registration{}, voiceservices.Registration{}, web.Registration{}, workloads.Registration{}, - postgres.Registration{}, } services = append(services, autoRegisteredTypedServices()...) return services From 3d7a797a5f35cb6033f393918deb1c907f86f52e Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:09:46 -0500 Subject: [PATCH 28/50] Update internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go Add context to error message Co-authored-by: stephybun --- ...stgresql_flexible_server_virtual_endpoint_resource_poller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go b/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go index 9addf4472bb8..b3818fb87d61 100644 --- a/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go +++ b/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go @@ -66,6 +66,6 @@ func (p postgresFlexibleServerVirtualEndpointPoller) Poll(ctx context.Context) ( HttpResponse: &client.Response{ Response: resp.HttpResponse, }, - Message: fmt.Sprintln("failed to delete flexible server virtual endpoint"), + Message: fmt.Sprintln("failed to delete %s", p.id), } } From b8e3e5d13b8db4fee37656b8fea88d21ce93dc9d Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:10:36 -0500 Subject: [PATCH 29/50] Update internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go Use built-in function for ID validation Co-authored-by: stephybun --- ...ql_flexible_server_virtual_endpoint_resource.go | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 3f8dcff8cf83..a2fadce166df 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -37,19 +37,7 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) ResourceType() string { } func (r PostgresqlFlexibleServerVirtualEndpointResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { - return func(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.ParseVirtualEndpointID(v); err != nil { - errors = append(errors, err) - } - - return - } + return virtualendpoints.ValidateVirtualEndpointID } func (r PostgresqlFlexibleServerVirtualEndpointResource) Attributes() map[string]*pluginsdk.Schema { From e729ac0a0f92ca6a42900616b9db796a5d7042e0 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:15:07 -0500 Subject: [PATCH 30/50] add validation to name prop --- ...stgresql_flexible_server_virtual_endpoint_resource.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index a2fadce166df..88db4a993023 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -47,10 +47,11 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Attributes() map[string func (r PostgresqlFlexibleServerVirtualEndpointResource) Arguments() map[string]*pluginsdk.Schema { return map[string]*pluginsdk.Schema{ "name": { - Type: pluginsdk.TypeString, - Description: "The name of the Virtual Endpoint", - ForceNew: true, - Required: true, + Type: pluginsdk.TypeString, + Description: "The name of the Virtual Endpoint", + ForceNew: true, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, }, "source_server_id": { Type: pluginsdk.TypeString, From 47c043914954c67e4c0bff8e9402704c979f7ef9 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:15:24 -0500 Subject: [PATCH 31/50] reorder services --- internal/provider/services.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/internal/provider/services.go b/internal/provider/services.go index c4d232b31944..4505a786a387 100644 --- a/internal/provider/services.go +++ b/internal/provider/services.go @@ -235,7 +235,6 @@ func SupportedUntypedServices() []sdk.UntypedServiceRegistration { analysisservices.Registration{}, apimanagement.Registration{}, appconfiguration.Registration{}, - springcloud.Registration{}, applicationinsights.Registration{}, arckubernetes.Registration{}, attestation.Registration{}, @@ -251,16 +250,16 @@ func SupportedUntypedServices() []sdk.UntypedServiceRegistration { compute.Registration{}, confidentialledger.Registration{}, connections.Registration{}, - containers.Registration{}, consumption.Registration{}, + containers.Registration{}, cosmos.Registration{}, customproviders.Registration{}, dashboard.Registration{}, + databasemigration.Registration{}, + databoxedge.Registration{}, databricks.Registration{}, datadog.Registration{}, datafactory.Registration{}, - databasemigration.Registration{}, - databoxedge.Registration{}, dataprotection.Registration{}, datashare.Registration{}, desktopvirtualization.Registration{}, @@ -273,15 +272,16 @@ func SupportedUntypedServices() []sdk.UntypedServiceRegistration { eventhub.Registration{}, firewall.Registration{}, frontdoor.Registration{}, - storagecache.Registration{}, - hsm.Registration{}, hdinsight.Registration{}, healthcare.Registration{}, - iothub.Registration{}, + hsm.Registration{}, iotcentral.Registration{}, + iothub.Registration{}, + iottimeseriesinsights.Registration{}, keyvault.Registration{}, kusto.Registration{}, legacy.Registration{}, + lighthouse.Registration{}, loadbalancer.Registration{}, loganalytics.Registration{}, logic.Registration{}, @@ -289,15 +289,14 @@ func SupportedUntypedServices() []sdk.UntypedServiceRegistration { machinelearning.Registration{}, maintenance.Registration{}, managedapplications.Registration{}, - lighthouse.Registration{}, - managementgroup.Registration{}, managedhsm.Registration{}, + managedidentity.Registration{}, + managementgroup.Registration{}, maps.Registration{}, mariadb.Registration{}, media.Registration{}, mixedreality.Registration{}, monitor.Registration{}, - managedidentity.Registration{}, mssql.Registration{}, mssqlmanagedinstance.Registration{}, mysql.Registration{}, @@ -321,12 +320,13 @@ func SupportedUntypedServices() []sdk.UntypedServiceRegistration { servicebus.Registration{}, servicefabric.Registration{}, signalr.Registration{}, + springcloud.Registration{}, sql.Registration{}, storage.Registration{}, + storagecache.Registration{}, streamanalytics.Registration{}, subscription.Registration{}, synapse.Registration{}, - iottimeseriesinsights.Registration{}, trafficmanager.Registration{}, videoanalyzer.Registration{}, vmware.Registration{}, From d26fe7fd2154528805e6b55471c0c7ba6591d466 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:15:41 -0500 Subject: [PATCH 32/50] remove test provider feature --- ...gresql_flexible_server_virtual_endpoint_resource_test.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index 88b2e52a9a83..b625f988e246 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -72,11 +72,7 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Destroy(ctx context.Con func (PostgresqlFlexibleServerVirtualEndpointResource) basic(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { - features { - resource_group { - prevent_deletion_if_contains_resources = false - } - } + features {} } resource "azurerm_resource_group" "test" { From 3e0556344332704d777c9f786431f62cc936ca9f Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:16:13 -0500 Subject: [PATCH 33/50] Update website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown Capitalization Co-authored-by: stephybun --- ...rm_postgresql_flexible_server_virtual_endpoint.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown b/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown index 2381eae15929..f0f4c0889c1d 100644 --- a/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown +++ b/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown @@ -3,7 +3,7 @@ subcategory: "Database" layout: "azurerm" page_title: "Azure Resource Manager: azurerm_postgresql_flexible_server_virtual_endpoint" description: |- - Manages a Virtual Endpoint on a PostgreSQL Flexible server + Manages a Virtual Endpoint on a PostgreSQL Flexible Server --- # azurerm_postgresql_flexible_server_virtual_endpoint From 5705d35a34f62f7558aa96e615427405164aa1ab Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:16:29 -0500 Subject: [PATCH 34/50] Update website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown use standardized import example Co-authored-by: stephybun --- ...rm_postgresql_flexible_server_virtual_endpoint.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown b/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown index f0f4c0889c1d..4347c27e1a04 100644 --- a/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown +++ b/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown @@ -89,5 +89,5 @@ The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/l A PostgreSQL Flexible Virtual Endpoint can be imported using the `resource id`, e.g. ```shell -terraform import azurerm_postgresql_flexible_server_virtual_endpoint.example subscriptions//resourceGroups//providers/Microsoft.DBForPostgreSql/flexibleServers//virtualendpoints/ +terraform import azurerm_postgresql_flexible_server_virtual_endpoint.example subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.DBForPostgreSql/flexibleServers/server1/virtualendpoints/endpoint1 ``` From 46ae63449b7c2a22ed049087acd4546f6b47269b Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:17:07 -0500 Subject: [PATCH 35/50] Update internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go Use standard error message Co-authored-by: stephybun --- ...postgresql_flexible_server_virtual_endpoint_resource_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index b625f988e246..691dfb596e28 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -63,7 +63,7 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Destroy(ctx context.Con } if err := postgres.DeletePostgresFlexibileServerVirtualEndpoint(ctx, client.Postgres.VirtualEndpointClient, id); err != nil { - return nil, fmt.Errorf("deleting Postgresql Virtual Endpoint (%s): %+v", id.String(), err) + return nil, fmt.Errorf("deleting %s: %+v", id, err) } return utils.Bool(true), nil From c1f312778b3d1fb73a71d7984fd56b7e0620cbe2 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:17:52 -0500 Subject: [PATCH 36/50] Update internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go use standard pointer conversion Co-authored-by: stephybun --- .../postgresql_flexible_server_virtual_endpoint_resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 88db4a993023..abaad713cd8a 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -108,7 +108,7 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Create() sdk.ResourceFu if err = client.CreateThenPoll(ctx, id, virtualendpoints.VirtualEndpointResource{ Name: &virtualEndpoint.Name, Properties: &virtualendpoints.VirtualEndpointResourceProperties{ - EndpointType: (*virtualendpoints.VirtualEndpointType)(&virtualEndpoint.Type), + EndpointType: pointer.To(virtualendpoints.VirtualEndpointType(virtualEndpoint.Type)), Members: &[]string{replicaServerId.FlexibleServerName}, }, }); err != nil { From 34ff00fca4590d85156667be47033b6fe0a7f74f Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:19:00 -0500 Subject: [PATCH 37/50] add pointer import --- .../postgresql_flexible_server_virtual_endpoint_resource.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index abaad713cd8a..939219b0fe48 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -6,6 +6,7 @@ import ( "log" "time" + "github.com/hashicorp/go-azure-helpers/lang/pointer" "github.com/hashicorp/go-azure-helpers/lang/response" "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/servers" "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints" From 43a63208061bd8288c3a6314b8d3452eeff901a7 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:20:08 -0500 Subject: [PATCH 38/50] Update internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go properly exit Delete method Co-authored-by: stephybun --- .../postgresql_flexible_server_virtual_endpoint_resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 939219b0fe48..72d1c72b8428 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -194,7 +194,7 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Delete() sdk.ResourceFu return err } - return metadata.MarkAsGone(id) // is this correct?? + return nil }, } } From a21a38a906b064175808cfd0057ccbb436f5eee1 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:21:14 -0500 Subject: [PATCH 39/50] Update internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go use standard error message Co-authored-by: stephybun --- ...postgresql_flexible_server_virtual_endpoint_resource_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index 691dfb596e28..6e3834287d2f 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -50,7 +50,7 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Exists(ctx context.Cont resp, err := clients.Postgres.VirtualEndpointClient.Get(ctx, *id) if err != nil { - return nil, fmt.Errorf("reading Postgresql Virtual Endpoint (%s): %+v", id.String(), err) + return nil, fmt.Errorf("retrieving %s: %+v", id, err) } return utils.Bool(resp.Model != nil && resp.Model.Properties != nil && resp.Model.Properties.Members != nil), nil From 8a274a88eaa98994050502f1324200a853af5109 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:21:40 -0500 Subject: [PATCH 40/50] Update internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go use standard pointer conversion Co-authored-by: stephybun --- .../postgresql_flexible_server_virtual_endpoint_resource.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 72d1c72b8428..f129f0c365c4 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -225,8 +225,8 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Update() sdk.ResourceFu if err := client.UpdateThenPoll(ctx, *id, virtualendpoints.VirtualEndpointResourceForPatch{ Properties: &virtualendpoints.VirtualEndpointResourceProperties{ - EndpointType: (*virtualendpoints.VirtualEndpointType)(&virtualEndpoint.Type), - Members: &[]string{replicaServerId.FlexibleServerName}, + EndpointType: pointer.To(virtualendpoints.VirtualEndpointType(virtualEndpoint.Type)), + Members: pointer.To([]string{replicaServerId.FlexibleServerName}), }, }); err != nil { return fmt.Errorf("updating %q: %+v", id, err) From a53f6393c79a1a307f9fc5868ad07100ec1f1ed0 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:23:55 -0500 Subject: [PATCH 41/50] Update internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go use more explicit pointer conversion on return type Co-authored-by: stephybun --- ...postgresql_flexible_server_virtual_endpoint_resource_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index 6e3834287d2f..9dce8e3eca77 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -53,7 +53,7 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Exists(ctx context.Cont return nil, fmt.Errorf("retrieving %s: %+v", id, err) } - return utils.Bool(resp.Model != nil && resp.Model.Properties != nil && resp.Model.Properties.Members != nil), nil + return pointer.To(resp.Model != nil && resp.Model.Properties != nil && resp.Model.Properties.Members != nil), nil } func (r PostgresqlFlexibleServerVirtualEndpointResource) Destroy(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { From db0169843e606bf3599c0a4110e71cfd1f1cd155 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:26:53 -0500 Subject: [PATCH 42/50] Update internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go Use consistent styling Co-authored-by: stephybun --- ...exible_server_virtual_endpoint_resource.go | 44 +++++++------------ 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index f129f0c365c4..0be06ea8caf9 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -127,7 +127,7 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Read() sdk.ResourceFunc return sdk.ResourceFunc{ Timeout: 5 * time.Minute, Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { - client := metadata.Client.Postgres.VirtualEndpointClient +state := PostgresqlFlexibleServerVirtualEndpointModel{} id, err := virtualendpoints.ParseVirtualEndpointID(metadata.ResourceData.Id()) if err != nil { @@ -137,41 +137,31 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Read() sdk.ResourceFunc resp, err := client.Get(ctx, *id) if err != nil { if response.WasNotFound(resp.HttpResponse) { - log.Printf("[INFO] Postgresql Flexible Server Endpoint %q does not exist - removing from state", metadata.ResourceData.Id()) + log.Printf("[INFO] %s does not exist - removing from state", metadata.ResourceData.Id()) return metadata.MarkAsGone(id) } return fmt.Errorf("retrieving %s: %+v", id, err) } - model := resp.Model - if model == nil { - return fmt.Errorf("retrieving %s: model was nil", id) - } + state.Name = id.VirtualEndpointName + + if model := resp.Model; model != nil { + if props := model.Properties; props != nil { + state.Type = string(pointer.From(props.EndpointType)) - properties := resp.Model.Properties - if properties == nil { - return fmt.Errorf("retrieving %s: properties were nil", id) - } - - virtualEndpoint := PostgresqlFlexibleServerVirtualEndpointModel{ - Name: *model.Name, - } + if props.Members == nil || len(*props.Members) != 2 { + // if members list is nil, this is an endpoint that was previously deleted + log.Printf("[INFO] Postgresql Flexible Server Endpoint %q was previously deleted - removing from state", id.ID()) + return metadata.MarkAsGone(id) + } - if properties.EndpointType != nil { - virtualEndpoint.Type = string(*model.Properties.EndpointType) - } - - if properties.Members == nil || len(*properties.Members) != 2 { - // if members list is nil, this is an endpoint that was previously deleted - log.Printf("[INFO] Postgresql Flexible Server Endpoint %q was previously deleted - removing from state", id.ID()) - return metadata.MarkAsGone(id) + // Model.Properties.Members should be a tuple => [source_server, replication_server] + state.SourceServerId = servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[0]).ID() + state.ReplicaServerId = servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[1]).ID() + } } - // Model.Properties.Members should be a tuple => [source_server, replication_server] - virtualEndpoint.SourceServerId = servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[0]).ID() - virtualEndpoint.ReplicaServerId = servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*resp.Model.Properties.Members)[1]).ID() - - return metadata.Encode(&virtualEndpoint) + return metadata.Encode(&state) }, } } From 7124d5d5b524ee87be8c99008766670a29c47614 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:27:48 -0500 Subject: [PATCH 43/50] fixup --- .../postgresql_flexible_server_virtual_endpoint_resource.go | 5 +++-- ...tgresql_flexible_server_virtual_endpoint_resource_test.go | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 0be06ea8caf9..7fd1a39b56b9 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -127,7 +127,8 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Read() sdk.ResourceFunc return sdk.ResourceFunc{ Timeout: 5 * time.Minute, Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { -state := PostgresqlFlexibleServerVirtualEndpointModel{} + client := metadata.Client.Postgres.VirtualEndpointClient + state := PostgresqlFlexibleServerVirtualEndpointModel{} id, err := virtualendpoints.ParseVirtualEndpointID(metadata.ResourceData.Id()) if err != nil { @@ -144,7 +145,7 @@ state := PostgresqlFlexibleServerVirtualEndpointModel{} } state.Name = id.VirtualEndpointName - + if model := resp.Model; model != nil { if props := model.Properties; props != nil { state.Type = string(pointer.From(props.EndpointType)) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index 9dce8e3eca77..68f5a1bcd585 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -8,6 +8,7 @@ import ( "fmt" "testing" + "github.com/hashicorp/go-azure-helpers/lang/pointer" "github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2023-06-01-preview/virtualendpoints" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" From b18031998cfc71c482a1f0393c4f5399917a1d1c Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:28:31 -0500 Subject: [PATCH 44/50] Update internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go remove unnecessary test conditions Co-authored-by: stephybun --- ...stgresql_flexible_server_virtual_endpoint_resource_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index 68f5a1bcd585..7156c84207b2 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -29,10 +29,6 @@ func TestAccPostgresqlFlexibleServerVirtualEndpoint_basic(t *testing.T) { Config: r.basic(data), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), - check.That(data.ResourceName).Key("name").Exists(), - check.That(data.ResourceName).Key("source_server_id").Exists(), - check.That(data.ResourceName).Key("replica_server_id").Exists(), - check.That(data.ResourceName).Key("type").Exists(), ), }, data.ImportStep("type"), From 0045f4f2f1c0e29ae2a30bbd52204444abff76a0 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 09:54:59 -0500 Subject: [PATCH 45/50] fix fmt --- ...stgresql_flexible_server_virtual_endpoint_resource_poller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go b/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go index b3818fb87d61..25315a3031eb 100644 --- a/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go +++ b/internal/services/postgres/custompollers/postgresql_flexible_server_virtual_endpoint_resource_poller.go @@ -66,6 +66,6 @@ func (p postgresFlexibleServerVirtualEndpointPoller) Poll(ctx context.Context) ( HttpResponse: &client.Response{ Response: resp.HttpResponse, }, - Message: fmt.Sprintln("failed to delete %s", p.id), + Message: fmt.Sprintf("failed to delete %s", p.id), } } From 6552b4ca5443eaff88d4898dc4c501109dc2e55f Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 15:01:36 -0500 Subject: [PATCH 46/50] add test for update --- ...e_server_virtual_endpoint_resource_test.go | 90 ++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index 7156c84207b2..ab019149a6cc 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -31,7 +31,7 @@ func TestAccPostgresqlFlexibleServerVirtualEndpoint_basic(t *testing.T) { check.That(data.ResourceName).ExistsInAzure(r), ), }, - data.ImportStep("type"), + data.ImportStep(), data.DisappearsStep(acceptance.DisappearsStepData{ Config: r.basic, TestResource: r, @@ -39,6 +39,28 @@ func TestAccPostgresqlFlexibleServerVirtualEndpoint_basic(t *testing.T) { }) } +func TestAccPostgresqlFlexibleServerVirtualEndpoint_update(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_virtual_endpoint", "test") + r := PostgresqlFlexibleServerVirtualEndpointResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.update(data, "azurerm_postgresql_flexible_server.test_replica.id"), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.update(data, "azurerm_postgresql_flexible_server.test_replica_2.id"), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func (r PostgresqlFlexibleServerVirtualEndpointResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := virtualendpoints.ParseVirtualEndpointID(state.ID) if err != nil { @@ -112,3 +134,69 @@ resource "azurerm_postgresql_flexible_server_virtual_endpoint" "test" { } `, data.RandomInteger, "eastus") // force region due to SKU constraints } + +func (PostgresqlFlexibleServerVirtualEndpointResource) update(data acceptance.TestData, replicaId string) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctest-psql-virtualendpoint-rg-%[1]d" + location = "%[2]s" +} + +resource "azurerm_postgresql_flexible_server" "test" { + name = "acctest-psql-virtualendpoint-primary-%[1]d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + version = "16" + public_network_access_enabled = false + administrator_login = "psqladmin" + administrator_password = "H@Sh1CoR3!" + zone = "1" + storage_mb = 32768 + storage_tier = "P30" + sku_name = "GP_Standard_D2ads_v5" +} + +resource "azurerm_postgresql_flexible_server" "test_replica" { + name = "acctest-psql-virtualendpoint-replica-%[1]d" + resource_group_name = azurerm_postgresql_flexible_server.test.resource_group_name + location = azurerm_postgresql_flexible_server.test.location + create_mode = "Replica" + source_server_id = azurerm_postgresql_flexible_server.test.id + version = azurerm_postgresql_flexible_server.test.version + public_network_access_enabled = azurerm_postgresql_flexible_server.test.public_network_access_enabled + zone = azurerm_postgresql_flexible_server.test.zone + storage_mb = azurerm_postgresql_flexible_server.test.storage_mb + storage_tier = azurerm_postgresql_flexible_server.test.storage_tier +} + +resource "azurerm_postgresql_flexible_server" "test_replica_2" { + name = "acctest-psql-virtualendpoint-replica-%[1]d-2" + resource_group_name = azurerm_postgresql_flexible_server.test.resource_group_name + location = azurerm_postgresql_flexible_server.test.location + create_mode = "Replica" + source_server_id = azurerm_postgresql_flexible_server.test.id + version = azurerm_postgresql_flexible_server.test.version + public_network_access_enabled = azurerm_postgresql_flexible_server.test.public_network_access_enabled + zone = azurerm_postgresql_flexible_server.test.zone + storage_mb = azurerm_postgresql_flexible_server.test.storage_mb + storage_tier = azurerm_postgresql_flexible_server.test.storage_tier + + ## this prevents a race condition that can occur in the test + depends_on = [azurerm_postgresql_flexible_server.test_replica] +} + +resource "azurerm_postgresql_flexible_server_virtual_endpoint" "test" { + name = "acctest-psqlvirtualendpoint-endpoint-%[1]d" + source_server_id = azurerm_postgresql_flexible_server.test.id + replica_server_id = %[3]s + type = "ReadWrite" + + ## this prevents a race condition that can occur in the test + depends_on = [azurerm_postgresql_flexible_server.test_replica, azurerm_postgresql_flexible_server.test_replica_2] +} +`, data.RandomInteger, "eastus", replicaId) // force region due to SKU constraints +} From 5b8adeb5ab97590b3c37179deaccb1e994423802 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 1 Aug 2024 15:35:24 -0500 Subject: [PATCH 47/50] slightly tweak test for readability --- ...ible_server_virtual_endpoint_resource_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index ab019149a6cc..b98c502df486 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -45,14 +45,14 @@ func TestAccPostgresqlFlexibleServerVirtualEndpoint_update(t *testing.T) { data.ResourceTest(t, r, []acceptance.TestStep{ { - Config: r.update(data, "azurerm_postgresql_flexible_server.test_replica.id"), + Config: r.update(data, "azurerm_postgresql_flexible_server.test_replica_0.id"), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), }, data.ImportStep(), { - Config: r.update(data, "azurerm_postgresql_flexible_server.test_replica_2.id"), + Config: r.update(data, "azurerm_postgresql_flexible_server.test_replica_1.id"), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), @@ -160,8 +160,8 @@ resource "azurerm_postgresql_flexible_server" "test" { sku_name = "GP_Standard_D2ads_v5" } -resource "azurerm_postgresql_flexible_server" "test_replica" { - name = "acctest-psql-virtualendpoint-replica-%[1]d" +resource "azurerm_postgresql_flexible_server" "test_replica_0" { + name = "${azurerm_postgresql_flexible_server.test.name}-replica-0" resource_group_name = azurerm_postgresql_flexible_server.test.resource_group_name location = azurerm_postgresql_flexible_server.test.location create_mode = "Replica" @@ -173,8 +173,8 @@ resource "azurerm_postgresql_flexible_server" "test_replica" { storage_tier = azurerm_postgresql_flexible_server.test.storage_tier } -resource "azurerm_postgresql_flexible_server" "test_replica_2" { - name = "acctest-psql-virtualendpoint-replica-%[1]d-2" +resource "azurerm_postgresql_flexible_server" "test_replica_1" { + name = "${azurerm_postgresql_flexible_server.test.name}-replica-1" resource_group_name = azurerm_postgresql_flexible_server.test.resource_group_name location = azurerm_postgresql_flexible_server.test.location create_mode = "Replica" @@ -186,7 +186,7 @@ resource "azurerm_postgresql_flexible_server" "test_replica_2" { storage_tier = azurerm_postgresql_flexible_server.test.storage_tier ## this prevents a race condition that can occur in the test - depends_on = [azurerm_postgresql_flexible_server.test_replica] + depends_on = [azurerm_postgresql_flexible_server.test_replica_0] } resource "azurerm_postgresql_flexible_server_virtual_endpoint" "test" { @@ -196,7 +196,7 @@ resource "azurerm_postgresql_flexible_server_virtual_endpoint" "test" { type = "ReadWrite" ## this prevents a race condition that can occur in the test - depends_on = [azurerm_postgresql_flexible_server.test_replica, azurerm_postgresql_flexible_server.test_replica_2] + depends_on = [azurerm_postgresql_flexible_server.test_replica_0, azurerm_postgresql_flexible_server.test_replica_1] } `, data.RandomInteger, "eastus", replicaId) // force region due to SKU constraints } From b72af1f49abd35e25675f2abe954c5f7c7be54aa Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Wed, 7 Aug 2024 09:40:47 -0500 Subject: [PATCH 48/50] rename docs page, add note --- ...> postgresql_flexible_server_virtual_endpoint.html.markdown} | 2 ++ 1 file changed, 2 insertions(+) rename website/docs/r/{azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown => postgresql_flexible_server_virtual_endpoint.html.markdown} (92%) diff --git a/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown b/website/docs/r/postgresql_flexible_server_virtual_endpoint.html.markdown similarity index 92% rename from website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown rename to website/docs/r/postgresql_flexible_server_virtual_endpoint.html.markdown index 4347c27e1a04..0b99bd2984ef 100644 --- a/website/docs/r/azurerm_postgresql_flexible_server_virtual_endpoint.html.markdown +++ b/website/docs/r/postgresql_flexible_server_virtual_endpoint.html.markdown @@ -57,6 +57,8 @@ resource "azurerm_postgresql_flexible_server_virtual_endpoint" "example" { } ``` +-> **Note:** If creating multiple replicas, an error can occur if virtual endpoints are created before all replicas have been completed. To avoid this error, use a `depends_on` property on `azurerm_postgresql_flexible_server_virtual_endpoint` that references all Postgres Flexible Server Replicas. + ## Argument Reference The following arguments are supported: From c1c6a8bef28a90d09704f7cdb7e2339a2b6c5937 Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Wed, 7 Aug 2024 09:41:03 -0500 Subject: [PATCH 49/50] shorten resource names to avoid errors --- ...le_server_virtual_endpoint_resource_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index b98c502df486..9b48f7bc1d6e 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -95,12 +95,12 @@ provider "azurerm" { } resource "azurerm_resource_group" "test" { - name = "acctest-psql-virtualendpoint-rg-%[1]d" + name = "acctest-ve-rg-%[1]d" location = "%[2]s" } resource "azurerm_postgresql_flexible_server" "test" { - name = "acctest-psql-virtualendpoint-primary-%[1]d" + name = "acctest-ve-primary-%[1]d" resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location version = "16" @@ -114,7 +114,7 @@ resource "azurerm_postgresql_flexible_server" "test" { } resource "azurerm_postgresql_flexible_server" "test_replica" { - name = "acctest-psql-virtualendpoint-replica-%[1]d" + name = "acctest-ve-replica-%[1]d" resource_group_name = azurerm_postgresql_flexible_server.test.resource_group_name location = azurerm_postgresql_flexible_server.test.location create_mode = "Replica" @@ -127,7 +127,7 @@ resource "azurerm_postgresql_flexible_server" "test_replica" { } resource "azurerm_postgresql_flexible_server_virtual_endpoint" "test" { - name = "acctest-psqlvirtualendpoint-endpoint-%[1]d" + name = "acctest-ve-%[1]d" source_server_id = azurerm_postgresql_flexible_server.test.id replica_server_id = azurerm_postgresql_flexible_server.test_replica.id type = "ReadWrite" @@ -142,12 +142,12 @@ provider "azurerm" { } resource "azurerm_resource_group" "test" { - name = "acctest-psql-virtualendpoint-rg-%[1]d" + name = "acctest-ve-rg-%[1]d" location = "%[2]s" } resource "azurerm_postgresql_flexible_server" "test" { - name = "acctest-psql-virtualendpoint-primary-%[1]d" + name = "acctest-ve-primary-%[1]d" resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location version = "16" @@ -185,17 +185,17 @@ resource "azurerm_postgresql_flexible_server" "test_replica_1" { storage_mb = azurerm_postgresql_flexible_server.test.storage_mb storage_tier = azurerm_postgresql_flexible_server.test.storage_tier - ## this prevents a race condition that can occur in the test + ## this prevents a race condition that can occur when 2 replicas are created simultaneously depends_on = [azurerm_postgresql_flexible_server.test_replica_0] } resource "azurerm_postgresql_flexible_server_virtual_endpoint" "test" { - name = "acctest-psqlvirtualendpoint-endpoint-%[1]d" + name = "acctest-ve-%[1]d" source_server_id = azurerm_postgresql_flexible_server.test.id replica_server_id = %[3]s type = "ReadWrite" - ## this prevents a race condition that can occur in the test + ## this prevents a race condition that can occur if the virtual endpoint is created while a replica is still initializing depends_on = [azurerm_postgresql_flexible_server.test_replica_0, azurerm_postgresql_flexible_server.test_replica_1] } `, data.RandomInteger, "eastus", replicaId) // force region due to SKU constraints From 1e03cf70bffe5018ddbf5afc394c7e0695792c7a Mon Sep 17 00:00:00 2001 From: Bruce Harrison Date: Thu, 8 Aug 2024 09:28:57 -0500 Subject: [PATCH 50/50] Update internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go Co-authored-by: stephybun --- .../postgresql_flexible_server_virtual_endpoint_resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 7fd1a39b56b9..104bacb7fcbc 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -79,7 +79,7 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Arguments() map[string] func (r PostgresqlFlexibleServerVirtualEndpointResource) Create() sdk.ResourceFunc { return sdk.ResourceFunc{ - Timeout: 5 * time.Minute, + Timeout: 10 * time.Minute, Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { var virtualEndpoint PostgresqlFlexibleServerVirtualEndpointModel