2019-01-31 15:48:46 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"sort"
|
|
|
|
|
2020-08-18 11:34:02 -04:00
|
|
|
"github.com/antihax/optional"
|
2019-01-31 15:48:46 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/outscale/osc-go/oapi"
|
2020-08-18 11:34:02 -04:00
|
|
|
"github.com/outscale/osc-sdk-go/osc"
|
2019-01-31 15:48:46 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepNetworkInfo queries OUTSCALE for information about
|
|
|
|
// NET's and Subnets that is used throughout the OMI creation process.
|
|
|
|
//
|
|
|
|
// Produces (adding them to the state bag):
|
|
|
|
// vpc_id string - the NET ID
|
|
|
|
// subnet_id string - the Subnet ID
|
|
|
|
// availability_zone string - the Subregion name
|
|
|
|
type StepNetworkInfo struct {
|
|
|
|
NetId string
|
|
|
|
NetFilter NetFilterOptions
|
|
|
|
SubnetId string
|
|
|
|
SubnetFilter SubnetFilterOptions
|
|
|
|
SubregionName string
|
|
|
|
SecurityGroupIds []string
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-18 11:34:02 -04:00
|
|
|
type subnetsOscSort []osc.Subnet
|
|
|
|
|
|
|
|
func (a subnetsOscSort) Len() int { return len(a) }
|
|
|
|
func (a subnetsOscSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a subnetsOscSort) Less(i, j int) bool {
|
|
|
|
return a[i].AvailableIpsCount < a[j].AvailableIpsCount
|
|
|
|
}
|
|
|
|
|
2019-01-31 15:48:46 -05:00
|
|
|
// 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]
|
|
|
|
}
|
|
|
|
|
2020-08-18 11:34:02 -04:00
|
|
|
// Returns the most recent OMI out of a slice of images.
|
|
|
|
func mostFreeOscSubnet(subnets []osc.Subnet) osc.Subnet {
|
|
|
|
sortedSubnets := subnets
|
|
|
|
sort.Sort(subnetsOscSort(sortedSubnets))
|
|
|
|
return sortedSubnets[len(sortedSubnets)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
//Run ...
|
2019-01-31 15:48:46 -05:00
|
|
|
func (s *StepNetworkInfo) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-08-18 11:34:02 -04:00
|
|
|
oscconn := state.Get("osc").(*osc.APIClient)
|
2019-01-31 15:48:46 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
// NET
|
|
|
|
if s.NetId == "" && !s.NetFilter.Empty() {
|
2020-08-18 11:34:02 -04:00
|
|
|
params := osc.ReadNetsRequest{}
|
|
|
|
params.Filters = buildOscNetFilters(s.NetFilter.Filters)
|
2019-01-31 15:48:46 -05:00
|
|
|
s.NetFilter.Filters["state"] = "available"
|
|
|
|
|
|
|
|
log.Printf("Using NET Filters %v", params)
|
|
|
|
|
2020-08-18 11:34:02 -04:00
|
|
|
vpcResp, _, err := oscconn.NetApi.ReadNets(context.Background(), &osc.ReadNetsOpts{
|
|
|
|
ReadNetsRequest: optional.NewInterface(params),
|
|
|
|
})
|
2019-01-31 15:48:46 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error querying NETs: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-08-18 11:34:02 -04:00
|
|
|
if len(vpcResp.Nets) != 1 {
|
|
|
|
err := fmt.Errorf("Exactly one NET should match the filter, but %d NET's was found matching filters: %v", len(vpcResp.Nets), params)
|
2019-01-31 15:48:46 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-08-18 11:34:02 -04:00
|
|
|
s.NetId = vpcResp.Nets[0].NetId
|
2019-01-31 15:48:46 -05:00
|
|
|
ui.Message(fmt.Sprintf("Found NET ID: %s", s.NetId))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subnet
|
|
|
|
if s.SubnetId == "" && !s.SubnetFilter.Empty() {
|
2020-08-18 11:34:02 -04:00
|
|
|
params := osc.ReadSubnetsRequest{}
|
2019-01-31 15:48:46 -05:00
|
|
|
s.SubnetFilter.Filters["state"] = "available"
|
|
|
|
|
|
|
|
if s.NetId != "" {
|
|
|
|
s.SubnetFilter.Filters["vpc-id"] = s.NetId
|
|
|
|
}
|
|
|
|
if s.SubregionName != "" {
|
|
|
|
s.SubnetFilter.Filters["availability-zone"] = s.SubregionName
|
|
|
|
}
|
2020-08-18 11:34:02 -04:00
|
|
|
params.Filters = buildOscSubnetFilters(s.SubnetFilter.Filters)
|
2019-01-31 15:48:46 -05:00
|
|
|
log.Printf("Using Subnet Filters %v", params)
|
|
|
|
|
2020-08-18 11:34:02 -04:00
|
|
|
subnetsResp, _, err := oscconn.SubnetApi.ReadSubnets(context.Background(), &osc.ReadSubnetsOpts{
|
|
|
|
ReadSubnetsRequest: optional.NewInterface(params),
|
|
|
|
})
|
2019-01-31 15:48:46 -05:00
|
|
|
if err != nil {
|
2020-08-18 11:34:02 -04:00
|
|
|
err := fmt.Errorf("error querying Subnets: %s", err)
|
2019-01-31 15:48:46 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-08-18 11:34:02 -04:00
|
|
|
if len(subnetsResp.Subnets) == 0 {
|
2019-01-31 15:48:46 -05:00
|
|
|
err := fmt.Errorf("No Subnets was found matching filters: %v", params)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-08-18 11:34:02 -04:00
|
|
|
if len(subnetsResp.Subnets) > 1 && !s.SubnetFilter.Random && !s.SubnetFilter.MostFree {
|
|
|
|
err := fmt.Errorf("your filter matched %d Subnets. Please try a more specific search, or set random or most_free to true", len(subnetsResp.Subnets))
|
2019-01-31 15:48:46 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-08-18 11:34:02 -04:00
|
|
|
var subnet osc.Subnet
|
2019-01-31 15:48:46 -05:00
|
|
|
switch {
|
|
|
|
case s.SubnetFilter.MostFree:
|
2020-08-18 11:34:02 -04:00
|
|
|
subnet = mostFreeOscSubnet(subnetsResp.Subnets)
|
2019-01-31 15:48:46 -05:00
|
|
|
case s.SubnetFilter.Random:
|
2020-08-18 11:34:02 -04:00
|
|
|
subnet = subnetsResp.Subnets[rand.Intn(len(subnetsResp.Subnets))]
|
2019-01-31 15:48:46 -05:00
|
|
|
default:
|
2020-08-18 11:34:02 -04:00
|
|
|
subnet = subnetsResp.Subnets[0]
|
2019-01-31 15:48:46 -05:00
|
|
|
}
|
|
|
|
s.SubnetId = subnet.SubnetId
|
|
|
|
ui.Message(fmt.Sprintf("Found Subnet ID: %s", s.SubnetId))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find Subregion and NET Id from Subnet if they are not yet found/given
|
|
|
|
if s.SubnetId != "" && (s.SubregionName == "" || s.NetId == "") {
|
|
|
|
log.Printf("[INFO] Finding Subregion and NetId for the given subnet '%s'", s.SubnetId)
|
2020-08-18 11:34:02 -04:00
|
|
|
resp, _, err := oscconn.SubnetApi.ReadSubnets(context.Background(), &osc.ReadSubnetsOpts{
|
|
|
|
ReadSubnetsRequest: optional.NewInterface(osc.ReadSubnetsRequest{
|
|
|
|
Filters: osc.FiltersSubnet{
|
2019-01-31 15:48:46 -05:00
|
|
|
SubnetIds: []string{s.SubnetId},
|
|
|
|
},
|
2020-08-18 11:34:02 -04:00
|
|
|
}),
|
|
|
|
})
|
2019-01-31 15:48:46 -05:00
|
|
|
if err != nil {
|
2020-08-18 11:34:02 -04:00
|
|
|
err := fmt.Errorf("describing the subnet: %s returned error: %s", s.SubnetId, err)
|
2019-01-31 15:48:46 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
if s.SubregionName == "" {
|
2020-08-18 11:34:02 -04:00
|
|
|
s.SubregionName = resp.Subnets[0].SubregionName
|
2019-01-31 15:48:46 -05:00
|
|
|
log.Printf("[INFO] SubregionName found: '%s'", s.SubregionName)
|
|
|
|
}
|
|
|
|
if s.NetId == "" {
|
2020-08-18 11:34:02 -04:00
|
|
|
s.NetId = resp.Subnets[0].NetId
|
2019-01-31 15:48:46 -05:00
|
|
|
log.Printf("[INFO] NetId found: '%s'", s.NetId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 16:20:26 -05:00
|
|
|
state.Put("net_id", s.NetId)
|
2019-02-04 11:37:31 -05:00
|
|
|
state.Put("subregion_name", s.SubregionName)
|
2019-01-31 15:48:46 -05:00
|
|
|
state.Put("subnet_id", s.SubnetId)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-08-18 11:34:02 -04:00
|
|
|
//Cleanup ...
|
2019-01-31 15:48:46 -05:00
|
|
|
func (s *StepNetworkInfo) Cleanup(multistep.StateBag) {}
|