Merge pull request #10 from marinsalinas/bsuvolume

BSU Volume sdk migration
This commit is contained in:
Marin Salinas 2020-10-09 14:37:53 -05:00 committed by GitHub
commit dcaf56596d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 300 additions and 950 deletions

View File

@ -9,9 +9,7 @@ package bsu
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"github.com/hashicorp/hcl/v2/hcldec"
osccommon "github.com/hashicorp/packer/builder/osc/common"
@ -21,7 +19,6 @@ import (
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi"
)
// The unique ID for this builder
@ -86,25 +83,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
}
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
clientConfig, err := b.config.Config()
if err != nil {
return nil, err
}
skipClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
oapiconn := oapi.NewClient(clientConfig, skipClient)
oscConn := b.config.NewOSCClient()
// Setup the state bag and initial state for the steps
state := new(multistep.BasicStateBag)
state.Put("config", &b.config)
state.Put("oapi", oapiconn)
state.Put("osc", oscConn)
state.Put("accessConfig", &b.config.AccessConfig)
state.Put("hook", hook)
@ -224,7 +207,6 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
artifact := &osccommon.Artifact{
Omis: omis.(map[string]string),
BuilderIdValue: BuilderId,
Config: clientConfig,
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
}

View File

@ -2,13 +2,9 @@
package bsu
import (
"crypto/tls"
"net/http"
"testing"
"github.com/hashicorp/packer/builder/osc/common"
builderT "github.com/hashicorp/packer/helper/builder/testing"
"github.com/outscale/osc-go/oapi"
)
func TestBuilderAcc_basic(t *testing.T) {
@ -23,22 +19,6 @@ func TestBuilderAcc_basic(t *testing.T) {
func testAccPreCheck(t *testing.T) {
}
func testOAPIConn() (*oapi.Client, error) {
access := &common.AccessConfig{RawRegion: "us-east-1"}
clientConfig, err := access.Config()
if err != nil {
return nil, err
}
skipClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
return oapi.NewClient(clientConfig, skipClient), nil
}
const testBuilderAccBasic = `
{
"builders": [{
@ -47,7 +27,7 @@ const testBuilderAccBasic = `
"vm_type": "t2.micro",
"source_omi": "ami-abe953fa",
"ssh_username": "outscale",
"omi_name": "packer-test {{timestamp}}",
"omi_name": "packer-test",
"associate_public_ip_address": true,
"force_deregister": true
}]

View File

@ -6,10 +6,8 @@ package bsusurrogate
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net/http"
"github.com/hashicorp/hcl/v2/hcldec"
osccommon "github.com/hashicorp/packer/builder/osc/common"
@ -19,7 +17,6 @@ import (
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi"
)
const BuilderId = "oapi.outscale.bsusurrogate"
@ -104,25 +101,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
}
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
clientConfig, err := b.config.Config()
if err != nil {
return nil, err
}
skipClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
oapiconn := oapi.NewClient(clientConfig, skipClient)
oscConn := b.config.NewOSCClient()
// Setup the state bag and initial state for the steps
state := new(multistep.BasicStateBag)
state.Put("config", &b.config)
state.Put("oapi", oapiconn)
state.Put("osc", oscConn)
state.Put("accessConfig", &b.config.AccessConfig)
state.Put("hook", hook)
@ -196,8 +179,8 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
},
&communicator.StepConnect{
Config: &b.config.RunConfig.Comm,
Host: osccommon.SSHHost(
oapiconn,
Host: osccommon.OscSSHHost(
oscConn.VmApi,
b.config.SSHInterface),
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
},
@ -251,7 +234,6 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
artifact := &osccommon.Artifact{
Omis: omis.(map[string]string),
BuilderIdValue: BuilderId,
Config: clientConfig,
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
}

View File

@ -8,7 +8,6 @@ import (
osccommon "github.com/hashicorp/packer/builder/osc/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
@ -23,7 +22,6 @@ type StepRegisterOMI struct {
func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
oapiconn := state.Get("oapi").(*oapi.Client)
oscconn := state.Get("osc").(*osc.APIClient)
snapshotIds := state.Get("snapshot_ids").(map[string]string)
ui := state.Get("ui").(packer.Ui)
@ -51,7 +49,7 @@ func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) mul
// Set the OMI ID in the state
ui.Say(fmt.Sprintf("OMI: %s", registerResp.Image.ImageId))
omis := make(map[string]string)
omis[oapiconn.GetConfig().Region] = registerResp.Image.ImageId
omis[s.RawRegion] = registerResp.Image.ImageId
state.Put("omis", omis)
// Wait for the image to become ready

View File

@ -1,13 +1,15 @@
package bsuvolume
import (
"context"
"fmt"
"log"
"sort"
"strings"
"github.com/antihax/optional"
"github.com/hashicorp/packer/packer"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
// map of region to list of volume IDs
@ -22,7 +24,7 @@ type Artifact struct {
BuilderIdValue string
// Client connection for performing API stuff.
Conn *oapi.Client
Conn *osc.APIClient
// StateData should store data such as GeneratedData
// to be shared with post-processors
@ -70,10 +72,12 @@ func (a *Artifact) Destroy() error {
for _, volumeID := range volumeIDs {
log.Printf("Deregistering Volume ID (%s) from region (%s)", volumeID, region)
input := oapi.DeleteVolumeRequest{
input := osc.DeleteVolumeRequest{
VolumeId: volumeID,
}
if _, err := a.Conn.POST_DeleteVolume(input); err != nil {
if _, _, err := a.Conn.VolumeApi.DeleteVolume(context.Background(), &osc.DeleteVolumeOpts{
DeleteVolumeRequest: optional.NewInterface(optional.NewInterface(input)),
}); err != nil {
errors = append(errors, err)
}
}

View File

@ -6,10 +6,8 @@ package bsuvolume
import (
"context"
"crypto/tls"
"fmt"
"log"
"net/http"
"github.com/hashicorp/hcl/v2/hcldec"
osccommon "github.com/hashicorp/packer/builder/osc/common"
@ -19,7 +17,6 @@ import (
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi"
)
const BuilderId = "oapi.outscale.bsuvolume"
@ -89,22 +86,22 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
}
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
clientConfig, err := b.config.Config()
if err != nil {
return nil, err
}
// clientConfig, err := b.config.Config()
// if err != nil {
// return nil, err
// }
skipClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
// skipClient := &http.Client{
// Transport: &http.Transport{
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
// },
// }
oapiconn := oapi.NewClient(clientConfig, skipClient)
oscConn := b.config.NewOSCClient()
// Setup the state bag and initial state for the steps
state := new(multistep.BasicStateBag)
state.Put("oapi", oapiconn)
state.Put("osc", oscConn)
state.Put("hook", hook)
state.Put("ui", ui)
@ -117,7 +114,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
Debug: b.config.PackerDebug,
BsuOptimized: b.config.BsuOptimized,
EnableT2Unlimited: b.config.EnableT2Unlimited,
ExpectedRootDevice: "ebs",
ExpectedRootDevice: "bsu",
IamVmProfile: b.config.IamVmProfile,
VmInitiatedShutdownBehavior: b.config.VmInitiatedShutdownBehavior,
VmType: b.config.VmType,
@ -170,8 +167,8 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
},
&communicator.StepConnect{
Config: &b.config.RunConfig.Comm,
Host: osccommon.SSHHost(
oapiconn,
Host: osccommon.OscSSHHost(
oscConn.VmApi,
b.config.SSHInterface),
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
},
@ -198,7 +195,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
artifact := &Artifact{
Volumes: state.Get("bsuvolumes").(BsuVolumes),
BuilderIdValue: BuilderId,
Conn: oapiconn,
Conn: oscConn,
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
}
ui.Say(fmt.Sprintf("Created Volumes: %s", artifact))

View File

@ -2,13 +2,9 @@
package bsuvolume
import (
"crypto/tls"
"net/http"
"testing"
"github.com/hashicorp/packer/builder/osc/common"
builderT "github.com/hashicorp/packer/helper/builder/testing"
"github.com/outscale/osc-go/oapi"
)
func TestBuilderAcc_basic(t *testing.T) {
@ -23,22 +19,6 @@ func TestBuilderAcc_basic(t *testing.T) {
func testAccPreCheck(t *testing.T) {
}
func testOAPIConn() (*oapi.Client, error) {
access := &common.AccessConfig{RawRegion: "us-east-1"}
clientConfig, err := access.Config()
if err != nil {
return nil, err
}
skipClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
return oapi.NewClient(clientConfig, skipClient), nil
}
const testBuilderAccBasic = `
{
"builders": [
@ -46,7 +26,7 @@ const testBuilderAccBasic = `
"type": "test",
"region": "eu-west-2",
"vm_type": "t2.micro",
"source_omi": "ami-65efcc11",
"source_omi": "ami-abe953fa",
"ssh_username": "outscale",
"bsu_volumes": [
{

View File

@ -4,28 +4,30 @@ import (
"context"
"fmt"
"github.com/antihax/optional"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
type stepTagBSUVolumes struct {
VolumeMapping []BlockDevice
RawRegion string
Ctx interpolate.Context
}
func (s *stepTagBSUVolumes) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
oapiconn := state.Get("oapi").(*oapi.Client)
vm := state.Get("vm").(oapi.Vm)
oscconn := state.Get("osc").(*osc.APIClient)
vm := state.Get("vm").(osc.Vm)
ui := state.Get("ui").(packer.Ui)
volumes := make(BsuVolumes)
for _, instanceBlockDevices := range vm.BlockDeviceMappings {
for _, configVolumeMapping := range s.VolumeMapping {
if configVolumeMapping.DeviceName == instanceBlockDevices.DeviceName {
volumes[oapiconn.GetConfig().Region] = append(
volumes[oapiconn.GetConfig().Region],
volumes[s.RawRegion] = append(
volumes[s.RawRegion],
instanceBlockDevices.Bsu.VolumeId)
}
}
@ -35,14 +37,14 @@ func (s *stepTagBSUVolumes) Run(_ context.Context, state multistep.StateBag) mul
if len(s.VolumeMapping) > 0 {
ui.Say("Tagging BSU volumes...")
toTag := map[string][]oapi.ResourceTag{}
toTag := map[string][]osc.ResourceTag{}
for _, mapping := range s.VolumeMapping {
if len(mapping.Tags) == 0 {
ui.Say(fmt.Sprintf("No tags specified for volume on %s...", mapping.DeviceName))
continue
}
tags, err := mapping.Tags.OAPITags(s.Ctx, oapiconn.GetConfig().Region, state)
tags, err := mapping.Tags.OSCTags(s.Ctx, s.RawRegion, state)
if err != nil {
err := fmt.Errorf("Error tagging device %s with %s", mapping.DeviceName, err)
state.Put("error", err)
@ -59,9 +61,11 @@ func (s *stepTagBSUVolumes) Run(_ context.Context, state multistep.StateBag) mul
}
for volumeId, tags := range toTag {
_, err := oapiconn.POST_CreateTags(oapi.CreateTagsRequest{
ResourceIds: []string{volumeId},
Tags: tags,
_, _, err := oscconn.TagApi.CreateTags(context.Background(), &osc.CreateTagsOpts{
CreateTagsRequest: optional.NewInterface(osc.CreateTagsRequest{
ResourceIds: []string{volumeId},
Tags: tags,
}),
})
if err != nil {
err := fmt.Errorf("Error tagging BSU Volume %s on %s: %s", volumeId, vm.VmId, err)

View File

@ -8,9 +8,7 @@ package chroot
import (
"context"
"crypto/tls"
"errors"
"net/http"
"runtime"
"github.com/hashicorp/hcl/v2/hcldec"
@ -20,7 +18,6 @@ import (
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi"
)
// The unique ID for this builder
@ -193,19 +190,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
return nil, errors.New("The outscale-chroot builder only works on Linux environments.")
}
clientConfig, err := b.config.Config()
if err != nil {
return nil, err
}
skipClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
oapiconn := oapi.NewClient(clientConfig, skipClient)
oscConn := b.config.NewOSCClient()
wrappedCommand := func(command string) (string, error) {
ctx := b.config.ctx
ctx.Data = &wrappedCommandTemplate{Command: command}
@ -215,8 +200,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
// Setup the state bag and initial state for the steps
state := new(multistep.BasicStateBag)
state.Put("config", &b.config)
state.Put("oapi", oapiconn)
state.Put("clientConfig", clientConfig)
state.Put("osc", oscConn)
state.Put("hook", hook)
state.Put("ui", ui)
state.Put("wrappedCommand", CommandWrapper(wrappedCommand))
@ -266,7 +250,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&StepCopyFiles{},
&StepChrootProvision{},
&StepEarlyCleanup{},
&StepSnapshot{},
&StepSnapshot{
RawRegion: b.config.RawRegion,
},
&osccommon.StepDeregisterOMI{
AccessConfig: &b.config.AccessConfig,
ForceDeregister: b.config.OMIForceDeregister,
@ -276,6 +262,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
},
&StepCreateOMI{
RootVolumeSize: b.config.RootVolumeSize,
RawRegion: b.config.RawRegion,
},
&osccommon.StepUpdateOMIAttributes{
AccountIds: b.config.OMIAccountIDs,
@ -307,7 +294,6 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
artifact := &osccommon.Artifact{
Omis: state.Get("omis").(map[string]string),
BuilderIdValue: BuilderId,
Config: clientConfig,
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
}

View File

@ -6,14 +6,14 @@ import (
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
// StepCheckRootDevice makes sure the root device on the OMI is BSU-backed.
type StepCheckRootDevice struct{}
func (s *StepCheckRootDevice) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
image := state.Get("source_image").(oapi.Image)
image := state.Get("source_image").(osc.Image)
ui := state.Get("ui").(packer.Ui)
ui.Say("Checking the root device on source OMI...")

View File

@ -4,42 +4,44 @@ import (
"context"
"fmt"
"github.com/antihax/optional"
osccommon "github.com/hashicorp/packer/builder/osc/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
// StepCreateOMI creates the OMI.
type StepCreateOMI struct {
RootVolumeSize int64
RawRegion string
}
func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
oapiconn := state.Get("oapi").(*oapi.Client)
osconn := state.Get("osc").(*osc.APIClient)
snapshotId := state.Get("snapshot_id").(string)
ui := state.Get("ui").(packer.Ui)
ui.Say("Creating the OMI...")
var (
registerOpts oapi.CreateImageRequest
mappings []oapi.BlockDeviceMappingImage
image oapi.Image
registerOpts osc.CreateImageRequest
mappings []osc.BlockDeviceMappingImage
image osc.Image
rootDeviceName string
)
if config.FromScratch {
mappings = config.OMIBlockDevices.BuildOMIDevices()
mappings = config.OMIBlockDevices.BuildOscOMIDevices()
rootDeviceName = config.RootDeviceName
} else {
image = state.Get("source_image").(oapi.Image)
image = state.Get("source_image").(osc.Image)
mappings = image.BlockDeviceMappings
rootDeviceName = image.RootDeviceName
}
newMappings := make([]oapi.BlockDeviceMappingImage, len(mappings))
newMappings := make([]osc.BlockDeviceMappingImage, len(mappings))
for i, device := range mappings {
newDevice := device
@ -48,14 +50,14 @@ func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
newDevice.Bsu.VolumeSize = gibSize
if newDevice.DeviceName == rootDeviceName {
if newDevice.Bsu != (oapi.BsuToCreate{}) {
if newDevice.Bsu != (osc.BsuToCreate{}) {
newDevice.Bsu.SnapshotId = snapshotId
} else {
newDevice.Bsu = oapi.BsuToCreate{SnapshotId: snapshotId}
newDevice.Bsu = osc.BsuToCreate{SnapshotId: snapshotId}
}
if config.FromScratch || s.RootVolumeSize > newDevice.Bsu.VolumeSize {
newDevice.Bsu.VolumeSize = s.RootVolumeSize
if config.FromScratch || int32(s.RootVolumeSize) > newDevice.Bsu.VolumeSize {
newDevice.Bsu.VolumeSize = int32(s.RootVolumeSize)
}
}
@ -63,7 +65,7 @@ func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
}
if config.FromScratch {
registerOpts = oapi.CreateImageRequest{
registerOpts = osc.CreateImageRequest{
ImageName: config.OMIName,
Architecture: "x86_64",
RootDeviceName: rootDeviceName,
@ -73,23 +75,25 @@ func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
registerOpts = buildRegisterOpts(config, image, newMappings)
}
registerResp, err := oapiconn.POST_CreateImage(registerOpts)
registerResp, _, err := osconn.ImageApi.CreateImage(context.Background(), &osc.CreateImageOpts{
CreateImageRequest: optional.NewInterface(registerOpts),
})
if err != nil {
state.Put("error", fmt.Errorf("Error registering OMI: %s", err))
ui.Error(state.Get("error").(error).Error())
return multistep.ActionHalt
}
imageID := registerResp.OK.Image.ImageId
imageID := registerResp.Image.ImageId
// Set the OMI ID in the state
ui.Say(fmt.Sprintf("OMI: %s", imageID))
omis := make(map[string]string)
omis[oapiconn.GetConfig().Region] = imageID
omis[s.RawRegion] = imageID
state.Put("omis", omis)
ui.Say("Waiting for OMI to become ready...")
if err := osccommon.WaitUntilImageAvailable(oapiconn, imageID); err != nil {
if err := osccommon.WaitUntilOscImageAvailable(osconn, imageID); err != nil {
err := fmt.Errorf("Error waiting for OMI: %s", err)
state.Put("error", err)
ui.Error(err.Error())
@ -101,8 +105,8 @@ func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
func (s *StepCreateOMI) Cleanup(state multistep.StateBag) {}
func buildRegisterOpts(config *Config, image oapi.Image, mappings []oapi.BlockDeviceMappingImage) oapi.CreateImageRequest {
registerOpts := oapi.CreateImageRequest{
func buildRegisterOpts(config *Config, image osc.Image, mappings []osc.BlockDeviceMappingImage) osc.CreateImageRequest {
registerOpts := osc.CreateImageRequest{
ImageName: config.OMIName,
Architecture: image.Architecture,
RootDeviceName: image.RootDeviceName,

View File

@ -6,11 +6,12 @@ import (
"fmt"
"log"
"github.com/antihax/optional"
osccommon "github.com/hashicorp/packer/builder/osc/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
// StepCreateVolume creates a new volume from the snapshot of the root
@ -23,18 +24,19 @@ type StepCreateVolume struct {
RootVolumeSize int64
RootVolumeType string
RootVolumeTags osccommon.TagMap
RawRegion string
Ctx interpolate.Context
}
func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
oapiconn := state.Get("oapi").(*oapi.Client)
vm := state.Get("vm").(oapi.Vm)
oscconn := state.Get("osc").(*osc.APIClient)
vm := state.Get("vm").(osc.Vm)
ui := state.Get("ui").(packer.Ui)
var err error
volTags, err := s.RootVolumeTags.OAPITags(s.Ctx, oapiconn.GetConfig().Region, state)
volTags, err := s.RootVolumeTags.OSCTags(s.Ctx, s.RawRegion, state)
if err != nil {
state.Put("error", err)
@ -42,7 +44,7 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
return multistep.ActionHalt
}
var createVolume *oapi.CreateVolumeRequest
var createVolume *osc.CreateVolumeRequest
if config.FromScratch {
rootVolumeType := osccommon.VolumeTypeGp2
if s.RootVolumeType == "io1" {
@ -53,17 +55,17 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
} else if s.RootVolumeType != "" {
rootVolumeType = s.RootVolumeType
}
createVolume = &oapi.CreateVolumeRequest{
createVolume = &osc.CreateVolumeRequest{
SubregionName: vm.Placement.SubregionName,
Size: s.RootVolumeSize,
Size: int32(s.RootVolumeSize),
VolumeType: rootVolumeType,
}
} else {
// Determine the root device snapshot
image := state.Get("source_image").(oapi.Image)
image := state.Get("source_image").(osc.Image)
log.Printf("Searching for root device of the image (%s)", image.RootDeviceName)
var rootDevice *oapi.BlockDeviceMappingImage
var rootDevice *osc.BlockDeviceMappingImage
for _, device := range image.BlockDeviceMappings {
if device.DeviceName == image.RootDeviceName {
rootDevice = &device
@ -82,7 +84,9 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
log.Printf("Create args: %+v", createVolume)
createVolumeResp, err := oapiconn.POST_CreateVolume(*createVolume)
createVolumeResp, _, err := oscconn.VolumeApi.CreateVolume(context.Background(), &osc.CreateVolumeOpts{
CreateVolumeRequest: optional.NewInterface(*createVolume),
})
if err != nil {
err := fmt.Errorf("Error creating root volume: %s", err)
state.Put("error", err)
@ -91,12 +95,12 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
}
// Set the volume ID so we remember to delete it later
s.volumeId = createVolumeResp.OK.Volume.VolumeId
s.volumeId = createVolumeResp.Volume.VolumeId
log.Printf("Volume ID: %s", s.volumeId)
//Create tags for volume
if len(volTags) > 0 {
if err := osccommon.CreateTags(oapiconn, s.volumeId, ui, volTags); err != nil {
if err := osccommon.CreateOSCTags(oscconn, s.volumeId, ui, volTags); err != nil {
err := fmt.Errorf("Error creating tags for volume: %s", err)
state.Put("error", err)
ui.Error(err.Error())
@ -105,7 +109,7 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu
}
// Wait for the volume to become ready
err = osccommon.WaitUntilVolumeAvailable(oapiconn, s.volumeId)
err = osccommon.WaitUntilOscVolumeAvailable(oscconn, s.volumeId)
if err != nil {
err := fmt.Errorf("Error waiting for volume: %s", err)
state.Put("error", err)
@ -122,32 +126,34 @@ func (s *StepCreateVolume) Cleanup(state multistep.StateBag) {
return
}
oapiconn := state.Get("oapi").(*oapi.Client)
oscconn := state.Get("osc").(*osc.APIClient)
ui := state.Get("ui").(packer.Ui)
ui.Say("Deleting the created BSU volume...")
_, err := oapiconn.POST_DeleteVolume(oapi.DeleteVolumeRequest{VolumeId: s.volumeId})
_, _, err := oscconn.VolumeApi.DeleteVolume(context.Background(), &osc.DeleteVolumeOpts{
DeleteVolumeRequest: optional.NewInterface(osc.DeleteVolumeRequest{VolumeId: s.volumeId}),
})
if err != nil {
ui.Error(fmt.Sprintf("Error deleting BSU volume: %s", err))
}
}
func (s *StepCreateVolume) buildCreateVolumeInput(suregionName string, rootDevice *oapi.BlockDeviceMappingImage) (*oapi.CreateVolumeRequest, error) {
func (s *StepCreateVolume) buildCreateVolumeInput(suregionName string, rootDevice *osc.BlockDeviceMappingImage) (*osc.CreateVolumeRequest, error) {
if rootDevice == nil {
return nil, fmt.Errorf("Couldn't find root device!")
return nil, fmt.Errorf("Couldn't find root device")
}
//FIX: Temporary fix
gibSize := rootDevice.Bsu.VolumeSize / (1024 * 1024 * 1024)
createVolumeInput := &oapi.CreateVolumeRequest{
createVolumeInput := &osc.CreateVolumeRequest{
SubregionName: suregionName,
Size: gibSize,
SnapshotId: rootDevice.Bsu.SnapshotId,
VolumeType: rootDevice.Bsu.VolumeType,
Iops: rootDevice.Bsu.Iops,
}
if s.RootVolumeSize > rootDevice.Bsu.VolumeSize {
createVolumeInput.Size = s.RootVolumeSize
if int32(s.RootVolumeSize) > rootDevice.Bsu.VolumeSize {
createVolumeInput.Size = int32(s.RootVolumeSize)
}
if s.RootVolumeType == "" || s.RootVolumeType == rootDevice.Bsu.VolumeType {

View File

@ -4,10 +4,11 @@ import (
"context"
"fmt"
"github.com/antihax/optional"
osccommon "github.com/hashicorp/packer/builder/osc/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
// StepLinkVolume attaches the previously created volume to an
@ -22,9 +23,9 @@ type StepLinkVolume struct {
}
func (s *StepLinkVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
oapiconn := state.Get("oapi").(*oapi.Client)
oscconn := state.Get("osc").(*osc.APIClient)
device := state.Get("device").(string)
vm := state.Get("vm").(oapi.Vm)
vm := state.Get("vm").(osc.Vm)
ui := state.Get("ui").(packer.Ui)
volumeId := state.Get("volume_id").(string)
@ -33,10 +34,12 @@ func (s *StepLinkVolume) Run(ctx context.Context, state multistep.StateBag) mult
linkVolume := device
ui.Say(fmt.Sprintf("Attaching the root volume to %s", linkVolume))
_, err := oapiconn.POST_LinkVolume(oapi.LinkVolumeRequest{
VmId: vm.VmId,
VolumeId: volumeId,
DeviceName: linkVolume,
_, _, err := oscconn.VolumeApi.LinkVolume(context.Background(), &osc.LinkVolumeOpts{
LinkVolumeRequest: optional.NewInterface(osc.LinkVolumeRequest{
VmId: vm.VmId,
VolumeId: volumeId,
DeviceName: linkVolume,
}),
})
if err != nil {
@ -51,7 +54,7 @@ func (s *StepLinkVolume) Run(ctx context.Context, state multistep.StateBag) mult
s.volumeId = volumeId
// Wait for the volume to become attached
err = osccommon.WaitUntilVolumeIsLinked(oapiconn, s.volumeId)
err = osccommon.WaitUntilOscVolumeIsLinked(oscconn, s.volumeId)
if err != nil {
err := fmt.Errorf("Error waiting for volume: %s", err)
state.Put("error", err)
@ -75,11 +78,14 @@ func (s *StepLinkVolume) CleanupFunc(state multistep.StateBag) error {
return nil
}
oapiconn := state.Get("oapi").(*oapi.Client)
oscconn := state.Get("osc").(*osc.APIClient)
ui := state.Get("ui").(packer.Ui)
ui.Say("Detaching BSU volume...")
_, err := oapiconn.POST_UnlinkVolume(oapi.UnlinkVolumeRequest{VolumeId: s.volumeId})
_, _, err := oscconn.VolumeApi.UnlinkVolume(context.Background(), &osc.UnlinkVolumeOpts{
UnlinkVolumeRequest: optional.NewInterface(osc.UnlinkVolumeRequest{VolumeId: s.volumeId}),
})
if err != nil {
return fmt.Errorf("Error detaching BSU volume: %s", err)
}
@ -87,7 +93,7 @@ func (s *StepLinkVolume) CleanupFunc(state multistep.StateBag) error {
s.attached = false
// Wait for the volume to detach
err = osccommon.WaitUntilVolumeIsUnlinked(oapiconn, s.volumeId)
err = osccommon.WaitUntilOscVolumeIsUnlinked(oscconn, s.volumeId)
if err != nil {
return fmt.Errorf("Error waiting for volume: %s", err)
}

View File

@ -5,10 +5,11 @@ import (
"fmt"
"time"
"github.com/antihax/optional"
osccommon "github.com/hashicorp/packer/builder/osc/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
// StepSnapshot creates a snapshot of the created volume.
@ -17,19 +18,22 @@ import (
// snapshot_id string - ID of the created snapshot
type StepSnapshot struct {
snapshotId string
RawRegion string
}
func (s *StepSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
oapiconn := state.Get("oapi").(*oapi.Client)
oscconn := state.Get("osc").(*osc.APIClient)
ui := state.Get("ui").(packer.Ui)
volumeId := state.Get("volume_id").(string)
ui.Say("Creating snapshot...")
description := fmt.Sprintf("Packer: %s", time.Now().String())
createSnapResp, err := oapiconn.POST_CreateSnapshot(oapi.CreateSnapshotRequest{
VolumeId: volumeId,
Description: description,
createSnapResp, _, err := oscconn.SnapshotApi.CreateSnapshot(context.Background(), &osc.CreateSnapshotOpts{
CreateSnapshotRequest: optional.NewInterface(osc.CreateSnapshotRequest{
VolumeId: volumeId,
Description: description,
}),
})
if err != nil {
err := fmt.Errorf("Error creating snapshot: %s", err)
@ -39,11 +43,11 @@ func (s *StepSnapshot) Run(ctx context.Context, state multistep.StateBag) multis
}
// Set the snapshot ID so we can delete it later
s.snapshotId = createSnapResp.OK.Snapshot.SnapshotId
s.snapshotId = createSnapResp.Snapshot.SnapshotId
ui.Message(fmt.Sprintf("Snapshot ID: %s", s.snapshotId))
// Wait for the snapshot to be ready
err = osccommon.WaitUntilSnapshotDone(oapiconn, s.snapshotId)
err = osccommon.WaitUntilOscSnapshotDone(oscconn, s.snapshotId)
if err != nil {
err := fmt.Errorf("Error waiting for snapshot: %s", err)
state.Put("error", err)
@ -54,7 +58,7 @@ func (s *StepSnapshot) Run(ctx context.Context, state multistep.StateBag) multis
state.Put("snapshot_id", s.snapshotId)
snapshots := map[string][]string{
oapiconn.GetConfig().Region: {s.snapshotId},
s.RawRegion: {s.snapshotId},
}
state.Put("snapshots", snapshots)
@ -70,10 +74,12 @@ func (s *StepSnapshot) Cleanup(state multistep.StateBag) {
_, halted := state.GetOk(multistep.StateHalted)
if cancelled || halted {
oapiconn := state.Get("oapi").(*oapi.Client)
oscconn := state.Get("osc").(*osc.APIClient)
ui := state.Get("ui").(packer.Ui)
ui.Say("Removing snapshot since we cancelled or halted...")
_, err := oapiconn.POST_DeleteSnapshot(oapi.DeleteSnapshotRequest{SnapshotId: s.snapshotId})
_, _, err := oscconn.SnapshotApi.DeleteSnapshot(context.Background(), &osc.DeleteSnapshotOpts{
DeleteSnapshotRequest: optional.NewInterface(osc.DeleteSnapshotRequest{SnapshotId: s.snapshotId}),
})
if err != nil {
ui.Error(fmt.Sprintf("Error: %s", err))
}

View File

@ -5,16 +5,17 @@ import (
"fmt"
"log"
"github.com/antihax/optional"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
// 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 {
oapiconn := state.Get("oapi").(*oapi.Client)
oscconn := state.Get("osc").(*osc.APIClient)
//session := state.Get("clientConfig").(*session.Session)
ui := state.Get("ui").(packer.Ui)
@ -36,9 +37,11 @@ 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 := oapiconn.POST_ReadVms(oapi.ReadVmsRequest{Filters: oapi.FiltersVm{
VmIds: []string{string(vmID)},
}})
resp, _, err := oscconn.VmApi.ReadVms(context.Background(), &osc.ReadVmsOpts{
ReadVmsRequest: optional.NewInterface(osc.ReadVmsRequest{Filters: osc.FiltersVm{
VmIds: []string{string(vmID)},
}}),
})
if err != nil {
err := fmt.Errorf("Error getting vm data: %s", err)
state.Put("error", err)
@ -46,10 +49,10 @@ func (s *StepVmInfo) Run(_ context.Context, state multistep.StateBag) multistep.
return multistep.ActionHalt
}
vmsResp := resp.OK
vmsResp := resp
if len(vmsResp.Vms) == 0 {
err := fmt.Errorf("Error getting vm data: no vm found.")
err := fmt.Errorf("Error getting vm data: no vm found")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt

View File

@ -8,7 +8,6 @@ import (
"os"
"github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
@ -24,49 +23,6 @@ type AccessConfig struct {
SkipValidation bool `mapstructure:"skip_region_validation"`
SkipMetadataApiCheck bool `mapstructure:"skip_metadata_api_check"`
Token string `mapstructure:"token"`
clientConfig *oapi.Config
getOAPIConnection func() oapi.OAPIClient
}
// Config returns a valid oapi.Config object for access to Outscale services, or
// an error if the authentication and region couldn't be resolved
func (c *AccessConfig) Config() (*oapi.Config, error) {
if c.clientConfig != nil {
return c.clientConfig, nil
}
//Check env variables if access configuration is not set.
if c.AccessKey == "" {
c.AccessKey = os.Getenv("OUTSCALE_ACCESSKEYID")
}
if c.SecretKey == "" {
c.SecretKey = os.Getenv("OUTSCALE_SECRETKEYID")
}
if c.RawRegion == "" {
c.RawRegion = os.Getenv("OUTSCALE_REGION")
}
if c.CustomEndpointOAPI == "" {
c.CustomEndpointOAPI = os.Getenv("OUTSCALE_OAPI_URL")
}
if c.CustomEndpointOAPI == "" {
c.CustomEndpointOAPI = "outscale.com/oapi/latest"
}
config := &oapi.Config{
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
Region: c.RawRegion,
URL: c.CustomEndpointOAPI,
Service: "api",
}
return config, nil
}
// NewOSCClient retrieves the Outscale OSC-SDK client
@ -115,29 +71,10 @@ func (c *AccessConfig) NewOSCClientByRegion(region string) *osc.APIClient {
DefaultHeader: make(map[string]string),
UserAgent: "packer-osc",
HTTPClient: skipClient,
Debug: true,
})
}
func (c *AccessConfig) NewOAPIConnection() (oapi.OAPIClient, error) {
if c.getOAPIConnection != nil {
return c.getOAPIConnection(), nil
}
oapicfg, err := c.Config()
if err != nil {
return nil, err
}
skipClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.InsecureSkipTLSVerify},
},
}
oapiClient := oapi.NewClient(oapicfg, skipClient)
return oapiClient, nil
}
func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
var errs []error

View File

@ -2,31 +2,10 @@ package common
import (
"testing"
"github.com/outscale/osc-go/oapi"
)
type mockOAPIClient struct {
oapi.OAPIClient
}
func testAccessConfig() *AccessConfig {
return &AccessConfig{
getOAPIConnection: func() oapi.OAPIClient {
return &mockOAPIClient{}
},
}
}
func (m *mockOAPIClient) POST_ReadRegions(oapi.ReadRegionsRequest) (*oapi.POST_ReadRegionsResponses, error) {
return &oapi.POST_ReadRegionsResponses{
OK: &oapi.ReadRegionsResponse{
Regions: []oapi.Region{
{RegionEndpoint: "us-west1", RegionName: "us-west1"},
{RegionEndpoint: "us-east-1", RegionName: "us-east-1"},
},
},
}, nil
return &AccessConfig{}
}
func TestAccessConfigPrepare_Region(t *testing.T) {

View File

@ -1,15 +1,15 @@
package common
import (
"crypto/tls"
"context"
"fmt"
"log"
"net/http"
"sort"
"strings"
"github.com/antihax/optional"
"github.com/hashicorp/packer/packer"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
// Artifact is an artifact implementation that contains built OMIs.
@ -20,9 +20,6 @@ type Artifact struct {
// BuilderId is the unique ID for the builder that created this OMI
BuilderIdValue string
// OAPI connection for performing API stuff.
Config *oapi.Config
// StateData should store data such as GeneratedData
// to be shared with post-processors
StateData map[string]interface{}
@ -74,47 +71,36 @@ func (a *Artifact) State(name string) interface{} {
func (a *Artifact) Destroy() error {
errors := make([]error, 0)
config := a.State("accessConfig").(*AccessConfig)
for region, imageId := range a.Omis {
log.Printf("Deregistering image ID (%s) from region (%s)", imageId, region)
newConfig := &oapi.Config{
UserAgent: a.Config.UserAgent,
AccessKey: a.Config.AccessKey,
SecretKey: a.Config.SecretKey,
Service: a.Config.Service,
Region: region, //New region
URL: a.Config.URL,
}
log.Printf("[DEBUG] New Client config %+v", newConfig)
skipClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
regionConn := oapi.NewClient(newConfig, skipClient)
regionConn := config.NewOSCClientByRegion(region)
// Get image metadata
imageResp, err := regionConn.POST_ReadImages(oapi.ReadImagesRequest{
Filters: oapi.FiltersImage{
ImageIds: []string{imageId},
},
imageResp, _, err := regionConn.ImageApi.ReadImages(context.Background(), &osc.ReadImagesOpts{
ReadImagesRequest: optional.NewInterface(osc.ReadImagesRequest{
Filters: osc.FiltersImage{
ImageIds: []string{imageId},
},
}),
})
if err != nil {
errors = append(errors, err)
}
if len(imageResp.OK.Images) == 0 {
if len(imageResp.Images) == 0 {
err := fmt.Errorf("Error retrieving details for OMI (%s), no images found", imageId)
errors = append(errors, err)
}
// Deregister ami
input := oapi.DeleteImageRequest{
input := osc.DeleteImageRequest{
ImageId: imageId,
}
if _, err := regionConn.POST_DeleteImage(input); err != nil {
if _, _, err := regionConn.ImageApi.DeleteImage(context.Background(), &osc.DeleteImageOpts{
DeleteImageRequest: optional.NewInterface(input),
}); err != nil {
errors = append(errors, err)
}

View File

@ -5,9 +5,7 @@ import (
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
@ -36,48 +34,6 @@ type LaunchBlockDevices struct {
LaunchMappings []BlockDevice `mapstructure:"launch_block_device_mappings"`
}
func buildBlockDevicesImage(b []BlockDevice) []oapi.BlockDeviceMappingImage {
var blockDevices []oapi.BlockDeviceMappingImage
for _, blockDevice := range b {
mapping := oapi.BlockDeviceMappingImage{
DeviceName: blockDevice.DeviceName,
}
if blockDevice.VirtualName != "" {
if strings.HasPrefix(blockDevice.VirtualName, "ephemeral") {
mapping.VirtualDeviceName = blockDevice.VirtualName
}
} else {
bsu := oapi.BsuToCreate{
DeleteOnVmDeletion: aws.Bool(blockDevice.DeleteOnVmDeletion),
}
if blockDevice.VolumeType != "" {
bsu.VolumeType = blockDevice.VolumeType
}
if blockDevice.VolumeSize > 0 {
bsu.VolumeSize = blockDevice.VolumeSize
}
// IOPS is only valid for io1 type
if blockDevice.VolumeType == "io1" {
bsu.Iops = blockDevice.IOPS
}
if blockDevice.SnapshotId != "" {
bsu.SnapshotId = blockDevice.SnapshotId
}
mapping.Bsu = bsu
}
blockDevices = append(blockDevices, mapping)
}
return blockDevices
}
func buildOscBlockDevicesImage(b []BlockDevice) []osc.BlockDeviceMappingImage {
var blockDevices []osc.BlockDeviceMappingImage
@ -120,52 +76,6 @@ func buildOscBlockDevicesImage(b []BlockDevice) []osc.BlockDeviceMappingImage {
return blockDevices
}
func buildBlockDevicesVmCreation(b []BlockDevice) []oapi.BlockDeviceMappingVmCreation {
log.Printf("[DEBUG] Launch Block Device %#v", b)
var blockDevices []oapi.BlockDeviceMappingVmCreation
for _, blockDevice := range b {
mapping := oapi.BlockDeviceMappingVmCreation{
DeviceName: blockDevice.DeviceName,
}
if blockDevice.NoDevice {
mapping.NoDevice = ""
} else if blockDevice.VirtualName != "" {
if strings.HasPrefix(blockDevice.VirtualName, "ephemeral") {
mapping.VirtualDeviceName = blockDevice.VirtualName
}
} else {
bsu := oapi.BsuToCreate{
DeleteOnVmDeletion: aws.Bool(blockDevice.DeleteOnVmDeletion),
}
if blockDevice.VolumeType != "" {
bsu.VolumeType = blockDevice.VolumeType
}
if blockDevice.VolumeSize > 0 {
bsu.VolumeSize = blockDevice.VolumeSize
}
// IOPS is only valid for io1 type
if blockDevice.VolumeType == "io1" {
bsu.Iops = blockDevice.IOPS
}
if blockDevice.SnapshotId != "" {
bsu.SnapshotId = blockDevice.SnapshotId
}
mapping.Bsu = bsu
}
blockDevices = append(blockDevices, mapping)
}
return blockDevices
}
func buildOscBlockDevicesVmCreation(b []BlockDevice) []osc.BlockDeviceMappingVmCreation {
log.Printf("[DEBUG] Launch Block Device %#v", b)
@ -234,18 +144,10 @@ func (b *BlockDevices) Prepare(ctx *interpolate.Context) (errs []error) {
return errs
}
func (b *OMIBlockDevices) BuildOMIDevices() []oapi.BlockDeviceMappingImage {
return buildBlockDevicesImage(b.OMIMappings)
}
func (b *OMIBlockDevices) BuildOscOMIDevices() []osc.BlockDeviceMappingImage {
return buildOscBlockDevicesImage(b.OMIMappings)
}
func (b *LaunchBlockDevices) BuildLaunchDevices() []oapi.BlockDeviceMappingVmCreation {
return buildBlockDevicesVmCreation(b.LaunchMappings)
}
func (b *LaunchBlockDevices) BuildOSCLaunchDevices() []osc.BlockDeviceMappingVmCreation {
return buildOscBlockDevicesVmCreation(b.LaunchMappings)
}

View File

@ -4,19 +4,13 @@ import (
"reflect"
"testing"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
func TestBlockDevice_LaunchDevices(t *testing.T) {
tr := new(bool)
f := new(bool)
*tr = true
*f = false
cases := []struct {
Config *BlockDevice
Result oapi.BlockDeviceMappingVmCreation
Result osc.BlockDeviceMappingVmCreation
}{
{
Config: &BlockDevice{
@ -27,13 +21,13 @@ func TestBlockDevice_LaunchDevices(t *testing.T) {
DeleteOnVmDeletion: true,
},
Result: oapi.BlockDeviceMappingVmCreation{
Result: osc.BlockDeviceMappingVmCreation{
DeviceName: "/dev/sdb",
Bsu: oapi.BsuToCreate{
Bsu: osc.BsuToCreate{
SnapshotId: "snap-1234",
VolumeType: "standard",
VolumeSize: 8,
DeleteOnVmDeletion: tr,
DeleteOnVmDeletion: true,
},
},
},
@ -43,11 +37,11 @@ func TestBlockDevice_LaunchDevices(t *testing.T) {
VolumeSize: 8,
},
Result: oapi.BlockDeviceMappingVmCreation{
Result: osc.BlockDeviceMappingVmCreation{
DeviceName: "/dev/sdb",
Bsu: oapi.BsuToCreate{
Bsu: osc.BsuToCreate{
VolumeSize: 8,
DeleteOnVmDeletion: f,
DeleteOnVmDeletion: false,
},
},
},
@ -60,12 +54,12 @@ func TestBlockDevice_LaunchDevices(t *testing.T) {
IOPS: 1000,
},
Result: oapi.BlockDeviceMappingVmCreation{
Result: osc.BlockDeviceMappingVmCreation{
DeviceName: "/dev/sdb",
Bsu: oapi.BsuToCreate{
Bsu: osc.BsuToCreate{
VolumeType: "io1",
VolumeSize: 8,
DeleteOnVmDeletion: tr,
DeleteOnVmDeletion: true,
Iops: 1000,
},
},
@ -78,12 +72,12 @@ func TestBlockDevice_LaunchDevices(t *testing.T) {
DeleteOnVmDeletion: true,
},
Result: oapi.BlockDeviceMappingVmCreation{
Result: osc.BlockDeviceMappingVmCreation{
DeviceName: "/dev/sdb",
Bsu: oapi.BsuToCreate{
Bsu: osc.BsuToCreate{
VolumeType: "gp2",
VolumeSize: 8,
DeleteOnVmDeletion: tr,
DeleteOnVmDeletion: true,
},
},
},
@ -95,12 +89,12 @@ func TestBlockDevice_LaunchDevices(t *testing.T) {
DeleteOnVmDeletion: true,
},
Result: oapi.BlockDeviceMappingVmCreation{
Result: osc.BlockDeviceMappingVmCreation{
DeviceName: "/dev/sdb",
Bsu: oapi.BsuToCreate{
Bsu: osc.BsuToCreate{
VolumeType: "gp2",
VolumeSize: 8,
DeleteOnVmDeletion: tr,
DeleteOnVmDeletion: true,
},
},
},
@ -111,11 +105,11 @@ func TestBlockDevice_LaunchDevices(t *testing.T) {
DeleteOnVmDeletion: true,
},
Result: oapi.BlockDeviceMappingVmCreation{
Result: osc.BlockDeviceMappingVmCreation{
DeviceName: "/dev/sdb",
Bsu: oapi.BsuToCreate{
Bsu: osc.BsuToCreate{
VolumeType: "standard",
DeleteOnVmDeletion: tr,
DeleteOnVmDeletion: true,
},
},
},
@ -125,7 +119,7 @@ func TestBlockDevice_LaunchDevices(t *testing.T) {
VirtualName: "ephemeral0",
},
Result: oapi.BlockDeviceMappingVmCreation{
Result: osc.BlockDeviceMappingVmCreation{
DeviceName: "/dev/sdb",
VirtualDeviceName: "ephemeral0",
},
@ -136,7 +130,7 @@ func TestBlockDevice_LaunchDevices(t *testing.T) {
NoDevice: true,
},
Result: oapi.BlockDeviceMappingVmCreation{
Result: osc.BlockDeviceMappingVmCreation{
DeviceName: "/dev/sdb",
NoDevice: "",
},
@ -149,9 +143,9 @@ func TestBlockDevice_LaunchDevices(t *testing.T) {
LaunchMappings: []BlockDevice{*tc.Config},
}
expected := []oapi.BlockDeviceMappingVmCreation{tc.Result}
expected := []osc.BlockDeviceMappingVmCreation{tc.Result}
launchResults := launchBlockDevices.BuildLaunchDevices()
launchResults := launchBlockDevices.BuildOSCLaunchDevices()
if !reflect.DeepEqual(expected, launchResults) {
t.Fatalf("Bad block device, \nexpected: %#v\n\ngot: %#v",
expected, launchResults)
@ -160,15 +154,9 @@ func TestBlockDevice_LaunchDevices(t *testing.T) {
}
func TestBlockDevice_OMI(t *testing.T) {
tr := new(bool)
f := new(bool)
*tr = true
*f = false
cases := []struct {
Config *BlockDevice
Result oapi.BlockDeviceMappingImage
Result osc.BlockDeviceMappingImage
}{
{
Config: &BlockDevice{
@ -179,27 +167,28 @@ func TestBlockDevice_OMI(t *testing.T) {
DeleteOnVmDeletion: true,
},
Result: oapi.BlockDeviceMappingImage{
Result: osc.BlockDeviceMappingImage{
DeviceName: "/dev/sdb",
Bsu: oapi.BsuToCreate{
Bsu: osc.BsuToCreate{
SnapshotId: "snap-1234",
VolumeType: "standard",
VolumeSize: 8,
DeleteOnVmDeletion: tr,
DeleteOnVmDeletion: true,
},
},
},
{
Config: &BlockDevice{
DeviceName: "/dev/sdb",
VolumeSize: 8,
DeviceName: "/dev/sdb",
VolumeSize: 8,
DeleteOnVmDeletion: true,
},
Result: oapi.BlockDeviceMappingImage{
Result: osc.BlockDeviceMappingImage{
DeviceName: "/dev/sdb",
Bsu: oapi.BsuToCreate{
Bsu: osc.BsuToCreate{
VolumeSize: 8,
DeleteOnVmDeletion: f,
DeleteOnVmDeletion: true,
},
},
},
@ -212,12 +201,12 @@ func TestBlockDevice_OMI(t *testing.T) {
IOPS: 1000,
},
Result: oapi.BlockDeviceMappingImage{
Result: osc.BlockDeviceMappingImage{
DeviceName: "/dev/sdb",
Bsu: oapi.BsuToCreate{
Bsu: osc.BsuToCreate{
VolumeType: "io1",
VolumeSize: 8,
DeleteOnVmDeletion: tr,
DeleteOnVmDeletion: true,
Iops: 1000,
},
},
@ -230,12 +219,12 @@ func TestBlockDevice_OMI(t *testing.T) {
DeleteOnVmDeletion: true,
},
Result: oapi.BlockDeviceMappingImage{
Result: osc.BlockDeviceMappingImage{
DeviceName: "/dev/sdb",
Bsu: oapi.BsuToCreate{
Bsu: osc.BsuToCreate{
VolumeType: "gp2",
VolumeSize: 8,
DeleteOnVmDeletion: tr,
DeleteOnVmDeletion: true,
},
},
},
@ -247,12 +236,12 @@ func TestBlockDevice_OMI(t *testing.T) {
DeleteOnVmDeletion: true,
},
Result: oapi.BlockDeviceMappingImage{
Result: osc.BlockDeviceMappingImage{
DeviceName: "/dev/sdb",
Bsu: oapi.BsuToCreate{
Bsu: osc.BsuToCreate{
VolumeType: "gp2",
VolumeSize: 8,
DeleteOnVmDeletion: tr,
DeleteOnVmDeletion: true,
},
},
},
@ -263,11 +252,11 @@ func TestBlockDevice_OMI(t *testing.T) {
DeleteOnVmDeletion: true,
},
Result: oapi.BlockDeviceMappingImage{
Result: osc.BlockDeviceMappingImage{
DeviceName: "/dev/sdb",
Bsu: oapi.BsuToCreate{
Bsu: osc.BsuToCreate{
VolumeType: "standard",
DeleteOnVmDeletion: tr,
DeleteOnVmDeletion: true,
},
},
},
@ -277,24 +266,24 @@ func TestBlockDevice_OMI(t *testing.T) {
VirtualName: "ephemeral0",
},
Result: oapi.BlockDeviceMappingImage{
Result: osc.BlockDeviceMappingImage{
DeviceName: "/dev/sdb",
VirtualDeviceName: "ephemeral0",
},
},
}
for _, tc := range cases {
for i, tc := range cases {
omiBlockDevices := OMIBlockDevices{
OMIMappings: []BlockDevice{*tc.Config},
}
expected := []oapi.BlockDeviceMappingImage{tc.Result}
expected := []osc.BlockDeviceMappingImage{tc.Result}
omiResults := omiBlockDevices.BuildOMIDevices()
omiResults := omiBlockDevices.BuildOscOMIDevices()
if !reflect.DeepEqual(expected, omiResults) {
t.Fatalf("Bad block device, \nexpected: %+#v\n\ngot: %+#v",
expected, omiResults)
t.Fatalf("%d - Bad block device, \nexpected: %+#v\n\ngot: %+#v",
i, expected, omiResults)
}
}
}

View File

@ -4,36 +4,9 @@ import (
"log"
"strconv"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
func buildNetFilters(input map[string]string) oapi.FiltersNet {
var filters oapi.FiltersNet
for k, v := range input {
filterValue := []string{v}
switch name := k; name {
case "ip-range":
filters.IpRanges = filterValue
case "dhcp-options-set-id":
filters.DhcpOptionsSetIds = filterValue
case "is-default":
if isDefault, err := strconv.ParseBool(v); err == nil {
filters.IsDefault = isDefault
}
case "state":
filters.States = filterValue
case "tag-key":
filters.TagKeys = filterValue
case "tag-value":
filters.TagValues = filterValue
default:
log.Printf("[Debug] Unknown Filter Name: %s.", name)
}
}
return filters
}
func buildOscNetFilters(input map[string]string) osc.FiltersNet {
var filters osc.FiltersNet
for k, v := range input {
@ -60,32 +33,6 @@ func buildOscNetFilters(input map[string]string) osc.FiltersNet {
return filters
}
func buildSubnetFilters(input map[string]string) oapi.FiltersSubnet {
var filters oapi.FiltersSubnet
for k, v := range input {
filterValue := []string{v}
switch name := k; name {
case "available-ips-counts":
if ipCount, err := strconv.Atoi(v); err == nil {
filters.AvailableIpsCounts = []int64{int64(ipCount)}
}
case "ip-ranges":
filters.IpRanges = filterValue
case "net-ids":
filters.NetIds = filterValue
case "states":
filters.States = filterValue
case "subnet-ids":
filters.SubnetIds = filterValue
case "sub-region-names":
filters.SubregionNames = filterValue
default:
log.Printf("[Debug] Unknown Filter Name: %s.", name)
}
}
return filters
}
func buildOscSubnetFilters(input map[string]string) osc.FiltersSubnet {
var filters osc.FiltersSubnet
for k, v := range input {
@ -112,38 +59,6 @@ func buildOscSubnetFilters(input map[string]string) osc.FiltersSubnet {
return filters
}
func buildOMIFilters(input map[string]string) oapi.FiltersImage {
var filters oapi.FiltersImage
for k, v := range input {
filterValue := []string{v}
switch name := k; name {
case "account-alias":
filters.AccountAliases = filterValue
case "account-id":
filters.AccountIds = filterValue
case "architecture":
filters.Architectures = filterValue
case "image-id":
filters.ImageIds = filterValue
case "image-name":
filters.ImageNames = filterValue
case "image-type":
filters.ImageTypes = filterValue
case "virtualization-type":
filters.VirtualizationTypes = filterValue
case "root-device-type":
filters.RootDeviceTypes = filterValue
case "block-device-mapping-volume-type":
filters.BlockDeviceMappingVolumeType = filterValue
//Some params are missing.
default:
log.Printf("[WARN] Unknown Filter Name: %s.", name)
}
}
return filters
}
func buildOSCOMIFilters(input map[string]string) osc.FiltersImage {
var filters osc.FiltersImage
for k, v := range input {

View File

@ -5,14 +5,14 @@ import (
"testing"
"github.com/hashicorp/packer/helper/multistep"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
func testImage() oapi.Image {
return oapi.Image{
func testImage() osc.Image {
return osc.Image{
ImageId: "ami-abcd1234",
ImageName: "ami_test_name",
Tags: []oapi.ResourceTag{
Tags: []osc.ResourceTag{
{
Key: "key-1",
Value: "value-1",

View File

@ -36,17 +36,13 @@ func TestOMIConfigPrepare_regions(t *testing.T) {
c.OMIRegions = nil
var errs []error
var err error
accessConf := testAccessConfig()
mockConn := &mockOAPIClient{}
if errs = c.prepareRegions(accessConf); len(errs) > 0 {
t.Fatalf("shouldn't have err: %#v", errs)
}
c.OMIRegions, err = listOAPIRegions(mockConn)
if err != nil {
t.Fatalf("shouldn't have err: %s", err.Error())
}
c.OMIRegions = []string{"us-east-1", "us-west-1"}
if errs = c.prepareRegions(accessConf); len(errs) > 0 {
t.Fatalf("shouldn't have err: %#v", errs)
}

View File

@ -1,19 +1,23 @@
package common
import (
"context"
"fmt"
"github.com/outscale/osc-go/oapi"
"github.com/antihax/optional"
"github.com/outscale/osc-sdk-go/osc"
)
func listOAPIRegions(oapiconn oapi.OAPIClient) ([]string, error) {
func listOSCRegions(oscconn *osc.RegionApiService) ([]string, error) {
var regions []string
resp, err := oapiconn.POST_ReadRegions(oapi.ReadRegionsRequest{})
if resp.OK == nil || err != nil {
resp, _, err := oscconn.ReadRegions(context.Background(), &osc.ReadRegionsOpts{
ReadRegionsRequest: optional.NewInterface(osc.ReadRegionsRequest{}),
})
if err != nil {
return []string{}, err
}
resultRegions := resp.OK
resultRegions := resp
for _, region := range resultRegions.Regions {
regions = append(regions, region.RegionName)
@ -25,12 +29,9 @@ func listOAPIRegions(oapiconn oapi.OAPIClient) ([]string, error) {
// ValidateRegion returns true if the supplied region is a valid Outscale
// region and false if it's not.
func (c *AccessConfig) ValidateRegion(regions ...string) error {
oapiconn, err := c.NewOAPIConnection()
if err != nil {
return err
}
oscconn := c.NewOSCClient()
validRegions, err := listOAPIRegions(oapiconn)
validRegions, err := listOSCRegions(oscconn.RegionApi)
if err != nil {
return err
}

View File

@ -10,14 +10,9 @@ import (
"github.com/antihax/optional"
"github.com/hashicorp/packer/helper/multistep"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
type oapiDescriber interface {
POST_ReadVms(oapi.ReadVmsRequest) (*oapi.POST_ReadVmsResponses, error)
}
type oscDescriber interface {
ReadVms(ctx context.Context, localVarOptionals *osc.ReadVmsOpts) (osc.ReadVmsResponse, *http.Response, error)
}
@ -29,7 +24,7 @@ var (
// SSHHost returns a function that can be given to the SSH communicator
// for determining the SSH address based on the vm DNS name.
func SSHHost(e oapiDescriber, sshInterface string) func(multistep.StateBag) (string, error) {
func SSHHost(e oscDescriber, sshInterface string) func(multistep.StateBag) (string, error) {
return func(state multistep.StateBag) (string, error) {
const tries = 2
// <= with current structure to check result of describing `tries` times
@ -72,20 +67,22 @@ func SSHHost(e oapiDescriber, sshInterface string) func(multistep.StateBag) (str
return host, nil
}
r, err := e.POST_ReadVms(oapi.ReadVmsRequest{
Filters: oapi.FiltersVm{
VmIds: []string{i.VmId},
},
r, _, err := e.ReadVms(context.Background(), &osc.ReadVmsOpts{
ReadVmsRequest: optional.NewInterface(osc.ReadVmsRequest{
Filters: osc.FiltersVm{
VmIds: []string{i.VmId},
},
}),
})
if err != nil {
return "", err
}
if len(r.OK.Vms) == 0 {
if len(r.Vms) == 0 {
return "", fmt.Errorf("vm not found: %s", i.VmId)
}
state.Put("vm", r.OK.Vms[0])
state.Put("vm", r.Vms[0])
time.Sleep(sshHostSleepDuration)
}

View File

@ -7,26 +7,11 @@ import (
"github.com/antihax/optional"
"github.com/hashicorp/packer/common"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
type stateRefreshFunc func() (string, error)
func waitForSecurityGroup(conn *oapi.Client, securityGroupID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "exists", securityGroupWaitFunc(conn, securityGroupID))
err := <-errCh
return err
}
func waitUntilForVmRunning(conn *oapi.Client, vmID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "running", waitUntilVmStateFunc(conn, vmID))
err := <-errCh
return err
}
func waitUntilForOscVmRunning(conn *osc.APIClient, vmID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "running", waitUntilOscVmStateFunc(conn, vmID))
@ -34,75 +19,51 @@ func waitUntilForOscVmRunning(conn *osc.APIClient, vmID string) error {
return err
}
func waitUntilVmDeleted(conn *oapi.Client, vmID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "terminated", waitUntilVmStateFunc(conn, vmID))
return <-errCh
}
func waitUntilOscVmDeleted(conn *osc.APIClient, vmID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "terminated", waitUntilOscVmStateFunc(conn, vmID))
return <-errCh
}
func waitUntilVmStopped(conn *oapi.Client, vmID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "stopped", waitUntilVmStateFunc(conn, vmID))
return <-errCh
}
func waitUntilOscVmStopped(conn *osc.APIClient, vmID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "stopped", waitUntilOscVmStateFunc(conn, vmID))
return <-errCh
}
func WaitUntilSnapshotCompleted(conn *oapi.Client, id string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "completed", waitUntilSnapshotStateFunc(conn, id))
return <-errCh
}
func WaitUntilOscSnapshotCompleted(conn *osc.APIClient, id string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "completed", waitUntilOscSnapshotStateFunc(conn, id))
return <-errCh
}
func WaitUntilImageAvailable(conn *oapi.Client, imageID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "available", waitUntilImageStateFunc(conn, imageID))
return <-errCh
}
func WaitUntilOscImageAvailable(conn *osc.APIClient, imageID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "available", waitUntilOscImageStateFunc(conn, imageID))
return <-errCh
}
func WaitUntilVolumeAvailable(conn *oapi.Client, volumeID string) error {
func WaitUntilOscVolumeAvailable(conn *osc.APIClient, volumeID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "available", volumeWaitFunc(conn, volumeID))
go waitForState(errCh, "available", volumeOscWaitFunc(conn, volumeID))
return <-errCh
}
func WaitUntilVolumeIsLinked(conn *oapi.Client, volumeID string) error {
func WaitUntilOscVolumeIsLinked(conn *osc.APIClient, volumeID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "attached", waitUntilVolumeLinkedStateFunc(conn, volumeID))
go waitForState(errCh, "attached", waitUntilOscVolumeLinkedStateFunc(conn, volumeID))
return <-errCh
}
func WaitUntilVolumeIsUnlinked(conn *oapi.Client, volumeID string) error {
func WaitUntilOscVolumeIsUnlinked(conn *osc.APIClient, volumeID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "dettached", waitUntilVolumeUnLinkedStateFunc(conn, volumeID))
go waitForState(errCh, "dettached", waitUntilOscVolumeUnLinkedStateFunc(conn, volumeID))
return <-errCh
}
func WaitUntilSnapshotDone(conn *oapi.Client, snapshotID string) error {
func WaitUntilOscSnapshotDone(conn *osc.APIClient, snapshotID string) error {
errCh := make(chan error, 1)
go waitForState(errCh, "completed", waitUntilSnapshotDoneStateFunc(conn, snapshotID))
go waitForState(errCh, "completed", waitUntilOscSnapshotDoneStateFunc(conn, snapshotID))
return <-errCh
}
@ -120,33 +81,6 @@ func waitForState(errCh chan<- error, target string, refresh stateRefreshFunc) e
return err
}
func waitUntilVmStateFunc(conn *oapi.Client, id string) stateRefreshFunc {
return func() (string, error) {
log.Printf("[Debug] Retrieving state for VM with id %s", id)
resp, err := conn.POST_ReadVms(oapi.ReadVmsRequest{
Filters: oapi.FiltersVm{
VmIds: []string{id},
},
})
log.Printf("[Debug] Read Response %+v", resp.OK)
if err != nil {
return "", err
}
if resp.OK == nil {
return "", fmt.Errorf("Vm with ID %s not Found", id)
}
if len(resp.OK.Vms) == 0 {
return "pending", nil
}
return resp.OK.Vms[0].State, nil
}
}
func waitUntilOscVmStateFunc(conn *osc.APIClient, id string) stateRefreshFunc {
return func() (string, error) {
log.Printf("[Debug] Retrieving state for VM with id %s", id)
@ -158,8 +92,6 @@ func waitUntilOscVmStateFunc(conn *osc.APIClient, id string) stateRefreshFunc {
}),
})
log.Printf("[Debug] Read Response %+v", resp)
if err != nil {
return "", err
}
@ -177,61 +109,53 @@ func waitUntilOscVmStateFunc(conn *osc.APIClient, id string) stateRefreshFunc {
}
}
func waitUntilVolumeLinkedStateFunc(conn *oapi.Client, id string) stateRefreshFunc {
func waitUntilOscVolumeLinkedStateFunc(conn *osc.APIClient, id string) stateRefreshFunc {
return func() (string, error) {
log.Printf("[Debug] Check if volume with id %s exists", id)
resp, err := conn.POST_ReadVolumes(oapi.ReadVolumesRequest{
Filters: oapi.FiltersVolume{
VolumeIds: []string{id},
},
resp, _, err := conn.VolumeApi.ReadVolumes(context.Background(), &osc.ReadVolumesOpts{
ReadVolumesRequest: optional.NewInterface(osc.ReadVolumesRequest{
Filters: osc.FiltersVolume{
VolumeIds: []string{id},
},
}),
})
if err != nil {
return "", err
}
if resp.OK == nil {
return "", fmt.Errorf("Vm with ID %s. Not Found", id)
}
log.Printf("[Debug] Read Response %+v", resp.OK)
if len(resp.OK.Volumes) == 0 {
if len(resp.Volumes) == 0 {
return "pending", nil
}
if len(resp.OK.Volumes[0].LinkedVolumes) == 0 {
if len(resp.Volumes[0].LinkedVolumes) == 0 {
return "pending", nil
}
return resp.OK.Volumes[0].LinkedVolumes[0].State, nil
return resp.Volumes[0].LinkedVolumes[0].State, nil
}
}
func waitUntilVolumeUnLinkedStateFunc(conn *oapi.Client, id string) stateRefreshFunc {
func waitUntilOscVolumeUnLinkedStateFunc(conn *osc.APIClient, id string) stateRefreshFunc {
return func() (string, error) {
log.Printf("[Debug] Check if volume with id %s exists", id)
resp, err := conn.POST_ReadVolumes(oapi.ReadVolumesRequest{
Filters: oapi.FiltersVolume{
VolumeIds: []string{id},
},
resp, _, err := conn.VolumeApi.ReadVolumes(context.Background(), &osc.ReadVolumesOpts{
ReadVolumesRequest: optional.NewInterface(osc.ReadVolumesRequest{
Filters: osc.FiltersVolume{
VolumeIds: []string{id},
},
}),
})
if err != nil {
return "", err
}
if resp.OK == nil {
return "", fmt.Errorf("Vm with ID %s. Not Found", id)
}
log.Printf("[Debug] Read Response %+v", resp.OK)
if len(resp.OK.Volumes) == 0 {
if len(resp.Volumes) == 0 {
return "pending", nil
}
if len(resp.OK.Volumes[0].LinkedVolumes) == 0 {
if len(resp.Volumes[0].LinkedVolumes) == 0 {
return "dettached", nil
}
@ -239,33 +163,6 @@ func waitUntilVolumeUnLinkedStateFunc(conn *oapi.Client, id string) stateRefresh
}
}
func waitUntilSnapshotStateFunc(conn *oapi.Client, id string) stateRefreshFunc {
return func() (string, error) {
log.Printf("[Debug] Check if Snapshot with id %s exists", id)
resp, err := conn.POST_ReadSnapshots(oapi.ReadSnapshotsRequest{
Filters: oapi.FiltersSnapshot{
SnapshotIds: []string{id},
},
})
log.Printf("[Debug] Read Response %+v", resp.OK)
if err != nil {
return "", err
}
if resp.OK == nil {
return "", fmt.Errorf("Vm with ID %s. Not Found", id)
}
if len(resp.OK.Snapshots) == 0 {
return "pending", nil
}
return resp.OK.Snapshots[0].State, nil
}
}
func waitUntilOscSnapshotStateFunc(conn *osc.APIClient, id string) stateRefreshFunc {
return func() (string, error) {
log.Printf("[Debug] Check if Snapshot with id %s exists", id)
@ -277,17 +174,10 @@ func waitUntilOscSnapshotStateFunc(conn *osc.APIClient, id string) stateRefreshF
}),
})
log.Printf("[Debug] Read Response %+v", resp)
if err != nil {
return "", err
}
//TODO: check if needed
// if resp.OK == nil {
// return "", fmt.Errorf("Vm with ID %s. Not Found", id)
// }
if len(resp.Snapshots) == 0 {
return "pending", nil
}
@ -296,37 +186,6 @@ func waitUntilOscSnapshotStateFunc(conn *osc.APIClient, id string) stateRefreshF
}
}
func waitUntilImageStateFunc(conn *oapi.Client, id string) stateRefreshFunc {
return func() (string, error) {
log.Printf("[Debug] Check if Image with id %s exists", id)
resp, err := conn.POST_ReadImages(oapi.ReadImagesRequest{
Filters: oapi.FiltersImage{
ImageIds: []string{id},
},
})
log.Printf("[Debug] Read Response %+v", resp.OK)
if err != nil {
return "", err
}
if resp.OK == nil {
return "", fmt.Errorf("Vm with ID %s. Not Found", id)
}
if len(resp.OK.Images) == 0 {
return "pending", nil
}
if resp.OK.Images[0].State == "failed" {
return resp.OK.Images[0].State, fmt.Errorf("Image (%s) creation is failed", id)
}
return resp.OK.Images[0].State, nil
}
}
func waitUntilOscImageStateFunc(conn *osc.APIClient, id string) stateRefreshFunc {
return func() (string, error) {
log.Printf("[Debug] Check if Image with id %s exists", id)
@ -338,17 +197,10 @@ func waitUntilOscImageStateFunc(conn *osc.APIClient, id string) stateRefreshFunc
}),
})
log.Printf("[Debug] Read Response %+v", resp)
if err != nil {
return "", err
}
//TODO: check if needed
// if resp.OK == nil {
// return "", fmt.Errorf("Vm with ID %s. Not Found", id)
// }
if len(resp.Images) == 0 {
return "pending", nil
}
@ -361,91 +213,56 @@ func waitUntilOscImageStateFunc(conn *osc.APIClient, id string) stateRefreshFunc
}
}
func securityGroupWaitFunc(conn *oapi.Client, id string) stateRefreshFunc {
return func() (string, error) {
log.Printf("[Debug] Check if SG with id %s exists", id)
resp, err := conn.POST_ReadSecurityGroups(oapi.ReadSecurityGroupsRequest{
Filters: oapi.FiltersSecurityGroup{
SecurityGroupIds: []string{id},
},
})
log.Printf("[Debug] Read Response %+v", resp.OK)
if err != nil {
return "", err
}
if resp.OK == nil {
return "", fmt.Errorf("Security Group with ID %s. Not Found", id)
}
if len(resp.OK.SecurityGroups) == 0 {
return "waiting", nil
}
return "exists", nil
}
}
func waitUntilSnapshotDoneStateFunc(conn *oapi.Client, id string) stateRefreshFunc {
func waitUntilOscSnapshotDoneStateFunc(conn *osc.APIClient, id string) stateRefreshFunc {
return func() (string, error) {
log.Printf("[Debug] Check if Snapshot with id %s exists", id)
resp, err := conn.POST_ReadSnapshots(oapi.ReadSnapshotsRequest{
Filters: oapi.FiltersSnapshot{
SnapshotIds: []string{id},
},
resp, _, err := conn.SnapshotApi.ReadSnapshots(context.Background(), &osc.ReadSnapshotsOpts{
ReadSnapshotsRequest: optional.NewInterface(osc.ReadSnapshotsRequest{
Filters: osc.FiltersSnapshot{
SnapshotIds: []string{id},
},
}),
})
log.Printf("[Debug] Read Response %+v", resp.OK)
if err != nil {
return "", err
}
if resp.OK == nil {
if len(resp.Snapshots) == 0 {
return "", fmt.Errorf("Snapshot with ID %s. Not Found", id)
}
if len(resp.OK.Snapshots) == 0 {
return "", fmt.Errorf("Snapshot with ID %s. Not Found", id)
if resp.Snapshots[0].State == "error" {
return resp.Snapshots[0].State, fmt.Errorf("Snapshot (%s) creation is failed", id)
}
if resp.OK.Snapshots[0].State == "error" {
return resp.OK.Snapshots[0].State, fmt.Errorf("Snapshot (%s) creation is failed", id)
}
return resp.OK.Snapshots[0].State, nil
return resp.Snapshots[0].State, nil
}
}
func volumeWaitFunc(conn *oapi.Client, id string) stateRefreshFunc {
func volumeOscWaitFunc(conn *osc.APIClient, id string) stateRefreshFunc {
return func() (string, error) {
log.Printf("[Debug] Check if SvolumeG with id %s exists", id)
resp, err := conn.POST_ReadVolumes(oapi.ReadVolumesRequest{
Filters: oapi.FiltersVolume{
VolumeIds: []string{id},
},
resp, _, err := conn.VolumeApi.ReadVolumes(context.Background(), &osc.ReadVolumesOpts{
ReadVolumesRequest: optional.NewInterface(osc.ReadVolumesRequest{
Filters: osc.FiltersVolume{
VolumeIds: []string{id},
},
}),
})
log.Printf("[Debug] Read Response %+v", resp.OK)
if err != nil {
return "", err
}
if resp.OK == nil {
return "", fmt.Errorf("Volume with ID %s. Not Found", id)
}
if len(resp.OK.Volumes) == 0 {
if len(resp.Volumes) == 0 {
return "waiting", nil
}
if resp.OK.Volumes[0].State == "error" {
return resp.OK.Volumes[0].State, fmt.Errorf("Volume (%s) creation is failed", id)
if resp.Volumes[0].State == "error" {
return resp.Volumes[0].State, fmt.Errorf("Volume (%s) creation is failed", id)
}
return resp.OK.Volumes[0].State, nil
return resp.Volumes[0].State, nil
}
}

View File

@ -39,8 +39,8 @@ func (s *StepDeregisterOMI) Run(_ context.Context, state multistep.StateBag) mul
resp, _, err := conn.ImageApi.ReadImages(context.Background(), &osc.ReadImagesOpts{
ReadImagesRequest: optional.NewInterface(osc.ReadImagesRequest{
Filters: osc.FiltersImage{
ImageNames: []string{s.OMIName},
AccountAliases: []string{"self"},
ImageNames: []string{s.OMIName},
//AccountAliases: []string{"self"},
},
}),
})

View File

@ -11,10 +11,10 @@ import (
"log"
"time"
"github.com/antihax/optional"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
@ -103,7 +103,7 @@ WaitLoop:
func (s *StepGetPassword) Cleanup(multistep.StateBag) {}
func (s *StepGetPassword) waitForPassword(state multistep.StateBag, cancel <-chan struct{}) (string, error) {
oapiconn := state.Get("oapi").(*oapi.Client)
oscconn := state.Get("osc").(*osc.APIClient)
vm := state.Get("vm").(osc.Vm)
privateKey := s.Comm.SSHPrivateKey
@ -115,17 +115,19 @@ func (s *StepGetPassword) waitForPassword(state multistep.StateBag, cancel <-cha
case <-time.After(5 * time.Second):
}
resp, err := oapiconn.POST_ReadAdminPassword(oapi.ReadAdminPasswordRequest{
VmId: vm.VmId,
resp, _, err := oscconn.VmApi.ReadAdminPassword(context.Background(), &osc.ReadAdminPasswordOpts{
ReadAdminPasswordRequest: optional.NewInterface(osc.ReadAdminPasswordRequest{
VmId: vm.VmId,
}),
})
if err != nil {
err := fmt.Errorf("Error retrieving auto-generated vm password: %s", err)
return "", err
}
if resp.OK.AdminPassword != "" {
if resp.AdminPassword != "" {
decryptedPassword, err := decryptPasswordDataWithPrivateKey(
resp.OK.AdminPassword, []byte(privateKey))
resp.AdminPassword, []byte(privateKey))
if err != nil {
err := fmt.Errorf("Error decrypting auto-generated vm password: %s", err)
return "", err

View File

@ -10,7 +10,6 @@ import (
"github.com/antihax/optional"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
@ -31,14 +30,6 @@ type StepNetworkInfo struct {
SecurityGroupFilter SecurityGroupFilterOptions
}
type subnetsSort []oapi.Subnet
func (a subnetsSort) Len() int { return len(a) }
func (a subnetsSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a subnetsSort) Less(i, j int) bool {
return a[i].AvailableIpsCount < a[j].AvailableIpsCount
}
type subnetsOscSort []osc.Subnet
func (a subnetsOscSort) Len() int { return len(a) }
@ -47,13 +38,6 @@ func (a subnetsOscSort) Less(i, j int) bool {
return a[i].AvailableIpsCount < a[j].AvailableIpsCount
}
// Returns the most recent OMI out of a slice of images.
func mostFreeSubnet(subnets []oapi.Subnet) oapi.Subnet {
sortedSubnets := subnets
sort.Sort(subnetsSort(sortedSubnets))
return sortedSubnets[len(sortedSubnets)-1]
}
// Returns the most recent OMI out of a slice of images.
func mostFreeOscSubnet(subnets []osc.Subnet) osc.Subnet {
sortedSubnets := subnets

View File

@ -10,7 +10,6 @@ import (
"github.com/antihax/optional"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
retry "github.com/hashicorp/packer/common"
@ -119,42 +118,8 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
Placement: osc.Placement{SubregionName: subregion},
BsuOptimized: s.BsuOptimized,
BlockDeviceMappings: s.BlockDevices.BuildOSCLaunchDevices(),
//IamVmProfile: oapi.IamVmProfileSpecification{Name: &s.IamVmProfile},
}
// if s.EnableT2Unlimited {
// creditOption := "unlimited"
// runOpts.CreditSpecification = &oapi.CreditSpecificationRequest{CpuCredits: &creditOption}
// }
// Collect tags for tagging on resource creation
// var tagSpecs []oapi.ResourceTag
// if len(oapiTags) > 0 {
// runTags := &oapi.ResourceTag{
// ResourceType: aws.String("vm"),
// Tags: oapiTags,
// }
// tagSpecs = append(tagSpecs, runTags)
// }
// if len(volTags) > 0 {
// runVolTags := &oapi.TagSpecification{
// ResourceType: aws.String("volume"),
// Tags: volTags,
// }
// tagSpecs = append(tagSpecs, runVolTags)
// }
// // If our region supports it, set tag specifications
// if len(tagSpecs) > 0 && !s.IsRestricted {
// runOpts.SetTagSpecifications(tagSpecs)
// oapiTags.Report(ui)
// volTags.Report(ui)
// }
if s.Comm.SSHKeyPairName != "" {
runOpts.KeypairName = s.Comm.SSHKeyPairName
}
@ -299,7 +264,7 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
volumeIds := make([]string, 0)
for _, v := range vm.BlockDeviceMappings {
if bsu := v.Bsu; !reflect.DeepEqual(bsu, oapi.BsuCreated{}) {
if bsu := v.Bsu; !reflect.DeepEqual(bsu, osc.BsuCreated{}) {
volumeIds = append(volumeIds, bsu.VolumeId)
}
}

View File

@ -10,7 +10,6 @@ import (
"github.com/antihax/optional"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
@ -18,24 +17,15 @@ import (
// that is used throughout the OMI creation process.
//
// Produces:
// source_image *oapi.Image - the source OMI info
// source_image *osc.Image - the source OMI info
type StepSourceOMIInfo struct {
SourceOmi string
OMIVirtType string
OmiFilters OmiFilterOptions
}
type imageSort []oapi.Image
type imageOscSort []osc.Image
func (a imageSort) Len() int { return len(a) }
func (a imageSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a imageSort) Less(i, j int) bool {
itime, _ := time.Parse(time.RFC3339, a[i].CreationDate)
jtime, _ := time.Parse(time.RFC3339, a[j].CreationDate)
return itime.Unix() < jtime.Unix()
}
func (a imageOscSort) Len() int { return len(a) }
func (a imageOscSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a imageOscSort) Less(i, j int) bool {
@ -44,13 +34,6 @@ func (a imageOscSort) Less(i, j int) bool {
return itime.Unix() < jtime.Unix()
}
// Returns the most recent OMI out of a slice of images.
func mostRecentOmi(images []oapi.Image) oapi.Image {
sortedImages := images
sort.Sort(imageSort(sortedImages))
return sortedImages[len(sortedImages)-1]
}
// Returns the most recent OMI out of a slice of images.
func mostRecentOscOmi(images []osc.Image) osc.Image {
sortedImages := images

View File

@ -8,21 +8,12 @@ import (
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
type TagMap map[string]string
type OAPITags []oapi.ResourceTag
type OSCTags []osc.ResourceTag
func (t OAPITags) Report(ui packer.Ui) {
for _, tag := range t {
ui.Message(fmt.Sprintf("Adding tag: \"%s\": \"%s\"",
tag.Key, tag.Value))
}
}
func (t OSCTags) Report(ui packer.Ui) {
for _, tag := range t {
ui.Message(fmt.Sprintf("Adding tag: \"%s\": \"%s\"",
@ -34,27 +25,6 @@ func (t TagMap) IsSet() bool {
return len(t) > 0
}
func (t TagMap) OAPITags(ctx interpolate.Context, region string, state multistep.StateBag) (OAPITags, error) {
var oapiTags []oapi.ResourceTag
ctx.Data = extractBuildInfo(region, state)
for key, value := range t {
interpolatedKey, err := interpolate.Render(key, &ctx)
if err != nil {
return nil, fmt.Errorf("Error processing tag: %s:%s - %s", key, value, err)
}
interpolatedValue, err := interpolate.Render(value, &ctx)
if err != nil {
return nil, fmt.Errorf("Error processing tag: %s:%s - %s", key, value, err)
}
oapiTags = append(oapiTags, oapi.ResourceTag{
Key: interpolatedKey,
Value: interpolatedValue,
})
}
return oapiTags, nil
}
func (t TagMap) OSCTags(ctx interpolate.Context, region string, state multistep.StateBag) (OSCTags, error) {
var oscTags []osc.ResourceTag
ctx.Data = extractBuildInfo(region, state)
@ -76,17 +46,6 @@ func (t TagMap) OSCTags(ctx interpolate.Context, region string, state multistep.
return oscTags, nil
}
func CreateTags(conn *oapi.Client, resourceID string, ui packer.Ui, tags OAPITags) error {
tags.Report(ui)
_, err := conn.POST_CreateTags(oapi.CreateTagsRequest{
ResourceIds: []string{resourceID},
Tags: tags,
})
return err
}
func CreateOSCTags(conn *osc.APIClient, resourceID string, ui packer.Ui, tags OSCTags) error {
tags.Report(ui)