refactor: change new sdk on step_source_omi_info
This commit is contained in:
parent
daefa4d086
commit
8649496c6c
|
@ -5,6 +5,7 @@ import (
|
|||
"strconv"
|
||||
|
||||
"github.com/outscale/osc-go/oapi"
|
||||
"github.com/outscale/osc-sdk-go/osc"
|
||||
)
|
||||
|
||||
func buildNetFilters(input map[string]string) oapi.FiltersNet {
|
||||
|
@ -91,6 +92,38 @@ func buildOMIFilters(input map[string]string) oapi.FiltersImage {
|
|||
return filters
|
||||
}
|
||||
|
||||
func buildOSCOMIFilters(input map[string]string) osc.FiltersImage {
|
||||
var filters osc.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 buildSecurityGroupFilters(input map[string]string) oapi.FiltersSecurityGroup {
|
||||
var filters oapi.FiltersSecurityGroup
|
||||
for k, v := range input {
|
||||
|
|
|
@ -2,7 +2,7 @@ package common
|
|||
|
||||
import (
|
||||
"github.com/hashicorp/packer/helper/multistep"
|
||||
"github.com/outscale/osc-go/oapi"
|
||||
"github.com/outscale/osc-sdk-go/osc"
|
||||
)
|
||||
|
||||
type BuildInfoTemplate struct {
|
||||
|
@ -20,7 +20,7 @@ func extractBuildInfo(region string, state multistep.StateBag) *BuildInfoTemplat
|
|||
}
|
||||
}
|
||||
|
||||
sourceOMI := rawSourceOMI.(oapi.Image)
|
||||
sourceOMI := rawSourceOMI.(osc.Image)
|
||||
sourceOMITags := make(map[string]string, len(sourceOMI.Tags))
|
||||
for _, tag := range sourceOMI.Tags {
|
||||
sourceOMITags[tag.Key] = tag.Value
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
|
||||
"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"
|
||||
"github.com/hashicorp/packer/helper/communicator"
|
||||
|
@ -67,7 +68,7 @@ func (s *StepRunSourceVm) Run(ctx context.Context, state multistep.StateBag) mul
|
|||
}
|
||||
|
||||
ui.Say("Launching a source OUTSCALE vm...")
|
||||
image, ok := state.Get("source_image").(oapi.Image)
|
||||
image, ok := state.Get("source_image").(osc.Image)
|
||||
if !ok {
|
||||
state.Put("error", fmt.Errorf("source_image type assertion failed"))
|
||||
return multistep.ActionHalt
|
||||
|
|
|
@ -7,9 +7,11 @@ import (
|
|||
"sort"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// StepSourceOMIInfo extracts critical information from the source OMI
|
||||
|
@ -24,6 +26,7 @@ type StepSourceOMIInfo struct {
|
|||
}
|
||||
|
||||
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] }
|
||||
|
@ -33,6 +36,14 @@ func (a imageSort) Less(i, j int) bool {
|
|||
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 {
|
||||
itime, _ := time.Parse(time.RFC3339, a[i].CreationDate)
|
||||
jtime, _ := time.Parse(time.RFC3339, a[j].CreationDate)
|
||||
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
|
||||
|
@ -40,12 +51,19 @@ func mostRecentOmi(images []oapi.Image) oapi.Image {
|
|||
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
|
||||
sort.Sort(imageOscSort(sortedImages))
|
||||
return sortedImages[len(sortedImages)-1]
|
||||
}
|
||||
|
||||
func (s *StepSourceOMIInfo) Run(_ 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)
|
||||
|
||||
params := oapi.ReadImagesRequest{
|
||||
Filters: oapi.FiltersImage{},
|
||||
params := osc.ReadImagesRequest{
|
||||
Filters: osc.FiltersImage{},
|
||||
}
|
||||
|
||||
if s.SourceOmi != "" {
|
||||
|
@ -54,7 +72,7 @@ func (s *StepSourceOMIInfo) Run(_ context.Context, state multistep.StateBag) mul
|
|||
|
||||
// We have filters to apply
|
||||
if len(s.OmiFilters.Filters) > 0 {
|
||||
params.Filters = buildOMIFilters(s.OmiFilters.Filters)
|
||||
params.Filters = buildOSCOMIFilters(s.OmiFilters.Filters)
|
||||
}
|
||||
//TODO:Check if AccountIds correspond to Owners.
|
||||
if len(s.OmiFilters.Owners) > 0 {
|
||||
|
@ -62,7 +80,9 @@ func (s *StepSourceOMIInfo) Run(_ context.Context, state multistep.StateBag) mul
|
|||
}
|
||||
|
||||
log.Printf("Using OMI Filters %#v", params)
|
||||
imageResp, err := oapiconn.POST_ReadImages(params)
|
||||
imageResp, _, err := oscconn.ImageApi.ReadImages(context.Background(), &osc.ReadImagesOpts{
|
||||
ReadImagesRequest: optional.NewInterface(params),
|
||||
})
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Error querying OMI: %s", err)
|
||||
state.Put("error", err)
|
||||
|
@ -70,25 +90,25 @@ func (s *StepSourceOMIInfo) Run(_ context.Context, state multistep.StateBag) mul
|
|||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
if len(imageResp.OK.Images) == 0 {
|
||||
if len(imageResp.Images) == 0 {
|
||||
err := fmt.Errorf("No OMI was found matching filters: %#v", params)
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
if len(imageResp.OK.Images) > 1 && !s.OmiFilters.MostRecent {
|
||||
err := fmt.Errorf("Your query returned more than one result. Please try a more specific search, or set most_recent to true.")
|
||||
if len(imageResp.Images) > 1 && !s.OmiFilters.MostRecent {
|
||||
err := fmt.Errorf("your query returned more than one result. Please try a more specific search, or set most_recent to true")
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
var image oapi.Image
|
||||
var image osc.Image
|
||||
if s.OmiFilters.MostRecent {
|
||||
image = mostRecentOmi(imageResp.OK.Images)
|
||||
image = mostRecentOscOmi(imageResp.Images)
|
||||
} else {
|
||||
image = imageResp.OK.Images[0]
|
||||
image = imageResp.Images[0]
|
||||
}
|
||||
|
||||
ui.Message(fmt.Sprintf("Found Image ID: %s", image.ImageId))
|
||||
|
|
Loading…
Reference in New Issue