refactor: change bsuvolume builder to new SDK

This commit is contained in:
Marin Salinas 2020-09-11 12:11:24 -05:00
parent b844b7f1c7
commit 645e5afa36
4 changed files with 38 additions and 33 deletions

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

@ -46,7 +46,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)