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 DNS PTR record to Compute Instance #1349

Merged
merged 5 commits into from
Apr 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
7 changes: 7 additions & 0 deletions google/compute_instance_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func flattenAccessConfigs(accessConfigs []*computeBeta.AccessConfig) ([]map[stri
"nat_ip": ac.NatIP,
"assigned_nat_ip": ac.NatIP,
}
if ac.SetPublicPtr {
flattened[i]["public_ptr_domain_name"] = ac.PublicPtrDomainName
}
if natIP == "" {
natIP = ac.NatIP
}
Expand Down Expand Up @@ -103,6 +106,10 @@ func expandAccessConfigs(configs []interface{}) []*computeBeta.AccessConfig {
Type: "ONE_TO_ONE_NAT",
NatIP: data["nat_ip"].(string),
}
if ptr, ok := data["public_ptr_domain_name"]; ok {
acs[i].SetPublicPtr = true
acs[i].PublicPtrDomainName = ptr.(string)
}
}
return acs
}
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need to set SetPublicPtr explicitely to true to turn it off if some was using public_ptr_domain_name and now removes it from their config?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't really know why the API requires that SetPublicPtr be true and PublicPtrDomainName have the value, rather than just doing a null check on the latter, but the docs are clear about that. Creating the new AccessConfig means that it won't be set unless that's set in the current config - we do a full replace.

Copy link
Contributor

Choose a reason for hiding this comment

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

We got bit a few times by that when in order to disable a feature in update, we had to use ForceSend false and nil values otherwise, the server would keep the value it currently had.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense! I don't think it can happen here at all since we do a full replace. Let me add that to the test to be 100% sure.

ac.PublicPtrDomainName = ptr.(string)
}
op, err := config.clientCompute.Instances.AddAccessConfig(
project, zone, d.Id(), networkName, ac).Do()
if err != nil {
Expand Down
77 changes: 77 additions & 0 deletions google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,41 @@ 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 ipName = 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),
),
},
computeInstanceImportStep("us-central1-a", instanceName, []string{"metadata.baz", "metadata.foo"}),
resource.TestStep{
Config: testAccComputeInstance_ip(ipName, instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
"google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceAccessConfigHasIP(&instance),
),
},
computeInstanceImportStep("us-central1-a", instanceName, []string{"metadata.baz", "metadata.foo"}),
},
})
}

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 +1183,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 +1799,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
2,118 changes: 1,582 additions & 536 deletions vendor/google.golang.org/api/compute/v0.beta/compute-api.json

Large diffs are not rendered by default.

Loading