From 9c9c8e3d46f95a752195cb18ed8441263f070690 Mon Sep 17 00:00:00 2001 From: Paul Meyer <49727155+katexochen@users.noreply.github.com> Date: Thu, 1 Dec 2022 10:26:35 +0100 Subject: [PATCH] versionsapi: rename package Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com> --- cli/internal/cmd/upgradeplan.go | 6 ++-- cli/internal/cmd/upgradeplan_test.go | 8 ++--- hack/add-version/main.go | 26 +++++++------- .../update.go => versionsapi/versionsapi.go} | 32 ++++++++--------- .../versionsapi_test.go} | 34 +++++++++---------- 5 files changed, 53 insertions(+), 53 deletions(-) rename internal/{update/update.go => versionsapi/versionsapi.go} (84%) rename internal/{update/update_test.go => versionsapi/versionsapi_test.go} (90%) diff --git a/cli/internal/cmd/upgradeplan.go b/cli/internal/cmd/upgradeplan.go index 287b6ff05..a34a092b1 100644 --- a/cli/internal/cmd/upgradeplan.go +++ b/cli/internal/cmd/upgradeplan.go @@ -22,7 +22,7 @@ import ( "github.com/edgelesssys/constellation/v2/internal/constants" "github.com/edgelesssys/constellation/v2/internal/file" "github.com/edgelesssys/constellation/v2/internal/sigstore" - "github.com/edgelesssys/constellation/v2/internal/update" + "github.com/edgelesssys/constellation/v2/internal/versionsapi" "github.com/manifoldco/promptui" "github.com/spf13/afero" "github.com/spf13/cobra" @@ -55,7 +55,7 @@ func runUpgradePlan(cmd *cobra.Command, args []string) error { if err != nil { return err } - patchLister := update.New() + patchLister := versionsapi.New() rekor, err := sigstore.NewRekor() if err != nil { return fmt.Errorf("constructing Rekor client: %w", err) @@ -335,5 +335,5 @@ type upgradePlanner interface { } type patchLister interface { - PatchVersionsOf(ctx context.Context, stream, minor, kind string) (*update.VersionsList, error) + PatchVersionsOf(ctx context.Context, stream, minor, kind string) (*versionsapi.List, error) } diff --git a/cli/internal/cmd/upgradeplan_test.go b/cli/internal/cmd/upgradeplan_test.go index f03e8b32d..f2fbf6acd 100644 --- a/cli/internal/cmd/upgradeplan_test.go +++ b/cli/internal/cmd/upgradeplan_test.go @@ -19,7 +19,7 @@ import ( "github.com/edgelesssys/constellation/v2/internal/config" "github.com/edgelesssys/constellation/v2/internal/constants" "github.com/edgelesssys/constellation/v2/internal/file" - "github.com/edgelesssys/constellation/v2/internal/update" + "github.com/edgelesssys/constellation/v2/internal/versionsapi" "github.com/spf13/afero" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" @@ -163,7 +163,7 @@ func TestGetCompatibleImageMeasurements(t *testing.T) { } func TestUpgradePlan(t *testing.T) { - availablePatches := update.VersionsList{ + availablePatches := versionsapi.List{ Versions: []string{"v1.0.0", "v1.0.1"}, } @@ -488,10 +488,10 @@ func (u stubUpgradePlanner) GetCurrentImage(context.Context) (*unstructured.Unst } type stubPatchLister struct { - list update.VersionsList + list versionsapi.List err error } -func (s stubPatchLister) PatchVersionsOf(ctx context.Context, stream, minor, kind string) (*update.VersionsList, error) { +func (s stubPatchLister) PatchVersionsOf(ctx context.Context, stream, minor, kind string) (*versionsapi.List, error) { return &s.list, s.err } diff --git a/hack/add-version/main.go b/hack/add-version/main.go index e1485ff4d..51460cbac 100644 --- a/hack/add-version/main.go +++ b/hack/add-version/main.go @@ -28,7 +28,7 @@ import ( s3types "github.com/aws/aws-sdk-go-v2/service/s3/types" "github.com/edgelesssys/constellation/v2/internal/constants" "github.com/edgelesssys/constellation/v2/internal/logger" - "github.com/edgelesssys/constellation/v2/internal/update" + "github.com/edgelesssys/constellation/v2/internal/versionsapi" "go.uber.org/zap" "go.uber.org/zap/zapcore" "golang.org/x/mod/semver" @@ -61,7 +61,7 @@ func main() { ctx := context.Background() - updateFetcher := update.New() + updateFetcher := versionsapi.New() versionManager, err := newVersionManager(ctx, *region, *bucket, *distributionID) if err != nil { log.With(zap.Error(err)).Fatalf("Failed to create version uploader") @@ -74,7 +74,7 @@ func main() { log.With(zap.Error(err)).Fatalf("Failed to get minor versions") } log.Infof("Version list for minor versions under %q does not exist. Creating new list.", major) - minorVersions = &update.VersionsList{ + minorVersions = &versionsapi.List{ Stream: stream, Granularity: "major", Base: major, @@ -98,7 +98,7 @@ func main() { log.With(zap.Error(err)).Fatalf("Failed to get patch versions") } log.Infof("Version list for patch versions under %q does not exist. Creating new list.", minor) - patchVersions = &update.VersionsList{ + patchVersions = &versionsapi.List{ Stream: stream, Granularity: "minor", Base: minor, @@ -150,7 +150,7 @@ func validateVersion(version string) error { return nil } -func ensureMinorVersionExists(ctx context.Context, fetcher *update.VersionsFetcher, version string) error { +func ensureMinorVersionExists(ctx context.Context, fetcher *versionsapi.Fetcher, version string) error { major := semver.Major(version) minor := semver.MajorMinor(version) existingMinorVersions, err := fetcher.MinorVersionsOf(ctx, stream, major, imageKind) @@ -163,7 +163,7 @@ func ensureMinorVersionExists(ctx context.Context, fetcher *update.VersionsFetch return nil } -func ensurePatchVersionExists(ctx context.Context, fetcher *update.VersionsFetcher, version string) error { +func ensurePatchVersionExists(ctx context.Context, fetcher *versionsapi.Fetcher, version string) error { minor := semver.MajorMinor(version) patch := semver.Canonical(version) existingPatchVersions, err := fetcher.PatchVersionsOf(ctx, stream, minor, imageKind) @@ -203,29 +203,29 @@ func newVersionManager(ctx context.Context, region, bucket, distributionID strin }, nil } -func (m *versionManager) getMinorVersions(ctx context.Context, version string) (*update.VersionsList, error) { +func (m *versionManager) getMinorVersions(ctx context.Context, version string) (*versionsapi.List, error) { baseVersion := semver.Major(version) return m.getVersions(ctx, baseVersion) } -func (m *versionManager) getPatchVersions(ctx context.Context, version string) (*update.VersionsList, error) { +func (m *versionManager) getPatchVersions(ctx context.Context, version string) (*versionsapi.List, error) { baseVersion := semver.MajorMinor(version) return m.getVersions(ctx, baseVersion) } -func (m *versionManager) addMinorVersion(ctx context.Context, version string, minorVersions *update.VersionsList) error { +func (m *versionManager) addMinorVersion(ctx context.Context, version string, minorVersions *versionsapi.List) error { baseVersion := semver.Major(version) minorVersion := semver.MajorMinor(version) return m.addVersion(ctx, baseVersion, minorVersion, minorVersions) } -func (m *versionManager) addPatchVersion(ctx context.Context, version string, patchVersions *update.VersionsList) error { +func (m *versionManager) addPatchVersion(ctx context.Context, version string, patchVersions *versionsapi.List) error { baseVersion := semver.MajorMinor(version) patchVersion := semver.Canonical(version) return m.addVersion(ctx, baseVersion, patchVersion, patchVersions) } -func (m *versionManager) getVersions(ctx context.Context, baseVersion string) (*update.VersionsList, error) { +func (m *versionManager) getVersions(ctx context.Context, baseVersion string) (*versionsapi.List, error) { granularity, err := granularityFromVersion(baseVersion) if err != nil { return nil, err @@ -242,14 +242,14 @@ func (m *versionManager) getVersions(ctx context.Context, baseVersion string) (* return nil, err } defer out.Body.Close() - var versions update.VersionsList + var versions versionsapi.List if err := json.NewDecoder(out.Body).Decode(&versions); err != nil { return nil, err } return &versions, nil } -func (m *versionManager) addVersion(ctx context.Context, baseVersion, version string, list *update.VersionsList) error { +func (m *versionManager) addVersion(ctx context.Context, baseVersion, version string, list *versionsapi.List) error { granularity, err := granularityFromVersion(baseVersion) if err != nil { return err diff --git a/internal/update/update.go b/internal/versionsapi/versionsapi.go similarity index 84% rename from internal/update/update.go rename to internal/versionsapi/versionsapi.go index 4620a3299..141e00aba 100644 --- a/internal/update/update.go +++ b/internal/versionsapi/versionsapi.go @@ -4,7 +4,7 @@ Copyright (c) Edgeless Systems GmbH SPDX-License-Identifier: AGPL-3.0-only */ -package update +package versionsapi import ( "context" @@ -20,14 +20,14 @@ import ( "golang.org/x/mod/semver" ) -// VersionsList represents a list of versions for a kind of resource. +// List represents a list of versions for a kind of resource. // It has a granularity of either "major" or "minor". // -// For example, a VersionsList with granularity "major" could contain +// For example, a List with granularity "major" could contain // the base version "v1" and a list of minor versions "v1.0", "v1.1", "v1.2" etc. -// A VersionsList with granularity "minor" could contain the base version +// A List with granularity "minor" could contain the base version // "v1.0" and a list of patch versions "v1.0.0", "v1.0.1", "v1.0.2" etc. -type VersionsList struct { +type List struct { // Stream is the update stream of the list. // Currently, only "stable" is supported. Stream string `json:"stream"` @@ -50,7 +50,7 @@ type VersionsList struct { // - The kind is supported. // - The base version is a valid semantic version that matches the granularity. // - All versions in the list are valid semantic versions that are finer-grained than the base version. -func (l *VersionsList) Validate() error { +func (l *List) Validate() error { var issues []string if l.Stream != "stable" { issues = append(issues, fmt.Sprintf("stream %q is not supported", l.Stream)) @@ -91,7 +91,7 @@ func (l *VersionsList) Validate() error { } // Contains returns true if the list contains the given version. -func (l *VersionsList) Contains(version string) bool { +func (l *List) Contains(version string) bool { for _, v := range l.Versions { if v == version { return true @@ -100,35 +100,35 @@ func (l *VersionsList) Contains(version string) bool { return false } -// VersionsFetcher fetches a list of versions. -type VersionsFetcher struct { +// Fetcher fetches a list of versions. +type Fetcher struct { httpc httpc } // New returns a new VersionsFetcher. -func New() *VersionsFetcher { - return &VersionsFetcher{ +func New() *Fetcher { + return &Fetcher{ httpc: http.DefaultClient, } } // MinorVersionsOf fetches the list of minor versions for a given stream, major version and kind. -func (f *VersionsFetcher) MinorVersionsOf(ctx context.Context, stream, major, kind string) (*VersionsList, error) { +func (f *Fetcher) MinorVersionsOf(ctx context.Context, stream, major, kind string) (*List, error) { return f.list(ctx, stream, "major", major, kind) } // PatchVersionsOf fetches the list of patch versions for a given stream, minor version and kind. -func (f *VersionsFetcher) PatchVersionsOf(ctx context.Context, stream, minor, kind string) (*VersionsList, error) { +func (f *Fetcher) PatchVersionsOf(ctx context.Context, stream, minor, kind string) (*List, error) { return f.list(ctx, stream, "minor", minor, kind) } // list fetches the list of versions for a given stream, granularity, base and kind. -func (f *VersionsFetcher) list(ctx context.Context, stream, granularity, base, kind string) (*VersionsList, error) { +func (f *Fetcher) list(ctx context.Context, stream, granularity, base, kind string) (*List, error) { raw, err := getFromURL(ctx, f.httpc, stream, granularity, base, kind) if err != nil { return nil, fmt.Errorf("fetching versions list: %w", err) } - list := &VersionsList{} + list := &List{} if err := json.Unmarshal(raw, &list); err != nil { return nil, fmt.Errorf("decoding versions list: %w", err) } @@ -141,7 +141,7 @@ func (f *VersionsFetcher) list(ctx context.Context, stream, granularity, base, k return list, nil } -func (f *VersionsFetcher) listMatchesRequest(list *VersionsList, stream, granularity, base, kind string) bool { +func (f *Fetcher) listMatchesRequest(list *List, stream, granularity, base, kind string) bool { return list.Stream == stream && list.Granularity == granularity && list.Base == base && list.Kind == kind } diff --git a/internal/update/update_test.go b/internal/versionsapi/versionsapi_test.go similarity index 90% rename from internal/update/update_test.go rename to internal/versionsapi/versionsapi_test.go index 903a62ec2..448bc6136 100644 --- a/internal/update/update_test.go +++ b/internal/versionsapi/versionsapi_test.go @@ -4,7 +4,7 @@ Copyright (c) Edgeless Systems GmbH SPDX-License-Identifier: AGPL-3.0-only */ -package update +package versionsapi import ( "bytes" @@ -25,8 +25,8 @@ func TestMain(m *testing.M) { func TestValidate(t *testing.T) { testCases := map[string]struct { - listFunc func() *VersionsList - overrideFunc func(list *VersionsList) + listFunc func() *List + overrideFunc func(list *List) wantErr bool }{ "valid major list": { @@ -37,56 +37,56 @@ func TestValidate(t *testing.T) { }, "invalid stream": { listFunc: majorList, - overrideFunc: func(list *VersionsList) { + overrideFunc: func(list *List) { list.Stream = "invalid" }, wantErr: true, }, "invalid granularity": { listFunc: majorList, - overrideFunc: func(list *VersionsList) { + overrideFunc: func(list *List) { list.Granularity = "invalid" }, wantErr: true, }, "invalid kind": { listFunc: majorList, - overrideFunc: func(list *VersionsList) { + overrideFunc: func(list *List) { list.Kind = "invalid" }, wantErr: true, }, "base ver is not semantic version": { listFunc: majorList, - overrideFunc: func(list *VersionsList) { + overrideFunc: func(list *List) { list.Base = "invalid" }, wantErr: true, }, "base ver does not reflect major granularity": { listFunc: majorList, - overrideFunc: func(list *VersionsList) { + overrideFunc: func(list *List) { list.Base = "v1.0" }, wantErr: true, }, "base ver does not reflect minor granularity": { listFunc: minorList, - overrideFunc: func(list *VersionsList) { + overrideFunc: func(list *List) { list.Base = "v1" }, wantErr: true, }, "version in list is not semantic version": { listFunc: majorList, - overrideFunc: func(list *VersionsList) { + overrideFunc: func(list *List) { list.Versions[0] = "invalid" }, wantErr: true, }, "version in list is not sub version of base": { listFunc: majorList, - overrideFunc: func(list *VersionsList) { + overrideFunc: func(list *List) { list.Versions[0] = "v2.1" }, wantErr: true, @@ -170,7 +170,7 @@ func TestList(t *testing.T) { testCases := map[string]struct { stream, granularity, base, kind string overrideFile string - wantList VersionsList + wantList List wantErr bool }{ "major list fetched remotely": { @@ -225,7 +225,7 @@ func TestList(t *testing.T) { kind = tc.kind } - fetcher := &VersionsFetcher{ + fetcher := &Fetcher{ httpc: client, } list, err := fetcher.list(context.Background(), stream, granularity, base, kind) @@ -254,8 +254,8 @@ func newTestClient(fn roundTripFunc) *http.Client { } } -func majorList() *VersionsList { - return &VersionsList{ +func majorList() *List { + return &List{ Stream: "stable", Granularity: "major", Base: "v1", @@ -266,8 +266,8 @@ func majorList() *VersionsList { } } -func minorList() *VersionsList { - return &VersionsList{ +func minorList() *List { + return &List{ Stream: "stable", Granularity: "minor", Base: "v1.1",