|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package product_versions |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "net/http" |
| 12 | + |
| 13 | + "github.com/elastic/elastic-agent/pkg/version" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + // Every product on the version list has a unique product ID |
| 18 | + // This product ID belongs to Elastic Agent excluding alpha/beta/RC versions. |
| 19 | + elasticAgentProductID = "bltce270507523f4c56" |
| 20 | + productVersionsAPIURL = "https://www.elastic.co/api/product_versions" |
| 21 | +) |
| 22 | + |
| 23 | +type item struct { |
| 24 | + // Version contains the actual semantic version, e.g. `8.12.1` |
| 25 | + Version string `json:"version_number"` |
| 26 | + // Product contains a list of product IDs, for the agent it should |
| 27 | + // be a single item that equals `elasticAgentProductID` |
| 28 | + Product []string `json:"product"` |
| 29 | +} |
| 30 | + |
| 31 | +type httpDoer interface { |
| 32 | + Do(req *http.Request) (*http.Response, error) |
| 33 | +} |
| 34 | + |
| 35 | +type ProductVersionsClientOpt func(pvc *ProductVersionsClient) |
| 36 | + |
| 37 | +func WithUrl(url string) ProductVersionsClientOpt { |
| 38 | + return func(pvc *ProductVersionsClient) { pvc.url = url } |
| 39 | +} |
| 40 | + |
| 41 | +func WithHttpClient(client httpDoer) ProductVersionsClientOpt { |
| 42 | + return func(pvc *ProductVersionsClient) { pvc.c = client } |
| 43 | +} |
| 44 | + |
| 45 | +type ProductVersionsClient struct { |
| 46 | + c httpDoer |
| 47 | + url string |
| 48 | +} |
| 49 | + |
| 50 | +// NewProductVersionsClient creates a new client applying all the given options. |
| 51 | +// If not set by the options, the new client will use the default HTTP client and |
| 52 | +// the default URL from `productVersionsAPIURL`. |
| 53 | +// |
| 54 | +// All the timeout/retry/backoff behavior must be implemented by the `httpDoer` interface |
| 55 | +// and set by the `WithClient` option. |
| 56 | +func NewProductVersionsClient(opts ...ProductVersionsClientOpt) *ProductVersionsClient { |
| 57 | + c := &ProductVersionsClient{ |
| 58 | + url: productVersionsAPIURL, |
| 59 | + c: http.DefaultClient, |
| 60 | + } |
| 61 | + |
| 62 | + for _, opt := range opts { |
| 63 | + opt(c) |
| 64 | + } |
| 65 | + |
| 66 | + return c |
| 67 | +} |
| 68 | + |
| 69 | +// FetchAgentVersions returns a sortable list of parsed semantic versions for Elastic Agent. |
| 70 | +// This list contains only publicly available versions/releases ordered by their creation date. |
| 71 | +func (pvc *ProductVersionsClient) FetchAgentVersions(ctx context.Context) (version.SortableParsedVersions, error) { |
| 72 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, pvc.url, nil) |
| 73 | + if err != nil { |
| 74 | + err = fmt.Errorf("failed to create request: %w", err) |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + resp, err := pvc.c.Do(req) |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("failed to do request: %w", err) |
| 81 | + } |
| 82 | + defer resp.Body.Close() |
| 83 | + if resp.StatusCode != http.StatusOK { |
| 84 | + return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) |
| 85 | + } |
| 86 | + |
| 87 | + // The body is large (> 15MB), so streaming decoder is used |
| 88 | + d := json.NewDecoder(resp.Body) |
| 89 | + |
| 90 | + var versions [][]item |
| 91 | + err = d.Decode(&versions) |
| 92 | + if err != nil { |
| 93 | + return nil, fmt.Errorf("failed to decode JSON: %w", err) |
| 94 | + } |
| 95 | + if len(versions) == 0 { |
| 96 | + return []*version.ParsedSemVer{}, nil |
| 97 | + } |
| 98 | + |
| 99 | + var versionList version.SortableParsedVersions |
| 100 | + // there are 2 array levels |
| 101 | + for _, i := range versions { |
| 102 | + for _, v := range i { |
| 103 | + if len(v.Product) != 1 { |
| 104 | + continue |
| 105 | + } |
| 106 | + if v.Product[0] != elasticAgentProductID { |
| 107 | + continue |
| 108 | + } |
| 109 | + parsed, err := version.ParseVersion(v.Version) |
| 110 | + if err != nil { |
| 111 | + return nil, fmt.Errorf("failed to parse %s: %w", v.Version, err) |
| 112 | + } |
| 113 | + versionList = append(versionList, parsed) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return versionList, nil |
| 118 | +} |
0 commit comments