Skip to content

Commit

Permalink
Merge pull request #266 from fernflower/supportsubnets
Browse files Browse the repository at this point in the history
Allow passing several subnets for vm creation
  • Loading branch information
ppc64le-cloud-bot authored Jan 15, 2025
2 parents 34944a2 + bc4b555 commit 1d6a6ae
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 26 deletions.
2 changes: 1 addition & 1 deletion builder/powervs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +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,
SubnetIDs: b.config.SubnetIDs,
DHCPNetwork: b.config.DHCPNetwork,
},
&StepCreateInstance{
Expand Down
4 changes: 2 additions & 2 deletions builder/powervs/builder.hcl2spec.go

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

12 changes: 6 additions & 6 deletions builder/powervs/common/run_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ 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"`
InstanceName string `mapstructure:"instance_name" required:"true"`
KeyPairName string `mapstructure:"key_pair_name" required:"true"`
SubnetIDs []string `mapstructure:"subnet_ids" required:"false"`
DHCPNetwork bool `mapstructure:"dhcp_network" required:"false"`
Source Source `mapstructure:"source" required:"true"`
Capture Capture `mapstructure:"capture" required:"true"`

// Communicator settings
Comm communicator.Config `mapstructure:",squash"`
Expand Down
20 changes: 16 additions & 4 deletions builder/powervs/step_create_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,23 @@ func (s *StepCreateInstance) Run(_ context.Context, state multistep.StateBag) mu
net := state.Get("network").(*models.Network)

imageRef := state.Get("source_image").(*models.ImageReference)
networks := []*models.PVMInstanceAddNetwork{
{
NetworkID: net.NetworkID,
},

networks := []*models.PVMInstanceAddNetwork{}

if state.Get("networks") != nil {
// Several subnets have been specified -> pass them all for vm creation
networks = []*models.PVMInstanceAddNetwork{}

for _, subnet := range state.Get("networks").([]string){
subnetAdd := &models.PVMInstanceAddNetwork{
NetworkID: &subnet,
}
networks = append(networks, subnetAdd)
}
} else {
networks = append(networks, &models.PVMInstanceAddNetwork{NetworkID: net.NetworkID})
}

body := &models.PVMInstanceCreate{
ImageID: imageRef.ImageID,
KeyPairName: s.KeyPairName,
Expand Down
34 changes: 21 additions & 13 deletions builder/powervs/step_create_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,37 @@ const (
)

type StepCreateNetwork struct {
SubnetID string
DHCPNetwork bool
doCleanup bool
SubnetIDs []string
DHCPNetwork bool
doCleanup bool
}


func (s *StepCreateNetwork) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packersdk.Ui)

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
if s.SubnetIDs != nil {
for i, subnetID := range s.SubnetIDs {
ui.Say("The subnet is specified by the user; reuse it instead of creating a new one.")
net, err := networkClient.Get(subnetID)
if err != nil {
ui.Error(fmt.Sprintf("failed to get subnet: %s, error: %v", subnetID, err))
return multistep.ActionHalt
}
ui.Message(fmt.Sprintf("Network found!, Name: %s, ID: %s", *net.Name, *net.NetworkID))
if i == 0 {
ui.Say(fmt.Sprintf("Registering subnet %s as interface for ssh", subnetID))
state.Put("network", net)
}
}
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
// The subnets have been validated, let's pass them further as plain ids
state.Put("networks", s.SubnetIDs)
// do not delete user specified subnets, hence skipping the cleanup
s.doCleanup = false
return multistep.ActionContinue
}
}

// If CreateDHCPNetwork is set, Create DHCP network.
if s.DHCPNetwork {
Expand Down

0 comments on commit 1d6a6ae

Please sign in to comment.