From e57a8161e06d7fd24904b23e474c40b063a78b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B2=E1=84=89=E1=85=A5=E1=86=BC=E1=84=83?= =?UTF-8?q?=E1=85=A5=E1=86=A8?= Date: Mon, 29 Jan 2018 21:47:58 +0900 Subject: [PATCH] remove delete step of `login key` and `public ip instance`. --- builder/ncloud/builder.go | 4 - builder/ncloud/step_create_login_key.go | 7 -- .../ncloud/step_create_public_ip_instance.go | 12 ++- builder/ncloud/step_delete_login_key.go | 51 ------------ builder/ncloud/step_delete_login_key_test.go | 55 ------------- .../ncloud/step_delete_public_ip_instance.go | 78 ------------------- .../step_delete_public_ip_instance_test.go | 57 -------------- builder/ncloud/step_get_rootpassword.go | 2 + 8 files changed, 7 insertions(+), 259 deletions(-) delete mode 100644 builder/ncloud/step_delete_login_key.go delete mode 100644 builder/ncloud/step_delete_login_key_test.go delete mode 100644 builder/ncloud/step_delete_public_ip_instance.go delete mode 100644 builder/ncloud/step_delete_public_ip_instance_test.go diff --git a/builder/ncloud/builder.go b/builder/ncloud/builder.go index 24ec670d3..21435e6d0 100644 --- a/builder/ncloud/builder.go +++ b/builder/ncloud/builder.go @@ -56,8 +56,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe NewStepCreateServerImage(conn, ui, b.config), NewStepDeleteBlockStorageInstance(conn, ui, b.config), NewStepTerminateServerInstance(conn, ui), - NewStepDeleteLoginKey(conn, ui), - NewStepDeletePublicIPInstance(conn, ui), } } else if b.config.Comm.Type == "Windows" { steps = []multistep.Step{ @@ -84,8 +82,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe NewStepCreateServerImage(conn, ui, b.config), NewStepDeleteBlockStorageInstance(conn, ui, b.config), NewStepTerminateServerInstance(conn, ui), - NewStepDeleteLoginKey(conn, ui), - NewStepDeletePublicIPInstance(conn, ui), } } diff --git a/builder/ncloud/step_create_login_key.go b/builder/ncloud/step_create_login_key.go index d200c3106..f79c4decf 100644 --- a/builder/ncloud/step_create_login_key.go +++ b/builder/ncloud/step_create_login_key.go @@ -57,13 +57,6 @@ func (s *StepCreateLoginKey) Run(state multistep.StateBag) multistep.StepAction } func (s *StepCreateLoginKey) Cleanup(state multistep.StateBag) { - _, cancelled := state.GetOk(multistep.StateCancelled) - _, halted := state.GetOk(multistep.StateHalted) - - if !cancelled && !halted { - return - } - if loginKey, ok := state.GetOk("LoginKey"); ok { s.Say("Clean up login key") s.Conn.DeleteLoginKey(loginKey.(*LoginKey).KeyName) diff --git a/builder/ncloud/step_create_public_ip_instance.go b/builder/ncloud/step_create_public_ip_instance.go index 897ff1504..d15fd9f54 100644 --- a/builder/ncloud/step_create_public_ip_instance.go +++ b/builder/ncloud/step_create_public_ip_instance.go @@ -105,13 +105,6 @@ func (s *StepCreatePublicIPInstance) Run(state multistep.StateBag) multistep.Ste } func (s *StepCreatePublicIPInstance) Cleanup(state multistep.StateBag) { - _, cancelled := state.GetOk(multistep.StateCancelled) - _, halted := state.GetOk(multistep.StateHalted) - - if !cancelled && !halted { - return - } - publicIPInstance, ok := state.GetOk("PublicIPInstance") if !ok { return @@ -148,6 +141,11 @@ func (s *StepCreatePublicIPInstance) waitPublicIPInstanceStatus(publicIPInstance return } + if resp.TotalRows == 0 { + c1 <- nil + return + } + instance := resp.PublicIPInstanceList[0] if instance.PublicIPInstanceStatus.Code == status && instance.PublicIPInstanceOperation.Code == "NULL" { c1 <- nil diff --git a/builder/ncloud/step_delete_login_key.go b/builder/ncloud/step_delete_login_key.go deleted file mode 100644 index 18256c7d8..000000000 --- a/builder/ncloud/step_delete_login_key.go +++ /dev/null @@ -1,51 +0,0 @@ -package ncloud - -import ( - "fmt" - - ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk" - "github.com/hashicorp/packer/packer" - "github.com/mitchellh/multistep" -) - -type StepDeleteLoginKey struct { - Conn *ncloud.Conn - DeleteLoginKey func(keyName string) error - Say func(message string) - Error func(e error) -} - -func NewStepDeleteLoginKey(conn *ncloud.Conn, ui packer.Ui) *StepDeleteLoginKey { - var step = &StepDeleteLoginKey{ - Conn: conn, - Say: func(message string) { ui.Say(message) }, - Error: func(e error) { ui.Error(e.Error()) }, - } - - step.DeleteLoginKey = step.deleteLoginKey - - return step -} - -func (s *StepDeleteLoginKey) deleteLoginKey(keyName string) error { - _, err := s.Conn.DeleteLoginKey(keyName) - if err != nil { - return err - } - - return nil -} - -func (s *StepDeleteLoginKey) Run(state multistep.StateBag) multistep.StepAction { - var loginKey = state.Get("LoginKey").(*LoginKey) - - err := s.DeleteLoginKey(loginKey.KeyName) - if err == nil { - s.Say(fmt.Sprintf("Login Key[%s] is deleted", loginKey.KeyName)) - } - - return processStepResult(err, s.Error, state) -} - -func (*StepDeleteLoginKey) Cleanup(multistep.StateBag) { -} diff --git a/builder/ncloud/step_delete_login_key_test.go b/builder/ncloud/step_delete_login_key_test.go deleted file mode 100644 index 38ce1e22c..000000000 --- a/builder/ncloud/step_delete_login_key_test.go +++ /dev/null @@ -1,55 +0,0 @@ -package ncloud - -import ( - "fmt" - "github.com/mitchellh/multistep" - "testing" -) - -func TestStepDeleteLoginKeyShouldFailIfOperationDeleteLoginKeyFails(t *testing.T) { - var testSubject = &StepDeleteLoginKey{ - DeleteLoginKey: func(keyName string) error { return fmt.Errorf("!! Unit Test FAIL !!") }, - Say: func(message string) {}, - Error: func(e error) {}, - } - - stateBag := DeleteTestStateBagStepDeleteLoginKey() - - var result = testSubject.Run(stateBag) - - if result != multistep.ActionHalt { - t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result) - } - - if _, ok := stateBag.GetOk("Error"); ok == false { - t.Fatal("Expected the step to set stateBag['Error'], but it was not.") - } -} - -func TestStepDeleteLoginKeyShouldPassIfOperationDeleteLoginKeyPasses(t *testing.T) { - var testSubject = &StepDeleteLoginKey{ - DeleteLoginKey: func(keyName string) error { return nil }, - Say: func(message string) {}, - Error: func(e error) {}, - } - - stateBag := DeleteTestStateBagStepDeleteLoginKey() - - var result = testSubject.Run(stateBag) - - if result != multistep.ActionContinue { - t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) - } - - if _, ok := stateBag.GetOk("Error"); ok == true { - t.Fatalf("Expected the step to not set stateBag['Error'], but it was.") - } -} - -func DeleteTestStateBagStepDeleteLoginKey() multistep.StateBag { - stateBag := new(multistep.BasicStateBag) - - stateBag.Put("LoginKey", &LoginKey{"a", "b"}) - - return stateBag -} diff --git a/builder/ncloud/step_delete_public_ip_instance.go b/builder/ncloud/step_delete_public_ip_instance.go deleted file mode 100644 index 4a1401984..000000000 --- a/builder/ncloud/step_delete_public_ip_instance.go +++ /dev/null @@ -1,78 +0,0 @@ -package ncloud - -import ( - "errors" - "fmt" - "log" - "time" - - ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk" - "github.com/hashicorp/packer/packer" - "github.com/mitchellh/multistep" -) - -type StepDeletePublicIPInstance struct { - Conn *ncloud.Conn - DeletePublicIPInstance func(publicIPInstanceNo string) error - Say func(message string) - Error func(e error) -} - -func NewStepDeletePublicIPInstance(conn *ncloud.Conn, ui packer.Ui) *StepDeletePublicIPInstance { - var step = &StepDeletePublicIPInstance{ - Conn: conn, - Say: func(message string) { ui.Say(message) }, - Error: func(e error) { ui.Error(e.Error()) }, - } - - step.DeletePublicIPInstance = step.deletePublicIPInstance - - return step -} - -func (s *StepDeletePublicIPInstance) deletePublicIPInstance(publicIPInstanceNo string) error { - reqParams := new(ncloud.RequestDeletePublicIPInstances) - reqParams.PublicIPInstanceNoList = []string{publicIPInstanceNo} - - c1 := make(chan error, 1) - - go func() { - for { - resp, err := s.Conn.DeletePublicIPInstances(reqParams) - if err != nil && (resp.ReturnCode == 24073 || resp.ReturnCode == 25032) { - // error code : 24073 : Unable to destroy the server since a public IP is associated with the server. First, please disassociate a public IP from the server. - // error code : 25032 : You may not delete sk since (other) user is changing the target official IP settings. - log.Println(resp.ReturnCode, resp.ReturnMessage) - } else if err != nil { - c1 <- fmt.Errorf("error code: %d, error message: %s", resp.ReturnCode, resp.ReturnMessage) - return - } else if err == nil { - s.Say(fmt.Sprintf("Public IP Instance [%s] is deleted.", publicIPInstanceNo)) - c1 <- nil - return - } - - time.Sleep(time.Second * 5) - } - }() - - select { - case res := <-c1: - return res - case <-time.After(time.Second * 60): - return errors.New("TIMEOUT : Can't delete server instance") - } -} - -func (s *StepDeletePublicIPInstance) Run(state multistep.StateBag) multistep.StepAction { - s.Say("Delete Public IP Instance") - - publicIPInstance := state.Get("PublicIPInstance").(*ncloud.PublicIPInstance) - - err := s.DeletePublicIPInstance(publicIPInstance.PublicIPInstanceNo) - - return processStepResult(err, s.Error, state) -} - -func (*StepDeletePublicIPInstance) Cleanup(multistep.StateBag) { -} diff --git a/builder/ncloud/step_delete_public_ip_instance_test.go b/builder/ncloud/step_delete_public_ip_instance_test.go deleted file mode 100644 index b9fa04ec8..000000000 --- a/builder/ncloud/step_delete_public_ip_instance_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package ncloud - -import ( - "fmt" - ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk" - "testing" - - "github.com/mitchellh/multistep" -) - -func TestStepDeletePublicIPInstanceShouldFailIfOperationDeletePublicIPInstanceFails(t *testing.T) { - var testSubject = &StepDeletePublicIPInstance{ - DeletePublicIPInstance: func(publicIPInstanceNo string) error { return fmt.Errorf("!! Unit Test FAIL !!") }, - Say: func(message string) {}, - Error: func(e error) {}, - } - - stateBag := createTestStateBagStepDeletePublicIPInstance() - - var result = testSubject.Run(stateBag) - - if result != multistep.ActionHalt { - t.Fatalf("Expected the step to return 'ActionHalt', but got '%d'.", result) - } - - if _, ok := stateBag.GetOk("Error"); ok == false { - t.Fatal("Expected the step to set stateBag['Error'], but it was not.") - } -} - -func TestStepDeletePublicIPInstanceShouldPassIfOperationDeletePublicIPInstancePasses(t *testing.T) { - var testSubject = &StepDeletePublicIPInstance{ - DeletePublicIPInstance: func(publicIPInstanceNo string) error { return nil }, - Say: func(message string) {}, - Error: func(e error) {}, - } - - stateBag := createTestStateBagStepDeletePublicIPInstance() - - var result = testSubject.Run(stateBag) - - if result != multistep.ActionContinue { - t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) - } - - if _, ok := stateBag.GetOk("Error"); ok == true { - t.Fatalf("Expected the step to not set stateBag['Error'], but it was.") - } -} - -func createTestStateBagStepDeletePublicIPInstance() multistep.StateBag { - stateBag := new(multistep.BasicStateBag) - - stateBag.Put("PublicIPInstance", &ncloud.PublicIPInstance{PublicIPInstanceNo: "22"}) - - return stateBag -} diff --git a/builder/ncloud/step_get_rootpassword.go b/builder/ncloud/step_get_rootpassword.go index 9ec0feae2..d9eda578a 100644 --- a/builder/ncloud/step_get_rootpassword.go +++ b/builder/ncloud/step_get_rootpassword.go @@ -1,6 +1,8 @@ package ncloud import ( + "fmt" + ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk" "github.com/hashicorp/packer/packer" "github.com/mitchellh/multistep"