Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds new SSHOptionWrapper #111

Merged
merged 2 commits into from
Mar 26, 2024
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
16 changes: 10 additions & 6 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func syncCommandRun(cmd *cobra.Command, args []string) {
sshVerbose = sshConfig.Verbose
}

// Here we have the default - let's add it to a wrapper
sshOptions := synchers.SSHOptions{
Host: sshHost,
PrivateKey: sshKey,
Expand All @@ -165,6 +166,8 @@ func syncCommandRun(cmd *cobra.Command, args []string) {
SkipAgent: SSHSkipAgent,
}

sshOptionWrapper := synchers.NewSshOptionWrapper(ProjectName, sshOptions)

// let's update the named transfer resource if it is set
if namedTransferResource != "" {
err = lagoonSyncer.SetTransferResource(namedTransferResource)
Expand All @@ -176,12 +179,13 @@ func syncCommandRun(cmd *cobra.Command, args []string) {
utils.LogDebugInfo("Config that is used for SSH", sshOptions)

err = runSyncProcess(synchers.RunSyncProcessFunctionTypeArguments{
SourceEnvironment: sourceEnvironment,
TargetEnvironment: targetEnvironment,
LagoonSyncer: lagoonSyncer,
SyncerType: SyncerType,
DryRun: dryRun,
SshOptions: sshOptions,
SourceEnvironment: sourceEnvironment,
TargetEnvironment: targetEnvironment,
LagoonSyncer: lagoonSyncer,
SyncerType: SyncerType,
DryRun: dryRun,
//SshOptions: sshOptions,
SshOptionWrapper: sshOptionWrapper,
SkipTargetCleanup: skipTargetCleanup,
SkipSourceCleanup: skipSourceCleanup,
SkipTargetImport: skipTargetImport,
Expand Down
18 changes: 10 additions & 8 deletions cmd/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ func Test_syncCommandRun(t *testing.T) {
},
},
runSyncProcess: func(args synchers.RunSyncProcessFunctionTypeArguments) error {
if args.SshOptions.Port != "32222" {
return errors.New(fmt.Sprintf("Expecting ssh port 32222 - found: %v", args.SshOptions.Port))
sshOptions := args.SshOptionWrapper.Default
if sshOptions.Port != "32222" {
return errors.New(fmt.Sprintf("Expecting ssh port 32222 - found: %v", sshOptions.Port))
}

if args.SshOptions.Host != "ssh.lagoon.amazeeio.cloud" {
return errors.New(fmt.Sprintf("Expecting ssh host ssh.lagoon.amazeeio.cloud - found: %v", args.SshOptions.Host))
if sshOptions.Host != "ssh.lagoon.amazeeio.cloud" {
return errors.New(fmt.Sprintf("Expecting ssh host ssh.lagoon.amazeeio.cloud - found: %v", sshOptions.Host))
}

return nil
Expand All @@ -52,12 +53,13 @@ func Test_syncCommandRun(t *testing.T) {
},
},
runSyncProcess: func(args synchers.RunSyncProcessFunctionTypeArguments) error {
if args.SshOptions.Port != "777" {
return errors.New(fmt.Sprintf("Expecting ssh port 777 - found: %v", args.SshOptions.Port))
sshOptions := args.SshOptionWrapper.Default
if sshOptions.Port != "777" {
return errors.New(fmt.Sprintf("Expecting ssh port 777 - found: %v", sshOptions.Port))
}

if args.SshOptions.Host != "example.ssh.lagoon.amazeeio.cloud" {
return errors.New(fmt.Sprintf("Expecting ssh host ssh.lagoon.amazeeio.cloud - found: %v", args.SshOptions.Host))
if sshOptions.Host != "example.ssh.lagoon.amazeeio.cloud" {
return errors.New(fmt.Sprintf("Expecting ssh host ssh.lagoon.amazeeio.cloud - found: %v", sshOptions.Host))
}

return nil
Expand Down
10 changes: 8 additions & 2 deletions synchers/prerequisiteSyncUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
"github.com/uselagoon/lagoon-sync/utils"
)

func RunPrerequisiteCommand(environment Environment, syncer Syncer, syncerType string, dryRun bool, sshOptions SSHOptions) (Environment, error) {
func RunPrerequisiteCommand(environment Environment, syncer Syncer, syncerType string, dryRun bool, sshOptionWrapper *SSHOptionWrapper) (Environment, error) {

sshOptions := sshOptionWrapper.getSSHOptionsForEnvironment(environment.EnvironmentName)

// We don't run prerequisite checks on these syncers for now.
if syncerType == "files" || syncerType == "drupalconfig" {
environment.RsyncPath = "rsync"
Expand Down Expand Up @@ -104,7 +107,10 @@ func RunPrerequisiteCommand(environment Environment, syncer Syncer, syncerType s
return environment, nil
}

func PrerequisiteCleanUp(environment Environment, rsyncPath string, dryRun bool, sshOptions SSHOptions) error {
func PrerequisiteCleanUp(environment Environment, rsyncPath string, dryRun bool, sshOptionWrapper *SSHOptionWrapper) error {

sshOptions := sshOptionWrapper.getSSHOptionsForEnvironment(environment.EnvironmentName)

if rsyncPath == "" || rsyncPath == "rsync" || !strings.Contains(rsyncPath, "/tmp/") {
return nil
}
Expand Down
30 changes: 30 additions & 0 deletions synchers/sshOptionWrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package synchers

// sshOptionWrapper.go contains the logic for the new system for passing ssh portal data

// SSHOptionWrapper is passed around instead of specific SSHOptions - this allows resolution of the ssh endpoint when and where it's needed
type SSHOptionWrapper struct {
ProjectName string // this is primarily used to ensure someone doesn't do something silly - it's an assertion
Options map[string]SSHOptions // a map off all named ssh options - environment => ssh config
Default SSHOptions // this will be returned if no explicit match is found in `Options`
}

func NewSshOptionWrapper(projectName string, defaultSshOptions SSHOptions) *SSHOptionWrapper {
return &SSHOptionWrapper{
ProjectName: projectName,
Options: map[string]SSHOptions{},
Default: defaultSshOptions,
}
}

func (receiver *SSHOptionWrapper) getSSHOptionsForEnvironment(environmentName string) SSHOptions {
sshOptionsMapValue, ok := receiver.Options[environmentName]
if ok {
return sshOptionsMapValue
}
return receiver.Default
}

func (receiver *SSHOptionWrapper) addSsshOptionForEnvironment(environmentName string, sshOptions SSHOptions) {
receiver.Options[environmentName] = sshOptions
}
124 changes: 124 additions & 0 deletions synchers/sshOptionWrapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package synchers

import (
"reflect"
"testing"
)

var testOptions = map[string]SSHOptions{
"env1": {
Host: "env1s.host.com", // Note, we're only really setting the host to differentiate during the test
},
"env2": {
Host: "env2s.host.com",
},
}

func TestSSHOptionWrapper_getSSHOptionsForEnvironment(t *testing.T) {
type fields struct {
ProjectName string
Options map[string]SSHOptions
Default SSHOptions
}
type args struct {
environmentName string
}
tests := []struct {
name string
fields fields
args args
want SSHOptions
}{
{
name: "Falls back to default",
fields: fields{
ProjectName: "test",
Options: testOptions,
Default: SSHOptions{
Host: "defaulthost",
},
},
want: SSHOptions{
Host: "defaulthost",
},
args: args{environmentName: "shoulddefault"},
},
{
name: "Gets named environment ssh details",
fields: fields{
ProjectName: "test",
Options: testOptions,
Default: SSHOptions{
Host: "defaulthost",
},
},
want: SSHOptions{
Host: "env1s.host.com",
},
args: args{environmentName: "env1"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
receiver := &SSHOptionWrapper{
ProjectName: tt.fields.ProjectName,
Options: tt.fields.Options,
Default: tt.fields.Default,
}
if got := receiver.getSSHOptionsForEnvironment(tt.args.environmentName); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getSSHOptionsForEnvironment() = %v, want %v", got, tt.want)
}
})
}
}

func TestSSHOptionWrapper_addSsshOptionForEnvironment(t *testing.T) {
type fields struct {
ProjectName string
Options map[string]SSHOptions
Default SSHOptions
}
type args struct {
environmentName string
environmentSSHOptions SSHOptions
}
tests := []struct {
name string
fields fields
args args
want SSHOptions
}{
{
name: "Adds a new item to the list",
fields: fields{
ProjectName: "test",
Options: testOptions,
Default: SSHOptions{
Host: "defaulthost",
},
},
want: SSHOptions{
Host: "newItem.ssh.com",
},
args: args{
environmentSSHOptions: SSHOptions{
Host: "newItem.ssh.com",
},
environmentName: "newItem",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
receiver := &SSHOptionWrapper{
ProjectName: tt.fields.ProjectName,
Options: tt.fields.Options,
Default: tt.fields.Default,
}
receiver.addSsshOptionForEnvironment(tt.args.environmentName, tt.args.environmentSSHOptions)
if got := receiver.getSSHOptionsForEnvironment(tt.args.environmentName); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getSSHOptionsForEnvironment() = %v, want %v", got, tt.want)
}
})
}
}
Loading
Loading