-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathingress.go
More file actions
317 lines (287 loc) · 10.9 KB
/
ingress.go
File metadata and controls
317 lines (287 loc) · 10.9 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package hypeman
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"slices"
"time"
"github.com/kernel/hypeman-go/internal/apijson"
"github.com/kernel/hypeman-go/internal/apiquery"
"github.com/kernel/hypeman-go/internal/requestconfig"
"github.com/kernel/hypeman-go/option"
"github.com/kernel/hypeman-go/packages/param"
"github.com/kernel/hypeman-go/packages/respjson"
)
// IngressService contains methods and other services that help with interacting
// with the hypeman API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewIngressService] method instead.
type IngressService struct {
Options []option.RequestOption
}
// NewIngressService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewIngressService(opts ...option.RequestOption) (r IngressService) {
r = IngressService{}
r.Options = opts
return
}
// Create ingress
func (r *IngressService) New(ctx context.Context, body IngressNewParams, opts ...option.RequestOption) (res *Ingress, err error) {
opts = slices.Concat(r.Options, opts)
path := "ingresses"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// List ingresses
func (r *IngressService) List(ctx context.Context, query IngressListParams, opts ...option.RequestOption) (res *[]Ingress, err error) {
opts = slices.Concat(r.Options, opts)
path := "ingresses"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return res, err
}
// Delete ingress
func (r *IngressService) Delete(ctx context.Context, id string, opts ...option.RequestOption) (err error) {
opts = slices.Concat(r.Options, opts)
opts = append([]option.RequestOption{option.WithHeader("Accept", "*/*")}, opts...)
if id == "" {
err = errors.New("missing required id parameter")
return err
}
path := fmt.Sprintf("ingresses/%s", id)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, nil, opts...)
return err
}
// Get ingress details
func (r *IngressService) Get(ctx context.Context, id string, opts ...option.RequestOption) (res *Ingress, err error) {
opts = slices.Concat(r.Options, opts)
if id == "" {
err = errors.New("missing required id parameter")
return nil, err
}
path := fmt.Sprintf("ingresses/%s", id)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
type Ingress struct {
// Auto-generated unique identifier
ID string `json:"id" api:"required"`
// Creation timestamp (RFC3339)
CreatedAt time.Time `json:"created_at" api:"required" format:"date-time"`
// Human-readable name
Name string `json:"name" api:"required"`
// Routing rules for this ingress
Rules []IngressRule `json:"rules" api:"required"`
// User-defined key-value tags.
Tags map[string]string `json:"tags"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ID respjson.Field
CreatedAt respjson.Field
Name respjson.Field
Rules respjson.Field
Tags respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r Ingress) RawJSON() string { return r.JSON.raw }
func (r *Ingress) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type IngressMatch struct {
// Hostname to match. Can be:
//
// - Literal: "api.example.com" (exact match on Host header)
// - Pattern: "{instance}.example.com" (dynamic routing based on subdomain)
//
// Pattern hostnames use named captures in curly braces (e.g., {instance}, {app})
// that extract parts of the hostname for routing. The extracted values can be
// referenced in the target.instance field.
Hostname string `json:"hostname" api:"required"`
// Host port to listen on for this rule (default 80)
Port int64 `json:"port"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Hostname respjson.Field
Port respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r IngressMatch) RawJSON() string { return r.JSON.raw }
func (r *IngressMatch) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// ToParam converts this IngressMatch to a IngressMatchParam.
//
// Warning: the fields of the param type will not be present. ToParam should only
// be used at the last possible moment before sending a request. Test for this with
// IngressMatchParam.Overrides()
func (r IngressMatch) ToParam() IngressMatchParam {
return param.Override[IngressMatchParam](json.RawMessage(r.RawJSON()))
}
// The property Hostname is required.
type IngressMatchParam struct {
// Hostname to match. Can be:
//
// - Literal: "api.example.com" (exact match on Host header)
// - Pattern: "{instance}.example.com" (dynamic routing based on subdomain)
//
// Pattern hostnames use named captures in curly braces (e.g., {instance}, {app})
// that extract parts of the hostname for routing. The extracted values can be
// referenced in the target.instance field.
Hostname string `json:"hostname" api:"required"`
// Host port to listen on for this rule (default 80)
Port param.Opt[int64] `json:"port,omitzero"`
paramObj
}
func (r IngressMatchParam) MarshalJSON() (data []byte, err error) {
type shadow IngressMatchParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *IngressMatchParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type IngressRule struct {
Match IngressMatch `json:"match" api:"required"`
Target IngressTarget `json:"target" api:"required"`
// Auto-create HTTP to HTTPS redirect for this hostname (only applies when tls is
// enabled)
RedirectHTTP bool `json:"redirect_http"`
// Enable TLS termination (certificate auto-issued via ACME).
Tls bool `json:"tls"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Match respjson.Field
Target respjson.Field
RedirectHTTP respjson.Field
Tls respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r IngressRule) RawJSON() string { return r.JSON.raw }
func (r *IngressRule) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// ToParam converts this IngressRule to a IngressRuleParam.
//
// Warning: the fields of the param type will not be present. ToParam should only
// be used at the last possible moment before sending a request. Test for this with
// IngressRuleParam.Overrides()
func (r IngressRule) ToParam() IngressRuleParam {
return param.Override[IngressRuleParam](json.RawMessage(r.RawJSON()))
}
// The properties Match, Target are required.
type IngressRuleParam struct {
Match IngressMatchParam `json:"match,omitzero" api:"required"`
Target IngressTargetParam `json:"target,omitzero" api:"required"`
// Auto-create HTTP to HTTPS redirect for this hostname (only applies when tls is
// enabled)
RedirectHTTP param.Opt[bool] `json:"redirect_http,omitzero"`
// Enable TLS termination (certificate auto-issued via ACME).
Tls param.Opt[bool] `json:"tls,omitzero"`
paramObj
}
func (r IngressRuleParam) MarshalJSON() (data []byte, err error) {
type shadow IngressRuleParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *IngressRuleParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type IngressTarget struct {
// Target instance name, ID, or capture reference.
//
// - For literal hostnames: Use the instance name or ID directly (e.g., "my-api")
// - For pattern hostnames: Reference a capture from the hostname (e.g.,
// "{instance}")
//
// When using pattern hostnames, the instance is resolved dynamically at request
// time.
Instance string `json:"instance" api:"required"`
// Target port on the instance
Port int64 `json:"port" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Instance respjson.Field
Port respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r IngressTarget) RawJSON() string { return r.JSON.raw }
func (r *IngressTarget) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// ToParam converts this IngressTarget to a IngressTargetParam.
//
// Warning: the fields of the param type will not be present. ToParam should only
// be used at the last possible moment before sending a request. Test for this with
// IngressTargetParam.Overrides()
func (r IngressTarget) ToParam() IngressTargetParam {
return param.Override[IngressTargetParam](json.RawMessage(r.RawJSON()))
}
// The properties Instance, Port are required.
type IngressTargetParam struct {
// Target instance name, ID, or capture reference.
//
// - For literal hostnames: Use the instance name or ID directly (e.g., "my-api")
// - For pattern hostnames: Reference a capture from the hostname (e.g.,
// "{instance}")
//
// When using pattern hostnames, the instance is resolved dynamically at request
// time.
Instance string `json:"instance" api:"required"`
// Target port on the instance
Port int64 `json:"port" api:"required"`
paramObj
}
func (r IngressTargetParam) MarshalJSON() (data []byte, err error) {
type shadow IngressTargetParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *IngressTargetParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type IngressNewParams struct {
// Human-readable name (lowercase letters, digits, and dashes only; cannot start or
// end with a dash)
Name string `json:"name" api:"required"`
// Routing rules for this ingress
Rules []IngressRuleParam `json:"rules,omitzero" api:"required"`
// User-defined key-value tags.
Tags map[string]string `json:"tags,omitzero"`
paramObj
}
func (r IngressNewParams) MarshalJSON() (data []byte, err error) {
type shadow IngressNewParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *IngressNewParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type IngressListParams struct {
// Filter ingresses by tag key-value pairs.
Tags map[string]string `query:"tags,omitzero" json:"-"`
paramObj
}
// URLQuery serializes [IngressListParams]'s query parameters as `url.Values`.
func (r IngressListParams) URLQuery() (v url.Values, err error) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}