Skip to content

Commit

Permalink
Add more formats for nat import (#1852)
Browse files Browse the repository at this point in the history
Merged PR #1852.
  • Loading branch information
rileykarson authored and modular-magician committed May 31, 2019
1 parent 8e1160c commit 56e1f18
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
21 changes: 12 additions & 9 deletions third_party/terraform/resources/resource_compute_router_nat.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func resourceComputeRouterNat() *schema.Resource {
Read: resourceComputeRouterNatRead,
Delete: resourceComputeRouterNatDelete,
Importer: &schema.ResourceImporter{
State: resourceComputeRouterNatImportState,
State: resourceComputeRouterNatImport,
},

Timeouts: &schema.ResourceTimeout{
Expand Down Expand Up @@ -230,7 +230,7 @@ func resourceComputeRouterNatCreate(d *schema.ResourceData, meta interface{}) er
if err != nil {
return fmt.Errorf("Error patching router %s/%s: %s", region, routerName, err)
}
d.SetId(fmt.Sprintf("%s/%s/%s", region, routerName, natName))
d.SetId(fmt.Sprintf("%s/%s/%s/%s", project, region, routerName, natName))
err = computeBetaOperationWaitTime(config.clientCompute, op, project, "Patching router", int(d.Timeout(schema.TimeoutCreate).Minutes()))
if err != nil {
d.SetId("")
Expand Down Expand Up @@ -374,15 +374,18 @@ func resourceComputeRouterNatDelete(d *schema.ResourceData, meta interface{}) er
return nil
}

func resourceComputeRouterNatImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 3 {
return nil, fmt.Errorf("Invalid router nat specifier. Expecting {region}/{router}/{nat}")
func resourceComputeRouterNatImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := parseImportId([]string{"(?P<project>[^/]+)/(?P<region>[^/]+)/(?P<router>[^/]+)/(?P<name>[^/]+)", "(?P<region>[^/]+)/(?P<router>[^/]+)/(?P<name>[^/]+)", "(?P<router>[^/]+)/(?P<name>[^/]+)"}, d, config); err != nil {
return nil, err
}

d.Set("region", parts[0])
d.Set("router", parts[1])
d.Set("name", parts[2])
// Replace import id for the resource id
id, err := replaceVars(d, config, "{{project}}/{{region}}/{{router}}/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return []*schema.ResourceData{d}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
func TestAccComputeRouterNat_basic(t *testing.T) {
t.Parallel()

project := getTestProjectFromEnv()
region := getTestRegionFromEnv()

testId := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -24,6 +27,25 @@ func TestAccComputeRouterNat_basic(t *testing.T) {
},
resource.TestStep{
ResourceName: "google_compute_router_nat.foobar",
// implicitly: ImportStateId: fmt.Sprintf("%s/%s/router-nat-test-%s/router-nat-test-%s", project, region, testId, testId),
ImportState: true,
ImportStateVerify: true,
},
resource.TestStep{
ResourceName: "google_compute_router_nat.foobar",
ImportStateId: fmt.Sprintf("%s/%s/router-nat-test-%s/router-nat-test-%s", project, region, testId, testId),
ImportState: true,
ImportStateVerify: true,
},
resource.TestStep{
ResourceName: "google_compute_router_nat.foobar",
ImportStateId: fmt.Sprintf("%s/router-nat-test-%s/router-nat-test-%s", region, testId, testId),
ImportState: true,
ImportStateVerify: true,
},
resource.TestStep{
ResourceName: "google_compute_router_nat.foobar",
ImportStateId: fmt.Sprintf("router-nat-test-%s/router-nat-test-%s", testId, testId),
ImportState: true,
ImportStateVerify: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,13 @@ The `log_config` block supports:

## Import

Router NATs can be imported using the `region`, `router`, and `name`, e.g.
Router NATs can be imported using any of these accepted formats:

```
$ terraform import google_compute_router_nat.my-nat us-central1/router-1/nat-1
$ terraform import google_compute_router_nat.default {{project}}/{{region}}/{{router}}/{{name}}
$ terraform import google_compute_router_nat.default {{region}}/{{router}}/{{name}}
$ terraform import google_compute_router_nat.default {{router}}/{{name}}
```

-> If you're importing a resource with beta features, make sure to include `-provider=google-beta`
as an argument so that Terraform uses the correct provider to import your resource.

0 comments on commit 56e1f18

Please sign in to comment.