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

Add ability to import existing VPC peering networks #4291

Closed
Show file tree
Hide file tree
Changes from all 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
20 changes: 19 additions & 1 deletion google/resource_compute_network_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ func resourceComputeNetworkPeering() *schema.Resource {
Create: resourceComputeNetworkPeeringCreate,
Read: resourceComputeNetworkPeeringRead,
Delete: resourceComputeNetworkPeeringDelete,

Importer: &schema.ResourceImporter{
State: resourceComputeNetworkPeeringImporter,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -61,6 +63,22 @@ func resourceComputeNetworkPeering() *schema.Resource {
}
}

func resourceComputeNetworkPeeringImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := parseImportId([]string{"(?P<network>[^/]+)/(?P<name>[^/]+)"}, d, config); err != nil {
return nil, err
}

// Replace import id for the resource id
id, err := replaceVars(d, config, "{{name}}")
Copy link

@mllu mllu Nov 25, 2019

Choose a reason for hiding this comment

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

randomly pass by, (also looking for this feature), it appears to me the format of id for google_compute_network_peering is network/name from my other self-managed resources.

I tried to checkout to this branch and was able to import my resource google_compute_network_peering, but the generated id seems only contains name.

Dunno if that matters, but wanna bring it up to discuss. Thanks in advance.

if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return []*schema.ResourceData{d}, nil
}

func resourceComputeNetworkPeeringCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkFieldValue, err := ParseNetworkFieldValue(d.Get("network").(string), d, config)
Expand Down
5 changes: 5 additions & 0 deletions google/resource_compute_network_peering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func TestAccComputeNetworkPeering_basic(t *testing.T) {
testAccCheckComputeNetworkPeeringAutoCreateRoutes(true, &peering_beta),
),
},
{
ResourceName: "google_compute_network_peering.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})

Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/compute_network_peering.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@ exported:
* `state` - State for the peering.

* `state_details` - Details about the current state of the peering.

## Import
VPC Peering Networks can be imported using the name of the network the peering exists in and the name of the peering network

```
$ terraform import google_compute_network_peering.peering_network network-name/peering-network-name
```