Skip to content

Commit

Permalink
feat: add AWS S3 feed type (#235)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Hill <6022646+funkbunker@users.noreply.github.com>
  • Loading branch information
rain-on and funkbunker authored Feb 12, 2024
1 parent 17b4d2a commit 6754870
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/feeds/feed_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type FeedResource struct {
Username string `json:"Username,omitempty"`
LayoutRegex string `json:"LayoutRegex,omitempty"`
Repository string `json:"Repository,omitempty"`

UseMachineCredentials bool `json:"UseMachineCredentials,omitempty"`
resources.Resource
}

Expand Down
1 change: 1 addition & 0 deletions pkg/feeds/feed_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ const (
FeedTypeNuGet = FeedType("NuGet")
FeedTypeOctopusProject = FeedType("OctopusProject")
FeedTypeArtifactoryGeneric = FeedType("ArtifactoryGeneric")
FeedTypeS3 = FeedType("S3")
)
11 changes: 11 additions & 0 deletions pkg/feeds/feed_utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ func ToFeed(feedResource *FeedResource) (IFeed, error) {
return nil, err
}
feed = artifactoryGenericFeed
case FeedTypeS3:
s3Feed, err := NewS3Feed(feedResource.GetName(), feedResource.AccessKey, feedResource.SecretKey, feedResource.UseMachineCredentials)
if err != nil {
return nil, err
}
feed = s3Feed
}

feed.SetID(feedResource.GetID())
Expand Down Expand Up @@ -166,6 +172,11 @@ func ToFeedResource(feed IFeed) (*FeedResource, error) {
feedResource.Repository = artifactoryGenericFeed.Repository
feedResource.LayoutRegex = artifactoryGenericFeed.LayoutRegex
feedResource.FeedURI = artifactoryGenericFeed.FeedURI
case FeedTypeS3:
s3Feed := feed.(*S3Feed)
feedResource.AccessKey = s3Feed.AccessKey
feedResource.SecretKey = s3Feed.SecretKey
feedResource.UseMachineCredentials = s3Feed.UseMachineCredentials
case FeedTypeOctopusProject:
// nothing to copy
}
Expand Down
55 changes: 55 additions & 0 deletions pkg/feeds/s3_bucket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package feeds

import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/go-playground/validator/v10"
"github.com/go-playground/validator/v10/non-standard/validators"
)

// S3Feed represents an Amazon Web Services (AWS) S3 Bucket Feed
type S3Feed struct {
AccessKey string `json:"AccessKey,omitempty"`
SecretKey *core.SensitiveValue `json:"SecretKey,omitempty"`
UseMachineCredentials bool `json:"UseMachineCredentials"`
feed
}

// NewS3Feed creates and initializes an Amazon S3 Bucket Feed
func NewS3Feed(name string, accessKey string, secretKey *core.SensitiveValue, useMachineCredentials bool) (*S3Feed, error) {
if internal.IsEmpty(name) {
return nil, internal.CreateRequiredParameterIsEmptyOrNilError("name")
}
if !useMachineCredentials {
if internal.IsEmpty(accessKey) {
return nil, internal.CreateRequiredParameterIsEmptyOrNilError("accessKey")
}

if secretKey == nil {
return nil, internal.CreateRequiredParameterIsEmptyOrNilError("secretKey")
}
}
feed := S3Feed{
AccessKey: accessKey,
SecretKey: secretKey,
UseMachineCredentials: useMachineCredentials,
feed: *newFeed(name, FeedTypeS3),
}

// validate to ensure that all expectations are met
if err := feed.Validate(); err != nil {
return nil, err
}

return &feed, nil
}

// Validate checks the state of this Amazon Web Services (AWS) S3 Bucket Feed
func (a *S3Feed) Validate() error {
v := validator.New()
err := v.RegisterValidation("notblank", validators.NotBlank)
if err != nil {
return err
}
return v.Struct(a)
}
40 changes: 40 additions & 0 deletions test/e2e/feed_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ func CreateTestNuGetFeed(t *testing.T, client *client.Client) feeds.IFeed {
return resource
}

func CreateTestS3Feed(t *testing.T, client *client.Client) feeds.IFeed {
if client == nil {
client = getOctopusClient()
}
require.NotNil(t, client)

name := "S3_FeedName"
accessKey := "access-key"
secretKey := core.NewSensitiveValue("secret-key")

feed, err := feeds.NewS3Feed(name, accessKey, secretKey, false)
require.NoError(t, err)

resource, err := client.Feeds.Add(feed)
require.NoError(t, err)

return resource

}

func DeleteTestFeed(t *testing.T, client *client.Client, feed feeds.IFeed) {
require.NotNil(t, feed)

Expand Down Expand Up @@ -165,6 +185,10 @@ func TestFeedServiceAdd(t *testing.T) {
feed = CreateTestNuGetFeed(t, client)
require.NotNil(t, feed)
defer DeleteTestFeed(t, client, feed)

feed = CreateTestS3Feed(t, client)
require.NotNil(t, feed)
defer DeleteTestFeed(t, client, feed)
}

func TestFeedServiceCRUD(t *testing.T) {
Expand Down Expand Up @@ -233,6 +257,22 @@ func TestFeedServiceCRUD(t *testing.T) {
actual, err = client.Feeds.Update(expected)
require.NoError(t, err)
AssertEqualFeeds(t, expected, actual)

expected = CreateTestS3Feed(t, client)
require.NotNil(t, expected)
defer DeleteTestFeed(t, client, expected)

actual, err = client.Feeds.GetByID(expected.GetID())
require.NoError(t, err)
AssertEqualFeeds(t, expected, actual)

name = internal.GetRandomName()
expected.SetName(name)

actual, err = client.Feeds.Update(expected)
require.NoError(t, err)
AssertEqualFeeds(t, expected, actual)

}

// TODO: fix test
Expand Down

0 comments on commit 6754870

Please sign in to comment.