2019-02-05 14:18:44 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-08-26 13:25:46 -04:00
|
|
|
"github.com/antihax/optional"
|
2019-02-05 14:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2020-08-26 13:25:46 -04:00
|
|
|
"github.com/outscale/osc-sdk-go/osc"
|
2019-02-05 14:18:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepUpdateOMIAttributes struct {
|
|
|
|
AccountIds []string
|
|
|
|
SnapshotAccountIds []string
|
2020-08-26 13:25:46 -04:00
|
|
|
RawRegion string
|
2019-02-05 14:18:44 -05:00
|
|
|
Ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepUpdateOMIAttributes) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-08-26 13:25:46 -04:00
|
|
|
config := state.Get("accessConfig").(*AccessConfig)
|
2019-02-05 14:18:44 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
omis := state.Get("omis").(map[string]string)
|
|
|
|
snapshots := state.Get("snapshots").(map[string][]string)
|
|
|
|
|
|
|
|
// Determine if there is any work to do.
|
|
|
|
valid := false
|
|
|
|
valid = valid || (s.AccountIds != nil && len(s.AccountIds) > 0)
|
|
|
|
valid = valid || (s.SnapshotAccountIds != nil && len(s.SnapshotAccountIds) > 0)
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-08-26 13:25:46 -04:00
|
|
|
s.Ctx.Data = extractBuildInfo(s.RawRegion, state)
|
2019-02-05 14:18:44 -05:00
|
|
|
|
2020-08-26 13:25:46 -04:00
|
|
|
updateSnapshoptRequest := osc.UpdateSnapshotRequest{
|
|
|
|
PermissionsToCreateVolume: osc.PermissionsOnResourceCreation{
|
|
|
|
Additions: osc.PermissionsOnResource{
|
2019-02-05 14:18:44 -05:00
|
|
|
AccountIds: s.AccountIds,
|
2019-05-01 17:10:35 -04:00
|
|
|
GlobalPermission: false,
|
2019-02-05 14:18:44 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-26 13:25:46 -04:00
|
|
|
updateImageRequest := osc.UpdateImageRequest{
|
|
|
|
PermissionsToLaunch: osc.PermissionsOnResourceCreation{
|
|
|
|
Additions: osc.PermissionsOnResource{
|
2019-02-05 14:18:44 -05:00
|
|
|
AccountIds: s.AccountIds,
|
2019-05-01 17:10:35 -04:00
|
|
|
GlobalPermission: false,
|
2019-02-05 14:18:44 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updating image attributes
|
|
|
|
for region, omi := range omis {
|
|
|
|
ui.Say(fmt.Sprintf("Updating attributes on OMI (%s)...", omi))
|
2020-08-26 13:25:46 -04:00
|
|
|
regionconn := config.NewOSCClientByRegion(region)
|
2019-02-05 14:18:44 -05:00
|
|
|
|
2020-08-26 13:25:46 -04:00
|
|
|
// newConfig := &osc.Configuration{
|
|
|
|
// UserAgent: config.UserAgent,
|
|
|
|
// AccessKey: config.AccessKey,
|
|
|
|
// SecretKey: config.SecretKey,
|
|
|
|
// Service: config.Service,
|
|
|
|
// Region: region, //New region
|
|
|
|
// URL: config.URL,
|
|
|
|
// }
|
2019-02-05 14:18:44 -05:00
|
|
|
|
2020-08-26 13:25:46 -04:00
|
|
|
// skipClient := &http.Client{
|
|
|
|
// Transport: &http.Transport{
|
|
|
|
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
|
|
|
|
//regionconn := oapi.NewClient(newConfig, skipClient)
|
2019-02-05 14:18:44 -05:00
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Updating: %s", omi))
|
|
|
|
updateImageRequest.ImageId = omi
|
2020-08-26 13:25:46 -04:00
|
|
|
_, _, err := regionconn.ImageApi.UpdateImage(context.Background(), &osc.UpdateImageOpts{
|
|
|
|
UpdateImageRequest: optional.NewInterface(updateImageRequest),
|
|
|
|
})
|
|
|
|
|
2019-02-05 14:18:44 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error updating OMI: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updating snapshot attributes
|
|
|
|
for region, region_snapshots := range snapshots {
|
|
|
|
for _, snapshot := range region_snapshots {
|
|
|
|
ui.Say(fmt.Sprintf("Updating attributes on snapshot (%s)...", snapshot))
|
2020-08-26 13:25:46 -04:00
|
|
|
regionconn := config.NewOSCClientByRegion(region)
|
2019-02-05 14:18:44 -05:00
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Updating: %s", snapshot))
|
|
|
|
updateSnapshoptRequest.SnapshotId = snapshot
|
2020-08-26 13:25:46 -04:00
|
|
|
_, _, err := regionconn.SnapshotApi.UpdateSnapshot(context.Background(), &osc.UpdateSnapshotOpts{
|
|
|
|
UpdateSnapshotRequest: optional.NewInterface(updateSnapshoptRequest),
|
|
|
|
})
|
2019-02-05 14:18:44 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error updating snapshot: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepUpdateOMIAttributes) Cleanup(state multistep.StateBag) {
|
|
|
|
// No cleanup...
|
|
|
|
}
|