Skip to content

Commit

Permalink
Add PTR records to compute instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
nat-henderson committed Apr 17, 2018
1 parent 8a2918c commit a8fc466
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
9 changes: 9 additions & 0 deletions google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ func resourceComputeInstance() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"public_ptr_domain_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
},
Expand Down Expand Up @@ -1120,6 +1125,10 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
Type: "ONE_TO_ONE_NAT",
NatIP: d.Get(acPrefix + ".nat_ip").(string),
}
if ptr, ok := d.GetOk(acPrefix + ".public_ptr_domain_name"); ok {
ac.SetPublicPtr = true
ac.PublicPtrDomainName = ptr.(string)
}
op, err := config.clientCompute.Instances.AddAccessConfig(
project, zone, d.Id(), networkName, ac).Do()
if err != nil {
Expand Down
66 changes: 66 additions & 0 deletions google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,30 @@ func TestAccComputeInstance_IP(t *testing.T) {
})
}

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

var instance compute.Instance
var ptrName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstance_PTRRecord(ptrName, instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
"google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceAccessConfigHasPTR(&instance),
),
},
},
})
}

func TestAccComputeInstance_GenerateIP(t *testing.T) {
var instance compute.Instance
var instanceName = fmt.Sprintf("instance-test-%s", acctest.RandString(10))
Expand Down Expand Up @@ -1148,6 +1172,20 @@ func testAccCheckComputeInstanceAccessConfigHasIP(instance *compute.Instance) re
}
}

func testAccCheckComputeInstanceAccessConfigHasPTR(instance *compute.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, i := range instance.NetworkInterfaces {
for _, c := range i.AccessConfigs {
if c.PublicPtrDomainName == "" {
return fmt.Errorf("no PTR Record")
}
}
}

return nil
}
}

func testAccCheckComputeInstanceDisk(instance *compute.Instance, source string, delete bool, boot bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
if instance.Disks == nil {
Expand Down Expand Up @@ -1750,6 +1788,34 @@ resource "google_compute_instance" "foobar" {
`, ip, instance)
}

func testAccComputeInstance_PTRRecord(record, instance string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "n1-standard-1"
zone = "us-central1-a"
tags = ["foo", "bar"]
boot_disk {
initialize_params{
image = "debian-8-jessie-v20160803"
}
}
network_interface {
network = "default"
access_config {
public_ptr_domain_name = "test-record.%s.hashicorptest.com."
}
}
metadata {
foo = "bar"
}
}
`, instance, record)
}

func testAccComputeInstance_generateIp(instance string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "foobar" {
Expand Down

0 comments on commit a8fc466

Please sign in to comment.