forked from mozilla-services/FindMyDevice
/
aws.go
36 lines (33 loc) · 895 Bytes
/
aws.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package util
import (
"io/ioutil"
"net/http"
"net/url"
)
/* Get the public AWS hostname for this machine.
* TODO: Make this a generic utility for getting public info from
* the aws meta server?
*/
func GetAWSPublicHostname() (hostname string, err error) {
req := &http.Request{Method: "GET",
URL: &url.URL{
Scheme: "http",
Host: "169.254.169.254",
Path: "/latest/meta-data/public-hostname"}}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var hostBytes []byte
hostBytes, err = ioutil.ReadAll(resp.Body)
if err == nil {
hostname = string(hostBytes)
}
}
return
}