Skip to content

Commit 9301777

Browse files
Merge pull request #44 from gridscale/fix/cannot-use-private-template-in-boot-storage
Fix cannot use private template in boot storage
2 parents 0cf1de8 + 53e8006 commit 9301777

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

builder/gridscale/builder.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
8585
ui: ui,
8686
},
8787
&stepCreateBootStorage{
88-
client: client,
89-
config: &b.config,
90-
ui: ui,
88+
client: client,
89+
templateClient: client,
90+
config: &b.config,
91+
ui: ui,
9192
},
9293
&stepLinkServerBootStorage{
9394
client: client,

builder/gridscale/step_create_boot_storage.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import (
1111
)
1212

1313
type stepCreateBootStorage struct {
14-
client gsclient.StorageOperator
15-
config *Config
16-
ui packer.Ui
14+
client gsclient.StorageOperator
15+
templateClient gsclient.TemplateOperator
16+
config *Config
17+
ui packer.Ui
1718
}
1819

1920
func (s *stepCreateBootStorage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
@@ -39,21 +40,31 @@ func (s *stepCreateBootStorage) Run(ctx context.Context, state multistep.StateBa
3940
state.Put("error", err)
4041
return multistep.ActionHalt
4142
}
42-
if sshKeyUUID == "" && c.Comm.SSHPassword == "" {
43-
ui.Error("No SSH key UUID and no SSH password are provided.")
44-
state.Put("error", "No SSH key UUID and no SSH password are provided.")
45-
return multistep.ActionHalt
46-
}
4743
storage_template := gsclient.StorageTemplate{
48-
Hostname: c.Hostname,
4944
TemplateUUID: c.BaseTemplateUUID,
5045
}
51-
if sshKeyUUID != "" {
52-
storage_template.Sshkeys = []string{sshKeyUUID}
46+
template, err := s.templateClient.GetTemplate(context.Background(), c.BaseTemplateUUID)
47+
if err != nil {
48+
ui.Error(fmt.Sprintf("Error getting template: %s", err))
49+
state.Put("error", err)
50+
return multistep.ActionHalt
5351
}
54-
if c.Comm.SSHPassword != "" {
55-
storage_template.Password = c.Comm.SSHPassword
56-
storage_template.PasswordType = gsclient.PlainPasswordType
52+
// Check if template is public, if so, either ssh key or password and hostname is required.
53+
// If template is private, ssh key or password will be ignored.
54+
if !template.Properties.Private {
55+
storage_template.Hostname = c.Hostname
56+
if sshKeyUUID == "" && c.Comm.SSHPassword == "" {
57+
ui.Error("No SSH key UUID and no SSH password are provided.")
58+
state.Put("error", "No SSH key UUID and no SSH password are provided.")
59+
return multistep.ActionHalt
60+
}
61+
if sshKeyUUID != "" {
62+
storage_template.Sshkeys = []string{sshKeyUUID}
63+
}
64+
if c.Comm.SSHPassword != "" {
65+
storage_template.Password = c.Comm.SSHPassword
66+
storage_template.PasswordType = gsclient.PlainPasswordType
67+
}
5768
}
5869
storageCreateReq.Template = &storage_template
5970
}

builder/gridscale/step_create_boot_storage_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ func Test_stepCreateBootStorage_Cleanup(t *testing.T) {
110110

111111
func Test_stepCreateBootStorage_Run(t *testing.T) {
112112
type fields struct {
113-
client gsclient.StorageOperator
114-
config *Config
115-
ui packer.Ui
113+
client gsclient.StorageOperator
114+
templateClient gsclient.TemplateOperator
115+
config *Config
116+
ui packer.Ui
116117
}
117118
type args struct {
118119
ctx context.Context
@@ -128,7 +129,8 @@ func Test_stepCreateBootStorage_Run(t *testing.T) {
128129
{
129130
name: "success",
130131
fields: fields{
131-
client: StorageOperatorMock{},
132+
client: StorageOperatorMock{},
133+
templateClient: TemplateOperatorMock{},
132134
config: produceTestConfig(map[string]interface{}{
133135
"server_name": "success",
134136
}),
@@ -145,7 +147,8 @@ func Test_stepCreateBootStorage_Run(t *testing.T) {
145147
{
146148
name: "API call fail",
147149
fields: fields{
148-
client: StorageOperatorMock{},
150+
client: StorageOperatorMock{},
151+
templateClient: TemplateOperatorMock{},
149152
config: produceTestConfig(map[string]interface{}{
150153
"server_name": "fail",
151154
}),
@@ -162,7 +165,8 @@ func Test_stepCreateBootStorage_Run(t *testing.T) {
162165
{
163166
name: "No SSH key UUID detected",
164167
fields: fields{
165-
client: StorageOperatorMock{},
168+
client: StorageOperatorMock{},
169+
templateClient: TemplateOperatorMock{},
166170
config: produceTestConfig(map[string]interface{}{
167171
"server_name": "success",
168172
}),
@@ -179,7 +183,8 @@ func Test_stepCreateBootStorage_Run(t *testing.T) {
179183
{
180184
name: "cannot convert ssh_key_uuid to string",
181185
fields: fields{
182-
client: StorageOperatorMock{},
186+
client: StorageOperatorMock{},
187+
templateClient: TemplateOperatorMock{},
183188
config: produceTestConfig(map[string]interface{}{
184189
"server_name": "success",
185190
}),
@@ -195,9 +200,10 @@ func Test_stepCreateBootStorage_Run(t *testing.T) {
195200
for _, tt := range tests {
196201
t.Run(tt.name, func(t *testing.T) {
197202
s := &stepCreateBootStorage{
198-
client: tt.fields.client,
199-
config: tt.fields.config,
200-
ui: tt.fields.ui,
203+
client: tt.fields.client,
204+
templateClient: tt.fields.templateClient,
205+
config: tt.fields.config,
206+
ui: tt.fields.ui,
201207
}
202208
if got := s.Run(tt.args.ctx, tt.args.state); got != tt.want {
203209
t.Errorf("stepCreateBootStorage_Run() = %v, want %v", got, tt.want)

0 commit comments

Comments
 (0)