-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata.go
More file actions
81 lines (74 loc) · 1.95 KB
/
metadata.go
File metadata and controls
81 lines (74 loc) · 1.95 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// This package implements a simple way to retrieve droplet metadata
// from the relevant digitalocean endpoint.
package do_metadata
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"time"
)
type Metadata struct {
DropletID int `json:"droplet_id"`
Hostname string `json:"hostname"`
VendorData string `json:"vendor_data"`
PublicKeys []string `json:"public_keys"`
Region string `json:"region"`
Interfaces struct {
Private []struct {
Ipv4 struct {
IPAddress string `json:"ip_address"`
Netmask string `json:"netmask"`
Gateway string `json:"gateway"`
} `json:"ipv4"`
Mac string `json:"mac"`
Type string `json:"type"`
} `json:"private"`
Public []struct {
Ipv4 struct {
IPAddress string `json:"ip_address"`
Netmask string `json:"netmask"`
Gateway string `json:"gateway"`
} `json:"ipv4"`
Ipv6 struct {
IPAddress string `json:"ip_address"`
Cidr int `json:"cidr"`
Gateway string `json:"gateway"`
} `json:"ipv6"`
Mac string `json:"mac"`
Type string `json:"type"`
} `json:"public"`
} `json:"interfaces"`
FloatingIP struct {
Ipv4 struct {
Active bool `json:"active"`
} `json:"ipv4"`
} `json:"floating_ip"`
DNS struct {
Nameservers []string `json:"nameservers"`
} `json:"dns"`
Features struct {
DhcpEnabled bool `json:"dhcp_enabled"`
} `json:"features"`
}
//RetrieveMetadata retrieves the metadata for this droplet from the droplet metadata endpoint
func RetrieveMetadata(timeout time.Duration) (*Metadata, error) {
var meta Metadata
cli := &http.Client{Timeout: timeout}
resp, err := cli.Get("http://169.254.169.254/metadata/v1.json")
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 10 * 1024))
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &meta); err != nil {
return nil, err
}
return &meta, nil
}