-
Notifications
You must be signed in to change notification settings - Fork 115
/
http_registry.go
124 lines (105 loc) · 3.19 KB
/
http_registry.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
package infrastructure
import (
"encoding/json"
"fmt"
"io/ioutil"
"time"
boshplat "github.com/cloudfoundry/bosh-agent/platform"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshhttp "github.com/cloudfoundry/bosh-utils/http"
boshhttpclient "github.com/cloudfoundry/bosh-utils/httpclient"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type httpRegistry struct {
metadataService MetadataService
platform boshplat.Platform
useServerNameAsID bool
logger boshlog.Logger
retryDelay time.Duration
}
func NewHTTPRegistryWithCustomDelay(
metadataService MetadataService,
platform boshplat.Platform,
useServerNameAsID bool,
logger boshlog.Logger,
retryDelay time.Duration,
) Registry {
return httpRegistry{
metadataService: metadataService,
platform: platform,
useServerNameAsID: useServerNameAsID,
logger: logger,
retryDelay: retryDelay,
}
}
func NewHTTPRegistry(
metadataService MetadataService,
platform boshplat.Platform,
useServerNameAsID bool,
logger boshlog.Logger,
) Registry {
return httpRegistry{
metadataService: metadataService,
platform: platform,
useServerNameAsID: useServerNameAsID,
logger: logger,
retryDelay: 1 * time.Second,
}
}
type settingsWrapperType struct {
Settings string
}
func (r httpRegistry) GetSettings() (boshsettings.Settings, error) {
var settings boshsettings.Settings
var identifier string
var err error
if r.useServerNameAsID {
identifier, err = r.metadataService.GetServerName()
if err != nil {
return settings, bosherr.WrapError(err, "Getting server name")
}
} else {
identifier, err = r.metadataService.GetInstanceID()
if err != nil {
return settings, bosherr.WrapError(err, "Getting instance id")
}
}
registryEndpoint, err := r.metadataService.GetRegistryEndpoint()
if err != nil {
return settings, bosherr.WrapError(err, "Getting registry endpoint")
}
networks, err := r.metadataService.GetNetworks()
if err != nil {
return settings, bosherr.WrapError(err, "Getting networks")
}
if len(networks) > 0 {
err = r.platform.SetupNetworking(networks)
if err != nil {
return settings, bosherr.WrapError(err, "Setting up networks")
}
}
settingsURL := fmt.Sprintf("%s/instances/%s/settings", registryEndpoint, identifier)
client := boshhttp.NewRetryClient(boshhttpclient.CreateDefaultClient(nil), 10, r.retryDelay, r.logger)
wrapperResponse, err := boshhttpclient.NewHTTPClient(client, r.logger).Get(settingsURL)
if err != nil {
return settings, bosherr.WrapError(err, "Getting settings from url")
}
defer func() {
_ = wrapperResponse.Body.Close()
}()
wrapperBytes, err := ioutil.ReadAll(wrapperResponse.Body)
if err != nil {
return settings, bosherr.WrapError(err, "Reading settings response body")
}
var wrapper settingsWrapperType
err = json.Unmarshal(wrapperBytes, &wrapper)
if err != nil {
return settings, bosherr.WrapError(err, "Unmarshalling settings wrapper")
}
err = json.Unmarshal([]byte(wrapper.Settings), &settings)
if err != nil {
return settings, bosherr.WrapError(err, "Unmarshalling wrapped settings")
}
return settings, nil
}