generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: option to remove network adapters
Adds the option to remove all network adapters at the end of the image build. Ref: #6 Signed-off-by: Ryan Johnson <[email protected]>
- Loading branch information
1 parent
de0c305
commit f224b75
Showing
13 changed files
with
244 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
//go:generate packer-sdc struct-markdown | ||
//go:generate packer-sdc mapstructure-to-hcl2 -type RemoveNetworkAdapterConfig | ||
|
||
package common | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/packer-plugin-sdk/multistep" | ||
packersdk "github.com/hashicorp/packer-plugin-sdk/packer" | ||
"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/driver" | ||
) | ||
|
||
type RemoveNetworkAdapterConfig struct { | ||
// Remove all network adapters from template. Defaults to `false`. | ||
RemoveNetworkAdapter bool `mapstructure:"remove_network_adapter"` | ||
} | ||
|
||
type StepRemoveNetworkAdapter struct { | ||
Config *RemoveNetworkAdapterConfig | ||
} | ||
|
||
func (s *StepRemoveNetworkAdapter) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { | ||
ui := state.Get("ui").(packersdk.Ui) | ||
vm := state.Get("vm").(driver.VirtualMachine) | ||
|
||
if s.Config.RemoveNetworkAdapter == true { | ||
ui.Say("Removing network adapters...") | ||
err := vm.RemoveNetworkAdapters() | ||
if err != nil { | ||
state.Put("error", fmt.Errorf("error removing network: %v", err)) | ||
return multistep.ActionHalt | ||
} | ||
} | ||
|
||
return multistep.ActionContinue | ||
} | ||
|
||
func (s *StepRemoveNetworkAdapter) Cleanup(state multistep.StateBag) { | ||
// no cleanup | ||
} |
31 changes: 31 additions & 0 deletions
31
builder/vsphere/common/step_remove_network_adapter.hcl2spec.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
builder/vsphere/common/step_remove_network_adapter_test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package common | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/hashicorp/packer-plugin-sdk/multistep" | ||
"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/driver" | ||
) | ||
|
||
func TestStepRemoveNetworkAdapter_Run(t *testing.T) { | ||
tc := []struct { | ||
name string | ||
step *StepRemoveNetworkAdapter | ||
expectedAction multistep.StepAction | ||
vmMock *driver.VirtualMachineMock | ||
expectedVmMock *driver.VirtualMachineMock | ||
errMessage string | ||
}{ | ||
{ | ||
name: "Successfully remove network adapter", | ||
step: &StepRemoveNetworkAdapter{ | ||
Config: &RemoveNetworkAdapterConfig{ | ||
RemoveNetworkAdapter: true, | ||
}, | ||
}, | ||
expectedAction: multistep.ActionContinue, | ||
vmMock: &driver.VirtualMachineMock{ | ||
RemoveNetworkAdaptersCalled: true, | ||
}, | ||
expectedVmMock: &driver.VirtualMachineMock{ | ||
RemoveNetworkAdaptersCalled: true, | ||
}, | ||
}, | ||
{ | ||
name: "Fail to remove network adapter", | ||
step: &StepRemoveNetworkAdapter{ | ||
Config: &RemoveNetworkAdapterConfig{ | ||
RemoveNetworkAdapter: true, | ||
}, | ||
}, | ||
expectedAction: multistep.ActionHalt, | ||
vmMock: &driver.VirtualMachineMock{ | ||
RemoveNetworkAdaptersCalled: true, | ||
RemoveNetworkAdaptersErr: fmt.Errorf("failed to remove network adapter"), | ||
}, | ||
expectedVmMock: &driver.VirtualMachineMock{ | ||
RemoveNetworkAdaptersCalled: true, | ||
}, | ||
errMessage: "error removing network: failed to remove network adapter", | ||
}, | ||
} | ||
|
||
for _, c := range tc { | ||
t.Run(c.name, func(t *testing.T) { | ||
state := basicStateBag(nil) | ||
state.Put("vm", c.vmMock) | ||
|
||
if action := c.step.Run(context.TODO(), state); action != c.expectedAction { | ||
t.Fatalf("unexpected action %v", action) | ||
} | ||
err, ok := state.Get("error").(error) | ||
if ok { | ||
if err.Error() != c.errMessage { | ||
t.Fatalf("unexpected error %s", err.Error()) | ||
} | ||
} else if c.errMessage != "" { | ||
t.Fatalf("expected to fail with %s but it didn't", c.errMessage) | ||
} | ||
|
||
if diff := cmp.Diff(c.vmMock, c.expectedVmMock, | ||
cmpopts.IgnoreInterfaces(struct{ error }{})); diff != "" { | ||
t.Fatalf("unexpected VirtualMachine calls: %s", diff) | ||
} | ||
}) | ||
} | ||
} |
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
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.
5 changes: 5 additions & 0 deletions
5
docs-partials/builder/vsphere/common/RemoveNetworkAdapterConfig-not-required.mdx
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,5 @@ | ||
<!-- Code generated from the comments of the RemoveNetworkAdapterConfig struct in builder/vsphere/common/step_remove_network_adapter.go; DO NOT EDIT MANUALLY --> | ||
|
||
- `remove_network_adapter` (bool) - Remove all network adapters from template. Defaults to `false`. | ||
|
||
<!-- End of code generated from the comments of the RemoveNetworkAdapterConfig struct in builder/vsphere/common/step_remove_network_adapter.go; --> |
5 changes: 5 additions & 0 deletions
5
docs-partials/builder/vsphere/common/RemoveNetworkConfig-not-required.mdx
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,5 @@ | ||
<!-- Code generated from the comments of the RemoveNetworkConfig struct in builder/vsphere/common/step_remove_network.go; DO NOT EDIT MANUALLY --> | ||
|
||
- `remove_network_adapter` (bool) - Remove all network adapters from template. Defaults to `false`. | ||
|
||
<!-- End of code generated from the comments of the RemoveNetworkConfig struct in builder/vsphere/common/step_remove_network.go; --> |