generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support skipping image creation
Adds the config option `skip_create_image` to the builders to support skipping image creation. Fixes: #180
- Loading branch information
Showing
17 changed files
with
207 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,33 @@ | ||
//go:generate packer-sdc struct-markdown | ||
|
||
package common | ||
|
||
import ( | ||
"github.com/hashicorp/packer-plugin-sdk/multistep" | ||
) | ||
|
||
const ( | ||
SkippingImageCreation = "Skipping image creation..." | ||
) | ||
|
||
type Config struct { | ||
// Skip creating the image. | ||
// Useful for setting to `true` during a build test stage. | ||
// Defaults to `false`. | ||
SkipCreateImage bool `mapstructure:"skip_create_image" required:"false"` | ||
} | ||
|
||
// CaptureSteps returns the steps unless `SkipCreateImage` is `true`. In that case it returns | ||
// a step that to inform the user that image capture is being skipped. | ||
func (config Config) CaptureSteps(say func(string), steps ...multistep.Step) []multistep.Step { | ||
if !config.SkipCreateImage { | ||
return steps | ||
} | ||
|
||
return []multistep.Step{ | ||
&StepNotify{ | ||
message: SkippingImageCreation, | ||
say: say, | ||
}, | ||
} | ||
} |
This file contains 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,44 @@ | ||
package common_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/packer-plugin-sdk/multistep" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/hashicorp/packer-plugin-azure/builder/azure/common" | ||
) | ||
|
||
func TestSkipCreateImage(t *testing.T) { | ||
var said []string | ||
|
||
say := func(what string) { | ||
said = append(said, what) | ||
} | ||
|
||
config := common.Config{} | ||
message := "Capture Image" | ||
|
||
steps := config.CaptureSteps(say, common.NewStepNotify(message, say)) | ||
state := &multistep.BasicStateBag{} | ||
|
||
ctx := context.Background() | ||
|
||
for _, step := range steps { | ||
step.Run(ctx, state) | ||
} | ||
|
||
assert.Equal(t, said, []string{message}) | ||
|
||
said = nil | ||
config.SkipCreateImage = true | ||
|
||
steps = config.CaptureSteps(say, common.NewStepNotify(message, say)) | ||
|
||
for _, step := range steps { | ||
step.Run(ctx, state) | ||
} | ||
|
||
assert.Equal(t, said, []string{common.SkippingImageCreation}) | ||
} |
This file contains 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,31 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/packer-plugin-sdk/multistep" | ||
) | ||
|
||
type StepNotify struct { | ||
message string | ||
say func(string) | ||
} | ||
|
||
func NewStepNotify(message string, say func(string)) *StepNotify { | ||
return &StepNotify{ | ||
message: message, | ||
say: say, | ||
} | ||
} | ||
|
||
func (step *StepNotify) Run( | ||
ctx context.Context, | ||
state multistep.StateBag, | ||
) multistep.StepAction { | ||
step.say(step.message) | ||
return multistep.ActionContinue | ||
} | ||
|
||
func (step *StepNotify) Cleanup(state multistep.StateBag) {} | ||
|
||
var _ multistep.Step = (*StepNotify)(nil) |
This file contains 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,30 @@ | ||
package common_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/packer-plugin-sdk/multistep" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/hashicorp/packer-plugin-azure/builder/azure/common" | ||
) | ||
|
||
func TestStepNotify(t *testing.T) { | ||
var said []string | ||
|
||
say := func(what string) { | ||
said = append(said, what) | ||
} | ||
|
||
message := "Notify Step" | ||
|
||
step := common.NewStepNotify(message, say) | ||
state := &multistep.BasicStateBag{} | ||
|
||
ctx := context.Background() | ||
|
||
step.Run(ctx, state) | ||
|
||
assert.Equal(t, said, []string{message}) | ||
} |
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,7 @@ | ||
<!-- Code generated from the comments of the Config struct in builder/azure/common/config.go; DO NOT EDIT MANUALLY --> | ||
|
||
- `skip_create_image` (bool) - Skip creating the image. | ||
Useful for setting to `true` during a build test stage. | ||
Defaults to `false`. | ||
|
||
<!-- End of code generated from the comments of the Config struct in builder/azure/common/config.go; --> |
This file contains 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 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 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