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

user supplied subnet #261

Merged
merged 1 commit into from
Jan 13, 2025
Merged
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
1 change: 1 addition & 0 deletions builder/powervs/builder.go
Original file line number Diff line number Diff line change
@@ -97,6 +97,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
Source: b.config.Source,
},
&StepCreateNetwork{
SubnetID: b.config.SubnetID,
DHCPNetwork: b.config.DHCPNetwork,
},
&StepCreateInstance{
2 changes: 2 additions & 0 deletions builder/powervs/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions builder/powervs/common/run_config.go
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ type CaptureCOS struct {
type RunConfig struct {
InstanceName string `mapstructure:"instance_name" required:"true"`
KeyPairName string `mapstructure:"key_pair_name" required:"true"`
SubnetID string `mapstructure:"subnet_id" required:"false"`
DHCPNetwork bool `mapstructure:"dhcp_network" required:"false"`
Source Source `mapstructure:"source" required:"true"`
Capture Capture `mapstructure:"capture" required:"true"`
9 changes: 8 additions & 1 deletion builder/powervs/common/ssh.go
Original file line number Diff line number Diff line change
@@ -25,21 +25,28 @@ func SSHHost() func(multistep.StateBag) (string, error) {
instanceClient := state.Get("instanceClient").(*instance.IBMPIInstanceClient)
host := ""
const tries = 25
subnet := state.Get("network").(*models.Network)
for j := 0; j <= tries; j++ {
i := state.Get("instance").(*models.PVMInstance)
in, err := instanceClient.Get(*i.PvmInstanceID)
if err != nil {
return "", errors.New("couldn't determine address for instance: failed to get instance")
}
for _, net := range in.Networks {
if net.ExternalIP != "" {
if *subnet.NetworkID != net.NetworkID {
continue
}
if *subnet.Type == "vlan" {
host = net.IPAddress
} else if *subnet.Type == "pub-vlan" {
host = net.ExternalIP
}
}
if host != "" {
return host, nil
}

// TODO: following code doesn't look good, needs refactoring.
dhcpServerID, ok := state.GetOk("dhcpServerID")
if !ok {
// if the dhcpServerID is not set, dont try to fetch IP from DHCP server, instead wait for address to get populated.
15 changes: 15 additions & 0 deletions builder/powervs/step_create_network.go
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ const (
)

type StepCreateNetwork struct {
SubnetID string
DHCPNetwork bool
doCleanup bool
}
@@ -29,6 +30,20 @@ func (s *StepCreateNetwork) Run(_ context.Context, state multistep.StateBag) mul

networkClient := state.Get("networkClient").(*instance.IBMPINetworkClient)

if s.SubnetID != "" {
ui.Say("The subnet is specified by the user; reuse it instead of creating a new one.")
net, err := networkClient.Get(s.SubnetID)
if err != nil {
ui.Error(fmt.Sprintf("failed to get subnet: %s, error: %v", s.SubnetID, err))
return multistep.ActionHalt
}
ui.Message(fmt.Sprintf("Network found!, Name: %s, ID: %s", *net.Name, *net.NetworkID))
state.Put("network", net)
// do not delete the user specified subnet, hence skipping the cleanup
s.doCleanup = false
return multistep.ActionContinue
}

// If CreateDHCPNetwork is set, Create DHCP network.
if s.DHCPNetwork {
ui.Say("Creating DHCP network")