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

Importing google_dns_managed_zone with project #1944

Merged
merged 5 commits into from
Sep 20, 2018
Merged
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
18 changes: 17 additions & 1 deletion google/resource_dns_managed_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func resourceDnsManagedZone() *schema.Resource {
Update: resourceDnsManagedZoneUpdate,
Delete: resourceDnsManagedZoneDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceDnsManagedZoneImport,
},
Schema: map[string]*schema.Schema{
"dns_name": &schema.Schema{
Expand Down Expand Up @@ -149,3 +149,19 @@ func resourceDnsManagedZoneDelete(d *schema.ResourceData, meta interface{}) erro
d.SetId("")
return nil
}

func resourceDnsManagedZoneImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
parseImportId([]string{"projects/(?P<project>[^/]+)/managedZones/(?P<name>[^/]+)",
"(?P<project>[^/]+)/managedZones/(?P<name>[^/]+)",
"(?P<name>[^/]+)"}, d, config)

// Replace import id for the resource id
id, err := replaceVars(d, config, "{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return []*schema.ResourceData{d}, nil
}
110 changes: 110 additions & 0 deletions google/resource_dns_managed_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,113 @@ resource "google_dns_managed_zone" "foobar" {
description = "%s"
}`, suffix, suffix, description)
}

func TestDnsManagedZoneImport_parseImportId(t *testing.T) {
zoneRegexes := []string{
"projects/(?P<project>[^/]+)/managedZones/(?P<name>[^/]+)",
"(?P<project>[^/]+)/managedZones/(?P<name>[^/]+)",
"(?P<name>[^/]+)",
}

cases := map[string]struct {
ImportId string
IdRegexes []string
Config *Config
ExpectedSchemaValues map[string]interface{}
ExpectError bool
}{
"full self_link": {
IdRegexes: zoneRegexes,
ImportId: "https://www.googleapis.com/dns/v1/projects/my-project/managedZones/my-zone",
ExpectedSchemaValues: map[string]interface{}{
"project": "my-project",
"name": "my-zone",
},
},
"relative self_link": {
IdRegexes: zoneRegexes,
ImportId: "projects/my-project/managedZones/my-zone",
ExpectedSchemaValues: map[string]interface{}{
"project": "my-project",
"name": "my-zone",
},
},
"short id": {
IdRegexes: zoneRegexes,
ImportId: "my-project/managedZones/my-zone",
ExpectedSchemaValues: map[string]interface{}{
"project": "my-project",
"name": "my-zone",
},
},
"short id with default project and region": {
IdRegexes: zoneRegexes,
ImportId: "my-zone",
Config: &Config{
Project: "default-project",
},
ExpectedSchemaValues: map[string]interface{}{
"project": "default-project",
"name": "my-zone",
},
},
}

for tn, tc := range cases {
d := &ResourceDataMock{
FieldsInSchema: make(map[string]interface{}),
id: tc.ImportId,
}
config := tc.Config
if config == nil {
config = &Config{}
}
//
if err := parseImportId(tc.IdRegexes, d, config); err == nil {
for k, expectedValue := range tc.ExpectedSchemaValues {
if v, ok := d.GetOk(k); ok {
if v != expectedValue {
t.Errorf("%s failed; Expected value %q for field %q, got %q", tn, expectedValue, k, v)
}
} else {
t.Errorf("%s failed; Expected a value for field %q", tn, k)
}
}
} else if !tc.ExpectError {
t.Errorf("%s failed; unexpected error: %s", tn, err)
}
}
}

func TestAccDnsManagedZone_importWithProject(t *testing.T) {
t.Parallel()

zoneSuffix := acctest.RandString(10)
project := getTestProjectFromEnv()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDnsManagedZoneDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDnsManagedZone_basicWithProject(zoneSuffix, "description1", project),
},
resource.TestStep{
ResourceName: "google_dns_managed_zone.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccDnsManagedZone_basicWithProject(suffix, description, project string) string {
return fmt.Sprintf(`
resource "google_dns_managed_zone" "foobar" {
name = "mzone-test-%s"
dns_name = "tf-acctest-%s.hashicorptest.com."
description = "%s"
project = "%s"
}`, suffix, suffix, description, project)
}
6 changes: 4 additions & 2 deletions website/docs/r/dns_managed_zone.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ exported:

## Import

DNS managed zones can be imported using the `name`, e.g.
Managed zones can be imported using any of these accepted formats:

```
$ terraform import google_dns_managed_zone.prod prod-zone
$ terraform import google_dns_managed_zone.prod projects/{{project-id}}/managedZones/{{zone}}
$ terraform import google_compute_disk.default {{project-id}}/managedZones/{{zone}}
$ terraform import google_compute_disk.default {{zone}}
```