compute/metadata: add Email method

Adds a method to retrieve the email address
associated with a service account. If the
service account is the empty string "", or
"default", the account's main/default will
be used.

Fixes #1551.

Change-Id: I139e2f5a432ffe2b872b904048a065e7ce6bb7b9
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/44431
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jean de Klerk <deklerk@google.com>
diff --git a/compute/metadata/metadata.go b/compute/metadata/metadata.go
index 125b703..4ff4e2f 100644
--- a/compute/metadata/metadata.go
+++ b/compute/metadata/metadata.go
@@ -227,6 +227,9 @@
 // ExternalIP returns the instance's primary external (public) IP address.
 func ExternalIP() (string, error) { return defaultClient.ExternalIP() }
 
+// Email calls Client.Email on the default client.
+func Email(serviceAccount string) (string, error) { return defaultClient.Email(serviceAccount) }
+
 // Hostname returns the instance's hostname. This will be of the form
 // "<instanceID>.c.<projID>.internal".
 func Hostname() (string, error) { return defaultClient.Hostname() }
@@ -367,6 +370,16 @@
 	return c.getTrimmed("instance/network-interfaces/0/ip")
 }
 
+// Email returns the email address associated with the service account.
+// The account may be empty or the string "default" to use the instance's
+// main account.
+func (c *Client) Email(serviceAccount string) (string, error) {
+	if serviceAccount == "" {
+		serviceAccount = "default"
+	}
+	return c.getTrimmed("instance/service-accounts/" + serviceAccount + "/email")
+}
+
 // ExternalIP returns the instance's primary external (public) IP address.
 func (c *Client) ExternalIP() (string, error) {
 	return c.getTrimmed("instance/network-interfaces/0/access-configs/0/external-ip")