generated from hashicorp/packer-plugin-scaffolding
/
step_create_secondary_storage.go
75 lines (69 loc) · 2.17 KB
/
step_create_secondary_storage.go
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
package gridscale
import (
"context"
"errors"
"fmt"
"github.com/gridscale/gsclient-go/v3"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
)
type stepCreateSecondaryStorage struct {
client gsclient.StorageOperator
config *Config
ui packer.Ui
}
func (s *stepCreateSecondaryStorage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
c := s.config
ui := s.ui
// Create a secondary storage
if c.SecondaryStorage {
client := s.client
ui.Say("Creating a secondary storage...")
storage, err := client.CreateStorage(
context.Background(),
gsclient.StorageCreateRequest{
Capacity: c.StorageCapacity,
Name: fmt.Sprintf("%s-secondary", c.ServerName),
StorageType: gsclient.InsaneStorageType,
})
if err != nil {
ui.Error(fmt.Sprintf(
"Error creating secondary storage: %s", err))
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put("secondary_storage_uuid", storage.ObjectUUID)
ui.Say(fmt.Sprintf("a secondary storage (%s) has been created", storage.ObjectUUID))
return multistep.ActionContinue
}
ui.Say("No secondary storage is requested. Skipping creating a secondary storage...")
return multistep.ActionContinue
}
func (s *stepCreateSecondaryStorage) Cleanup(state multistep.StateBag) {
client := s.client
ui := s.ui
c := s.config
if c.SecondaryStorage {
// Destroy the created secondary storage
secondaryStorageUUID, ok := state.Get("secondary_storage_uuid").(string)
if !ok {
err := errors.New("cannot convert secondary_storage_uuid to string")
ui.Error(err.Error())
state.Put("error", err)
return
}
if secondaryStorageUUID == "" {
ui.Say("No secondary storage UUID detected.")
return
}
ui.Say(fmt.Sprintf("Destroying the secondary storage (%s)...", secondaryStorageUUID))
err := client.DeleteStorage(context.Background(), secondaryStorageUUID)
if err != nil {
ui.Error(fmt.Sprintf(
"Error destroying secondary storage (%s). Please destroy it manually: %s", secondaryStorageUUID, err))
return
}
ui.Say(fmt.Sprintf("Destroyed the secondary storage (%s)", secondaryStorageUUID))
}
}