generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add block_volumes #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
450bbe9
feat: add block_volumes
Codelax 7162a8c
update doc
Codelax 8cf99aa
make generate
Codelax 7b7b6a0
create sbs volumes for each block_volume
Codelax 9fc638f
add iops configuration and importing from snapshot
Codelax 6667909
fix(tester): prepare fake env only when using cassettes
Codelax 4a2453a
refactor block volume size to size_in_gb
Codelax b03bfa0
test: add check for image extra volumes types
Codelax 2e237b8
test: add TestBlock
Codelax 009a294
lint
Codelax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package scaleway | ||
|
||
import ( | ||
"fmt" | ||
|
||
packersdk "github.com/hashicorp/packer-plugin-sdk/packer" | ||
"github.com/hashicorp/packer-plugin-sdk/uuid" | ||
"github.com/scaleway/scaleway-sdk-go/api/instance/v1" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
) | ||
|
||
func prepareBlockVolumes(volumes []ConfigBlockVolume) *packersdk.MultiError { | ||
var errs *packersdk.MultiError | ||
|
||
for i := range volumes { | ||
volume := &volumes[i] | ||
|
||
if volume.Name == "" { | ||
volume.Name = "packer-" + uuid.TimeOrderedUUID() | ||
} | ||
if volume.SizeInGB != 0 && volume.SnapshotID != "" { | ||
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("volume (index: %d) can't have a snapshot_id and a size", i)) | ||
} | ||
if volume.SizeInGB == 0 && volume.SnapshotID == "" { | ||
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("volume (index: %d) must have a snapshot_id or a size", i)) | ||
} | ||
} | ||
|
||
return errs | ||
} | ||
|
||
func (blockVolume *ConfigBlockVolume) VolumeTemplate() *instance.VolumeServerTemplate { | ||
return &instance.VolumeServerTemplate{ | ||
Name: &blockVolume.Name, | ||
Size: scw.SizePtr(scw.Size(blockVolume.SizeInGB) * scw.GB), | ||
BaseSnapshot: &blockVolume.SnapshotID, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package scaleway | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/packer-plugin-sdk/multistep" | ||
packersdk "github.com/hashicorp/packer-plugin-sdk/packer" | ||
"github.com/scaleway/packer-plugin-scaleway/internal/httperrors" | ||
block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1" | ||
"github.com/scaleway/scaleway-sdk-go/api/instance/v1" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
) | ||
|
||
const StateKeyCreatedVolumes = "created_volumes" | ||
|
||
type stepCreateVolume struct{} | ||
|
||
func (s *stepCreateVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { | ||
client := state.Get("client").(*scw.Client) | ||
blockAPI := block.NewAPI(client) | ||
ui := state.Get("ui").(packersdk.Ui) | ||
c := state.Get("config").(*Config) | ||
|
||
volumeTemplates := []*instance.VolumeServerTemplate(nil) | ||
for _, requestedVolume := range c.BlockVolumes { | ||
req := &block.CreateVolumeRequest{ | ||
Zone: scw.Zone(c.Zone), | ||
Name: requestedVolume.Name, | ||
PerfIops: requestedVolume.IOPS, | ||
ProjectID: c.ProjectID, | ||
} | ||
if requestedVolume.SnapshotID != "" { | ||
req.FromSnapshot = &block.CreateVolumeRequestFromSnapshot{} | ||
} else { | ||
req.FromEmpty = &block.CreateVolumeRequestFromEmpty{ | ||
Size: scw.Size(requestedVolume.SizeInGB) * scw.GB, | ||
} | ||
} | ||
volume, err := blockAPI.CreateVolume(req, scw.WithContext(ctx)) | ||
if err != nil { | ||
state.Put("error", err) | ||
ui.Error(err.Error()) | ||
return multistep.ActionHalt | ||
} | ||
|
||
volumeTemplates = append(volumeTemplates, &instance.VolumeServerTemplate{ | ||
ID: &volume.ID, | ||
VolumeType: instance.VolumeVolumeTypeSbsVolume, | ||
}) | ||
} | ||
|
||
state.Put(StateKeyCreatedVolumes, volumeTemplates) | ||
|
||
return multistep.ActionContinue | ||
} | ||
|
||
func (s *stepCreateVolume) Cleanup(state multistep.StateBag) { | ||
ui := state.Get("ui").(packersdk.Ui) | ||
c := state.Get("config").(*Config) | ||
|
||
if !c.RemoveVolume { | ||
return | ||
} | ||
|
||
blockAPI := block.NewAPI(state.Get("client").(*scw.Client)) | ||
|
||
_, serverWasCreated := state.GetOk("server_id") | ||
createdVolumesI, createdVolumesExists := state.GetOk("created_volumes") | ||
if !serverWasCreated && createdVolumesExists { | ||
// If server was not created, we need to clean up manually created volumes | ||
createdVolumes := createdVolumesI.([]*instance.VolumeServerTemplate) | ||
for _, volume := range createdVolumes { | ||
err := blockAPI.DeleteVolume(&block.DeleteVolumeRequest{ | ||
Zone: scw.Zone(c.Zone), | ||
VolumeID: *volume.ID, | ||
}) | ||
if err != nil { | ||
ui.Error(fmt.Sprintf("failed to cleanup block volume %s: %s", *volume.ID, err)) | ||
} | ||
} | ||
} | ||
|
||
if _, ok := state.GetOk("snapshots"); !ok { | ||
// volume will be detached from server only after snapshotting ... so we don't | ||
// need to remove volume before snapshot step. | ||
return | ||
} | ||
|
||
instanceAPI := instance.NewAPI(state.Get("client").(*scw.Client)) | ||
|
||
ui.Say("Removing Volumes ...") | ||
|
||
volumes := state.Get("volumes").([]*instance.VolumeServer) | ||
for _, volume := range volumes { | ||
err := instanceAPI.DeleteVolume(&instance.DeleteVolumeRequest{ | ||
VolumeID: volume.ID, | ||
Zone: scw.Zone(c.Zone), | ||
}) | ||
if err != nil && !httperrors.Is404(err) { | ||
err := fmt.Errorf("error removing block volume %s: %s", volume.ID, err) | ||
state.Put("error", err) | ||
ui.Error(fmt.Sprintf("Error removing block volume %s: %s. (Ignored)", volume.ID, err)) | ||
} | ||
if err == nil { | ||
continue | ||
} | ||
|
||
err = blockAPI.DeleteVolume(&block.DeleteVolumeRequest{ | ||
Zone: scw.Zone(c.Zone), | ||
VolumeID: volume.ID, | ||
}) | ||
if err != nil { | ||
err := fmt.Errorf("error removing block volume %s: %s", volume.ID, err) | ||
state.Put("error", err) | ||
ui.Error(fmt.Sprintf("Error removing block volume %s: %s. (Ignored)", volume.ID, err)) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
docs-partials/builder/scaleway/ConfigBlockVolume-not-required.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!-- Code generated from the comments of the ConfigBlockVolume struct in builder/scaleway/config.go; DO NOT EDIT MANUALLY --> | ||
|
||
- `name` (string) - The name of the created volume | ||
|
||
- `snapshot_id` (string) - ID of the snapshot to create the volume from | ||
|
||
- `size_in_gb` (uint64) - Size of the newly created volume | ||
|
||
- `iops` (\*uint32) - IOPS is the number of requested iops for the server's volume. This will not impact created snapshot. | ||
|
||
<!-- End of code generated from the comments of the ConfigBlockVolume struct in builder/scaleway/config.go; --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.