2018-01-11 04:57:53 -05:00
|
|
|
package ncloud
|
|
|
|
|
|
|
|
import (
|
2018-01-29 08:41:22 -05:00
|
|
|
"context"
|
2018-01-11 04:57:53 -05:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
|
2018-01-29 08:41:22 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-11 04:57:53 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LoginKey struct {
|
|
|
|
KeyName string
|
|
|
|
PrivateKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
type StepCreateLoginKey struct {
|
2020-02-03 08:55:01 -05:00
|
|
|
Conn *NcloudAPIClient
|
2018-01-11 04:57:53 -05:00
|
|
|
CreateLoginKey func() (*LoginKey, error)
|
|
|
|
Say func(message string)
|
|
|
|
Error func(e error)
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
func NewStepCreateLoginKey(conn *NcloudAPIClient, ui packer.Ui) *StepCreateLoginKey {
|
2018-01-11 04:57:53 -05:00
|
|
|
var step = &StepCreateLoginKey{
|
|
|
|
Conn: conn,
|
|
|
|
Say: func(message string) { ui.Say(message) },
|
|
|
|
Error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
}
|
|
|
|
|
|
|
|
step.CreateLoginKey = step.createLoginKey
|
|
|
|
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateLoginKey) createLoginKey() (*LoginKey, error) {
|
2020-02-03 08:55:01 -05:00
|
|
|
keyName := fmt.Sprintf("packer-%d", time.Now().Unix())
|
|
|
|
reqParams := &server.CreateLoginKeyRequest{KeyName: &keyName}
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
privateKey, err := s.Conn.server.V2Api.CreateLoginKey(reqParams)
|
2018-01-11 04:57:53 -05:00
|
|
|
if err != nil {
|
2018-01-11 20:12:47 -05:00
|
|
|
return nil, err
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
return &LoginKey{keyName, *privateKey.PrivateKey}, nil
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCreateLoginKey) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-01-11 04:57:53 -05:00
|
|
|
s.Say("Create Login Key")
|
|
|
|
|
|
|
|
loginKey, err := s.CreateLoginKey()
|
|
|
|
if err == nil {
|
|
|
|
state.Put("LoginKey", loginKey)
|
|
|
|
s.Say(fmt.Sprintf("Login Key[%s] is created", loginKey.KeyName))
|
|
|
|
}
|
|
|
|
|
|
|
|
return processStepResult(err, s.Error, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateLoginKey) Cleanup(state multistep.StateBag) {
|
|
|
|
if loginKey, ok := state.GetOk("LoginKey"); ok {
|
|
|
|
s.Say("Clean up login key")
|
2020-02-03 08:55:01 -05:00
|
|
|
reqParams := &server.DeleteLoginKeyRequest{KeyName: &loginKey.(*LoginKey).KeyName}
|
|
|
|
s.Conn.server.V2Api.DeleteLoginKey(reqParams)
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
}
|