Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 1 | package api |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | type ServiceRouterConfigEntry struct { |
| 9 | Kind string |
| 10 | Name string |
| 11 | |
| 12 | Routes []ServiceRoute `json:",omitempty"` |
| 13 | |
| 14 | CreateIndex uint64 |
| 15 | ModifyIndex uint64 |
| 16 | } |
| 17 | |
| 18 | func (e *ServiceRouterConfigEntry) GetKind() string { return e.Kind } |
| 19 | func (e *ServiceRouterConfigEntry) GetName() string { return e.Name } |
| 20 | func (e *ServiceRouterConfigEntry) GetCreateIndex() uint64 { return e.CreateIndex } |
| 21 | func (e *ServiceRouterConfigEntry) GetModifyIndex() uint64 { return e.ModifyIndex } |
| 22 | |
| 23 | type ServiceRoute struct { |
| 24 | Match *ServiceRouteMatch `json:",omitempty"` |
| 25 | Destination *ServiceRouteDestination `json:",omitempty"` |
| 26 | } |
| 27 | |
| 28 | type ServiceRouteMatch struct { |
| 29 | HTTP *ServiceRouteHTTPMatch `json:",omitempty"` |
| 30 | } |
| 31 | |
| 32 | type 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 | |
| 42 | type 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 | |
| 52 | type ServiceRouteHTTPMatchQueryParam struct { |
| 53 | Name string |
| 54 | Present bool `json:",omitempty"` |
| 55 | Exact string `json:",omitempty"` |
| 56 | Regex string `json:",omitempty"` |
| 57 | } |
| 58 | |
| 59 | type 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 | |
| 70 | func (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 | |
| 86 | func (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 | |
| 106 | type ServiceSplitterConfigEntry struct { |
| 107 | Kind string |
| 108 | Name string |
| 109 | |
| 110 | Splits []ServiceSplit `json:",omitempty"` |
| 111 | |
| 112 | CreateIndex uint64 |
| 113 | ModifyIndex uint64 |
| 114 | } |
| 115 | |
| 116 | func (e *ServiceSplitterConfigEntry) GetKind() string { return e.Kind } |
| 117 | func (e *ServiceSplitterConfigEntry) GetName() string { return e.Name } |
| 118 | func (e *ServiceSplitterConfigEntry) GetCreateIndex() uint64 { return e.CreateIndex } |
| 119 | func (e *ServiceSplitterConfigEntry) GetModifyIndex() uint64 { return e.ModifyIndex } |
| 120 | |
| 121 | type ServiceSplit struct { |
| 122 | Weight float32 |
| 123 | Service string `json:",omitempty"` |
| 124 | ServiceSubset string `json:",omitempty"` |
| 125 | Namespace string `json:",omitempty"` |
| 126 | } |
| 127 | |
| 128 | type 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 | |
| 142 | func (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 | |
| 158 | func (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 | |
| 178 | func (e *ServiceResolverConfigEntry) GetKind() string { return e.Kind } |
| 179 | func (e *ServiceResolverConfigEntry) GetName() string { return e.Name } |
| 180 | func (e *ServiceResolverConfigEntry) GetCreateIndex() uint64 { return e.CreateIndex } |
| 181 | func (e *ServiceResolverConfigEntry) GetModifyIndex() uint64 { return e.ModifyIndex } |
| 182 | |
| 183 | type ServiceResolverSubset struct { |
| 184 | Filter string `json:",omitempty"` |
| 185 | OnlyPassing bool `json:",omitempty"` |
| 186 | } |
| 187 | |
| 188 | type ServiceResolverRedirect struct { |
| 189 | Service string `json:",omitempty"` |
| 190 | ServiceSubset string `json:",omitempty"` |
| 191 | Namespace string `json:",omitempty"` |
| 192 | Datacenter string `json:",omitempty"` |
| 193 | } |
| 194 | |
| 195 | type ServiceResolverFailover struct { |
| 196 | Service string `json:",omitempty"` |
| 197 | ServiceSubset string `json:",omitempty"` |
| 198 | Namespace string `json:",omitempty"` |
| 199 | Datacenters []string `json:",omitempty"` |
| 200 | } |