Add option to use prive ip for oci builder

This commit is contained in:
Robert Neumayer 2018-02-13 14:20:26 +01:00
parent 104d8cebe8
commit 30fa1494d5
6 changed files with 56 additions and 12 deletions

View File

@ -26,12 +26,13 @@ type Config struct {
AccessCfgFileAccount string `mapstructure:"access_cfg_file_account"` AccessCfgFileAccount string `mapstructure:"access_cfg_file_account"`
// Access config overrides // Access config overrides
UserID string `mapstructure:"user_ocid"` UserID string `mapstructure:"user_ocid"`
TenancyID string `mapstructure:"tenancy_ocid"` TenancyID string `mapstructure:"tenancy_ocid"`
Region string `mapstructure:"region"` Region string `mapstructure:"region"`
Fingerprint string `mapstructure:"fingerprint"` Fingerprint string `mapstructure:"fingerprint"`
KeyFile string `mapstructure:"key_file"` KeyFile string `mapstructure:"key_file"`
PassPhrase string `mapstructure:"pass_phrase"` PassPhrase string `mapstructure:"pass_phrase"`
UsePrivateIP bool `mapstructure:"use_private_ip"`
AvailabilityDomain string `mapstructure:"availability_domain"` AvailabilityDomain string `mapstructure:"availability_domain"`
CompartmentID string `mapstructure:"compartment_ocid"` CompartmentID string `mapstructure:"compartment_ocid"`

View File

@ -24,7 +24,8 @@ func testConfig(accessConfFile *os.File) map[string]interface{} {
"subnet_ocid": "ocd1...", "subnet_ocid": "ocd1...",
// Comm // Comm
"ssh_username": "opc", "ssh_username": "opc",
"use_private_ip": false,
} }
} }

View File

@ -24,6 +24,8 @@ type driverMock struct {
WaitForImageCreationErr error WaitForImageCreationErr error
WaitForInstanceStateErr error WaitForInstanceStateErr error
cfg *Config
} }
// CreateInstance creates a new compute instance. // CreateInstance creates a new compute instance.
@ -57,11 +59,14 @@ func (d *driverMock) DeleteImage(id string) error {
return nil 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) { func (d *driverMock) GetInstanceIP(id string) (string, error) {
if d.GetInstanceIPErr != nil { if d.GetInstanceIPErr != nil {
return "", d.GetInstanceIPErr return "", d.GetInstanceIPErr
} }
if d.cfg.UsePrivateIP {
return "private_ip", nil
}
return "ip", nil return "ip", nil
} }

View File

@ -63,7 +63,7 @@ func (d *driverOCI) DeleteImage(id string) error {
return d.client.Compute.Images.Delete(&client.DeleteImageParams{ID: id}) 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) { func (d *driverOCI) GetInstanceIP(id string) (string, error) {
// get nvic and cross ref to find pub ip address // get nvic and cross ref to find pub ip address
vnics, err := d.client.Compute.VNICAttachments.List( 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) return "", fmt.Errorf("Error getting VNIC details: %s", err)
} }
if d.cfg.UsePrivateIP {
return vnic.PrivateIP, nil
}
return vnic.PublicIP, nil return vnic.PublicIP, nil
} }

View File

@ -1,11 +1,13 @@
package oci package oci
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"testing" "testing"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
) )
func TestInstanceInfo(t *testing.T) { 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) { func TestInstanceInfo_GetInstanceIPErr(t *testing.T) {
state := testState() state := testState()
state.Put("instance_id", "ocid1...") state.Put("instance_id", "ocid1...")

View File

@ -36,7 +36,8 @@ func baseTestConfig() *Config {
"key_file": keyFile.Name(), "key_file": keyFile.Name(),
// Comm // Comm
"ssh_username": "opc", "ssh_username": "opc",
"use_private_ip": false,
}) })
// Once we have a config object they key file isn't re-read so we can // 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 { func testState() multistep.StateBag {
baseTestConfig := baseTestConfig()
state := new(multistep.BasicStateBag) state := new(multistep.BasicStateBag)
state.Put("config", baseTestConfig()) state.Put("config", baseTestConfig)
state.Put("driver", &driverMock{}) state.Put("driver", &driverMock{cfg: baseTestConfig})
state.Put("hook", &packer.MockHook{}) state.Put("hook", &packer.MockHook{})
state.Put("ui", &packer.BasicUi{ state.Put("ui", &packer.BasicUi{
Reader: new(bytes.Buffer), Reader: new(bytes.Buffer),