Skip to content

Commit

Permalink
Merge pull request #44 from gridscale/fix/cannot-use-private-template…
Browse files Browse the repository at this point in the history
…-in-boot-storage

Fix cannot use private template in boot storage
  • Loading branch information
nvthongswansea authored Jan 15, 2025
2 parents 0cf1de8 + 53e8006 commit 9301777
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
7 changes: 4 additions & 3 deletions builder/gridscale/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
ui: ui,
},
&stepCreateBootStorage{
client: client,
config: &b.config,
ui: ui,
client: client,
templateClient: client,
config: &b.config,
ui: ui,
},
&stepLinkServerBootStorage{
client: client,
Expand Down
39 changes: 25 additions & 14 deletions builder/gridscale/step_create_boot_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
)

type stepCreateBootStorage struct {
client gsclient.StorageOperator
config *Config
ui packer.Ui
client gsclient.StorageOperator
templateClient gsclient.TemplateOperator
config *Config
ui packer.Ui
}

func (s *stepCreateBootStorage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
Expand All @@ -39,21 +40,31 @@ func (s *stepCreateBootStorage) Run(ctx context.Context, state multistep.StateBa
state.Put("error", err)
return multistep.ActionHalt
}
if sshKeyUUID == "" && c.Comm.SSHPassword == "" {
ui.Error("No SSH key UUID and no SSH password are provided.")
state.Put("error", "No SSH key UUID and no SSH password are provided.")
return multistep.ActionHalt
}
storage_template := gsclient.StorageTemplate{
Hostname: c.Hostname,
TemplateUUID: c.BaseTemplateUUID,
}
if sshKeyUUID != "" {
storage_template.Sshkeys = []string{sshKeyUUID}
template, err := s.templateClient.GetTemplate(context.Background(), c.BaseTemplateUUID)
if err != nil {
ui.Error(fmt.Sprintf("Error getting template: %s", err))
state.Put("error", err)
return multistep.ActionHalt
}
if c.Comm.SSHPassword != "" {
storage_template.Password = c.Comm.SSHPassword
storage_template.PasswordType = gsclient.PlainPasswordType
// Check if template is public, if so, either ssh key or password and hostname is required.
// If template is private, ssh key or password will be ignored.
if !template.Properties.Private {
storage_template.Hostname = c.Hostname
if sshKeyUUID == "" && c.Comm.SSHPassword == "" {
ui.Error("No SSH key UUID and no SSH password are provided.")
state.Put("error", "No SSH key UUID and no SSH password are provided.")
return multistep.ActionHalt
}
if sshKeyUUID != "" {
storage_template.Sshkeys = []string{sshKeyUUID}
}
if c.Comm.SSHPassword != "" {
storage_template.Password = c.Comm.SSHPassword
storage_template.PasswordType = gsclient.PlainPasswordType
}
}
storageCreateReq.Template = &storage_template
}
Expand Down
26 changes: 16 additions & 10 deletions builder/gridscale/step_create_boot_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ func Test_stepCreateBootStorage_Cleanup(t *testing.T) {

func Test_stepCreateBootStorage_Run(t *testing.T) {
type fields struct {
client gsclient.StorageOperator
config *Config
ui packer.Ui
client gsclient.StorageOperator
templateClient gsclient.TemplateOperator
config *Config
ui packer.Ui
}
type args struct {
ctx context.Context
Expand All @@ -128,7 +129,8 @@ func Test_stepCreateBootStorage_Run(t *testing.T) {
{
name: "success",
fields: fields{
client: StorageOperatorMock{},
client: StorageOperatorMock{},
templateClient: TemplateOperatorMock{},
config: produceTestConfig(map[string]interface{}{
"server_name": "success",
}),
Expand All @@ -145,7 +147,8 @@ func Test_stepCreateBootStorage_Run(t *testing.T) {
{
name: "API call fail",
fields: fields{
client: StorageOperatorMock{},
client: StorageOperatorMock{},
templateClient: TemplateOperatorMock{},
config: produceTestConfig(map[string]interface{}{
"server_name": "fail",
}),
Expand All @@ -162,7 +165,8 @@ func Test_stepCreateBootStorage_Run(t *testing.T) {
{
name: "No SSH key UUID detected",
fields: fields{
client: StorageOperatorMock{},
client: StorageOperatorMock{},
templateClient: TemplateOperatorMock{},
config: produceTestConfig(map[string]interface{}{
"server_name": "success",
}),
Expand All @@ -179,7 +183,8 @@ func Test_stepCreateBootStorage_Run(t *testing.T) {
{
name: "cannot convert ssh_key_uuid to string",
fields: fields{
client: StorageOperatorMock{},
client: StorageOperatorMock{},
templateClient: TemplateOperatorMock{},
config: produceTestConfig(map[string]interface{}{
"server_name": "success",
}),
Expand All @@ -195,9 +200,10 @@ func Test_stepCreateBootStorage_Run(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &stepCreateBootStorage{
client: tt.fields.client,
config: tt.fields.config,
ui: tt.fields.ui,
client: tt.fields.client,
templateClient: tt.fields.templateClient,
config: tt.fields.config,
ui: tt.fields.ui,
}
if got := s.Run(tt.args.ctx, tt.args.state); got != tt.want {
t.Errorf("stepCreateBootStorage_Run() = %v, want %v", got, tt.want)
Expand Down

0 comments on commit 9301777

Please sign in to comment.