Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions pkg/platformhubaccounts/platform_hub_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package platformhubaccounts

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

// IPlatformHubAccount defines the interface for Platform Hub accounts.
type IPlatformHubAccount interface {
GetAccountType() PlatformHubAccountType
GetDescription() string
SetDescription(string)

resources.IHasName
resources.IResource
}

// platformHubAccount is the embedded struct used for all Platform Hub accounts.
type platformHubAccount struct {
AccountType PlatformHubAccountType `json:"AccountType" validate:"required"`
Description string `json:"Description,omitempty"`
Name string `json:"Name" validate:"required,notblank"`

resources.Resource
}

// newPlatformHubAccount creates and initializes a Platform Hub account.
func newPlatformHubAccount(name string, accountType PlatformHubAccountType) *platformHubAccount {
return &platformHubAccount{
AccountType: accountType,
Name: name,
Resource: *resources.NewResource(),
}
}

// GetAccountType returns the type of this account.
func (a *platformHubAccount) GetAccountType() PlatformHubAccountType {
return a.AccountType
}

// GetDescription returns the description of the account.
func (a *platformHubAccount) GetDescription() string {
return a.Description
}

// GetName returns the name of the account.
func (a *platformHubAccount) GetName() string {
return a.Name
}

// SetDescription sets the description of the account.
func (a *platformHubAccount) SetDescription(description string) {
a.Description = description
}

// SetName sets the name of the account.
func (a *platformHubAccount) SetName(name string) {
a.Name = name
}

// Validate checks the state of the account and returns an error if invalid.
func (a *platformHubAccount) Validate() error {
v := validator.New()
err := v.RegisterValidation("notblank", validators.NotBlank)
if err != nil {
return err
}
err = v.RegisterValidation("notall", validation.NotAll)
if err != nil {
return err
}
return v.Struct(a)
}

var _ IPlatformHubAccount = &platformHubAccount{}
117 changes: 117 additions & 0 deletions pkg/platformhubaccounts/platform_hub_account_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package platformhubaccounts

import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
)

// PlatformHubAccountResource represents details for all Platform Hub accounts.
type PlatformHubAccountResource struct {
AccountType PlatformHubAccountType `json:"AccountType" validate:"required,oneof=AmazonWebServicesAccount"`
Description string `json:"Description,omitempty"`
Name string `json:"Name" validate:"required,notall"`
AccessKey string `json:"AccessKey,omitempty"`
SecretKey *core.SensitiveValue `json:"SecretKey,omitempty"`

resources.Resource
}

// NewPlatformHubAccountResource creates and initializes a Platform Hub account resource.
func NewPlatformHubAccountResource(name string, accountType PlatformHubAccountType) *PlatformHubAccountResource {
return &PlatformHubAccountResource{
AccountType: accountType,
Name: name,
Resource: *resources.NewResource(),
}
}

// GetAccountType returns the type of this account resource.
func (r *PlatformHubAccountResource) GetAccountType() PlatformHubAccountType {
return r.AccountType
}

// GetDescription returns the description of this account resource.
func (r *PlatformHubAccountResource) GetDescription() string {
return r.Description
}

// GetName returns the name of this account resource.
func (r *PlatformHubAccountResource) GetName() string {
return r.Name
}

// SetDescription sets the description of the account resource.
func (r *PlatformHubAccountResource) SetDescription(description string) {
r.Description = description
}

// SetName sets the name of this account resource.
func (r *PlatformHubAccountResource) SetName(name string) {
r.Name = name
}

// ToPlatformHubAccount converts a PlatformHubAccountResource to the appropriate concrete account type.
func (r *PlatformHubAccountResource) ToPlatformHubAccount() (IPlatformHubAccount, error) {
if r == nil {
return nil, internal.CreateInvalidParameterError("ToPlatformHubAccount", "PlatformHubAccountResource")
}

if err := r.Validate(); err != nil {
return nil, err
}

var account IPlatformHubAccount

switch r.AccountType {
case AccountTypePlatformHubAwsAccount:
awsAccount, err := NewPlatformHubAwsAccount(r.GetName(), r.AccessKey, r.SecretKey)
if err != nil {
return nil, err
}
account = awsAccount
default:
return nil, internal.CreateInvalidParameterError("ToPlatformHubAccount", "AccountType")
}

account.SetDescription(r.Description)
account.SetID(r.GetID())
account.SetLinks(r.GetLinks())
account.SetModifiedBy(r.GetModifiedBy())
account.SetModifiedOn(r.GetModifiedOn())

return account, nil
}

// ToPlatformHubAccountResource converts a concrete account type to PlatformHubAccountResource.
func ToPlatformHubAccountResource(account IPlatformHubAccount) (*PlatformHubAccountResource, error) {
if account == nil {
return nil, internal.CreateInvalidParameterError("ToPlatformHubAccountResource", "PlatformHubAccount")
}

// conversion unnecessary if input account is *PlatformHubAccountResource
if v, ok := account.(*PlatformHubAccountResource); ok {
return v, nil
}

resource := NewPlatformHubAccountResource(account.GetName(), account.GetAccountType())

switch resource.AccountType {
case AccountTypePlatformHubAwsAccount:
awsAccount := account.(*PlatformHubAwsAccount)
resource.AccessKey = awsAccount.AccessKey
resource.SecretKey = awsAccount.SecretKey
default:
return nil, internal.CreateInvalidParameterError("ToPlatformHubAccountResource", "AccountType")
}

resource.SetDescription(account.GetDescription())
resource.SetID(account.GetID())
resource.SetLinks(account.GetLinks())
resource.SetModifiedBy(account.GetModifiedBy())
resource.SetModifiedOn(account.GetModifiedOn())

return resource, nil
}

var _ IPlatformHubAccount = &PlatformHubAccountResource{}
106 changes: 106 additions & 0 deletions pkg/platformhubaccounts/platform_hub_account_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package platformhubaccounts

import (
"testing"

"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/stretchr/testify/require"
)

func TestToPlatformHubAccount(t *testing.T) {
// Test with nil resource
var accountResource *PlatformHubAccountResource
account, err := accountResource.ToPlatformHubAccount()
require.Nil(t, account)
require.Error(t, err)

// Test with empty name and account type
accountResource = NewPlatformHubAccountResource("", "")
account, err = accountResource.ToPlatformHubAccount()
require.Nil(t, account)
require.Error(t, err)

// Test with name but empty account type
accountResource = NewPlatformHubAccountResource(internal.GetRandomName(), "")
account, err = accountResource.ToPlatformHubAccount()
require.Nil(t, account)
require.Error(t, err)

// Test with unknown account type (default case)
accountResource = NewPlatformHubAccountResource(internal.GetRandomName(), PlatformHubAccountType("UnknownAccountType"))
account, err = accountResource.ToPlatformHubAccount()
require.Nil(t, account)
require.Error(t, err)

// Test with valid AWS account
accessKey := internal.GetRandomName()
name := internal.GetRandomName()
secretKey := core.NewSensitiveValue(internal.GetRandomName())

accountResource = NewPlatformHubAccountResource(name, AccountTypePlatformHubAwsAccount)
accountResource.AccessKey = accessKey
accountResource.SecretKey = secretKey
accountResource.Description = "Test description"
accountResource.ID = "test-id"

account, err = accountResource.ToPlatformHubAccount()
require.NotNil(t, account)
require.NoError(t, err)
require.Equal(t, AccountTypePlatformHubAwsAccount, account.GetAccountType())
require.Equal(t, name, account.GetName())
require.Equal(t, "Test description", account.GetDescription())
require.Equal(t, "test-id", account.GetID())

// Verify it's an AWS account
awsAccount, ok := account.(*PlatformHubAwsAccount)
require.True(t, ok)
require.Equal(t, accessKey, awsAccount.AccessKey)
require.Equal(t, secretKey, awsAccount.SecretKey)
}

func TestToPlatformHubAccountResource(t *testing.T) {
// Test with nil account
accountResource, err := ToPlatformHubAccountResource(nil)
require.Nil(t, accountResource)
require.Error(t, err)

// Test with valid AWS account
accessKey := internal.GetRandomName()
name := internal.GetRandomName()
secretKey := core.NewSensitiveValue(internal.GetRandomName())

awsAccount, err := NewPlatformHubAwsAccount(name, accessKey, secretKey)
require.NoError(t, err)
require.NotNil(t, awsAccount)

awsAccount.Description = "Test description"
awsAccount.ID = "test-id"

accountResource, err = ToPlatformHubAccountResource(awsAccount)
require.NotNil(t, accountResource)
require.NoError(t, err)
require.Equal(t, AccountTypePlatformHubAwsAccount, accountResource.GetAccountType())
require.Equal(t, name, accountResource.GetName())
require.Equal(t, "Test description", accountResource.GetDescription())
require.Equal(t, "test-id", accountResource.GetID())
require.Equal(t, accessKey, accountResource.AccessKey)
require.Equal(t, secretKey, accountResource.SecretKey)

// Test that converting a resource returns it as-is
accountResource2, err := ToPlatformHubAccountResource(accountResource)
require.NotNil(t, accountResource2)
require.NoError(t, err)
require.Equal(t, accountResource, accountResource2)
}

func TestToPlatformHubAccountResourceUnknownType(t *testing.T) {
// Create a resource with an unknown account type
name := internal.GetRandomName()
accountResource := NewPlatformHubAccountResource(name, PlatformHubAccountType("UnknownAccountType"))

// This should fail when trying to convert back to account
account, err := accountResource.ToPlatformHubAccount()
require.Nil(t, account)
require.Error(t, err)
}
Loading
Loading