• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

astronomer / astro-cli / 49762f05-7c9b-4bc2-ad5b-90283f076e72

20 Mar 2026 10:25PM UTC coverage: 36.028% (+0.2%) from 35.862%
49762f05-7c9b-4bc2-ad5b-90283f076e72

Pull #2042

circleci

kaxil
Add `astro api registry` command for querying the Airflow Provider Registry

Ports the `af registry` CLI from astronomer/agents to the astro-cli,
giving users access to the public Airflow Provider Registry
(https://airflow.apache.org/registry) without leaving the astro CLI.

New subcommands:
  astro api registry providers              - list all providers
  astro api registry modules <provider>     - list operators/hooks/sensors
  astro api registry parameters <provider>  - show constructor parameters
  astro api registry connections <provider>  - show connection types

Supports --version/-v to pin a provider version, --jq/-q and
--template/-t for output filtering, --registry-url to override the
base URL (also ASTRO_REGISTRY_URL env), and --no-cache to bypass
the file-based cache (~/.astro/.registry_cache/).
Pull Request #2042: Add `astro api registry` command

190 of 213 new or added lines in 3 files covered. (89.2%)

19 existing lines in 1 file now uncovered.

24422 of 67787 relevant lines covered (36.03%)

8.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.04
/pkg/openapi/cache.go
1
package openapi
2

3
import (
4
        "context"
5
        "encoding/json"
6
        "fmt"
7
        "io"
8
        "net/http"
9
        "os"
10
        "path/filepath"
11
        "strings"
12
        "time"
13

14
        "github.com/astronomer/astro-cli/config"
15
        v3high "github.com/pb33f/libopenapi/datamodel/high/v3"
16
)
17

18
const (
19
        // SpecURL is the URL to fetch the Astro Cloud API OpenAPI specification.
20
        SpecURL = "https://api.astronomer.io/spec/v1.0"
21
        // CloudCacheFileName is the name of the cache file for the Astro Cloud API.
22
        CloudCacheFileName = "openapi-cache.json"
23
        // AirflowCacheFileNameTemplate is the template for version-specific Airflow cache files.
24
        AirflowCacheFileNameTemplate = "openapi-airflow-%s-cache.json"
25
        // CacheTTL is how long the cache is valid.
26
        CacheTTL = 24 * time.Hour
27
        // FetchTimeout is the timeout for fetching the spec.
28
        FetchTimeout = 30 * time.Second
29
        // dirPermissions is the permission mode for created directories.
30
        dirPermissions = 0o755
31
        // filePermissions is the permission mode for the cache file.
32
        filePermissions = 0o600
33
)
34

35
// AirflowCacheFileNameForVersion returns the cache file name for a specific Airflow version.
36
// The version is normalized to strip build metadata (e.g., "3.1.7+astro.1" -> "3.1.7").
37
func AirflowCacheFileNameForVersion(version string) string {
7✔
38
        normalizedVersion := NormalizeAirflowVersion(version)
7✔
39
        return fmt.Sprintf(AirflowCacheFileNameTemplate, normalizedVersion)
7✔
40
}
7✔
41

42
// CloudCacheFileNameForDomain returns the cache file name for a specific domain.
43
// For the default domain (astronomer.io), it returns the standard cache file name
44
// for backward compatibility. For other domains, a domain-qualified name is used
45
// to avoid cache collisions between environments.
46
func CloudCacheFileNameForDomain(domain string) string {
7✔
47
        if domain == "" || domain == "astronomer.io" {
9✔
48
                return CloudCacheFileName
2✔
49
        }
2✔
50
        normalized := strings.ReplaceAll(domain, ".", "_")
5✔
51
        return fmt.Sprintf("openapi-cache-%s.json", normalized)
5✔
52
}
53

54
// CachedSpec wraps raw spec data with metadata for caching.
55
// RawSpec is stored as []byte which json.Marshal encodes as base64.
56
type CachedSpec struct {
57
        RawSpec   []byte    `json:"rawSpec"`
58
        FetchedAt time.Time `json:"fetchedAt"`
59
}
60

61
// Cache manages fetching and caching of an OpenAPI specification.
62
type Cache struct {
63
        specURL     string
64
        cachePath   string
65
        stripPrefix string // optional prefix to strip from endpoint paths (e.g. "/api/v2")
66
        httpClient  *http.Client
67
        doc         *v3high.Document
68
        rawSpec     []byte // raw spec bytes for cache serialization
69
        fetchedAt   time.Time
70
}
71

72
// getHTTPClient returns the configured HTTP client, defaulting to http.DefaultClient.
73
func (c *Cache) getHTTPClient() *http.Client {
15✔
74
        if c.httpClient != nil {
16✔
75
                return c.httpClient
1✔
76
        }
1✔
77
        return http.DefaultClient
14✔
78
}
79

80
// SetHTTPClient configures the HTTP client used to fetch specs.
81
func (c *Cache) SetHTTPClient(client *http.Client) {
1✔
82
        c.httpClient = client
1✔
83
}
1✔
84

85
// NewCache creates a new OpenAPI cache with default settings.
86
func NewCache() *Cache {
1✔
87
        return &Cache{
1✔
88
                specURL:   SpecURL,
1✔
89
                cachePath: filepath.Join(config.HomeConfigPath, CloudCacheFileName),
1✔
90
        }
1✔
91
}
1✔
92

93
// NewCacheWithOptions creates a new OpenAPI cache with custom settings.
94
func NewCacheWithOptions(specURL, cachePath string) *Cache {
5✔
95
        return &Cache{
5✔
96
                specURL:   specURL,
5✔
97
                cachePath: cachePath,
5✔
98
        }
5✔
99
}
5✔
100

101
// NewAirflowCacheForVersion creates a new OpenAPI cache configured for a specific Airflow version.
102
// It automatically determines the correct spec URL and cache file path based on the version.
103
// Endpoint paths are stripped of their API version prefix (/api/v1 or /api/v2) so that
104
// callers work with version-agnostic paths like /dags instead of /api/v2/dags.
105
func NewAirflowCacheForVersion(version string) (*Cache, error) {
4✔
106
        specURL, err := BuildAirflowSpecURL(version)
4✔
107
        if err != nil {
5✔
108
                return nil, fmt.Errorf("building spec URL for version %s: %w", version, err)
1✔
109
        }
1✔
110

111
        cachePath := filepath.Join(config.HomeConfigPath, AirflowCacheFileNameForVersion(version))
3✔
112

3✔
113
        // Determine the API prefix to strip based on major version
3✔
114
        prefix := "/api/v2"
3✔
115
        normalized := NormalizeAirflowVersion(version)
3✔
116
        if strings.HasPrefix(normalized, "2.") {
4✔
117
                prefix = "/api/v1"
1✔
118
        }
1✔
119

120
        return &Cache{
3✔
121
                specURL:     specURL,
3✔
122
                cachePath:   cachePath,
3✔
123
                stripPrefix: prefix,
3✔
124
        }, nil
3✔
125
}
126

127
// Load loads the OpenAPI spec, using cache if valid or fetching if needed.
128
// If forceRefresh is true, the cache is ignored and a fresh spec is fetched.
129
func (c *Cache) Load(forceRefresh bool) error {
7✔
130
        if !forceRefresh {
13✔
131
                // Try to load from cache first
6✔
132
                if err := c.readCache(); err == nil && !c.isExpired() {
8✔
133
                        return nil
2✔
134
                }
2✔
135
        }
136

137
        // Fetch fresh spec
138
        if err := c.fetchSpec(); err != nil {
6✔
139
                // If fetch fails and we have a stale cache, use it
1✔
140
                if c.doc != nil {
1✔
UNCOV
141
                        return nil
×
UNCOV
142
                }
×
143
                return err
1✔
144
        }
145

146
        // Save to cache
147
        return c.saveCache()
4✔
148
}
149

150
// GetSpecURL returns the URL used to fetch the OpenAPI spec.
UNCOV
151
func (c *Cache) GetSpecURL() string {
×
UNCOV
152
        return c.specURL
×
UNCOV
153
}
×
154

155
// GetDoc returns the loaded OpenAPI document.
156
func (c *Cache) GetDoc() *v3high.Document {
4✔
157
        return c.doc
4✔
158
}
4✔
159

160
// GetEndpoints extracts all endpoints from the loaded spec.
161
// If a stripPrefix is configured, it is removed from each endpoint path.
162
func (c *Cache) GetEndpoints() []Endpoint {
3✔
163
        if c.doc == nil {
4✔
164
                return nil
1✔
165
        }
1✔
166
        endpoints := ExtractEndpoints(c.doc)
2✔
167
        if c.stripPrefix != "" {
3✔
168
                for i := range endpoints {
3✔
169
                        endpoints[i].Path = strings.TrimPrefix(endpoints[i].Path, c.stripPrefix)
2✔
170
                }
2✔
171
        }
172
        return endpoints
2✔
173
}
174

175
// IsLoaded returns true if a spec has been loaded.
176
func (c *Cache) IsLoaded() bool {
2✔
177
        return c.doc != nil
2✔
178
}
2✔
179

180
// readCache attempts to read the cached spec from disk.
181
func (c *Cache) readCache() error {
9✔
182
        data, err := os.ReadFile(c.cachePath)
9✔
183
        if err != nil {
14✔
184
                return err
5✔
185
        }
5✔
186

187
        var cached CachedSpec
4✔
188
        if err := json.Unmarshal(data, &cached); err != nil {
5✔
189
                return err
1✔
190
        }
1✔
191

192
        doc, err := parseSpec(cached.RawSpec)
3✔
193
        if err != nil {
3✔
UNCOV
194
                return err
×
UNCOV
195
        }
×
196

197
        c.doc = doc
3✔
198
        c.rawSpec = cached.RawSpec
3✔
199
        c.fetchedAt = cached.FetchedAt
3✔
200
        return nil
3✔
201
}
202

203
// saveCache saves the current spec to disk.
204
func (c *Cache) saveCache() error {
5✔
205
        cached := CachedSpec{
5✔
206
                RawSpec:   c.rawSpec,
5✔
207
                FetchedAt: c.fetchedAt,
5✔
208
        }
5✔
209

5✔
210
        data, err := json.Marshal(cached)
5✔
211
        if err != nil {
5✔
UNCOV
212
                return err
×
UNCOV
213
        }
×
214

215
        // Ensure directory exists
216
        dir := filepath.Dir(c.cachePath)
5✔
217
        if err := os.MkdirAll(dir, dirPermissions); err != nil {
5✔
218
                return err
×
219
        }
×
220

221
        return os.WriteFile(c.cachePath, data, filePermissions)
5✔
222
}
223

224
// isExpired returns true if the cached spec has expired.
225
func (c *Cache) isExpired() bool {
5✔
226
        return time.Since(c.fetchedAt) > CacheTTL
5✔
227
}
5✔
228

229
// fetchSpec fetches the OpenAPI spec from the remote URL.
230
func (c *Cache) fetchSpec() error {
13✔
231
        ctx, cancel := context.WithTimeout(context.Background(), FetchTimeout)
13✔
232
        defer cancel()
13✔
233

13✔
234
        req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.specURL, http.NoBody)
13✔
235
        if err != nil {
13✔
UNCOV
236
                return fmt.Errorf("creating request: %w", err)
×
UNCOV
237
        }
×
238

239
        // Accept both JSON and YAML
240
        req.Header.Set("Accept", "application/json, application/yaml, text/yaml, */*")
13✔
241

13✔
242
        resp, err := c.getHTTPClient().Do(req)
13✔
243
        if err != nil {
13✔
UNCOV
244
                return fmt.Errorf("fetching OpenAPI spec: %w", err)
×
UNCOV
245
        }
×
246
        defer resp.Body.Close()
13✔
247

13✔
248
        if resp.StatusCode != http.StatusOK {
15✔
249
                return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
2✔
250
        }
2✔
251

252
        body, err := io.ReadAll(resp.Body)
11✔
253
        if err != nil {
11✔
UNCOV
254
                return fmt.Errorf("reading response body: %w", err)
×
UNCOV
255
        }
×
256

257
        doc, err := parseSpec(body)
11✔
258
        if err != nil {
13✔
259
                return fmt.Errorf("parsing OpenAPI spec: %w", err)
2✔
260
        }
2✔
261

262
        c.doc = doc
9✔
263
        c.rawSpec = body
9✔
264
        c.fetchedAt = time.Now()
9✔
265
        return nil
9✔
266
}
267

268
// parseSpec parses raw bytes into an OpenAPI v3 document using libopenapi.
269
// This handles both JSON and YAML formats and resolves $ref references.
270
func parseSpec(data []byte) (*v3high.Document, error) {
23✔
271
        doc, err := newDocument(data)
23✔
272
        if err != nil {
25✔
273
                return nil, fmt.Errorf("creating document: %w", err)
2✔
274
        }
2✔
275
        model, err := doc.BuildV3Model()
21✔
276
        if err != nil {
21✔
UNCOV
277
                return nil, fmt.Errorf("building v3 model: %w", err)
×
UNCOV
278
        }
×
279
        if model == nil {
21✔
UNCOV
280
                return nil, fmt.Errorf("building v3 model: unknown error")
×
UNCOV
281
        }
×
282
        return &model.Model, nil
21✔
283
}
284

285
// ClearCache removes the cached spec file.
286
func (c *Cache) ClearCache() error {
2✔
287
        return os.Remove(c.cachePath)
2✔
288
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc