-
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
Data Source: azurerm_virtual_network_peering
- resolve issue (#27486)
#27530
Data Source: azurerm_virtual_network_peering
- resolve issue (#27486)
#27530
Conversation
azurerm_virtual_network_peering
- resolve issue (#27486)
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.
Thanks for this PR @ning-kang!
I see that this is adding a new untyped data source to the provider, which we currently no longer accept into the provider.
It would be greatly appreciated if you could re-write this as a typed data source. Our Contributor Docs have a guide on how to add a new typed data source to the provider to help get you started.
Thank you @stephybun for the review. I have refactored the PR to comply with the typed data source pattern. Please kindly have a review again. Much appreciate! |
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.
Thanks for rewriting this as a typed data source @ning-kang!
It looks like the contributor docs for adding a new data source are slightly out of date, apologies for that. I left some further review comments that need to be fixed up before we can merge this.
"resource_group_name": commonschema.ResourceGroupName(), | ||
|
||
"virtual_network_name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, |
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.
We can replace these two properties with the virtual_network_id
since we can infer the resource group name after parsing the ID in the read function
"resource_group_name": commonschema.ResourceGroupName(), | |
"virtual_network_name": { | |
Type: pluginsdk.TypeString, | |
Required: true, | |
ValidateFunc: validation.StringIsNotEmpty, | |
}, | |
"virtual_network_id": { | |
Type: pluginsdk.TypeString, | |
Required: true, | |
ValidateFunc: commonids.ValidateVirtualNetworkID, | |
}, |
} | ||
|
||
func (VirtualNetworkPeeringDataSource) ModelObject() interface{} { | ||
return nil |
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.
We should define a struct Model for the data source and return it here. I left a comment further up with a suggestion on what the struct should look like since we typically define this at the top of the resource
return nil | |
return &VirtualNetworkPeeringDataSourceModel |
|
||
subscriptionId := metadata.Client.Account.SubscriptionId | ||
name := metadata.ResourceData.Get("name").(string) | ||
resource_group_name := metadata.ResourceData.Get("resource_group_name").(string) | ||
virtual_network_name := metadata.ResourceData.Get("virtual_network_name").(string) | ||
id := virtualnetworkpeerings.NewVirtualNetworkPeeringID(subscriptionId, resource_group_name, virtual_network_name, name) |
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.
Defining a struct for the model means we can decode it and access properties via the fields of the struct
subscriptionId := metadata.Client.Account.SubscriptionId | |
name := metadata.ResourceData.Get("name").(string) | |
resource_group_name := metadata.ResourceData.Get("resource_group_name").(string) | |
virtual_network_name := metadata.ResourceData.Get("virtual_network_name").(string) | |
id := virtualnetworkpeerings.NewVirtualNetworkPeeringID(subscriptionId, resource_group_name, virtual_network_name, name) | |
subscriptionId := metadata.Client.Account.SubscriptionId | |
var state VirtualNetworkPeeringDataSourceModel | |
if err := metadata.Decode(&state); err != nil { | |
return fmt.Errorf("decoding: %+v", err) | |
} | |
virtualNetworkId, err := commonids.ParseVirtualNetworkID(state.VirtualNetworkId) | |
if err != nil { | |
return err | |
} | |
id := virtualnetworkpeerings.NewVirtualNetworkPeeringID(subscriptionId, virtualNetworkId.ResourceGroupName, virtualNetworkId.VirtualNetworkName, state.Name) |
metadata.ResourceData.Set("name", id.VirtualNetworkPeeringName) | ||
metadata.ResourceData.Set("resource_group_name", id.ResourceGroupName) | ||
metadata.ResourceData.Set("virtual_network_name", id.VirtualNetworkName) |
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.
These don't need to be set back into state
metadata.ResourceData.Set("name", id.VirtualNetworkPeeringName) | |
metadata.ResourceData.Set("resource_group_name", id.ResourceGroupName) | |
metadata.ResourceData.Set("virtual_network_name", id.VirtualNetworkName) |
metadata.ResourceData.Set("allow_virtual_network_access", peer.AllowVirtualNetworkAccess) | ||
metadata.ResourceData.Set("allow_forwarded_traffic", peer.AllowForwardedTraffic) | ||
metadata.ResourceData.Set("allow_gateway_transit", peer.AllowGatewayTransit) | ||
metadata.ResourceData.Set("peer_complete_virtual_networks_enabled", peer.PeerCompleteVnets) | ||
metadata.ResourceData.Set("only_ipv6_peering_enabled", peer.EnableOnlyIPv6Peering) | ||
metadata.ResourceData.Set("use_remote_gateways", peer.UseRemoteGateways) |
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.
metadata.ResourceData.Set("allow_virtual_network_access", peer.AllowVirtualNetworkAccess) | |
metadata.ResourceData.Set("allow_forwarded_traffic", peer.AllowForwardedTraffic) | |
metadata.ResourceData.Set("allow_gateway_transit", peer.AllowGatewayTransit) | |
metadata.ResourceData.Set("peer_complete_virtual_networks_enabled", peer.PeerCompleteVnets) | |
metadata.ResourceData.Set("only_ipv6_peering_enabled", peer.EnableOnlyIPv6Peering) | |
metadata.ResourceData.Set("use_remote_gateways", peer.UseRemoteGateways) | |
state.AllowVirtualNetworkAccess = pointer.From(props.AllowVirtualNetworkAccess) | |
state.AllowForwardedTraffic = pointer.From(props.AllowForwardedTraffic) | |
... etc. | |
} | ||
remoteVirtualNetworkId = parsed.ID() | ||
} | ||
metadata.ResourceData.Set("remote_virtual_network_id", remoteVirtualNetworkId) |
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.
metadata.ResourceData.Set("remote_virtual_network_id", remoteVirtualNetworkId) | |
state.RemoteVirtualNetworkId = remoteVirtualNetworkId |
metadata.ResourceData.Set("remote_virtual_network_id", remoteVirtualNetworkId) | ||
} | ||
} | ||
return nil |
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.
return nil | |
return metadata.Encode(&state) |
name = "peer1to2" | ||
resource_group_name = azurerm_resource_group.test.name | ||
virtual_network_name = azurerm_virtual_network.test-1.name | ||
remote_virtual_network_id = azurerm_virtual_network.test-2.id | ||
allow_virtual_network_access = true | ||
} | ||
|
||
resource "azurerm_virtual_network_peering" "test-2" { | ||
name = "peer2to1" | ||
resource_group_name = azurerm_resource_group.test.name | ||
virtual_network_name = azurerm_virtual_network.test-2.name | ||
remote_virtual_network_id = azurerm_virtual_network.test-1.id | ||
allow_virtual_network_access = true | ||
} | ||
|
||
data "azurerm_virtual_network_peering" "test-1" { | ||
name = "peer1to2" | ||
virtual_network_name = azurerm_virtual_network.test-1.name | ||
resource_group_name = azurerm_resource_group.test.name | ||
} | ||
|
||
data "azurerm_virtual_network_peering" "test-2" { | ||
name = "peer2to1" | ||
virtual_network_name = azurerm_virtual_network.test-2.name | ||
resource_group_name = azurerm_resource_group.test.name | ||
} | ||
`, data.RandomInteger, data.Locations.Primary) | ||
} |
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.
Can we prefix these with acctest
so that it's clear what resources were created by our acceptance tests and so that our automation can clean these up in case of any failures
name = "peer1to2" | |
resource_group_name = azurerm_resource_group.test.name | |
virtual_network_name = azurerm_virtual_network.test-1.name | |
remote_virtual_network_id = azurerm_virtual_network.test-2.id | |
allow_virtual_network_access = true | |
} | |
resource "azurerm_virtual_network_peering" "test-2" { | |
name = "peer2to1" | |
resource_group_name = azurerm_resource_group.test.name | |
virtual_network_name = azurerm_virtual_network.test-2.name | |
remote_virtual_network_id = azurerm_virtual_network.test-1.id | |
allow_virtual_network_access = true | |
} | |
data "azurerm_virtual_network_peering" "test-1" { | |
name = "peer1to2" | |
virtual_network_name = azurerm_virtual_network.test-1.name | |
resource_group_name = azurerm_resource_group.test.name | |
} | |
data "azurerm_virtual_network_peering" "test-2" { | |
name = "peer2to1" | |
virtual_network_name = azurerm_virtual_network.test-2.name | |
resource_group_name = azurerm_resource_group.test.name | |
} | |
`, data.RandomInteger, data.Locations.Primary) | |
} | |
name = "acctestpeer1to2" | |
resource_group_name = azurerm_resource_group.test.name | |
virtual_network_name = azurerm_virtual_network.test-1.name | |
remote_virtual_network_id = azurerm_virtual_network.test-2.id | |
allow_virtual_network_access = true | |
} | |
resource "azurerm_virtual_network_peering" "test-2" { | |
name = "acctestpeer2to1" | |
resource_group_name = azurerm_resource_group.test.name | |
virtual_network_name = azurerm_virtual_network.test-2.name | |
remote_virtual_network_id = azurerm_virtual_network.test-1.id | |
allow_virtual_network_access = true | |
} | |
data "azurerm_virtual_network_peering" "test-1" { | |
name = "acctestpeer1to2" | |
virtual_network_name = azurerm_virtual_network.test-1.name | |
resource_group_name = azurerm_resource_group.test.name | |
} | |
data "azurerm_virtual_network_peering" "test-2" { | |
name = "acctestpeer2to1" | |
virtual_network_name = azurerm_virtual_network.test-2.name | |
resource_group_name = azurerm_resource_group.test.name | |
} | |
`, data.RandomInteger, data.Locations.Primary) | |
} |
* `resource_group_name` - (Required) The name of the resource group where the virtual network peering exists. | ||
|
||
* `virtual_network_name` - (Required) The name of the local virtual network. |
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.
* `resource_group_name` - (Required) The name of the resource group where the virtual network peering exists. | |
* `virtual_network_name` - (Required) The name of the local virtual network. | |
* `virtual_network_id` - (Required) The resource ID of the virtual network. |
…hub.com/ning-kang/terraform-provider-azurerm into add-virtual-network-peering-data-source
@stephybun Sorry to be a pain. I think I have found the issue and fixed the tests. Have also uplifted the sdk version due to a vendor package update. Please kindly have a review again. |
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.
Different failure now:
------- Stdout: -------
=== RUN TestAccDataSourceVirtualNetworkPeering_basic
=== PAUSE TestAccDataSourceVirtualNetworkPeering_basic
=== CONT TestAccDataSourceVirtualNetworkPeering_basic
testcase.go:173: Step 1/1 error: Check failed: Check 2/12 error: data.azurerm_virtual_network_peering.test-1: Attribute 'allow_forwarded_traffic' expected "true", got "false"
--- FAIL: TestAccDataSourceVirtualNetworkPeering_basic (207.40s)
FAIL
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.
Thanks @ning-kang, test is passing now. LGTM 🚀
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. |
Community Note
Description
This Pull Request resolves #27486 by adding a new data source for virtual network peering with corresponding tests and documentations.
PR Checklist
For example: “
resource_name_here
- description of change e.g. adding propertynew_property_name_here
”Changes to existing Resource / Data Source
Testing
Change Log
Below please provide what should go into the changelog (if anything) conforming to the Changelog Format documented here.
azurerm_virtual_network_peering
- added theazurerm_virtual_network_peering
data source.This is a (please select all that apply):
Related Issue(s)
Fixes #27486
Note
If this PR changes meaningfully during the course of review please update the title and description as required.