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

fix osc sdk go v2 migration and pointers #115

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ issues:
linters:
disable-all: true
enable:
- deadcode
outscale-mdr marked this conversation as resolved.
Show resolved Hide resolved
- errcheck
- goimports
- gosimple
Expand All @@ -36,7 +35,6 @@ linters:
- staticcheck
- unconvert
- unused
- varcheck
fast: true

# options for analysis running
Expand Down
6 changes: 4 additions & 2 deletions builder/osc/bsusurrogate/step_register_omi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bsusurrogate
import (
"context"
"fmt"
"log"

"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -121,7 +122,7 @@ func (s *StepRegisterOMI) combineDevices(snapshotIDs map[string]string) []oscgo.
// one designated as the root device in ami_root_device
for _, device := range s.LaunchDevices {
snapshotID, ok := snapshotIDs[device.GetDeviceName()]
if ok {
if ok && snapshotID != "" {
device.Bsu.SnapshotId = &snapshotID
}
if device.GetDeviceName() == s.RootDevice.SourceDeviceName {
Expand All @@ -130,7 +131,7 @@ func (s *StepRegisterOMI) combineDevices(snapshotIDs map[string]string) []oscgo.
if _, ok := device.Bsu.GetVolumeTypeOk(); ok {
device.Bsu.VolumeType = &s.RootDevice.VolumeType
if device.Bsu.GetVolumeType() != "io1" {
*device.Bsu.Iops = 0
device.Bsu.Iops = nil
}
}

Expand All @@ -146,6 +147,7 @@ func (s *StepRegisterOMI) combineDevices(snapshotIDs map[string]string) []oscgo.
}

func copyToDeviceMappingImage(device osc.BlockDeviceMappingVmCreation) oscgo.BlockDeviceMappingImage {
log.Printf("Copy device mapping image ")
deviceImage := oscgo.BlockDeviceMappingImage{
DeviceName: device.DeviceName,
VirtualDeviceName: device.VirtualDeviceName,
Expand Down
2 changes: 1 addition & 1 deletion builder/osc/bsusurrogate/step_snapshop_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s *StepSnapshotVolumes) snapshotVolume(ctx context.Context, deviceName str
var volumeId string
for _, volume := range *vm.BlockDeviceMappings {
if volume.GetDeviceName() == deviceName {
volumeId = volume.Bsu.GetVolumeId()
volumeId = *volume.GetBsu().VolumeId
}
}
if volumeId == "" {
Expand Down
6 changes: 4 additions & 2 deletions builder/osc/chroot/step_vm_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
oscgo "github.com/outscale/osc-sdk-go/v2"
osccommon "github.com/outscale/packer-plugin-outscale/builder/osc/common"
)

// StepVmInfo verifies that this builder is running on an Outscale vm.
type StepVmInfo struct{}

func (s *StepVmInfo) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
oscconn := state.Get("osc").(*oscgo.APIClient)
oscconn := state.Get("osc").(*osccommon.OscClient)
//session := state.Get("clientConfig").(*session.Session)
ui := state.Get("ui").(packersdk.Ui)

Expand All @@ -36,7 +37,8 @@ func (s *StepVmInfo) Run(_ context.Context, state multistep.StateBag) multistep.
log.Printf("[Debug] VmID got: %s", string(vmID))

// Query the entire vm metadata
resp, _, err := oscconn.VmApi.ReadVms(context.Background()).ReadVmsRequest(oscgo.ReadVmsRequest{

resp, _, err := oscconn.Api.VmApi.ReadVms(oscconn.Auth).ReadVmsRequest(oscgo.ReadVmsRequest{
Filters: &oscgo.FiltersVm{
VmIds: &[]string{string(vmID)},
},
Expand Down
2 changes: 2 additions & 0 deletions builder/osc/common/block_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ func buildOscBlockDevicesVmCreation(b []BlockDevice) []oscgo.BlockDeviceMappingV
mapping.Bsu = &bsu
}

log.Printf("block device mapping")

blockDevices = append(blockDevices, mapping)
}
return blockDevices
Expand Down
4 changes: 2 additions & 2 deletions builder/osc/common/step_get_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ WaitLoop:
func (s *StepGetPassword) Cleanup(multistep.StateBag) {}

func (s *StepGetPassword) waitForPassword(state multistep.StateBag, cancel <-chan struct{}) (string, error) {
oscconn := state.Get("osc").(*oscgo.APIClient)
oscconn := state.Get("osc").(*OscClient)
vm := state.Get("vm").(oscgo.Vm)
privateKey := s.Comm.SSHPrivateKey

Expand All @@ -114,7 +114,7 @@ func (s *StepGetPassword) waitForPassword(state multistep.StateBag, cancel <-cha
case <-time.After(5 * time.Second):
}

resp, _, err := oscconn.VmApi.ReadAdminPassword(context.Background()).ReadAdminPasswordRequest(oscgo.ReadAdminPasswordRequest{VmId: *vm.VmId}).Execute()
resp, _, err := oscconn.Api.VmApi.ReadAdminPassword(oscconn.Auth).ReadAdminPasswordRequest(oscgo.ReadAdminPasswordRequest{VmId: vm.GetVmId()}).Execute()
if err != nil {
err := fmt.Errorf("Error retrieving auto-generated vm password: %s", err)
return "", err
Expand Down
27 changes: 16 additions & 11 deletions builder/osc/common/step_run_source_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,30 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
return multistep.ActionHalt
}

subregion := state.Get("subregion_name").(string)
blockDevice := s.BlockDevices.BuildOSCLaunchDevices()
vmcount := int32(1)
subregion := state.Get("subregion_name").(string)
subnetID := state.Get("subnet_id").(string)

runOpts := oscgo.CreateVmsRequest{
ImageId: s.SourceOMI,
VmType: &s.VmType,
UserData: &userData,
MaxVmsCount: &vmcount,
MinVmsCount: &vmcount,
Placement: &oscgo.Placement{SubregionName: &subregion},
BsuOptimized: &s.BsuOptimized,
//BlockDeviceMappings: s.BlockDevices.BuildOSCLaunchDevices(),
ImageId: s.SourceOMI,
VmType: &s.VmType,
UserData: &userData,
MaxVmsCount: &vmcount,
MinVmsCount: &vmcount,
BsuOptimized: &s.BsuOptimized,
BlockDeviceMappings: &blockDevice,
}

log.Printf("subregion is %s", subregion)
if subregion != "" {
runOpts.Placement = &oscgo.Placement{SubregionName: &subregion}
}

if s.Comm.SSHKeyPairName != "" {
runOpts.KeypairName = &s.Comm.SSHKeyPairName
}

subnetID := state.Get("subnet_id").(string)

runOpts.SubnetId = &subnetID
runOpts.SecurityGroupIds = &securityGroupIds

Expand Down
5 changes: 4 additions & 1 deletion builder/osc/common/step_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ func (s *StepSecurityGroup) Run(_ context.Context, state multistep.StateBag) mul

createSGReq := oscgo.CreateSecurityGroupRequest{
SecurityGroupName: groupName,
NetId: &netID,
Description: "Temporary group for Packer",
}

if netID != "" {
createSGReq.NetId = &netID
}

resp, _, err := conn.Api.SecurityGroupApi.CreateSecurityGroup(conn.Auth).CreateSecurityGroupRequest(createSGReq).Execute()

if err != nil {
Expand Down