Skip to content
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_palo_alto_next_generation_firewall_virtual_network_local_rulestack: add lock of ruleStackID in creating #23601

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/paloaltonetworks/2022-08-29/firewalls"
"github.com/hashicorp/go-azure-sdk/resource-manager/paloaltonetworks/2022-08-29/localrulestacks"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/paloalto/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/paloalto/validate"
Expand Down Expand Up @@ -79,7 +80,7 @@ func (r NextGenerationFirewallVHubLocalRuleStackResource) Attributes() map[strin

func (r NextGenerationFirewallVHubLocalRuleStackResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 2 * time.Hour,
Timeout: 3 * time.Hour,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.PaloAlto.Client.Firewalls
localRuleStackClient := metadata.Client.PaloAlto.Client.LocalRulestacks
Expand Down Expand Up @@ -137,6 +138,9 @@ func (r NextGenerationFirewallVHubLocalRuleStackResource) Create() sdk.ResourceF
Tags: tags.Expand(model.Tags),
}

locks.ByID(ruleStackID.ID())
defer locks.UnlockByID(ruleStackID.ID())

if err = client.CreateOrUpdateThenPoll(ctx, id, firewall); err != nil {
return err
}
Expand Down Expand Up @@ -218,7 +222,7 @@ func (r NextGenerationFirewallVHubLocalRuleStackResource) Delete() sdk.ResourceF

func (r NextGenerationFirewallVHubLocalRuleStackResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 2 * time.Hour,
Timeout: 3 * time.Hour,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.PaloAlto.Client.Firewalls

Expand Down Expand Up @@ -276,6 +280,15 @@ func (r NextGenerationFirewallVHubLocalRuleStackResource) Update() sdk.ResourceF
firewall.Tags = tags.Expand(model.Tags)
}

if metadata.ResourceData.HasChange("rulestack_id") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we be locking when rulestack_id has not changed too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I'm not sure about this because it seems to be related to the API implementation. But intuitively, we only need lock when rulestack_id changes as it's only a reference to the rulestack resource.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could add the locks after line 251 then to avoid checking if it's changed and parsing it twice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have ever thought about it before, but now moved the locks after L251.

ruleStackID, err := localrulestacks.ParseLocalRulestackID(model.RuleStackId)
if err != nil {
return fmt.Errorf("parsing rulestack_id %s: %+v", model.RuleStackId, err)
}
locks.ByID(ruleStackID.ID())
defer locks.UnlockByID(ruleStackID.ID())
}

if err = client.CreateOrUpdateThenPoll(ctx, *id, firewall); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/paloaltonetworks/2022-08-29/firewalls"
"github.com/hashicorp/go-azure-sdk/resource-manager/paloaltonetworks/2022-08-29/localrulestacks"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/paloalto/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/paloalto/validate"
Expand Down Expand Up @@ -74,7 +75,7 @@ func (r NextGenerationFirewallVNetLocalRulestackResource) ResourceType() string

func (r NextGenerationFirewallVNetLocalRulestackResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 2 * time.Hour,
Timeout: 3 * time.Hour,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.PaloAlto.Client.Firewalls
localRulestackClient := metadata.Client.PaloAlto.Client.LocalRulestacks
Expand Down Expand Up @@ -131,6 +132,9 @@ func (r NextGenerationFirewallVNetLocalRulestackResource) Create() sdk.ResourceF
Tags: tags.Expand(model.Tags),
}

locks.ByID(ruleStackID.ID())
defer locks.UnlockByID(ruleStackID.ID())

if err = client.CreateOrUpdateThenPoll(ctx, id, firewall); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}
Expand Down Expand Up @@ -212,7 +216,7 @@ func (r NextGenerationFirewallVNetLocalRulestackResource) IDValidationFunc() plu

func (r NextGenerationFirewallVNetLocalRulestackResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 2 * time.Hour,
Timeout: 3 * time.Hour,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.PaloAlto.Client.Firewalls

Expand Down Expand Up @@ -271,6 +275,15 @@ func (r NextGenerationFirewallVNetLocalRulestackResource) Update() sdk.ResourceF
firewall.Tags = tags.Expand(model.Tags)
}

if metadata.ResourceData.HasChange("rulestack_id") {
ruleStackID, err := localrulestacks.ParseLocalRulestackID(model.RuleStackId)
if err != nil {
return fmt.Errorf("parsing rulestack_id %s: %+v", model.RuleStackId, err)
}
locks.ByID(ruleStackID.ID())
defer locks.UnlockByID(ruleStackID.ID())
}

if err = client.CreateOrUpdateThenPoll(ctx, *id, firewall); err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ In addition to the Arguments listed above - the following Attributes are exporte

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:

* `create` - (Defaults to 2 hours) Used when creating the Palo Alto Next Generation Firewall VHub Local Rulestack.
* `create` - (Defaults to 3 hours) Used when creating the Palo Alto Next Generation Firewall VHub Local Rulestack.
* `read` - (Defaults to 5 minutes) Used when retrieving the Palo Alto Next Generation Firewall VHub Local Rulestack.
* `update` - (Defaults to 2 hours) Used when updating the Palo Alto Next Generation Firewall VHub Local Rulestack.
* `update` - (Defaults to 3 hours) Used when updating the Palo Alto Next Generation Firewall VHub Local Rulestack.
* `delete` - (Defaults to 2 hours) Used when deleting the Palo Alto Next Generation Firewall VHub Local Rulestack.

## Import
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ In addition to the Arguments listed above - the following Attributes are exporte

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:

* `create` - (Defaults to 2 hours) Used when creating the Palo Alto Next Generation Firewall Virtual Network Local Rulestack.
* `create` - (Defaults to 3 hours) Used when creating the Palo Alto Next Generation Firewall Virtual Network Local Rulestack.
* `read` - (Defaults to 5 minutes) Used when retrieving the Palo Alto Next Generation Firewall Virtual Network Local Rulestack.
* `update` - (Defaults to 2 hours) Used when updating the Palo Alto Next Generation Firewall Virtual Network Local Rulestack.
* `update` - (Defaults to 3 hours) Used when updating the Palo Alto Next Generation Firewall Virtual Network Local Rulestack.
* `delete` - (Defaults to 2 hours) Used when deleting the Palo Alto Next Generation Firewall Virtual Network Local Rulestack.

## Import
Expand Down