diff --git a/google/resource_compute_instance.go b/google/resource_compute_instance.go index 71de97cc..46a7dbd0 100644 --- a/google/resource_compute_instance.go +++ b/google/resource_compute_instance.go @@ -537,6 +537,12 @@ func resourceComputeInstance() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "hostname": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, CustomizeDiff: customdiff.All( customdiff.If( @@ -674,6 +680,7 @@ func expandComputeInstance(project string, zone *compute.Zone, d *schema.Resourc MinCpuPlatform: d.Get("min_cpu_platform").(string), Scheduling: scheduling, DeletionProtection: d.Get("deletion_protection").(bool), + Hostname: d.Get("hostname").(string), ForceSendFields: []string{"CanIpForward", "DeletionProtection"}, }, nil } @@ -892,6 +899,7 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error d.Set("project", project) d.Set("zone", GetResourceNameFromSelfLink(instance.Zone)) d.Set("name", instance.Name) + d.Set("hostname", instance.Hostname) d.SetId(instance.Name) return nil diff --git a/google/resource_compute_instance_test.go b/google/resource_compute_instance_test.go index 0a23538b..827ebd24 100644 --- a/google/resource_compute_instance_test.go +++ b/google/resource_compute_instance_test.go @@ -1043,6 +1043,26 @@ func TestAccComputeInstance_secondaryAliasIpRange(t *testing.T) { }) } +func TestAccComputeInstance_hostname(t *testing.T) { + t.Parallel() + + 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{ + { + Config: testAccComputeInstance_hostname(instanceName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("google_compute_instance.foobar", "hostname"), + ), + }, + computeInstanceImportStep("us-central1-a", instanceName, []string{}), + }, + }) +} + func testAccCheckComputeInstanceUpdateMachineType(n string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -2908,6 +2928,33 @@ resource "google_compute_instance" "foobar" { }`, network, subnet, instance) } +func testAccComputeInstance_hostname(instance string) string { + return fmt.Sprintf(` +data "google_compute_image" "my_image" { + family = "debian-9" + project = "debian-cloud" +} + +resource "google_compute_instance" "foobar" { + name = "%s" + machine_type = "n1-standard-1" + zone = "us-central1-a" + can_ip_forward = false + + boot_disk { + initialize_params{ + image = "${data.google_compute_image.my_image.self_link}" + } + } + + network_interface { + network = "default" + } + + hostname = "%s.test" +}`, instance, instance) +} + // Set fields that require stopping the instance: machine_type, min_cpu_platform, and service_account func testAccComputeInstance_stopInstanceToUpdate(instance string) string { return fmt.Sprintf(` diff --git a/website/docs/r/compute_instance.html.markdown b/website/docs/r/compute_instance.html.markdown index 2a1541c0..5df64781 100644 --- a/website/docs/r/compute_instance.html.markdown +++ b/website/docs/r/compute_instance.html.markdown @@ -94,6 +94,10 @@ The following arguments are supported: * `deletion_protection` - (Optional) Enable deletion protection on this instance. Defaults to false. **Note:** you must disable deletion protection before removing the resource (e.g., via `terraform destroy`), or the instance cannot be deleted and the Terraform run will not complete successfully. +* `hostname` - (Optional) A custom hostname for the instance. Must be a fully qualified DNS name and RFC-1035-valid. + Valid format is a series of labels 1-63 characters long matching the regular expression `[a-z]([-a-z0-9]*[a-z0-9])`, concatenated with periods. + The entire hostname must not exceed 253 characters. Changing this forces a new resource to be created. + * `guest_accelerator` - (Optional) List of the type and count of accelerator cards attached to the instance. Structure documented below. **Note:** GPU accelerators can only be used with [`on_host_maintenance`](#on_host_maintenance) option set to TERMINATE.