-
Notifications
You must be signed in to change notification settings - Fork 7
/
template.go
135 lines (123 loc) · 3.8 KB
/
template.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"bufio"
"context"
"github.com/gridscale/gsclient-go"
log "github.com/sirupsen/logrus"
"os"
)
const locationUUID = "45ed677b-3702-4b36-be2a-a2eab9827950"
var emptyCtx = context.Background()
func main() {
uuid := os.Getenv("GRIDSCALE_UUID")
token := os.Getenv("GRIDSCALE_TOKEN")
config := gsclient.DefaultConfiguration(uuid, token)
client := gsclient.NewClient(config)
log.Info("gridscale client configured")
log.Info("Create template: Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
//In order to create a template, we need to create a storage and its snapshot
//Create storage
cStorage, err := client.CreateStorage(
emptyCtx,
gsclient.StorageCreateRequest{
Capacity: 1,
LocationUUID: locationUUID,
Name: "go-client-storage",
})
if err != nil {
log.Error("Create storage has failed with error", err)
return
}
defer func() {
err := client.DeleteStorage(emptyCtx, cStorage.ObjectUUID)
if err != nil {
log.Error("Delete storage has failed with error", err)
return
}
log.Info("Storage successfully deleted")
}()
//Create storage snapshot
cSnapshot, err := client.CreateStorageSnapshot(
emptyCtx,
cStorage.ObjectUUID,
gsclient.StorageSnapshotCreateRequest{
Name: "go-client-snapshot",
})
if err != nil {
log.Error("Create storage snapshot has failed with error", err)
return
}
defer func() {
err := client.DeleteStorageSnapshot(emptyCtx, cStorage.ObjectUUID, cSnapshot.ObjectUUID)
if err != nil {
log.Error("Delete storage snapshot has failed with error", err)
return
}
log.Info("Storage snapshot successfully deleted")
}()
//Create template
cTemplate, err := client.CreateTemplate(emptyCtx, gsclient.TemplateCreateRequest{
Name: "go-client-template",
SnapshotUUID: cSnapshot.ObjectUUID,
})
if err != nil {
log.Error("Create template has failed with error", err)
return
}
log.WithFields(log.Fields{
"template_uuid": cTemplate.ObjectUUID,
}).Info("Template successfully created")
defer func() {
err := client.DeleteTemplate(emptyCtx, cTemplate.ObjectUUID)
if err != nil {
log.Error("Delete template has failed with error", err)
return
}
log.Info("Template successfully deleted")
log.Info("Get deleted templates: Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
templates, err := client.GetDeletedTemplates(emptyCtx)
if err != nil {
log.Error("Get deleted templates has failed with error", err)
return
}
log.WithFields(log.Fields{
"templates": templates,
}).Info("Retrieved deleted templates successfully")
}()
//get a template to update
template, err := client.GetTemplate(emptyCtx, cTemplate.ObjectUUID)
if err != nil {
log.Error("Get template has failed with error", err)
return
}
log.WithFields(log.Fields{
"template_uuid": template.Properties.ObjectUUID,
}).Info("Template successfully retrieved")
log.Info("Update template: press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
//Update template
err = client.UpdateTemplate(emptyCtx, template.Properties.ObjectUUID, gsclient.TemplateUpdateRequest{
Name: "updated template",
Labels: template.Properties.Labels,
})
if err != nil {
log.Error("Update template has failed with error", err)
return
}
log.Info("Template successfully updated")
log.Info("Get template's events: press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
//Get template's events
events, err := client.GetTemplateEventList(emptyCtx, template.Properties.ObjectUUID)
if err != nil {
log.Error("Get template's events has failed with error", err)
return
}
log.WithFields(log.Fields{
"events": events,
}).Info("Template's events successfully retrieved")
log.Info("Delete template: press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}