Skip to content

Commit ae50ddc

Browse files
Serial port datasource (#3247) (#1860)
* Add serial port datasource * Add doc for serial port ds * Serial port test * Formatting * Markdown fixes * Remove pagination, we should always be able to retrieve 1MB Signed-off-by: Modular Magician <[email protected]>
1 parent b873b76 commit ae50ddc

File tree

5 files changed

+246
-0
lines changed

5 files changed

+246
-0
lines changed

.changelog/3247.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_compute_instance_serial_port`
3+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package google
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+
)
6+
7+
func dataSourceGoogleComputeInstanceSerialPort() *schema.Resource {
8+
return &schema.Resource{
9+
Read: computeInstanceSerialPortRead,
10+
Schema: map[string]*schema.Schema{
11+
"port": {
12+
Type: schema.TypeInt,
13+
Required: true,
14+
},
15+
"instance": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
},
19+
"zone": {
20+
Type: schema.TypeString,
21+
Optional: true,
22+
Computed: true,
23+
},
24+
"project": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
Computed: true,
28+
},
29+
"contents": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
},
33+
},
34+
}
35+
}
36+
37+
func computeInstanceSerialPortRead(d *schema.ResourceData, meta interface{}) error {
38+
config := meta.(*Config)
39+
project, err := getProject(d, config)
40+
if err != nil {
41+
return err
42+
}
43+
d.Set("project", project)
44+
zone, err := getZone(d, config)
45+
if err != nil {
46+
return err
47+
}
48+
d.Set("zone", zone)
49+
50+
port := int64(d.Get("port").(int))
51+
output, err := config.clientCompute.Instances.GetSerialPortOutput(project, zone, d.Get("instance").(string)).Port(port).Do()
52+
if err != nil {
53+
return err
54+
}
55+
56+
d.Set("contents", output.Contents)
57+
d.SetId(output.SelfLink)
58+
return nil
59+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
10+
)
11+
12+
func TestAccDataSourceComputeInstanceSerialPort_basic(t *testing.T) {
13+
instanceName := fmt.Sprintf("tf-test-serial-data-%s", acctest.RandString(10))
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheck(t) },
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccComputeInstanceSerialPort(instanceName),
20+
Check: resource.ComposeTestCheckFunc(
21+
// Contents of serial port output include lots of initialization logging
22+
resource.TestMatchResourceAttr("data.google_compute_instance_serial_port.serial", "contents",
23+
regexp.MustCompile("Initializing cgroup subsys")),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
func testAccComputeInstanceSerialPort(instanceName string) string {
31+
return fmt.Sprintf(`
32+
resource "google_compute_instance" "default" {
33+
name = "%s"
34+
machine_type = "n1-standard-1"
35+
zone = "us-central1-a"
36+
37+
boot_disk {
38+
initialize_params {
39+
image = "debian-8-jessie-v20160803"
40+
}
41+
}
42+
43+
// Local SSD disk
44+
scratch_disk {
45+
interface = "SCSI"
46+
}
47+
48+
network_interface {
49+
network = "default"
50+
51+
access_config {
52+
// Ephemeral IP
53+
}
54+
}
55+
56+
metadata = {
57+
foo = "bar"
58+
serial-port-logging-enable = "TRUE"
59+
windows-keys = jsonencode(
60+
{
61+
62+
expireOn = "2020-04-14T01:37:19Z"
63+
exponent = "AQAB"
64+
modulus = "wgsquN4IBNPqIUnu+h/5Za1kujb2YRhX1vCQVQAkBwnWigcCqOBVfRa5JoZfx6KIvEXjWqa77jPvlsxM4WPqnDIM2qiK36up3SKkYwFjff6F2ni/ry8vrwXCX3sGZ1hbIHlK0O012HpA3ISeEswVZmX2X67naOvJXfY5v0hGPWqCADao+xVxrmxsZD4IWnKl1UaZzI5lhAzr8fw6utHwx1EZ/MSgsEki6tujcZfN+GUDRnmJGQSnPTXmsf7Q4DKreTZk49cuyB3prV91S0x3DYjCUpSXrkVy1Ha5XicGD/q+ystuFsJnrrhbNXJbpSjM6sjo/aduAkZJl4FmOt0R7Q=="
65+
userName = "example-user"
66+
}
67+
)
68+
}
69+
70+
service_account {
71+
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
72+
}
73+
}
74+
75+
data "google_compute_instance_serial_port" "serial" {
76+
instance = google_compute_instance.default.name
77+
zone = google_compute_instance.default.zone
78+
port = 1
79+
}
80+
`, instanceName)
81+
}

google-beta/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ func Provider() terraform.ResourceProvider {
506506
"google_compute_global_address": dataSourceGoogleComputeGlobalAddress(),
507507
"google_compute_image": dataSourceGoogleComputeImage(),
508508
"google_compute_instance": dataSourceGoogleComputeInstance(),
509+
"google_compute_instance_serial_port": dataSourceGoogleComputeInstanceSerialPort(),
509510
"google_compute_instance_group": dataSourceGoogleComputeInstanceGroup(),
510511
"google_compute_lb_ip_ranges": dataSourceGoogleComputeLbIpRanges(),
511512
"google_compute_network": dataSourceGoogleComputeNetwork(),
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
subcategory: "Compute Engine"
3+
layout: "google"
4+
page_title: "Google: google_compute_instance_serial_port"
5+
sidebar_current: "docs-google-datasource-compute-instance-serial-port"
6+
description: |-
7+
Get the serial port output from a Compute Instance.
8+
---
9+
10+
# google\_compute\_instance\_serial\_port
11+
12+
Get the serial port output from a Compute Instance. For more information see
13+
the official [API](https://cloud.google.com/compute/docs/instances/viewing-serial-port-output) documentation.
14+
15+
## Example Usage
16+
17+
```hcl
18+
data "google_compute_instance_serial_port" "serial" {
19+
instance = "my-instance"
20+
zone = "us-central1-a"
21+
port = 1
22+
}
23+
24+
output "serial_out" {
25+
value = data.google_compute_instance_serial_port.serial.contents
26+
}
27+
```
28+
29+
Using the serial port output to generate a windows password, derived from the [official guide](https://cloud.google.com/compute/docs/instances/windows/automate-pw-generation):
30+
31+
```hcl
32+
resource "google_compute_instance" "windows" {
33+
name = "windows-instance"
34+
machine_type = "n1-standard-1"
35+
zone = "us-central1-a"
36+
37+
boot_disk {
38+
initialize_params {
39+
image = "gce-uefi-images/windows-2019"
40+
}
41+
}
42+
43+
network_interface {
44+
network = "default"
45+
46+
access_config {
47+
// Ephemeral IP
48+
}
49+
}
50+
51+
metadata = {
52+
serial-port-logging-enable = "TRUE"
53+
// Derived from https://cloud.google.com/compute/docs/instances/windows/automate-pw-generation
54+
windows-keys = jsonencode(
55+
{
56+
57+
expireOn = "2020-04-14T01:37:19Z"
58+
exponent = "AQAB"
59+
modulus = "wgsquN4IBNPqIUnu+h/5Za1kujb2YRhX1vCQVQAkBwnWigcCqOBVfRa5JoZfx6KIvEXjWqa77jPvlsxM4WPqnDIM2qiK36up3SKkYwFjff6F2ni/ry8vrwXCX3sGZ1hbIHlK0O012HpA3ISeEswVZmX2X67naOvJXfY5v0hGPWqCADao+xVxrmxsZD4IWnKl1UaZzI5lhAzr8fw6utHwx1EZ/MSgsEki6tujcZfN+GUDRnmJGQSnPTXmsf7Q4DKreTZk49cuyB3prV91S0x3DYjCUpSXrkVy1Ha5XicGD/q+ystuFsJnrrhbNXJbpSjM6sjo/aduAkZJl4FmOt0R7Q=="
60+
userName = "example-user"
61+
}
62+
)
63+
}
64+
65+
service_account {
66+
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
67+
}
68+
}
69+
70+
data "google_compute_instance_serial_port" "serial" {
71+
instance = google_compute_instance.windows.name
72+
zone = google_compute_instance.windows.zone
73+
port = 4
74+
}
75+
76+
output "serial_out" {
77+
value = data.google_compute_instance_serial_port.serial.contents
78+
}
79+
```
80+
81+
## Argument Reference
82+
83+
The following arguments are supported:
84+
85+
* `instance` - (Required) The name of the Compute Instance to read output from.
86+
87+
* `port` - (Required) The number of the serial port to read output from. Possible values are 1-4.
88+
89+
- - -
90+
91+
* `project` - (Optional) The project in which the Compute Instance exists. If it
92+
is not provided, the provider project is used.
93+
94+
* `zone` - (Optional) The zone in which the Compute Instance exists.
95+
If it is not provided, the provider zone is used.
96+
97+
## Attributes Reference
98+
99+
In addition to the arguments listed above, the following computed attributes are
100+
exported:
101+
102+
* `contents` - The output of the serial port. Serial port output is available only when the VM instance is running, and logs are limited to the most recent 1 MB of output per port.

0 commit comments

Comments
 (0)