-
Notifications
You must be signed in to change notification settings - Fork 2
/
types.go
executable file
·259 lines (231 loc) · 8.32 KB
/
types.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2016 IBM Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//Package goEurekaClient Implements a go client that interacts with a eureka server
package goEurekaClient
import (
"encoding/json"
"fmt"
)
const (
// UP means the application instance is up.
UP StatusType = "UP"
// DOWN means the application instance is down.
DOWN StatusType = "DOWN"
// STARTING means the application instance is initializing .
STARTING StatusType = "STARTING"
// OUTOFSERVICE means the application instance is out of service.
OUTOFSERVICE StatusType = "OUT_OF_SERVICE"
// UNKNOWN means the application instance status is unknown.
UNKNOWN StatusType = "UNKNOWN"
)
// Port encapsulates information needed for a port information
type Port struct {
Enabled string `json:"@enabled,omitempty"`
Value interface{} `json:"$,omitempty"`
}
// DatacenterMetadata encapsulates information needed for a datacenter metadata information
type DatacenterMetadata map[string]interface{}
// DatacenterInfo encapsulates information needed for a datacenter information
type DatacenterInfo struct {
Class string `json:"@class,omitempty"`
Name string `json:"name,omitempty"`
Metadata DatacenterMetadata `json:"metadata,omitempty"`
}
// LeaseInfo encapsulates information needed for a lease information
type LeaseInfo struct {
RenewalInt uint32 `json:"renewalIntervalInSecs,omitempty"`
DurationInt uint32 `json:"durationInSecs,omitempty"`
RegistrationTs int64 `json:"registrationTimestamp,omitempty"`
LastRenewalTs int64 `json:"lastRenewalTimestamp,omitempty"`
}
// Instance encapsulates information needed for a service instance information
type Instance struct {
ID string `json:"instanceId,omitempty"`
HostName string `json:"hostName,omitempty"`
Application string `json:"app,omitempty"`
GroupName string `json:"appGroupName,omitempty"`
IPAddr string `json:"ipAddr,omitempty"`
VIPAddr string `json:"vipAddress,omitempty"`
SecVIPAddr string `json:"secureVipAddress,omitempty"`
Status string `json:"status,omitempty"`
OvrStatus string `json:"overriddenstatus,omitempty"`
CountryID int `json:"countryId,omitempty"`
Port *Port `json:"port,omitempty"`
SecPort *Port `json:"securePort,omitempty"`
HomePage string `json:"homePageUrl,omitempty"`
StatusPage string `json:"statusPageUrl,omitempty"`
HealthCheck string `json:"healthCheckUrl,omitempty"`
Datacenter *DatacenterInfo `json:"dataCenterInfo,omitempty"`
Lease *LeaseInfo `json:"leaseInfo,omitempty"`
Metadata json.RawMessage `json:"metadata,omitempty"`
CordServer interface{} `json:"isCoordinatingDiscoveryServer,omitempty"`
LastUpdatedTs interface{} `json:"lastUpdatedTimestamp,omitempty"`
LastDirtyTs interface{} `json:"lastDirtyTimestamp,omitempty"`
ActionType string `json:"actionType,omitempty"`
}
// InstanceWrapper encapsulates information needed for a service instance registration
type instanceWrapper struct {
Inst *Instance `json:"instance,omitempty"`
}
// String output the structure
func (ir *Instance) String() string {
mtlen := 0
if ir.Metadata != nil {
mtlen = len(ir.Metadata)
}
return fmt.Sprintf("vip_addres: %s, endpoint: %s:%d, hostname: %s, status: %s, metadata: %d",
ir.VIPAddr, ir.IPAddr, ir.Port.Value, ir.HostName, ir.Status, mtlen)
}
func (ir *Instance) deepCopy() Instance {
copyPort := Port{
Enabled: ir.Port.Enabled,
Value: ir.Port.Value,
}
copySecPort := Port{
Enabled: ir.SecPort.Enabled,
Value: ir.SecPort.Value,
}
copyDatacenter := DatacenterInfo{
Class: ir.Datacenter.Class,
Metadata: ir.Datacenter.Metadata,
Name: ir.Datacenter.Name,
}
copyLease := LeaseInfo{
DurationInt: ir.Lease.DurationInt,
LastRenewalTs: ir.Lease.LastRenewalTs,
RegistrationTs: ir.Lease.RegistrationTs,
RenewalInt: ir.Lease.RenewalInt,
}
copyInst := Instance{
ID: ir.ID,
HostName: ir.HostName,
Application: ir.Application,
GroupName: ir.GroupName,
IPAddr: ir.IPAddr,
VIPAddr: ir.VIPAddr,
SecVIPAddr: ir.SecVIPAddr,
Status: ir.Status,
OvrStatus: ir.OvrStatus,
CountryID: ir.CountryID,
Port: ©Port,
SecPort: ©SecPort,
HomePage: ir.HomePage,
StatusPage: ir.StatusPage,
HealthCheck: ir.HealthCheck,
Datacenter: ©Datacenter,
Lease: ©Lease,
Metadata: ir.Metadata,
CordServer: ir.CordServer,
LastUpdatedTs: ir.LastDirtyTs,
LastDirtyTs: ir.LastDirtyTs,
ActionType: ir.ActionType,
}
return copyInst
}
//StatusType defines status of the application instances.
type StatusType string
//DataCenterType defines the DataCenter
type DataCenterType string
// InstanceEventHandler can handle notifications for events that happen
// to a instance. The events are information only, so you can't return
// an error.
type InstanceEventHandler interface {
OnAdd(inst *Instance)
OnUpdate(oldInst, newInst *Instance)
OnDelete(int *Instance)
}
// Application is an array of instances
type Application struct {
Name string `json:"name,omitempty"`
Instances []*Instance `json:"instance,omitempty"`
}
// UnmarshalJSON parses the JSON object of Application struct.
// We need this specific implementation because the Eureka server
// marshals differently single instance (object) and multiple instances (array).
func (app *Application) UnmarshalJSON(b []byte) error {
type singleApplication struct {
Name string `json:"name,omitempty"`
Instance *Instance `json:"instance,omitempty"`
}
type multiApplication struct {
Name string `json:"name,omitempty"`
Instances []*Instance `json:"instance,omitempty"`
}
var mApp multiApplication
err := json.Unmarshal(b, &mApp)
if err != nil {
// error probably means that we have a single instance object.
// Thus, we try to unmarshal to a different object type
var sApp singleApplication
err = json.Unmarshal(b, &sApp)
if err != nil {
return err
}
app.Name = sApp.Name
if sApp.Instance != nil {
app.Instances = []*Instance{sApp.Instance}
}
return nil
}
app.Name = mApp.Name
app.Instances = mApp.Instances
return nil
}
type appVersion struct {
VersionDelta int64 `json:"versions__delta,omitempty"`
Hashcode string `json:"apps__hashcode,omitempty"`
}
// Applications is an array of application objects
type Applications struct {
appVersion
Application []*Application `json:"application,omitempty"`
}
// UnmarshalJSON parses the JSON object of Applications struct.
// We need this specific implementation because the Eureka server
// marshals differently single application (object) and multiple applications (array).
func (apps *Applications) UnmarshalJSON(b []byte) error {
type singleApplications struct {
appVersion
Application *Application `json:"application,omitempty"`
}
type multiApplications struct {
appVersion
Application []*Application `json:"application,omitempty"`
}
var mApps multiApplications
err := json.Unmarshal(b, &mApps)
if err != nil {
// error probably means that we have a single Application object.
// Thus, we try to unmarshal to a different object type
var sApps singleApplications
err = json.Unmarshal(b, &sApps)
if err != nil {
return err
}
apps.Hashcode = sApps.Hashcode
apps.VersionDelta = sApps.VersionDelta
if sApps.Application != nil {
apps.Application = []*Application{sApps.Application}
}
return nil
}
apps.Hashcode = mApps.Hashcode
apps.VersionDelta = mApps.VersionDelta
apps.Application = mApps.Application
return nil
}
// ApplicationsList is a list of application objects
type applicationsList struct {
Applications *Applications `json:"applications,omitempty"`
}