@@ -9,34 +9,24 @@ import (
9
9
"encoding/json"
10
10
"errors"
11
11
"fmt"
12
- "io"
13
12
"net/http"
14
13
"net/url"
15
- "sort"
16
14
"time"
17
-
18
- "github.com/elastic/elastic-agent/pkg/version"
19
15
)
20
16
21
17
const (
22
18
defaultArtifactAPIURL = "https://artifacts-api.elastic.co/"
23
19
24
- artifactsAPIV1VersionsEndpoint = "v1/versions/"
25
20
artifactsAPIV1VersionBuildsEndpoint = "v1/versions/%s/builds/"
26
21
artifactAPIV1BuildDetailsEndpoint = "v1/versions/%s/builds/%s"
27
- // artifactAPIV1SearchVersionPackage = "v1/search/%s/%s"
28
22
29
23
artifactElasticAgentProject = "elastic-agent-package"
30
24
maxAttemptsForArtifactsAPICall = 6
31
25
retryIntervalForArtifactsAPICall = 5 * time .Second
32
26
)
33
27
34
28
var (
35
- ErrLatestVersionNil = errors .New ("latest version is nil" )
36
- ErrSnapshotVersionsEmpty = errors .New ("snapshot list is nil" )
37
- ErrInvalidVersionRetrieved = errors .New ("invalid version retrieved from artifact API" )
38
- ErrBuildNotFound = errors .New ("there are no build that satisfy given conditions" )
39
-
29
+ ErrBuildNotFound = errors .New ("there are no builds that satisfy given conditions" )
40
30
ErrBadHTTPStatusCode = errors .New ("bad http status code" )
41
31
)
42
32
@@ -45,12 +35,6 @@ type Manifests struct {
45
35
SecondsSinceLastUpdate int `json:"seconds-since-last-update"`
46
36
}
47
37
48
- type VersionList struct {
49
- Versions []string `json:"versions"`
50
- Aliases []string `json:"aliases"`
51
- Manifests Manifests `json:"manifests"`
52
- }
53
-
54
38
type VersionBuilds struct {
55
39
Builds []string `json:"builds"`
56
40
Manifests Manifests `json:"manifests"`
@@ -107,11 +91,6 @@ type BuildDetails struct {
107
91
Manifests Manifests `json:"manifests"`
108
92
}
109
93
110
- type SearchPackageResult struct {
111
- Packages map [string ]Package `json:"packages"`
112
- Manifests Manifests `json:"manifests"`
113
- }
114
-
115
94
type httpDoer interface {
116
95
Do (req * http.Request ) (* http.Response , error )
117
96
}
@@ -156,26 +135,10 @@ func NewArtifactAPIClient(opts ...ArtifactAPIClientOpt) *ArtifactAPIClient {
156
135
return c
157
136
}
158
137
159
- // GetVersions returns a list of versions as server by the Artifact API along with some aliases and manifest information
160
- func (aac ArtifactAPIClient ) GetVersions (ctx context.Context ) (list * VersionList , err error ) {
161
- joinedURL , err := aac .composeURL (artifactsAPIV1VersionsEndpoint )
162
- if err != nil {
163
- return nil , err
164
- }
165
-
166
- resp , err := aac .createAndPerformRequest (ctx , joinedURL )
167
- if err != nil {
168
- return nil , fmt .Errorf ("getting versions: %w" , err )
169
- }
170
-
171
- defer resp .Body .Close ()
172
- return checkResponseAndUnmarshal [VersionList ](resp )
173
- }
174
-
175
138
// GetBuildsForVersion returns a list of builds for a specific version.
176
139
// version should be one of the version strings returned by the GetVersions (expected format is semver
177
140
// with optional prerelease but no build metadata, for example 8.9.0-SNAPSHOT)
178
- func (aac ArtifactAPIClient ) GetBuildsForVersion (ctx context.Context , version string ) (builds * VersionBuilds , err error ) {
141
+ func (aac ArtifactAPIClient ) getBuildsForVersion (ctx context.Context , version string ) (builds * VersionBuilds , err error ) {
179
142
joinedURL , err := aac .composeURL (fmt .Sprintf (artifactsAPIV1VersionBuildsEndpoint , version ))
180
143
if err != nil {
181
144
return nil , err
@@ -196,15 +159,15 @@ func (aac ArtifactAPIClient) GetBuildsForVersion(ctx context.Context, version st
196
159
// Setting `offset` to 0 includes all builds, 1 skips the latest, and so forth.
197
160
// If there are no builds matching these conditions, returns `ErrBuildNotFound`.
198
161
func (aac ArtifactAPIClient ) FindBuild (ctx context.Context , version , excludeHash string , offset int ) (buildDetails * BuildDetails , err error ) {
199
- resp , err := aac .GetBuildsForVersion (ctx , version )
162
+ resp , err := aac .getBuildsForVersion (ctx , version )
200
163
if err != nil {
201
164
return nil , fmt .Errorf ("failed to get a list of builds: %w" , err )
202
165
}
203
166
if len (resp .Builds ) < offset + 1 {
204
167
return nil , ErrBuildNotFound
205
168
}
206
169
for _ , buildID := range resp .Builds [offset :] {
207
- details , err := aac .GetBuildDetails (ctx , version , buildID )
170
+ details , err := aac .getBuildDetails (ctx , version , buildID )
208
171
if err != nil {
209
172
return nil , fmt .Errorf ("failed to get build information for %q: %w" , buildID , err )
210
173
}
@@ -219,7 +182,7 @@ func (aac ArtifactAPIClient) FindBuild(ctx context.Context, version, excludeHash
219
182
// GetBuildDetails returns the list of project and artifacts related to a specific build.
220
183
// Version parameter format follows semver (without build metadata) and buildID format is <major>.<minor>.<patch>-<buildhash> as returned by
221
184
// GetBuildsForVersion()
222
- func (aac ArtifactAPIClient ) GetBuildDetails (ctx context.Context , version string , buildID string ) (buildDetails * BuildDetails , err error ) {
185
+ func (aac ArtifactAPIClient ) getBuildDetails (ctx context.Context , version string , buildID string ) (buildDetails * BuildDetails , err error ) {
223
186
joinedURL , err := aac .composeURL (fmt .Sprintf (artifactAPIV1BuildDetailsEndpoint , version , buildID ))
224
187
if err != nil {
225
188
return nil , err
@@ -291,59 +254,12 @@ func checkResponseAndUnmarshal[T any](resp *http.Response) (*T, error) {
291
254
return nil , fmt .Errorf ("%d: %w" , resp .StatusCode , ErrBadHTTPStatusCode )
292
255
}
293
256
294
- respBytes , err := io .ReadAll (resp .Body )
295
- if err != nil {
296
- return nil , fmt .Errorf ("reading response body: %w" , err )
297
- }
257
+ d := json .NewDecoder (resp .Body )
298
258
result := new (T )
299
- err = json .Unmarshal (respBytes , result )
300
-
259
+ err := d .Decode (& result )
301
260
if err != nil {
302
- return nil , fmt .Errorf ("unmarshaling : %w" , err )
261
+ return nil , fmt .Errorf ("failed to parse response : %w" , err )
303
262
}
304
263
305
264
return result , nil
306
265
}
307
-
308
- func (aac ArtifactAPIClient ) GetLatestSnapshotVersion (ctx context.Context ) (* version.ParsedSemVer , error ) {
309
- vList , err := aac .GetVersions (ctx )
310
- if err != nil {
311
- return nil , err
312
- }
313
-
314
- if vList == nil {
315
- return nil , ErrSnapshotVersionsEmpty
316
- }
317
-
318
- sortedParsedVersions := make (version.SortableParsedVersions , 0 , len (vList .Versions ))
319
- for _ , v := range vList .Versions {
320
- pv , err := version .ParseVersion (v )
321
- if err != nil {
322
- aac .logFunc ("invalid version retrieved from artifact API: %q" , v )
323
- return nil , ErrInvalidVersionRetrieved
324
- }
325
- sortedParsedVersions = append (sortedParsedVersions , pv )
326
- }
327
-
328
- if len (sortedParsedVersions ) == 0 {
329
- return nil , ErrSnapshotVersionsEmpty
330
- }
331
-
332
- // normally the output of the versions returned by artifact API is already
333
- // sorted in ascending order.If we want to sort in descending order we need
334
- // to pass a sort.Reverse to sort.Sort.
335
- sort .Sort (sort .Reverse (sortedParsedVersions ))
336
-
337
- var latestSnapshotVersion * version.ParsedSemVer
338
- // fetch the latest SNAPSHOT build
339
- for _ , pv := range sortedParsedVersions {
340
- if pv .IsSnapshot () {
341
- latestSnapshotVersion = pv
342
- break
343
- }
344
- }
345
- if latestSnapshotVersion == nil {
346
- return nil , ErrLatestVersionNil
347
- }
348
- return latestSnapshotVersion , nil
349
- }
0 commit comments