Skip to content

Commit

Permalink
Adds new SSHOptionWrapper (#111)
Browse files Browse the repository at this point in the history
* Adds new SSHOptionWrapper

* Uses new ssh options wrapper

---------

Co-authored-by: Blaize Kaye <[email protected]>
  • Loading branch information
bomoko and Blaize Kaye authored Mar 26, 2024
1 parent 6815673 commit 10100a0
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 47 deletions.
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

0 comments on commit 10100a0

Please sign in to comment.