blob: 77acfbddf1ea0d4b93f2912b6209eb2abce8055b [file] [log] [blame]
David Bainbridge788e5202019-10-21 18:49:40 +00001package api
2
3import (
4 "encoding/json"
5 "time"
6)
7
8type ServiceRouterConfigEntry struct {
9 Kind string
10 Name string
11
12 Routes []ServiceRoute `json:",omitempty"`
13
14 CreateIndex uint64
15 ModifyIndex uint64
16}
17
18func (e *ServiceRouterConfigEntry) GetKind() string { return e.Kind }
19func (e *ServiceRouterConfigEntry) GetName() string { return e.Name }
20func (e *ServiceRouterConfigEntry) GetCreateIndex() uint64 { return e.CreateIndex }
21func (e *ServiceRouterConfigEntry) GetModifyIndex() uint64 { return e.ModifyIndex }
22
23type ServiceRoute struct {
24 Match *ServiceRouteMatch `json:",omitempty"`
25 Destination *ServiceRouteDestination `json:",omitempty"`
26}
27
28type ServiceRouteMatch struct {
29 HTTP *ServiceRouteHTTPMatch `json:",omitempty"`
30}
31
32type ServiceRouteHTTPMatch struct {
33 PathExact string `json:",omitempty"`
34 PathPrefix string `json:",omitempty"`
35 PathRegex string `json:",omitempty"`
36
37 Header []ServiceRouteHTTPMatchHeader `json:",omitempty"`
38 QueryParam []ServiceRouteHTTPMatchQueryParam `json:",omitempty"`
39 Methods []string `json:",omitempty"`
40}
41
42type ServiceRouteHTTPMatchHeader struct {
43 Name string
44 Present bool `json:",omitempty"`
45 Exact string `json:",omitempty"`
46 Prefix string `json:",omitempty"`
47 Suffix string `json:",omitempty"`
48 Regex string `json:",omitempty"`
49 Invert bool `json:",omitempty"`
50}
51
52type ServiceRouteHTTPMatchQueryParam struct {
53 Name string
54 Present bool `json:",omitempty"`
55 Exact string `json:",omitempty"`
56 Regex string `json:",omitempty"`
57}
58
59type ServiceRouteDestination struct {
60 Service string `json:",omitempty"`
61 ServiceSubset string `json:",omitempty"`
62 Namespace string `json:",omitempty"`
63 PrefixRewrite string `json:",omitempty"`
64 RequestTimeout time.Duration `json:",omitempty"`
65 NumRetries uint32 `json:",omitempty"`
66 RetryOnConnectFailure bool `json:",omitempty"`
67 RetryOnStatusCodes []uint32 `json:",omitempty"`
68}
69
70func (e *ServiceRouteDestination) MarshalJSON() ([]byte, error) {
71 type Alias ServiceRouteDestination
72 exported := &struct {
73 RequestTimeout string `json:",omitempty"`
74 *Alias
75 }{
76 RequestTimeout: e.RequestTimeout.String(),
77 Alias: (*Alias)(e),
78 }
79 if e.RequestTimeout == 0 {
80 exported.RequestTimeout = ""
81 }
82
83 return json.Marshal(exported)
84}
85
86func (e *ServiceRouteDestination) UnmarshalJSON(data []byte) error {
87 type Alias ServiceRouteDestination
88 aux := &struct {
89 RequestTimeout string
90 *Alias
91 }{
92 Alias: (*Alias)(e),
93 }
94 if err := json.Unmarshal(data, &aux); err != nil {
95 return err
96 }
97 var err error
98 if aux.RequestTimeout != "" {
99 if e.RequestTimeout, err = time.ParseDuration(aux.RequestTimeout); err != nil {
100 return err
101 }
102 }
103 return nil
104}
105
106type ServiceSplitterConfigEntry struct {
107 Kind string
108 Name string
109
110 Splits []ServiceSplit `json:",omitempty"`
111
112 CreateIndex uint64
113 ModifyIndex uint64
114}
115
116func (e *ServiceSplitterConfigEntry) GetKind() string { return e.Kind }
117func (e *ServiceSplitterConfigEntry) GetName() string { return e.Name }
118func (e *ServiceSplitterConfigEntry) GetCreateIndex() uint64 { return e.CreateIndex }
119func (e *ServiceSplitterConfigEntry) GetModifyIndex() uint64 { return e.ModifyIndex }
120
121type ServiceSplit struct {
122 Weight float32
123 Service string `json:",omitempty"`
124 ServiceSubset string `json:",omitempty"`
125 Namespace string `json:",omitempty"`
126}
127
128type ServiceResolverConfigEntry struct {
129 Kind string
130 Name string
131
132 DefaultSubset string `json:",omitempty"`
133 Subsets map[string]ServiceResolverSubset `json:",omitempty"`
134 Redirect *ServiceResolverRedirect `json:",omitempty"`
135 Failover map[string]ServiceResolverFailover `json:",omitempty"`
136 ConnectTimeout time.Duration `json:",omitempty"`
137
138 CreateIndex uint64
139 ModifyIndex uint64
140}
141
142func (e *ServiceResolverConfigEntry) MarshalJSON() ([]byte, error) {
143 type Alias ServiceResolverConfigEntry
144 exported := &struct {
145 ConnectTimeout string `json:",omitempty"`
146 *Alias
147 }{
148 ConnectTimeout: e.ConnectTimeout.String(),
149 Alias: (*Alias)(e),
150 }
151 if e.ConnectTimeout == 0 {
152 exported.ConnectTimeout = ""
153 }
154
155 return json.Marshal(exported)
156}
157
158func (e *ServiceResolverConfigEntry) UnmarshalJSON(data []byte) error {
159 type Alias ServiceResolverConfigEntry
160 aux := &struct {
161 ConnectTimeout string
162 *Alias
163 }{
164 Alias: (*Alias)(e),
165 }
166 if err := json.Unmarshal(data, &aux); err != nil {
167 return err
168 }
169 var err error
170 if aux.ConnectTimeout != "" {
171 if e.ConnectTimeout, err = time.ParseDuration(aux.ConnectTimeout); err != nil {
172 return err
173 }
174 }
175 return nil
176}
177
178func (e *ServiceResolverConfigEntry) GetKind() string { return e.Kind }
179func (e *ServiceResolverConfigEntry) GetName() string { return e.Name }
180func (e *ServiceResolverConfigEntry) GetCreateIndex() uint64 { return e.CreateIndex }
181func (e *ServiceResolverConfigEntry) GetModifyIndex() uint64 { return e.ModifyIndex }
182
183type ServiceResolverSubset struct {
184 Filter string `json:",omitempty"`
185 OnlyPassing bool `json:",omitempty"`
186}
187
188type ServiceResolverRedirect struct {
189 Service string `json:",omitempty"`
190 ServiceSubset string `json:",omitempty"`
191 Namespace string `json:",omitempty"`
192 Datacenter string `json:",omitempty"`
193}
194
195type ServiceResolverFailover struct {
196 Service string `json:",omitempty"`
197 ServiceSubset string `json:",omitempty"`
198 Namespace string `json:",omitempty"`
199 Datacenters []string `json:",omitempty"`
200}