-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
azurerm_private_endpoint
Try to add retry on creation
#16315
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a9ee7fa
try to add retry on creation
lonegunmanb 651a545
retry on update and poll
lonegunmanb d2775db
add lock on subnet id on create/update/delete, change retryable error…
lonegunmanb 3f1569a
Fix tf lint issue
lonegunmanb 5186632
Fix private link service id error when use alias by adding retry
lonegunmanb 7a48ddb
Add update to test. Convert if-elseif to switch to finx golint issue
lonegunmanb c856eaa
Fix fmt issue
lonegunmanb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,6 +14,7 @@ import ( | |||||
"github.com/hashicorp/go-azure-sdk/resource-manager/postgresql/2017-12-01/servers" | ||||||
"github.com/hashicorp/go-azure-sdk/resource-manager/privatedns/2018-09-01/privatezones" | ||||||
"github.com/hashicorp/go-azure-sdk/resource-manager/signalr/2022-02-01/signalr" | ||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||||||
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure" | ||||||
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf" | ||||||
"github.com/hashicorp/terraform-provider-azurerm/internal/clients" | ||||||
|
@@ -282,24 +283,61 @@ func resourcePrivateEndpointCreate(d *pluginsdk.ResourceData, meta interface{}) | |||||
Tags: tags.Expand(d.Get("tags").(map[string]interface{})), | ||||||
} | ||||||
|
||||||
err = validatePrivateLinkServiceId(*parameters.PrivateEndpointProperties.PrivateLinkServiceConnections) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
err = validatePrivateLinkServiceId(*parameters.PrivateEndpointProperties.ManualPrivateLinkServiceConnections) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
cosmosDbResIds := getCosmosDbResIdInPrivateServiceConnections(parameters.PrivateEndpointProperties) | ||||||
for _, cosmosDbResId := range cosmosDbResIds { | ||||||
log.Printf("[DEBUG] Add Lock For Private Endpoint %q, lock name: %q", id.Name, cosmosDbResId) | ||||||
locks.ByName(cosmosDbResId, "azurerm_private_endpoint") | ||||||
//goland:noinspection GoDeferInLoop | ||||||
defer locks.UnlockByName(cosmosDbResId, "azurerm_private_endpoint") | ||||||
} | ||||||
locks.ByName(subnetId, "azurerm_private_endpoint") | ||||||
defer locks.UnlockByName(subnetId, "azurerm_private_endpoint") | ||||||
|
||||||
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, parameters) | ||||||
if err != nil { | ||||||
if strings.EqualFold(err.Error(), "is missing required parameter 'group Id'") { | ||||||
return fmt.Errorf("creating Private Endpoint %q (Resource Group %q) due to missing 'group Id', ensure that the 'subresource_names' type is populated: %+v", id.Name, id.ResourceGroup, err) | ||||||
} else { | ||||||
return fmt.Errorf("creating Private Endpoint %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||||||
err = pluginsdk.Retry(d.Timeout(pluginsdk.TimeoutCreate), func() *resource.RetryError { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, parameters) | ||||||
if err != nil { | ||||||
switch { | ||||||
case strings.EqualFold(err.Error(), "is missing required parameter 'group Id'"): | ||||||
{ | ||||||
return &resource.RetryError{ | ||||||
Err: fmt.Errorf("creating Private Endpoint %q (Resource Group %q) due to missing 'group Id', ensure that the 'subresource_names' type is populated: %+v", id.Name, id.ResourceGroup, err), | ||||||
Retryable: false, | ||||||
} | ||||||
} | ||||||
case strings.Contains(err.Error(), "PrivateLinkServiceId Invalid private link service id"): | ||||||
{ | ||||||
return &resource.RetryError{ | ||||||
Err: fmt.Errorf("creating Private Endpoint %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err), | ||||||
Retryable: true, | ||||||
} | ||||||
} | ||||||
default: | ||||||
return &resource.RetryError{ | ||||||
Err: fmt.Errorf("creating Private Endpoint %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err), | ||||||
Retryable: false, | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||||||
return fmt.Errorf("waiting for creation of Private Endpoint %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||||||
|
||||||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||||||
return &resource.RetryError{ | ||||||
Err: fmt.Errorf("waiting for creation of Private Endpoint %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err), | ||||||
Retryable: false, | ||||||
} | ||||||
} | ||||||
return nil | ||||||
}) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
d.SetId(id.ID()) | ||||||
|
@@ -317,6 +355,20 @@ func resourcePrivateEndpointCreate(d *pluginsdk.ResourceData, meta interface{}) | |||||
return resourcePrivateEndpointRead(d, meta) | ||||||
} | ||||||
|
||||||
func validatePrivateLinkServiceId(endpoints []network.PrivateLinkServiceConnection) error { | ||||||
for _, connection := range endpoints { | ||||||
_, errors := azure.ValidateResourceID(*connection.PrivateLinkServiceID, "PrivateLinkServiceID") | ||||||
if len(errors) == 0 { | ||||||
continue | ||||||
} | ||||||
_, errors = validate.PrivateConnectionResourceAlias(*connection.PrivateLinkServiceID, "PrivateLinkServiceID") | ||||||
if len(errors) != 0 { | ||||||
return fmt.Errorf("PrivateLinkServiceId Invalid: %q", *connection.PrivateLinkServiceID) | ||||||
} | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
func getCosmosDbResIdInPrivateServiceConnections(p *network.PrivateEndpointProperties) []string { | ||||||
var ids []string | ||||||
exists := make(map[string]struct{}) | ||||||
|
@@ -379,16 +431,53 @@ func resourcePrivateEndpointUpdate(d *pluginsdk.ResourceData, meta interface{}) | |||||
Tags: tags.Expand(d.Get("tags").(map[string]interface{})), | ||||||
} | ||||||
|
||||||
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, parameters) | ||||||
err = validatePrivateLinkServiceId(*parameters.PrivateEndpointProperties.PrivateLinkServiceConnections) | ||||||
if err != nil { | ||||||
if strings.EqualFold(err.Error(), "is missing required parameter 'group Id'") { | ||||||
return fmt.Errorf("updating Private Endpoint %q (Resource Group %q) due to missing 'group Id', ensure that the 'subresource_names' type is populated: %+v", id.Name, id.ResourceGroup, err) | ||||||
} else { | ||||||
return fmt.Errorf("updating Private Endpoint %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||||||
} | ||||||
return err | ||||||
} | ||||||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||||||
return fmt.Errorf("waiting for update of Private Endpoint %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||||||
err = validatePrivateLinkServiceId(*parameters.PrivateEndpointProperties.ManualPrivateLinkServiceConnections) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
locks.ByName(subnetId, "azurerm_private_endpoint") | ||||||
defer locks.UnlockByName(subnetId, "azurerm_private_endpoint") | ||||||
|
||||||
err = pluginsdk.Retry(d.Timeout(pluginsdk.TimeoutCreate), func() *resource.RetryError { | ||||||
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, parameters) | ||||||
if err != nil { | ||||||
switch { | ||||||
case strings.EqualFold(err.Error(), "is missing required parameter 'group Id'"): | ||||||
{ | ||||||
return &resource.RetryError{ | ||||||
Err: fmt.Errorf("updating Private Endpoint %q (Resource Group %q) due to missing 'group Id', ensure that the 'subresource_names' type is populated: %+v", id.Name, id.ResourceGroup, err), | ||||||
Retryable: false, | ||||||
} | ||||||
} | ||||||
case strings.Contains(err.Error(), "PrivateLinkServiceId Invalid private link service id"): | ||||||
{ | ||||||
return &resource.RetryError{ | ||||||
Err: fmt.Errorf("creating Private Endpoint %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err), | ||||||
Retryable: true, | ||||||
} | ||||||
} | ||||||
default: | ||||||
return &resource.RetryError{ | ||||||
Err: fmt.Errorf("updating Private Endpoint %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err), | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||||||
return &resource.RetryError{ | ||||||
Err: fmt.Errorf("waiting for update of Private Endpoint %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err), | ||||||
Retryable: false, | ||||||
} | ||||||
} | ||||||
return nil | ||||||
}) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
// 1 Private Endpoint can have 1 Private DNS Zone Group - so to update we need to Delete & Recreate | ||||||
|
@@ -547,6 +636,7 @@ func resourcePrivateEndpointDelete(d *pluginsdk.ResourceData, meta interface{}) | |||||
} | ||||||
log.Printf("[DEBUG] Deleted the Private DNS Zone Group associated with Private Endpoint %q / Resource Group %q.", id.Name, id.ResourceGroup) | ||||||
|
||||||
subnetId := d.Get("subnet_id").(string) | ||||||
privateServiceConnections := d.Get("private_service_connection").([]interface{}) | ||||||
parameters := network.PrivateEndpoint{ | ||||||
PrivateEndpointProperties: &network.PrivateEndpointProperties{ | ||||||
|
@@ -560,6 +650,8 @@ func resourcePrivateEndpointDelete(d *pluginsdk.ResourceData, meta interface{}) | |||||
//goland:noinspection GoDeferInLoop | ||||||
defer locks.UnlockByName(cosmosDbResId, "azurerm_private_endpoint") | ||||||
} | ||||||
locks.ByName(subnetId, "azurerm_private_endpoint") | ||||||
defer locks.UnlockByName(subnetId, "azurerm_private_endpoint") | ||||||
|
||||||
log.Printf("[DEBUG] Deleting the Private Endpoint %q / Resource Group %q..", id.Name, id.ResourceGroup) | ||||||
future, err := client.Delete(ctx, id.ResourceGroup, id.Name) | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now that we are locking can we revert this and remove the retry? as it shouldn't be required
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy that.