-
Notifications
You must be signed in to change notification settings - Fork 891
/
history_collector.go
181 lines (142 loc) · 3.95 KB
/
history_collector.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package simulator
import (
"container/list"
"sync"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
)
var (
maxPageSize = 1000
maxCollectors int32 = 32 // the VC default limit
defaultPageSize = 10
errInvalidArgMaxCount = &types.InvalidArgument{InvalidProperty: "maxCount"}
)
type HistoryCollector struct {
parent *history
root types.ManagedObjectReference
size int
fill func(*Context)
page *list.List
pos *list.Element
}
type history struct {
sync.Mutex
page *list.List
collectors map[types.ManagedObjectReference]mo.Reference
}
func newHistory() *history {
return &history{
page: list.New(),
collectors: make(map[types.ManagedObjectReference]mo.Reference),
}
}
func (c *history) add(ctx *Context, collector mo.Reference) types.ManagedObjectReference {
ref := ctx.Session.Put(collector).Reference()
c.Lock()
c.collectors[ref] = collector
c.Unlock()
return ref
}
func (c *history) remove(ctx *Context, ref types.ManagedObjectReference) {
ctx.Session.Remove(ctx, ref)
c.Lock()
delete(c.collectors, ref)
c.Unlock()
}
func newHistoryCollector(ctx *Context, h *history, size int) *HistoryCollector {
return &HistoryCollector{
parent: h,
root: ctx.Map.content().RootFolder,
size: size,
page: list.New(),
}
}
func (c *HistoryCollector) SetCollectorPageSize(ctx *Context, req *types.SetCollectorPageSize) soap.HasFault {
body := new(methods.SetCollectorPageSizeBody)
size, err := validatePageSize(req.MaxCount)
if err != nil {
body.Fault_ = err
return body
}
c.size = size
c.page = list.New()
c.fill(ctx)
body.Res = new(types.SetCollectorPageSizeResponse)
return body
}
func (c *HistoryCollector) ResetCollector(ctx *Context, req *types.ResetCollector) soap.HasFault {
c.pos = c.page.Back()
return &methods.ResetCollectorBody{
Res: new(types.ResetCollectorResponse),
}
}
func (c *HistoryCollector) RewindCollector(ctx *Context, req *types.RewindCollector) soap.HasFault {
c.pos = c.page.Front()
return &methods.RewindCollectorBody{
Res: new(types.RewindCollectorResponse),
}
}
func (c *HistoryCollector) DestroyCollector(ctx *Context, req *types.DestroyCollector) soap.HasFault {
ctx.Session.Remove(ctx, req.This)
c.parent.remove(ctx, req.This)
return &methods.DestroyCollectorBody{
Res: new(types.DestroyCollectorResponse),
}
}
func (c *HistoryCollector) read(max int32, next func() *list.Element, take func(*list.Element)) {
for i := 0; i < int(max); i++ {
e := next()
if e == nil {
break
}
take(e)
c.pos = e
}
}
func (c *HistoryCollector) next(max int32, take func(*list.Element)) {
next := func() *list.Element {
if c.pos != nil {
return c.pos.Next()
}
return c.page.Front()
}
c.read(max, next, take)
}
func (c *HistoryCollector) prev(max int32, take func(*list.Element)) {
next := func() *list.Element {
if c.pos != nil {
return c.pos.Prev()
}
return c.page.Back()
}
c.read(max, next, take)
}
func pushHistory(l *list.List, item types.AnyType) {
if l.Len() > maxPageSize {
l.Remove(l.Front()) // Prune history
}
l.PushBack(item)
}
func validatePageSize(count int32) (int, *soap.Fault) {
size := int(count)
if size == 0 {
size = defaultPageSize
} else if size < 0 || size > maxPageSize {
return -1, Fault("", errInvalidArgMaxCount)
}
return size, nil
}