-
Notifications
You must be signed in to change notification settings - Fork 0
/
streams.go
125 lines (104 loc) · 2.54 KB
/
streams.go
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
package streams
import (
"context"
"encoding/json"
"fmt"
"github.com/patrickmn/go-cache"
"io/ioutil"
"net/http"
"net/url"
"time"
)
const (
streamCachePrefix = "stream cache for: "
)
type Arch struct {
Artifacts map[string]Artifact `json:"artifacts"`
Images CloudImages `json:"images"`
}
type CloudImages struct {
AWS AWSImages `json:"aws"`
GCP GCPImages `json:"gcp"`
}
type AWSRegionalOffering struct {
Release string `json:"release"`
Image string `json:"image"`
}
type AWSImages struct {
Regions map[string]AWSRegionalOffering `json:"regions"`
}
type GCPImages struct {
Project string `json:"project"`
Family string `json:"family"`
Name string `json:"name"`
}
type StreamMetadata struct {
LastModified string `json:"last-modified"`
}
type Formats map[string]map[string]*Resource
type Artifact struct {
Release string `json:"release"`
Formats `json:"formats"`
}
type Resource struct {
Location string `json:"location"`
Signature string `json:"signature"`
Sha256 string `json:"sha256"`
}
type Stream struct {
Name string `json:"stream"`
Metadata StreamMetadata `json:"metadata"`
Architectures map[string]Arch `json:"architectures"`
}
type Resolver struct {
cli *http.Client
cache *cache.Cache
}
func New() Resolver {
return Resolver{
cli: &http.Client{Timeout: 30 * time.Second},
cache: cache.New(5*time.Minute, 10*time.Minute),
}
}
func (r Resolver) Resolve(ctx context.Context, stream string) (*Stream, error) {
key := streamCachePrefix + stream
cached, found := r.cache.Get(key)
if found {
return cached.(*Stream), nil
}
s, err := r.resolve(ctx, stream)
if err != nil {
return nil, err
}
r.cache.Set(key, s, cache.DefaultExpiration)
return s, nil
}
func (r Resolver) resolve(ctx context.Context, stream string) (*Stream, error) {
u := &url.URL{
Scheme: "https",
Host: "builds.coreos.fedoraproject.org",
Path: "/streams/" + stream + ".json",
}
req := http.Request{
Method: "GET",
URL: u,
}
req = *req.WithContext(ctx)
resp, err := r.cli.Do(&req)
if err != nil {
return nil, fmt.Errorf("failed to fetch %v: %w", u, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("got non-OK status when fetching %v: %v", u, resp.Status)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read body of %v: %w", u, err)
}
strms := &Stream{}
err = json.Unmarshal(bodyBytes, strms)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal contents of %v: %w", u, err)
}
return strms, nil
}