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"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/ncloud"
|
|
|
|
"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 StepCreatePublicIPInstance struct {
|
2020-02-03 08:55:01 -05:00
|
|
|
Conn *NcloudAPIClient
|
|
|
|
CreatePublicIPInstance func(serverInstanceNo string) (*server.PublicIpInstance, error)
|
2018-01-11 04:57:53 -05:00
|
|
|
WaiterAssociatePublicIPToServerInstance func(serverInstanceNo string, publicIP string) error
|
|
|
|
Say func(message string)
|
|
|
|
Error func(e error)
|
|
|
|
Config *Config
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
func NewStepCreatePublicIPInstance(conn *NcloudAPIClient, ui packer.Ui, config *Config) *StepCreatePublicIPInstance {
|
2018-01-11 04:57:53 -05:00
|
|
|
var step = &StepCreatePublicIPInstance{
|
|
|
|
Conn: conn,
|
|
|
|
Say: func(message string) { ui.Say(message) },
|
|
|
|
Error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
Config: config,
|
|
|
|
}
|
|
|
|
|
|
|
|
step.CreatePublicIPInstance = step.createPublicIPInstance
|
|
|
|
step.WaiterAssociatePublicIPToServerInstance = step.waiterAssociatePublicIPToServerInstance
|
|
|
|
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreatePublicIPInstance) waiterAssociatePublicIPToServerInstance(serverInstanceNo string, publicIP string) error {
|
2020-02-03 08:55:01 -05:00
|
|
|
reqParams := new(server.GetServerInstanceListRequest)
|
|
|
|
reqParams.ServerInstanceNoList = []*string{&serverInstanceNo}
|
2018-01-11 04:57:53 -05:00
|
|
|
|
|
|
|
c1 := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2020-02-03 08:55:01 -05:00
|
|
|
serverInstanceList, err := s.Conn.server.V2Api.GetServerInstanceList(reqParams)
|
2018-01-11 04:57:53 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c1 <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
if publicIP == *serverInstanceList.ServerInstanceList[0].PublicIp {
|
2018-01-11 04:57:53 -05:00
|
|
|
c1 <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Say("Wait to associate public ip serverInstance")
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case res := <-c1:
|
|
|
|
return res
|
|
|
|
case <-time.After(time.Second * 60):
|
|
|
|
return fmt.Errorf("TIMEOUT : association public ip[%s] to server instance[%s] Failed", publicIP, serverInstanceNo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
func (s *StepCreatePublicIPInstance) createPublicIPInstance(serverInstanceNo string) (*server.PublicIpInstance, error) {
|
|
|
|
reqParams := new(server.CreatePublicIpInstanceRequest)
|
|
|
|
reqParams.ServerInstanceNo = &serverInstanceNo
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
publicIPInstanceList, err := s.Conn.server.V2Api.CreatePublicIpInstance(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
|
|
|
publicIPInstance := publicIPInstanceList.PublicIpInstanceList[0]
|
|
|
|
publicIP := publicIPInstance.PublicIp
|
|
|
|
s.Say(fmt.Sprintf("Public IP Instance [%s:%s] is created", *publicIPInstance.PublicIpInstanceNo, *publicIP))
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
err = s.waiterAssociatePublicIPToServerInstance(serverInstanceNo, *publicIP)
|
2019-11-11 23:56:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-11 04:57:53 -05:00
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
return publicIPInstance, nil
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCreatePublicIPInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-01-11 04:57:53 -05:00
|
|
|
s.Say("Create Public IP Instance")
|
|
|
|
|
|
|
|
serverInstanceNo := state.Get("InstanceNo").(string)
|
|
|
|
|
|
|
|
publicIPInstance, err := s.CreatePublicIPInstance(serverInstanceNo)
|
|
|
|
if err == nil {
|
2020-02-03 08:55:01 -05:00
|
|
|
state.Put("PublicIP", *publicIPInstance.PublicIp)
|
2018-01-11 04:57:53 -05:00
|
|
|
state.Put("PublicIPInstance", publicIPInstance)
|
2019-12-13 14:57:01 -05:00
|
|
|
// instance_id is the generic term used so that users can have access to the
|
|
|
|
// instance id inside of the provisioners, used in step_provision.
|
2020-02-03 08:55:01 -05:00
|
|
|
state.Put("instance_id", *publicIPInstance)
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return processStepResult(err, s.Error, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreatePublicIPInstance) Cleanup(state multistep.StateBag) {
|
|
|
|
publicIPInstance, ok := state.GetOk("PublicIPInstance")
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Say("Clean up Public IP Instance")
|
2020-02-03 08:55:01 -05:00
|
|
|
publicIPInstanceNo := publicIPInstance.(*server.PublicIpInstance).PublicIpInstanceNo
|
2018-01-11 04:57:53 -05:00
|
|
|
s.waitPublicIPInstanceStatus(publicIPInstanceNo, "USED")
|
|
|
|
|
|
|
|
log.Println("Disassociate Public IP Instance ", publicIPInstanceNo)
|
2020-02-03 08:55:01 -05:00
|
|
|
reqParams := &server.DisassociatePublicIpFromServerInstanceRequest{PublicIpInstanceNo: publicIPInstanceNo}
|
|
|
|
s.Conn.server.V2Api.DisassociatePublicIpFromServerInstance(reqParams)
|
2018-01-11 04:57:53 -05:00
|
|
|
|
|
|
|
s.waitPublicIPInstanceStatus(publicIPInstanceNo, "CREAT")
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
reqDeleteParams := &server.DeletePublicIpInstancesRequest{
|
|
|
|
PublicIpInstanceNoList: ncloud.StringList([]string{*publicIPInstanceNo}),
|
|
|
|
}
|
2018-01-11 04:57:53 -05:00
|
|
|
|
|
|
|
log.Println("Delete Public IP Instance ", publicIPInstanceNo)
|
2020-02-03 08:55:01 -05:00
|
|
|
s.Conn.server.V2Api.DeletePublicIpInstances(reqDeleteParams)
|
2018-01-11 04:57:53 -05:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
func (s *StepCreatePublicIPInstance) waitPublicIPInstanceStatus(publicIPInstanceNo *string, status string) {
|
2018-01-11 04:57:53 -05:00
|
|
|
c1 := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
2020-02-03 08:55:01 -05:00
|
|
|
reqParams := new(server.GetPublicIpInstanceListRequest)
|
|
|
|
reqParams.PublicIpInstanceNoList = []*string{publicIPInstanceNo}
|
2018-01-11 04:57:53 -05:00
|
|
|
|
|
|
|
for {
|
2020-02-03 08:55:01 -05:00
|
|
|
resp, err := s.Conn.server.V2Api.GetPublicIpInstanceList(reqParams)
|
2018-01-11 04:57:53 -05:00
|
|
|
if err != nil {
|
2018-01-11 20:12:47 -05:00
|
|
|
log.Printf(err.Error())
|
2018-01-11 04:57:53 -05:00
|
|
|
c1 <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
if *resp.TotalRows == 0 {
|
2018-01-29 07:47:58 -05:00
|
|
|
c1 <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:55:01 -05:00
|
|
|
instance := resp.PublicIpInstanceList[0]
|
|
|
|
if *instance.PublicIpInstanceStatus.Code == status && *instance.PublicIpInstanceOperation.Code == "NULL" {
|
2018-01-11 04:57:53 -05:00
|
|
|
c1 <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c1:
|
|
|
|
return
|
|
|
|
case <-time.After(time.Second * 60):
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|