From 1d2223ab3e7e8c0fe89ec647bf748cad8acd8e20 Mon Sep 17 00:00:00 2001 From: denys-octopus <102932057+denys-octopus@users.noreply.github.com> Date: Fri, 18 Oct 2024 11:33:33 +1300 Subject: [PATCH] feat: Add support for OCI Registry feeds (#273) --- pkg/feeds/feed_types.go | 1 + pkg/feeds/feed_utilities.go | 7 +++++ pkg/feeds/feed_utilities_test.go | 47 +++++++++++++++++++++++++++++++- pkg/feeds/oci_registry_feed.go | 42 ++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 pkg/feeds/oci_registry_feed.go diff --git a/pkg/feeds/feed_types.go b/pkg/feeds/feed_types.go index 0430e92d..81d83c52 100644 --- a/pkg/feeds/feed_types.go +++ b/pkg/feeds/feed_types.go @@ -13,4 +13,5 @@ const ( FeedTypeOctopusProject = FeedType("OctopusProject") FeedTypeArtifactoryGeneric = FeedType("ArtifactoryGeneric") FeedTypeS3 = FeedType("S3") + FeedTypeOCIRegistry = FeedType("OciRegistry") ) diff --git a/pkg/feeds/feed_utilities.go b/pkg/feeds/feed_utilities.go index ac1105c6..36ca6e3b 100644 --- a/pkg/feeds/feed_utilities.go +++ b/pkg/feeds/feed_utilities.go @@ -100,6 +100,13 @@ func ToFeed(feedResource *FeedResource) (IFeed, error) { return nil, err } feed = s3Feed + case FeedTypeOCIRegistry: + ociFeed, err := NewOCIRegistryFeed(feedResource.GetName()) + if err != nil { + return nil, err + } + ociFeed.FeedURI = feedResource.FeedURI + feed = ociFeed default: return nil, errors.New("unknown feed type: " + fmt.Sprint(feedResource.GetFeedType())) } diff --git a/pkg/feeds/feed_utilities_test.go b/pkg/feeds/feed_utilities_test.go index cbeefd8f..3a51fad9 100644 --- a/pkg/feeds/feed_utilities_test.go +++ b/pkg/feeds/feed_utilities_test.go @@ -340,7 +340,6 @@ func TestMaven(t *testing.T) { if typedFeed.FeedURI != "http://example.com" { t.Fatalf("FeedURI does not match") } - } func TestNuget(t *testing.T) { @@ -543,3 +542,49 @@ func TestS3(t *testing.T) { t.Fatalf("UseMachineCredentials does not match") } } + +func TestOCIRegistry(t *testing.T) { + feedResource := FeedResource{ + AccessKey: "", + APIVersion: "test", + DeleteUnreleasedPackagesAfterDays: 10, + DownloadAttempts: 5, + DownloadRetryBackoffSeconds: 3, + EnhancedMode: false, + FeedType: FeedTypeOCIRegistry, + FeedURI: "oci://test-registry.docker.io", + IsBuiltInRepoSyncEnabled: true, + Name: "Test Registry", + Password: core.NewSensitiveValue("test-password"), + PackageAcquisitionLocationOptions: nil, + Region: "", + RegistryPath: "", + SecretKey: nil, + SpaceID: "", + Username: "test-username", + LayoutRegex: "", + Repository: "", + UseMachineCredentials: false, + Resource: resources.Resource{}, + } + + feed, err := ToFeed(&feedResource) + + if err != nil { + t.Fatalf("Error should not have been returned. %s", err) + } + + typedFeed := feed.(*OCIRegistryFeed) + + if typedFeed.Name != "Test Registry" { + t.Fatalf("Name does not match") + } + + if typedFeed.FeedURI != "oci://test-registry.docker.io" { + t.Fatalf("FeedURI does not match") + } + + if typedFeed.Username != "test-username" { + t.Fatalf("Username does not match") + } +} diff --git a/pkg/feeds/oci_registry_feed.go b/pkg/feeds/oci_registry_feed.go new file mode 100644 index 00000000..87236894 --- /dev/null +++ b/pkg/feeds/oci_registry_feed.go @@ -0,0 +1,42 @@ +package feeds + +import ( + "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" + "github.com/go-playground/validator/v10" + "github.com/go-playground/validator/v10/non-standard/validators" +) + +// OCIRegistryFeed represents a feed of Open Container Initiative Registry. +type OCIRegistryFeed struct { + FeedURI string `json:"FeedUri,omitempty"` + + feed +} + +// NewOCIRegistryFeed creates and initializes a OCIRegistry feed. +func NewOCIRegistryFeed(name string) (*OCIRegistryFeed, error) { + if internal.IsEmpty(name) { + return nil, internal.CreateRequiredParameterIsEmptyOrNilError("name") + } + + feed := OCIRegistryFeed{ + FeedURI: "oci://registry-1.docker.io", + feed: *newFeed(name, FeedTypeOCIRegistry), + } + + if err := feed.Validate(); err != nil { + return nil, err + } + + return &feed, nil +} + +// Validate checks the state of this feed and returns an error if invalid. +func (m *OCIRegistryFeed) Validate() error { + v := validator.New() + err := v.RegisterValidation("notblank", validators.NotBlank) + if err != nil { + return err + } + return v.Struct(m) +}