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