Merge pull request #5893 from neumayer/privateip
Allow using private ip addresses for oracle oci builder
This commit is contained in:
commit
95aa7e67bd
|
@ -32,6 +32,7 @@ type Config struct {
|
|||
Fingerprint string `mapstructure:"fingerprint"`
|
||||
KeyFile string `mapstructure:"key_file"`
|
||||
PassPhrase string `mapstructure:"pass_phrase"`
|
||||
UsePrivateIP bool `mapstructure:"use_private_ip"`
|
||||
|
||||
AvailabilityDomain string `mapstructure:"availability_domain"`
|
||||
CompartmentID string `mapstructure:"compartment_ocid"`
|
||||
|
|
|
@ -25,6 +25,7 @@ func testConfig(accessConfFile *os.File) map[string]interface{} {
|
|||
|
||||
// Comm
|
||||
"ssh_username": "opc",
|
||||
"use_private_ip": false,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -24,6 +24,8 @@ type driverMock struct {
|
|||
WaitForImageCreationErr error
|
||||
|
||||
WaitForInstanceStateErr error
|
||||
|
||||
cfg *Config
|
||||
}
|
||||
|
||||
// CreateInstance creates a new compute instance.
|
||||
|
@ -57,11 +59,14 @@ func (d *driverMock) DeleteImage(id string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// GetInstanceIP returns the public IP corresponding to the given instance id.
|
||||
// GetInstanceIP returns the public or private IP corresponding to the given instance id.
|
||||
func (d *driverMock) GetInstanceIP(id string) (string, error) {
|
||||
if d.GetInstanceIPErr != nil {
|
||||
return "", d.GetInstanceIPErr
|
||||
}
|
||||
if d.cfg.UsePrivateIP {
|
||||
return "private_ip", nil
|
||||
}
|
||||
return "ip", nil
|
||||
}
|
||||
|
||||
|
|
|
@ -63,7 +63,7 @@ func (d *driverOCI) DeleteImage(id string) error {
|
|||
return d.client.Compute.Images.Delete(&client.DeleteImageParams{ID: id})
|
||||
}
|
||||
|
||||
// GetInstanceIP returns the public IP corresponding to the given instance id.
|
||||
// GetInstanceIP returns the public or private IP corresponding to the given instance id.
|
||||
func (d *driverOCI) GetInstanceIP(id string) (string, error) {
|
||||
// get nvic and cross ref to find pub ip address
|
||||
vnics, err := d.client.Compute.VNICAttachments.List(
|
||||
|
@ -85,6 +85,9 @@ func (d *driverOCI) GetInstanceIP(id string) (string, error) {
|
|||
return "", fmt.Errorf("Error getting VNIC details: %s", err)
|
||||
}
|
||||
|
||||
if d.cfg.UsePrivateIP {
|
||||
return vnic.PrivateIP, nil
|
||||
}
|
||||
return vnic.PublicIP, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ func (s *stepInstanceInfo) Run(_ context.Context, state multistep.StateBag) mult
|
|||
|
||||
ip, err := driver.GetInstanceIP(id)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Error getting instance's public IP: %s", err)
|
||||
err = fmt.Errorf("Error getting instance's IP: %s", err)
|
||||
ui.Error(err.Error())
|
||||
state.Put("error", err)
|
||||
return multistep.ActionHalt
|
||||
|
@ -27,7 +27,7 @@ func (s *stepInstanceInfo) Run(_ context.Context, state multistep.StateBag) mult
|
|||
|
||||
state.Put("instance_ip", ip)
|
||||
|
||||
ui.Say(fmt.Sprintf("Instance has public IP: %s.", ip))
|
||||
ui.Say(fmt.Sprintf("Instance has IP: %s.", ip))
|
||||
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
package oci
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/packer/helper/multistep"
|
||||
"github.com/hashicorp/packer/packer"
|
||||
)
|
||||
|
||||
func TestInstanceInfo(t *testing.T) {
|
||||
|
@ -29,6 +31,36 @@ func TestInstanceInfo(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestInstanceInfoPrivateIP(t *testing.T) {
|
||||
baseTestConfig := baseTestConfig()
|
||||
baseTestConfig.UsePrivateIP = true
|
||||
state := new(multistep.BasicStateBag)
|
||||
state.Put("config", baseTestConfig)
|
||||
state.Put("driver", &driverMock{cfg: baseTestConfig})
|
||||
state.Put("hook", &packer.MockHook{})
|
||||
state.Put("ui", &packer.BasicUi{
|
||||
Reader: new(bytes.Buffer),
|
||||
Writer: new(bytes.Buffer),
|
||||
})
|
||||
state.Put("instance_id", "ocid1...")
|
||||
|
||||
step := new(stepInstanceInfo)
|
||||
defer step.Cleanup(state)
|
||||
|
||||
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
||||
t.Fatalf("bad action: %#v", action)
|
||||
}
|
||||
|
||||
instanceIPRaw, ok := state.GetOk("instance_ip")
|
||||
if !ok {
|
||||
t.Fatalf("should have instance_ip")
|
||||
}
|
||||
|
||||
if instanceIPRaw.(string) != "private_ip" {
|
||||
t.Fatalf("should've got ip ('%s' != 'private_ip')", instanceIPRaw.(string))
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstanceInfo_GetInstanceIPErr(t *testing.T) {
|
||||
state := testState()
|
||||
state.Put("instance_id", "ocid1...")
|
||||
|
|
|
@ -37,6 +37,7 @@ func baseTestConfig() *Config {
|
|||
|
||||
// Comm
|
||||
"ssh_username": "opc",
|
||||
"use_private_ip": false,
|
||||
})
|
||||
|
||||
// Once we have a config object they key file isn't re-read so we can
|
||||
|
@ -50,9 +51,10 @@ func baseTestConfig() *Config {
|
|||
}
|
||||
|
||||
func testState() multistep.StateBag {
|
||||
baseTestConfig := baseTestConfig()
|
||||
state := new(multistep.BasicStateBag)
|
||||
state.Put("config", baseTestConfig())
|
||||
state.Put("driver", &driverMock{})
|
||||
state.Put("config", baseTestConfig)
|
||||
state.Put("driver", &driverMock{cfg: baseTestConfig})
|
||||
state.Put("hook", &packer.MockHook{})
|
||||
state.Put("ui", &packer.BasicUi{
|
||||
Reader: new(bytes.Buffer),
|
||||
|
|
|
@ -123,6 +123,8 @@ builder.
|
|||
value provided by the [OCI config file](https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/sdkconfig.htm)
|
||||
if present.
|
||||
|
||||
- `use_private_ip` (boolean) - Use private ip addresses to connect to the instance via ssh.
|
||||
|
||||
|
||||
## Basic Example
|
||||
|
||||
|
|
Loading…
Reference in New Issue