forked from AbelHu/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_metadata_service.go
126 lines (97 loc) · 2.82 KB
/
http_metadata_service.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package infrastructure
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
)
type httpMetadataService struct {
metadataHost string
resolver DNSResolver
}
func NewHTTPMetadataService(
metadataHost string,
resolver DNSResolver,
) httpMetadataService {
return httpMetadataService{
metadataHost: metadataHost,
resolver: resolver,
}
}
func (ms httpMetadataService) Load() error {
return nil
}
func (ms httpMetadataService) GetPublicKey() (string, error) {
url := fmt.Sprintf("%s/latest/meta-data/public-keys/0/openssh-key", ms.metadataHost)
resp, err := http.Get(url)
if err != nil {
return "", bosherr.WrapError(err, "Getting open ssh key")
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", bosherr.WrapError(err, "Reading ssh key response body")
}
return string(bytes), nil
}
func (ms httpMetadataService) GetInstanceID() (string, error) {
url := fmt.Sprintf("%s/latest/meta-data/instance-id", ms.metadataHost)
resp, err := http.Get(url)
if err != nil {
return "", bosherr.WrapError(err, "Getting instance id from url")
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", bosherr.WrapError(err, "Reading instance id response body")
}
return string(bytes), nil
}
func (ms httpMetadataService) GetServerName() (string, error) {
userData, err := ms.getUserData()
if err != nil {
return "", bosherr.WrapError(err, "Getting user data")
}
serverName := userData.Server.Name
if len(serverName) == 0 {
return "", bosherr.Error("Empty server name")
}
return serverName, nil
}
func (ms httpMetadataService) GetRegistryEndpoint() (string, error) {
userData, err := ms.getUserData()
if err != nil {
return "", bosherr.WrapError(err, "Getting user data")
}
endpoint := userData.Registry.Endpoint
nameServers := userData.DNS.Nameserver
if len(nameServers) > 0 {
endpoint, err = ms.resolver.LookupHost(nameServers, endpoint)
if err != nil {
return "", bosherr.WrapError(err, "Resolving registry endpoint")
}
}
return endpoint, nil
}
func (ms httpMetadataService) IsAvailable() bool {
return true
}
func (ms httpMetadataService) getUserData() (UserDataContentsType, error) {
var userData UserDataContentsType
userDataURL := fmt.Sprintf("%s/latest/user-data", ms.metadataHost)
userDataResp, err := http.Get(userDataURL)
if err != nil {
return userData, bosherr.WrapError(err, "Getting user data from url")
}
defer userDataResp.Body.Close()
userDataBytes, err := ioutil.ReadAll(userDataResp.Body)
if err != nil {
return userData, bosherr.WrapError(err, "Reading user data response body")
}
err = json.Unmarshal(userDataBytes, &userData)
if err != nil {
return userData, bosherr.WrapError(err, "Unmarshalling user data")
}
return userData, nil
}