refactor: change bsuvolume builder to new SDK
This commit is contained in:
parent
b844b7f1c7
commit
645e5afa36
|
@ -1,13 +1,15 @@
|
||||||
package bsuvolume
|
package bsuvolume
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/antihax/optional"
|
||||||
"github.com/hashicorp/packer/packer"
|
"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
|
// map of region to list of volume IDs
|
||||||
|
@ -22,7 +24,7 @@ type Artifact struct {
|
||||||
BuilderIdValue string
|
BuilderIdValue string
|
||||||
|
|
||||||
// Client connection for performing API stuff.
|
// Client connection for performing API stuff.
|
||||||
Conn *oapi.Client
|
Conn *osc.APIClient
|
||||||
|
|
||||||
// StateData should store data such as GeneratedData
|
// StateData should store data such as GeneratedData
|
||||||
// to be shared with post-processors
|
// to be shared with post-processors
|
||||||
|
@ -70,10 +72,12 @@ func (a *Artifact) Destroy() error {
|
||||||
for _, volumeID := range volumeIDs {
|
for _, volumeID := range volumeIDs {
|
||||||
log.Printf("Deregistering Volume ID (%s) from region (%s)", volumeID, region)
|
log.Printf("Deregistering Volume ID (%s) from region (%s)", volumeID, region)
|
||||||
|
|
||||||
input := oapi.DeleteVolumeRequest{
|
input := osc.DeleteVolumeRequest{
|
||||||
VolumeId: volumeID,
|
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)
|
errors = append(errors, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,8 @@ package bsuvolume
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/hashicorp/hcl/v2/hcldec"
|
"github.com/hashicorp/hcl/v2/hcldec"
|
||||||
osccommon "github.com/hashicorp/packer/builder/osc/common"
|
osccommon "github.com/hashicorp/packer/builder/osc/common"
|
||||||
|
@ -19,7 +17,6 @@ import (
|
||||||
"github.com/hashicorp/packer/helper/multistep"
|
"github.com/hashicorp/packer/helper/multistep"
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
"github.com/hashicorp/packer/template/interpolate"
|
"github.com/hashicorp/packer/template/interpolate"
|
||||||
"github.com/outscale/osc-go/oapi"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const BuilderId = "oapi.outscale.bsuvolume"
|
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) {
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
||||||
clientConfig, err := b.config.Config()
|
// clientConfig, err := b.config.Config()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return nil, err
|
// return nil, err
|
||||||
}
|
// }
|
||||||
|
|
||||||
skipClient := &http.Client{
|
// skipClient := &http.Client{
|
||||||
Transport: &http.Transport{
|
// Transport: &http.Transport{
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
},
|
// },
|
||||||
}
|
// }
|
||||||
|
|
||||||
oapiconn := oapi.NewClient(clientConfig, skipClient)
|
oscConn := b.config.NewOSCClient()
|
||||||
|
|
||||||
// Setup the state bag and initial state for the steps
|
// Setup the state bag and initial state for the steps
|
||||||
state := new(multistep.BasicStateBag)
|
state := new(multistep.BasicStateBag)
|
||||||
state.Put("oapi", oapiconn)
|
state.Put("osc", oscConn)
|
||||||
state.Put("hook", hook)
|
state.Put("hook", hook)
|
||||||
state.Put("ui", ui)
|
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,
|
Debug: b.config.PackerDebug,
|
||||||
BsuOptimized: b.config.BsuOptimized,
|
BsuOptimized: b.config.BsuOptimized,
|
||||||
EnableT2Unlimited: b.config.EnableT2Unlimited,
|
EnableT2Unlimited: b.config.EnableT2Unlimited,
|
||||||
ExpectedRootDevice: "ebs",
|
ExpectedRootDevice: "bsu",
|
||||||
IamVmProfile: b.config.IamVmProfile,
|
IamVmProfile: b.config.IamVmProfile,
|
||||||
VmInitiatedShutdownBehavior: b.config.VmInitiatedShutdownBehavior,
|
VmInitiatedShutdownBehavior: b.config.VmInitiatedShutdownBehavior,
|
||||||
VmType: b.config.VmType,
|
VmType: b.config.VmType,
|
||||||
|
@ -170,8 +167,8 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
||||||
},
|
},
|
||||||
&communicator.StepConnect{
|
&communicator.StepConnect{
|
||||||
Config: &b.config.RunConfig.Comm,
|
Config: &b.config.RunConfig.Comm,
|
||||||
Host: osccommon.SSHHost(
|
Host: osccommon.OscSSHHost(
|
||||||
oapiconn,
|
oscConn.VmApi,
|
||||||
b.config.SSHInterface),
|
b.config.SSHInterface),
|
||||||
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
|
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{
|
artifact := &Artifact{
|
||||||
Volumes: state.Get("bsuvolumes").(BsuVolumes),
|
Volumes: state.Get("bsuvolumes").(BsuVolumes),
|
||||||
BuilderIdValue: BuilderId,
|
BuilderIdValue: BuilderId,
|
||||||
Conn: oapiconn,
|
Conn: oscConn,
|
||||||
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
StateData: map[string]interface{}{"generated_data": state.Get("generated_data")},
|
||||||
}
|
}
|
||||||
ui.Say(fmt.Sprintf("Created Volumes: %s", artifact))
|
ui.Say(fmt.Sprintf("Created Volumes: %s", artifact))
|
||||||
|
|
|
@ -46,7 +46,7 @@ const testBuilderAccBasic = `
|
||||||
"type": "test",
|
"type": "test",
|
||||||
"region": "eu-west-2",
|
"region": "eu-west-2",
|
||||||
"vm_type": "t2.micro",
|
"vm_type": "t2.micro",
|
||||||
"source_omi": "ami-65efcc11",
|
"source_omi": "ami-abe953fa",
|
||||||
"ssh_username": "outscale",
|
"ssh_username": "outscale",
|
||||||
"bsu_volumes": [
|
"bsu_volumes": [
|
||||||
{
|
{
|
||||||
|
|
|
@ -4,28 +4,30 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/antihax/optional"
|
||||||
"github.com/hashicorp/packer/helper/multistep"
|
"github.com/hashicorp/packer/helper/multistep"
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
"github.com/hashicorp/packer/template/interpolate"
|
"github.com/hashicorp/packer/template/interpolate"
|
||||||
"github.com/outscale/osc-go/oapi"
|
"github.com/outscale/osc-sdk-go/osc"
|
||||||
)
|
)
|
||||||
|
|
||||||
type stepTagBSUVolumes struct {
|
type stepTagBSUVolumes struct {
|
||||||
VolumeMapping []BlockDevice
|
VolumeMapping []BlockDevice
|
||||||
|
RawRegion string
|
||||||
Ctx interpolate.Context
|
Ctx interpolate.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stepTagBSUVolumes) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *stepTagBSUVolumes) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
oapiconn := state.Get("oapi").(*oapi.Client)
|
oscconn := state.Get("osc").(*osc.APIClient)
|
||||||
vm := state.Get("vm").(oapi.Vm)
|
vm := state.Get("vm").(osc.Vm)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
volumes := make(BsuVolumes)
|
volumes := make(BsuVolumes)
|
||||||
for _, instanceBlockDevices := range vm.BlockDeviceMappings {
|
for _, instanceBlockDevices := range vm.BlockDeviceMappings {
|
||||||
for _, configVolumeMapping := range s.VolumeMapping {
|
for _, configVolumeMapping := range s.VolumeMapping {
|
||||||
if configVolumeMapping.DeviceName == instanceBlockDevices.DeviceName {
|
if configVolumeMapping.DeviceName == instanceBlockDevices.DeviceName {
|
||||||
volumes[oapiconn.GetConfig().Region] = append(
|
volumes[s.RawRegion] = append(
|
||||||
volumes[oapiconn.GetConfig().Region],
|
volumes[s.RawRegion],
|
||||||
instanceBlockDevices.Bsu.VolumeId)
|
instanceBlockDevices.Bsu.VolumeId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,14 +37,14 @@ func (s *stepTagBSUVolumes) Run(_ context.Context, state multistep.StateBag) mul
|
||||||
if len(s.VolumeMapping) > 0 {
|
if len(s.VolumeMapping) > 0 {
|
||||||
ui.Say("Tagging BSU volumes...")
|
ui.Say("Tagging BSU volumes...")
|
||||||
|
|
||||||
toTag := map[string][]oapi.ResourceTag{}
|
toTag := map[string][]osc.ResourceTag{}
|
||||||
for _, mapping := range s.VolumeMapping {
|
for _, mapping := range s.VolumeMapping {
|
||||||
if len(mapping.Tags) == 0 {
|
if len(mapping.Tags) == 0 {
|
||||||
ui.Say(fmt.Sprintf("No tags specified for volume on %s...", mapping.DeviceName))
|
ui.Say(fmt.Sprintf("No tags specified for volume on %s...", mapping.DeviceName))
|
||||||
continue
|
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 {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error tagging device %s with %s", mapping.DeviceName, err)
|
err := fmt.Errorf("Error tagging device %s with %s", mapping.DeviceName, err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
@ -59,9 +61,11 @@ func (s *stepTagBSUVolumes) Run(_ context.Context, state multistep.StateBag) mul
|
||||||
}
|
}
|
||||||
|
|
||||||
for volumeId, tags := range toTag {
|
for volumeId, tags := range toTag {
|
||||||
_, err := oapiconn.POST_CreateTags(oapi.CreateTagsRequest{
|
_, _, err := oscconn.TagApi.CreateTags(context.Background(), &osc.CreateTagsOpts{
|
||||||
|
CreateTagsRequest: optional.NewInterface(osc.CreateTagsRequest{
|
||||||
ResourceIds: []string{volumeId},
|
ResourceIds: []string{volumeId},
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error tagging BSU Volume %s on %s: %s", volumeId, vm.VmId, err)
|
err := fmt.Errorf("Error tagging BSU Volume %s on %s: %s", volumeId, vm.VmId, err)
|
||||||
|
|
Loading…
Reference in New Issue