-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.go
More file actions
184 lines (166 loc) · 6.44 KB
/
Copy pathbuilder.go
File metadata and controls
184 lines (166 loc) · 6.44 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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package hypeman
import (
"context"
"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"
)
// BuilderService 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 [NewBuilderService] method instead.
type BuilderService struct {
Options []option.RequestOption
}
// NewBuilderService 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 NewBuilderService(opts ...option.RequestOption) (r BuilderService) {
r = BuilderService{}
r.Options = opts
return
}
// Creates a builder and its cache disk. One build at a time runs per builder.
func (r *BuilderService) New(ctx context.Context, body BuilderNewParams, opts ...option.RequestOption) (res *Builder, err error) {
opts = slices.Concat(r.Options, opts)
path := "builders"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// List builders
func (r *BuilderService) List(ctx context.Context, query BuilderListParams, opts ...option.RequestOption) (res *[]Builder, err error) {
opts = slices.Concat(r.Options, opts)
path := "builders"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return res, err
}
// Permanently deletes a builder and its cache disk.
func (r *BuilderService) 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("builders/%s", id)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, nil, opts...)
return err
}
// Get builder details
func (r *BuilderService) Get(ctx context.Context, id string, opts ...option.RequestOption) (res *Builder, err error) {
opts = slices.Concat(r.Options, opts)
if id == "" {
err = errors.New("missing required id parameter")
return nil, err
}
path := fmt.Sprintf("builders/%s", id)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
// Resets the builder's cache disk. The builder transitions to pruning, then ready.
// Builder identity is preserved.
func (r *BuilderService) Prune(ctx context.Context, id string, opts ...option.RequestOption) (res *Builder, err error) {
opts = slices.Concat(r.Options, opts)
if id == "" {
err = errors.New("missing required id parameter")
return nil, err
}
path := fmt.Sprintf("builders/%s/prune", id)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
return res, err
}
type Builder struct {
// Builder identifier
ID string `json:"id" api:"required"`
// Creation timestamp (RFC3339)
CreatedAt time.Time `json:"created_at" api:"required" format:"date-time"`
// Persistent builder cache disk size in gigabytes. Cannot be changed after
// creation.
DiskSizeGB int64 `json:"disk_size_gb" api:"required"`
// Maximum concurrent builds on this builder. Currently fixed at 1.
MaxConcurrency int64 `json:"max_concurrency" api:"required"`
// Point-in-time IDs of queued builds waiting for this builder, oldest first
QueuedBuilds []string `json:"queued_builds" api:"required"`
// Builder lifecycle status
//
// Any of "ready", "pruning", "deleting", "error".
Status BuilderStatus `json:"status" api:"required"`
// Point-in-time ID of the build currently running on this builder
ActiveBuildID string `json:"active_build_id" api:"nullable"`
// When a build last ran on this builder
LastUsedAt time.Time `json:"last_used_at" api:"nullable" format:"date-time"`
// Optional non-unique display name
Name string `json:"name"`
// 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
DiskSizeGB respjson.Field
MaxConcurrency respjson.Field
QueuedBuilds respjson.Field
Status respjson.Field
ActiveBuildID respjson.Field
LastUsedAt respjson.Field
Name respjson.Field
Tags respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r Builder) RawJSON() string { return r.JSON.raw }
func (r *Builder) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Builder lifecycle status
type BuilderStatus string
const (
BuilderStatusReady BuilderStatus = "ready"
BuilderStatusPruning BuilderStatus = "pruning"
BuilderStatusDeleting BuilderStatus = "deleting"
BuilderStatusError BuilderStatus = "error"
)
type BuilderNewParams struct {
// Optional caller-supplied identifier, auto-generated if not provided
ID param.Opt[string] `json:"id,omitzero"`
// Cache disk size in gigabytes. Omit to use the server default.
DiskSizeGB param.Opt[int64] `json:"disk_size_gb,omitzero"`
// Optional non-unique display name
Name param.Opt[string] `json:"name,omitzero"`
// User-defined key-value tags.
Tags map[string]string `json:"tags,omitzero"`
paramObj
}
func (r BuilderNewParams) MarshalJSON() (data []byte, err error) {
type shadow BuilderNewParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *BuilderNewParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type BuilderListParams struct {
// Filter builders by tag key-value pairs.
Tags map[string]string `query:"tags,omitzero" json:"-"`
paramObj
}
// URLQuery serializes [BuilderListParams]'s query parameters as `url.Values`.
func (r BuilderListParams) URLQuery() (v url.Values, err error) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}