|
| 1 | +package appregistry |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + |
| 6 | + appr "github.com/operator-framework/go-appr/appregistry" |
| 7 | + appr_blobs "github.com/operator-framework/go-appr/appregistry/blobs" |
| 8 | + appr_package "github.com/operator-framework/go-appr/appregistry/package_appr" |
| 9 | + appr_models "github.com/operator-framework/go-appr/models" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + mediaType string = "helm" |
| 14 | +) |
| 15 | + |
| 16 | +// This interface (internal to this package) encapsulates nitty gritty details of go-appr client bindings |
| 17 | +type apprApiAdapter interface { |
| 18 | + // ListPackages returns a list of package(s) available to the user |
| 19 | + ListPackages() (appr_models.Packages, error) |
| 20 | + |
| 21 | + // GetPackageMetadata returns metadata associated with a given package |
| 22 | + GetPackageMetadata(namespace string, repository string, release string) (*appr_models.Package, error) |
| 23 | + |
| 24 | + // DownloadOperatorManifest downloads the blob associated with a given digest that directly corresponds to a package release |
| 25 | + DownloadOperatorManifest(namespace string, repository string, digest string) ([]byte, error) |
| 26 | +} |
| 27 | + |
| 28 | +type apprApiAdapterImpl struct { |
| 29 | + client *appr.Appregistry |
| 30 | +} |
| 31 | + |
| 32 | +func (a *apprApiAdapterImpl) ListPackages() (appr_models.Packages, error) { |
| 33 | + params := appr_package.NewListPackagesParams() |
| 34 | + |
| 35 | + packages, err := a.client.PackageAppr.ListPackages(params) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + |
| 40 | + return packages.Payload, nil |
| 41 | +} |
| 42 | + |
| 43 | +func (a *apprApiAdapterImpl) GetPackageMetadata(namespace string, repository string, release string) (*appr_models.Package, error) { |
| 44 | + params := appr_package.NewShowPackageParams(). |
| 45 | + WithNamespace(namespace). |
| 46 | + WithPackage(repository). |
| 47 | + WithRelease(release). |
| 48 | + WithMediaType(mediaType) |
| 49 | + |
| 50 | + pkg, err := a.client.PackageAppr.ShowPackage(params) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + return pkg.Payload, nil |
| 56 | +} |
| 57 | + |
| 58 | +func (a *apprApiAdapterImpl) DownloadOperatorManifest(namespace string, repository string, digest string) ([]byte, error) { |
| 59 | + params := appr_blobs.NewPullBlobParams(). |
| 60 | + WithNamespace(namespace). |
| 61 | + WithPackage(repository). |
| 62 | + WithDigest(digest) |
| 63 | + |
| 64 | + writer := &bytes.Buffer{} |
| 65 | + _, err := a.client.Blobs.PullBlob(params, writer) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + return writer.Bytes(), nil |
| 71 | +} |
0 commit comments