File: step_delete_block_storage_instance.go

package info (click to toggle)
packer 1.6.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,156 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (98 lines) | stat: -rw-r--r-- 2,965 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package ncloud

import (
	"context"
	"errors"
	"fmt"
	"log"
	"time"

	"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server"
	"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
	packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
)

type StepDeleteBlockStorageInstance struct {
	Conn                       *NcloudAPIClient
	DeleteBlockStorageInstance func(blockStorageInstanceNo string) error
	Say                        func(message string)
	Error                      func(e error)
	Config                     *Config
}

func NewStepDeleteBlockStorageInstance(conn *NcloudAPIClient, ui packersdk.Ui, config *Config) *StepDeleteBlockStorageInstance {
	var step = &StepDeleteBlockStorageInstance{
		Conn:   conn,
		Say:    func(message string) { ui.Say(message) },
		Error:  func(e error) { ui.Error(e.Error()) },
		Config: config,
	}

	step.DeleteBlockStorageInstance = step.deleteBlockStorageInstance

	return step
}

func (s *StepDeleteBlockStorageInstance) getBlockInstanceList(serverInstanceNo string) []*string {
	reqParams := new(server.GetBlockStorageInstanceListRequest)
	reqParams.ServerInstanceNo = &serverInstanceNo

	blockStorageInstanceList, err := s.Conn.server.V2Api.GetBlockStorageInstanceList(reqParams)
	if err != nil {
		return nil
	}

	if *blockStorageInstanceList.TotalRows == 1 {
		return nil
	}

	var instanceList []*string

	for _, blockStorageInstance := range blockStorageInstanceList.BlockStorageInstanceList {
		log.Println(blockStorageInstance)
		if *blockStorageInstance.BlockStorageType.Code != "BASIC" {
			instanceList = append(instanceList, blockStorageInstance.BlockStorageInstanceNo)
		}
	}

	return instanceList
}

func (s *StepDeleteBlockStorageInstance) deleteBlockStorageInstance(serverInstanceNo string) error {
	blockStorageInstanceList := s.getBlockInstanceList(serverInstanceNo)
	if blockStorageInstanceList == nil || len(blockStorageInstanceList) == 0 {
		return nil
	}
	reqParams := server.DeleteBlockStorageInstancesRequest{
		BlockStorageInstanceNoList: blockStorageInstanceList,
	}
	_, err := s.Conn.server.V2Api.DeleteBlockStorageInstances(&reqParams)
	if err != nil {
		return err
	}

	s.Say(fmt.Sprintf("Block Storage Instance is deleted. Block Storage Instance List is %v", blockStorageInstanceList))

	if err := waiterDetachedBlockStorageInstance(s.Conn, serverInstanceNo, time.Minute); err != nil {
		return errors.New("TIMEOUT : Block Storage instance status is not deattached")
	}

	return nil
}

func (s *StepDeleteBlockStorageInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
	if s.Config.BlockStorageSize == 0 {
		return processStepResult(nil, s.Error, state)
	}

	s.Say("Delete Block Storage Instance")

	var serverInstanceNo = state.Get("InstanceNo").(string)

	err := s.DeleteBlockStorageInstance(serverInstanceNo)

	return processStepResult(err, s.Error, state)
}

func (*StepDeleteBlockStorageInstance) Cleanup(multistep.StateBag) {
}