-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata_cloudscale.go
68 lines (51 loc) · 1.12 KB
/
metadata_cloudscale.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
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
package main
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"time"
"github.com/gofrs/uuid"
)
const (
cloudscaleMetadataURL string = "http://169.254.169.254/openstack/latest/meta_data.json"
)
type cloudscaleMetadata struct {
Name string `json:"name"`
Meta struct {
CloudscaleUUID *uuid.UUID `json:"cloudscale_uuid"`
} `json:"meta"`
}
func findCloudscaleServerMetadata() (*cloudscaleMetadata, error) {
var md *cloudscaleMetadata
req, err := http.NewRequest("GET", cloudscaleMetadataURL, nil)
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", newVersionInfo().HTTPUserAgent())
client := http.Client{
Timeout: 5 * time.Second,
}
fn := func() error {
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// Limit accepted response size
lr := io.LimitReader(resp.Body, 1024*1024)
body, err := ioutil.ReadAll(lr)
if err != nil {
return err
}
md = &cloudscaleMetadata{}
if err = json.Unmarshal(body, md); err != nil {
return err
}
return err
}
if err := metadataRetry(fn); err != nil {
return nil, err
}
return md, nil
}