From 6f0bb33c9a2a90f938159673b4388d7bede4f644 Mon Sep 17 00:00:00 2001 From: Marin Salinas Date: Thu, 20 Aug 2020 20:35:51 -0500 Subject: [PATCH] refactor: change tags ssh file to new OSC SDK --- builder/osc/common/ssh.go | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/builder/osc/common/ssh.go b/builder/osc/common/ssh.go index 829165d0c..55e39289f 100644 --- a/builder/osc/common/ssh.go +++ b/builder/osc/common/ssh.go @@ -1,18 +1,27 @@ package common import ( + "context" "errors" "fmt" "time" + "net/http" + + "github.com/antihax/optional" "github.com/hashicorp/packer/helper/multistep" "github.com/outscale/osc-go/oapi" + "github.com/outscale/osc-sdk-go/osc" ) type oapiDescriber interface { POST_ReadVms(oapi.ReadVmsRequest) (*oapi.POST_ReadVmsResponses, error) } +type oscDescriber interface { + ReadVms(ctx context.Context, localVarOptionals *osc.ReadVmsOpts) (osc.ReadVmsResponse, *http.Response, error) +} + var ( // modified in tests sshHostSleepDuration = time.Second @@ -83,3 +92,72 @@ func SSHHost(e oapiDescriber, sshInterface string) func(multistep.StateBag) (str return "", errors.New("couldn't determine address for vm") } } + +// SSHHost returns a function that can be given to the SSH communicator +// for determining the SSH address based on the vm DNS name. +func OscSSHHost(e oscDescriber, sshInterface string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + const tries = 2 + // <= with current structure to check result of describing `tries` times + for j := 0; j <= tries; j++ { + var host string + i := state.Get("vm").(osc.Vm) + + if sshInterface != "" { + switch sshInterface { + case "public_ip": + if i.PublicIp != "" { + host = i.PublicIp + } + case "public_dns": + if i.PublicDnsName != "" { + host = i.PublicDnsName + } + case "private_ip": + if i.PrivateIp != "" { + host = i.PrivateIp + } + case "private_dns": + if i.PrivateDnsName != "" { + host = i.PrivateDnsName + } + default: + panic(fmt.Sprintf("Unknown interface type: %s", sshInterface)) + } + } else if i.NetId != "" { + if i.PublicIp != "" { + host = i.PublicIp + } else if i.PrivateIp != "" { + host = i.PrivateIp + } + } else if i.PublicDnsName != "" { + host = i.PublicDnsName + } + + if host != "" { + return host, nil + } + + r, _, err := e.ReadVms(context.Background(), &osc.ReadVmsOpts{ + ReadVmsRequest: optional.NewInterface(osc.ReadVmsRequest{ + Filters: osc.FiltersVm{ + VmIds: []string{i.VmId}, + }, + }), + }) + + if err != nil { + return "", err + } + + if len(r.Vms) == 0 { + return "", fmt.Errorf("vm not found: %s", i.VmId) + } + + state.Put("vm", r.Vms[0]) + time.Sleep(sshHostSleepDuration) + } + + return "", errors.New("couldn't determine address for vm") + } +}