-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtemplate_create.go
142 lines (130 loc) · 3.35 KB
/
template_create.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
136
137
138
139
140
141
142
package ovirtclient
import (
"fmt"
"time"
ovirtsdk "github.com/ovirt/go-ovirt"
)
func (o *oVirtClient) CreateTemplate(
vmID VMID,
name string,
params OptionalTemplateCreateParameters,
retries ...RetryStrategy,
) (result Template, err error) {
retries = defaultRetries(retries, defaultReadTimeouts(o))
if params == nil {
params = &templateCreateParameters{}
}
err = retry(
fmt.Sprintf("creating template from VM %s", vmID),
o.logger,
retries,
func() error {
tpl := ovirtsdk.NewTemplateBuilder()
tpl.VmBuilder(ovirtsdk.NewVmBuilder().Id(string(vmID)))
tpl.Name(name)
if desc := params.Description(); desc != nil {
tpl.Description(*desc)
}
response, err := o.conn.SystemService().TemplatesService().Add().Template(tpl.MustBuild()).Send()
if err != nil {
return err
}
sdkObject, ok := response.Template()
if !ok {
return newError(
ENotFound,
"no template returned after creating template from VM %s",
vmID,
)
}
result, err = convertSDKTemplate(sdkObject, o)
if err != nil {
return wrap(
err,
EBug,
"failed to convert template from VM %s",
vmID,
)
}
return nil
})
return result, err
}
func (m *mockClient) CreateTemplate(
vmID VMID,
name string,
params OptionalTemplateCreateParameters,
_ ...RetryStrategy,
) (Template, error) {
m.lock.Lock()
defer m.lock.Unlock()
vm, ok := m.vms[vmID]
if !ok {
return nil, newError(ENotFound, "VM with ID %s not found", vmID)
}
for _, tpl := range m.templates {
if tpl.name == name {
return nil, newError(EConflict, "A template with the name \"%s\" already exists.", name)
}
}
if params == nil {
params = &templateCreateParameters{}
}
description := ""
if desc := params.Description(); desc != nil {
description = *desc
}
tpl := &template{
client: m,
id: TemplateID(m.GenerateUUID()),
name: name,
description: description,
status: TemplateStatusLocked,
cpu: vm.cpu.clone(),
}
m.templates[tpl.ID()] = tpl
m.templateDiskAttachmentsByTemplate[tpl.ID()] = make(
[]*templateDiskAttachment,
len(m.vmDiskAttachmentsByVM[vmID]),
)
m.attachTemplateDisks(vmID, tpl)
go m.handlePostTemplateCreation(tpl)
return tpl, nil
}
func (m *mockClient) handlePostTemplateCreation(tpl *template) {
func() {
time.Sleep(2 * time.Second)
m.lock.Lock()
defer m.lock.Unlock()
if tpl.status == TemplateStatusIllegal {
return
}
for _, attachment := range m.templateDiskAttachmentsByTemplate[tpl.id] {
disk := m.disks[attachment.diskID]
disk.Unlock()
}
tpl.status = TemplateStatusOK
}()
}
func (m *mockClient) attachTemplateDisks(vmID VMID, tpl *template) {
i := 0
for _, attachment := range m.vmDiskAttachmentsByVM[vmID] {
disk := m.disks[attachment.diskID]
newDisk := disk.clone(nil)
_ = newDisk.Lock()
newDisk.alias = fmt.Sprintf("disk-%s", generateRandomID(5, m.nonSecureRandom))
m.disks[newDisk.ID()] = newDisk
tplAttachment := &templateDiskAttachment{
client: m,
id: TemplateDiskAttachmentID(m.GenerateUUID()),
templateID: tpl.ID(),
diskID: newDisk.ID(),
diskInterface: attachment.diskInterface,
bootable: attachment.bootable,
active: attachment.active,
}
m.templateDiskAttachmentsByDisk[newDisk.id] = tplAttachment
m.templateDiskAttachmentsByTemplate[tpl.id][i] = tplAttachment
i++
}
}