Add hostname field to compute_instance (#2952)

Signed-off-by: Modular Magician <magic-modules@google.com>
This commit is contained in:
The Magician 2019-01-29 09:44:21 -08:00 committed by emily
parent 60264e386c
commit ced540ceca
3 changed files with 59 additions and 0 deletions

View File

@ -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

View File

@ -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(`

View File

@ -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.