Skip to content

Commit

Permalink
Merge pull request #261 from mkumatag/subnet
Browse files Browse the repository at this point in the history
user supplied subnet
  • Loading branch information
ppc64le-cloud-bot authored Jan 13, 2025
2 parents 014a5d8 + 0fdf9b3 commit 41c31fb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions builder/powervs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
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
Expand Up @@ -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"`
Expand Down
9 changes: 8 additions & 1 deletion builder/powervs/common/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions builder/powervs/step_create_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
)

type StepCreateNetwork struct {
SubnetID string
DHCPNetwork bool
doCleanup bool
}
Expand All @@ -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")
Expand Down

0 comments on commit 41c31fb

Please sign in to comment.