-
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
Add virtual resource association: azurerm_nat_gateway_public_ip #6450
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4824f75
Add association between azurerm_nat_gateway and azurerm_public_ip
fc9b24e
Add virtual resource association: azurerm_nat_gateway_public_ip
e742586
Update code
08ad935
Update code
b3b977c
Update code
31851d2
Update code
db13686
Update code
c1d2d4f
Update code
8e5ca33
Update code
2fc33aa
Update code
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
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package parse | ||
|
||
import ( | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
) | ||
|
||
type NatGatewayId struct { | ||
Name string | ||
ResourceGroup string | ||
} | ||
|
||
func NatGatewayID(input string) (*NatGatewayId, error) { | ||
id, err := azure.ParseAzureResourceID(input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
natGateway := NatGatewayId{ | ||
ResourceGroup: id.ResourceGroup, | ||
} | ||
|
||
if natGateway.Name, err = id.PopSegment("natGateways"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := id.ValidateNoEmptySegments(input); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &natGateway, nil | ||
} |
70 changes: 70 additions & 0 deletions
70
azurerm/internal/services/network/parse/nat_gateway_test.go
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package parse | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestNatGatewayID(t *testing.T) { | ||
testData := []struct { | ||
Name string | ||
Input string | ||
Error bool | ||
Expect *NatGatewayId | ||
}{ | ||
{ | ||
Name: "Empty", | ||
Input: "", | ||
Error: true, | ||
}, | ||
{ | ||
Name: "No Resource Groups Segment", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000", | ||
Error: true, | ||
}, | ||
{ | ||
Name: "No Resource Groups Value", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups", | ||
Error: true, | ||
}, | ||
{ | ||
Name: "Resource Group ID", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1", | ||
Error: true, | ||
}, | ||
{ | ||
Name: "Missing Nat Gateway Value", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/natGateways", | ||
Error: true, | ||
}, | ||
{ | ||
Name: "Nat Gateway ID", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/natGateways/gateway1", | ||
Error: false, | ||
Expect: &NatGatewayId{ | ||
Name: "gateway1", | ||
ResourceGroup: "group1", | ||
}, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Testing %q", v.Name) | ||
|
||
actual, err := NatGatewayID(v.Input) | ||
if err != nil { | ||
if v.Error { | ||
continue | ||
} | ||
|
||
t.Fatalf("Expected a value but got an error: %s", err) | ||
} | ||
|
||
if actual.Name != v.Expect.Name { | ||
t.Fatalf("Expected %q but got %q for Name", v.Expect.Name, actual.Name) | ||
} | ||
|
||
if actual.ResourceGroup != v.Expect.ResourceGroup { | ||
t.Fatalf("Expected %q but got %q for Resource Group", v.Expect.ResourceGroup, actual.ResourceGroup) | ||
} | ||
} | ||
} |
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
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
195 changes: 195 additions & 0 deletions
195
azurerm/internal/services/network/resource_arm_nat_gateway_public_ip_association.go
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 |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package network | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-09-01/network" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network/parse" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network/validate" | ||
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmNatGatewayPublicIpAssociation() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmNatGatewayPublicIpAssociationCreate, | ||
Read: resourceArmNatGatewayPublicIpAssociationRead, | ||
Delete: resourceArmNatGatewayPublicIpAssociationDelete, | ||
|
||
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { | ||
_, err := parse.NatGatewayID(id) | ||
return err | ||
}), | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(30 * time.Minute), | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
Delete: schema.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"nat_gateway_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.NatGatewayID, | ||
}, | ||
|
||
"public_ip_address_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: azure.ValidateResourceID, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmNatGatewayPublicIpAssociationCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Network.NatGatewayClient | ||
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
log.Printf("[INFO] preparing arguments for Nat Gateway <-> Public Ip Association creation.") | ||
natGatewayId := d.Get("nat_gateway_id").(string) | ||
publicIpAddressId := d.Get("public_ip_address_id").(string) | ||
parsedNatGatewayId, err := parse.NatGatewayID(natGatewayId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
locks.ByName(parsedNatGatewayId.Name, natGatewayResourceName) | ||
defer locks.UnlockByName(parsedNatGatewayId.Name, natGatewayResourceName) | ||
|
||
natGateway, err := client.Get(ctx, parsedNatGatewayId.ResourceGroup, parsedNatGatewayId.Name, "") | ||
if err != nil { | ||
if utils.ResponseWasNotFound(natGateway.Response) { | ||
return fmt.Errorf("Nat Gateway %q (Resource Group %q) was not found.", parsedNatGatewayId.Name, parsedNatGatewayId.ResourceGroup) | ||
} | ||
return fmt.Errorf("failed to retrieve Nat Gateway %q (Resource Group %q): %+v", parsedNatGatewayId.Name, parsedNatGatewayId.ResourceGroup, err) | ||
} | ||
|
||
publicIpAddresses := make([]network.SubResource, 0) | ||
if natGateway.PublicIPAddresses != nil { | ||
for _, existingPublicIPAddress := range *natGateway.PublicIPAddresses { | ||
if existingPublicIPAddress.ID != nil { | ||
if *existingPublicIPAddress.ID == publicIpAddressId { | ||
return tf.ImportAsExistsError("azurerm_nat_gateway_public_ip_association", *natGateway.ID) | ||
} | ||
|
||
publicIpAddresses = append(publicIpAddresses, existingPublicIPAddress) | ||
} | ||
} | ||
} | ||
|
||
publicIpAddresses = append(publicIpAddresses, network.SubResource{ | ||
ID: utils.String(publicIpAddressId), | ||
}) | ||
natGateway.PublicIPAddresses = &publicIpAddresses | ||
|
||
future, err := client.CreateOrUpdate(ctx, parsedNatGatewayId.ResourceGroup, parsedNatGatewayId.Name, natGateway) | ||
if err != nil { | ||
return fmt.Errorf("failed to update Public IP Association for Nat Gateway %q (Resource Group %q): %+v", parsedNatGatewayId.Name, parsedNatGatewayId.ResourceGroup, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("failed to wait for completion of Public IP Association for Nat Gateway %q (Resource Group %q): %+v", parsedNatGatewayId.Name, parsedNatGatewayId.ResourceGroup, err) | ||
} | ||
|
||
resp, err := client.Get(ctx, parsedNatGatewayId.ResourceGroup, parsedNatGatewayId.Name, "") | ||
if err != nil { | ||
return fmt.Errorf("failed to retrieve Nat Gateway %q (Resource Group %q): %+v", parsedNatGatewayId.Name, parsedNatGatewayId.ResourceGroup, err) | ||
} | ||
d.SetId(*resp.ID) | ||
|
||
return resourceArmNatGatewayPublicIpAssociationRead(d, meta) | ||
} | ||
|
||
func resourceArmNatGatewayPublicIpAssociationRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Network.NatGatewayClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.NatGatewayID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
natGateway, err := client.Get(ctx, id.ResourceGroup, id.Name, "") | ||
if err != nil { | ||
if utils.ResponseWasNotFound(natGateway.Response) { | ||
log.Printf("[DEBUG] Nat Gateway %q (Resource Group %q) could not be found - removing from state!", id.Name, id.ResourceGroup) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("failed to retrieve Nat Gateway %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||
} | ||
|
||
if natGateway.PublicIPAddresses == nil { | ||
log.Printf("[DEBUG] Nat Gateway %q (Resource Group %q) doesn't have a Public IP - removing from state!", id.Name, id.ResourceGroup) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.Set("nat_gateway_id", natGateway.ID) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmNatGatewayPublicIpAssociationDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Network.NatGatewayClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.NatGatewayID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
publicIpAddressId := d.Get("public_ip_address_id").(string) | ||
|
||
locks.ByName(id.Name, natGatewayResourceName) | ||
defer locks.UnlockByName(id.Name, natGatewayResourceName) | ||
|
||
natGateway, err := client.Get(ctx, id.ResourceGroup, id.Name, "") | ||
if err != nil { | ||
if utils.ResponseWasNotFound(natGateway.Response) { | ||
return fmt.Errorf("Nat Gateway %q (Resource Group %q) was not found.", id.Name, id.ResourceGroup) | ||
} | ||
|
||
return fmt.Errorf("failed to retrieve Nat Gateway %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||
} | ||
|
||
publicIpAddresses := make([]network.SubResource, 0) | ||
if publicIPAddresses := natGateway.PublicIPAddresses; publicIPAddresses != nil { | ||
for _, publicIPAddress := range *publicIPAddresses { | ||
if publicIPAddress.ID == nil { | ||
continue | ||
} | ||
|
||
if *publicIPAddress.ID != publicIpAddressId { | ||
publicIpAddresses = append(publicIpAddresses, publicIPAddress) | ||
} | ||
} | ||
} | ||
natGateway.PublicIPAddresses = &publicIpAddresses | ||
|
||
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, natGateway) | ||
if err != nil { | ||
return fmt.Errorf("failed to remove Public Ip Association for Nat Gateway %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("failed to wait for removal of Public Ip Association for Nat Gateway %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
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.
I think this still makes sense for the data source?
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.
Seems no need to remove. Thanks.Updated