Skip to content

Commit 24c95fb

Browse files
authored
refactor(plugin): layout and tooling (#20)
- refactor 'internal' folder - update Makefile targets - code generation with packer-sdc - clean up Config type by removing non-config fields - update CHANGELOG - added version package (versioning is required by validation tooling) - fix network_interfaces config
1 parent 99335b6 commit 24c95fb

File tree

15 files changed

+303
-178
lines changed

15 files changed

+303
-178
lines changed

CHANGELOG.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,40 @@
1-
# Change log
1+
# Changelog
22

3-
## 1.0.0
3+
All notable changes to this project will be documented in this file.
4+
See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)
45

5-
* Upgrade to Packer 1.7.0
6-
* Copy codebase from https://github.com/UpCloudLtd/upcloud-packer
6+
## [Unreleased]
7+
8+
### Added
9+
- add new template name param
10+
11+
### Changed
12+
- update README file
13+
- update acceptance test to embed HCL2 configs
14+
15+
### Fixed
16+
- fix network interface config
17+
18+
## [1.2.0] - 2021-06-17
19+
20+
### Fixed
21+
22+
- fix template prefix usage
23+
24+
## [1.1.0] 2021-05-30
25+
26+
### Changed
27+
- bump go version to 1.6 to enable darwin/arm build
28+
- update dependencies
29+
- update intergration tests
30+
31+
## [1.0.0] 2021-02-19
32+
33+
### Changed
34+
- Upgrade to Packer 1.7.0
35+
- Copy codebase from https://github.com/UpCloudLtd/upcloud-packer
36+
37+
[Unreleased]: https://github.com/UpCloudLtd/packer-plugin-upcloud/compare/v1.2.0...HEAD
38+
[1.2.0]: https://github.com/UpCloudLtd/packer-plugin-upcloud/compare/v1.1.0...v1.2.0
39+
[1.1.0]: https://github.com/UpCloudLtd/packer-plugin-upcloud/compare/v1.0.0...v1.1.0
40+
[1.0.0]: https://github.com/UpCloudLtd/packer-plugin-upcloud/releases/tag/v1.0.0

Makefile

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
1+
BINARY=packer-plugin-upcloud
2+
HASHICORP_PACKER_PLUGIN_SDK_VERSION?=$(shell go list -m github.com/hashicorp/packer-plugin-sdk | cut -d " " -f2)
3+
COUNT?=1
4+
TEST?=$(shell go list ./...)
5+
6+
ifeq (,$(shell go env GOBIN))
7+
GOBIN=$(shell go env GOPATH)/bin
8+
else
9+
GOBIN=$(shell go env GOBIN)
10+
endif
11+
12+
PACKER_SDC=$(GOBIN)/packer-sdc
13+
114
default: build
215

316
test:
4-
go test -v ./...
17+
@go test -race -count $(COUNT) $(TEST) -timeout=3m
518

619
test_integration: build
7-
cp ./packer-plugin-upcloud builder/upcloud/
20+
cp $(BINARY) builder/upcloud/
821
PACKER_ACC=1 go test -count 1 -v ./... -timeout=120m
922

1023
lint:
1124
go vet .
1225
golint .
1326

1427
build:
15-
go build -v
28+
go build -v -o $(BINARY)
1629

1730
install: build
18-
mkdir -p ~/.packer.d/plugins
19-
install ./packer-plugin-upcloud ~/.packer.d/plugins/
31+
@mkdir -p ~/.packer.d/plugins
32+
install $(BINARY) ~/.packer.d/plugins/
33+
34+
install-packer-sdc: ## Install packer sofware development command
35+
go install github.com/hashicorp/packer-plugin-sdk/cmd/packer-sdc@$(HASHICORP_PACKER_PLUGIN_SDK_VERSION)
36+
37+
ci-release-docs: install-packer-sdc
38+
@$(PACKER_SDC) renderdocs -src docs -partials docs-partials/ -dst docs/
39+
@/bin/sh -c "[ -d docs ] && zip -r docs.zip docs/"
40+
41+
plugin-check: install-packer-sdc build
42+
$(PACKER_SDC) plugin-check $(BINARY)
43+
44+
generate: install-packer-sdc
45+
@PATH=$(PATH):$(GOBIN) go generate ./...
46+
packer-sdc renderdocs -src ./docs -dst ./.docs -partials ./docs-partials
2047

2148
.PHONY: default test test_integration lint build install

builder/upcloud/artifact.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"fmt"
55
"strings"
66

7+
"github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
78
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
8-
internal "github.com/UpCloudLtd/packer-plugin-upcloud/internal"
99
)
1010

1111
// packersdk.Artifact implementation
1212
type Artifact struct {
1313
config *Config
14-
driver internal.Driver
14+
driver driver.Driver
1515
Templates []*upcloud.Storage
1616

1717
// StateData should store data such as GeneratedData

builder/upcloud/builder.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"context"
55
"fmt"
66

7-
internal "github.com/UpCloudLtd/packer-plugin-upcloud/internal"
7+
"github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
88
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
99
"github.com/hashicorp/hcl/v2/hcldec"
1010
"github.com/hashicorp/packer-plugin-sdk/communicator"
1111
"github.com/hashicorp/packer-plugin-sdk/multistep"
1212
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
13-
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
13+
"github.com/hashicorp/packer-plugin-sdk/packer"
1414
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
1515
)
1616

@@ -19,7 +19,7 @@ const BuilderId = "upcloud.builder"
1919
type Builder struct {
2020
config Config
2121
runner multistep.Runner
22-
driver internal.Driver
22+
driver driver.Driver
2323
}
2424

2525
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
@@ -41,9 +41,9 @@ func (b *Builder) Prepare(raws ...interface{}) (generatedVars []string, warnings
4141
return buildGeneratedData, nil, nil
4242
}
4343

44-
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
44+
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
4545
// Setup the state bag and initial state for the steps
46-
b.driver = internal.NewDriver(&internal.DriverConfig{
46+
b.driver = driver.NewDriver(&driver.DriverConfig{
4747
Username: b.config.Username,
4848
Password: b.config.Password,
4949
Timeout: b.config.Timeout,
@@ -70,7 +70,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
7070
},
7171
&communicator.StepConnect{
7272
Config: &b.config.Comm,
73-
Host: internal.SshHostCallback,
73+
Host: sshHostCallback,
7474
SSHConfig: b.config.Comm.SSHConfigFunc(),
7575
},
7676
&commonsteps.StepProvision{},

builder/upcloud/builder_acc_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"strings"
1212
"testing"
1313

14-
internal "github.com/UpCloudLtd/packer-plugin-upcloud/internal"
14+
"github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
1515
"github.com/hashicorp/packer-plugin-sdk/acctest"
1616
)
1717

@@ -202,15 +202,15 @@ func teardown(testName string) func() error {
202202
return err
203203
}
204204

205-
driver := internal.NewDriver(&internal.DriverConfig{
205+
drv := driver.NewDriver(&driver.DriverConfig{
206206
Username: os.Getenv("UPCLOUD_API_USER"),
207207
Password: os.Getenv("UPCLOUD_API_PASSWORD"),
208208
Timeout: DefaultTimeout,
209209
})
210210

211211
for _, u := range uuids {
212212
fmt.Printf("Cleaning up created templates: %s\n", u)
213-
if err := driver.DeleteTemplate(u); err != nil {
213+
if err := drv.DeleteTemplate(u); err != nil {
214214
return err
215215
}
216216
}

builder/upcloud/config.go

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1+
//go:generate packer-sdc mapstructure-to-hcl2 -type Config,NetworkInterface,IPAddress
12
package upcloud
23

34
import (
45
"errors"
5-
"fmt"
6-
"io/ioutil"
76
"os"
87
"time"
98

10-
internal "github.com/UpCloudLtd/packer-plugin-upcloud/internal"
119
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
1210
"github.com/UpCloudLtd/upcloud-go-api/upcloud/request"
1311
"github.com/hashicorp/packer-plugin-sdk/common"
@@ -37,6 +35,18 @@ var (
3735
}
3836
)
3937

38+
// for config type convertion
39+
type NetworkInterface struct {
40+
IPAddresses []IPAddress `mapstructure:"ip_addresses"`
41+
Type string `mapstructure:"type"`
42+
Network string `mapstructure:"network,omitempty"`
43+
}
44+
45+
type IPAddress struct {
46+
Family string `mapstructure:"family"`
47+
Address string `mapstructure:"address,omitempty"`
48+
}
49+
4050
type Config struct {
4151
common.PackerConfig `mapstructure:",squash"`
4252
Comm communicator.Config `mapstructure:",squash"`
@@ -55,13 +65,10 @@ type Config struct {
5565
Timeout time.Duration `mapstructure:"state_timeout_duration"`
5666
CloneZones []string `mapstructure:"clone_zones"`
5767

58-
RawNetworking []internal.NetworkInterface `mapstructure:"network_interfaces"`
59-
Networking []request.CreateServerInterface
68+
NetworkInterfaces []NetworkInterface `mapstructure:"network_interfaces"`
6069

6170
SSHPrivateKeyPath string `mapstructure:"ssh_private_key_path"`
6271
SSHPublicKeyPath string `mapstructure:"ssh_public_key_path"`
63-
SSHPrivateKey []byte
64-
SSHPublicKey []byte
6572

6673
ctx interpolate.Context
6774
}
@@ -79,7 +86,7 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
7986
c.setEnv()
8087

8188
// defaults
82-
if c.TemplatePrefix == "" && len(c.TemplateName) == 0{
89+
if c.TemplatePrefix == "" && len(c.TemplateName) == 0 {
8390
c.TemplatePrefix = DefaultTemplatePrefix
8491
}
8592

@@ -95,12 +102,6 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
95102
c.Comm.SSHUsername = DefaultSSHUsername
96103
}
97104

98-
if len(c.RawNetworking) == 0 {
99-
c.Networking = DefaultNetworking
100-
} else {
101-
c.Networking = internal.ConvertNetworkTypes(c.RawNetworking)
102-
}
103-
104105
// validate
105106
var errs *packer.MultiError
106107
if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
@@ -131,22 +132,6 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
131132
)
132133
}
133134

134-
if c.SSHPrivateKeyPath != "" {
135-
c.SSHPrivateKey, err = ioutil.ReadFile(c.SSHPrivateKeyPath)
136-
if err != nil {
137-
errs = packer.MultiErrorAppend(
138-
errs, fmt.Errorf("Failed to read private key: %s", err))
139-
}
140-
}
141-
142-
if c.SSHPublicKeyPath != "" {
143-
c.SSHPublicKey, err = ioutil.ReadFile(c.SSHPublicKeyPath)
144-
if err != nil {
145-
errs = packer.MultiErrorAppend(
146-
errs, fmt.Errorf("Failed to read public key: %s", err))
147-
}
148-
}
149-
150135
if len(c.TemplatePrefix) > 40 {
151136
errs = packer.MultiErrorAppend(
152137
errs, errors.New("'template_prefix' must be 0-40 characters"),

0 commit comments

Comments
 (0)