/
operations.go
155 lines (134 loc) · 3.95 KB
/
operations.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
package c8ywaiter
import (
"context"
"fmt"
"strings"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmderrors"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/timestamp"
"github.com/reubenmiller/go-c8y/pkg/c8y"
)
// OperationState operation state checker
type OperationState struct {
ID string
Client *c8y.Client
Status []string
}
func (s *OperationState) SetValue(v interface{}) error {
if id, ok := v.(string); ok {
s.ID = id
}
return nil
}
// Check check if operation has reached its expected status
func (s *OperationState) Check(m interface{}) (done bool, err error) {
if op, ok := m.(*c8y.Operation); ok {
done = strings.Contains(strings.ToLower(strings.Join(s.Status, " ")), strings.ToLower(op.Status))
if done {
return done, nil
}
if op.Status == "SUCCESSFUL" || op.Status == "FAILED" {
if len(s.Status) == 1 {
return true, fmt.Errorf("Operation completed but did not match expected status. wanted=%s, got=%s", s.Status[0], op.Status)
}
return true, fmt.Errorf("Operation completed but did not match expected status. wanted=%s, got=%s", s.Status, op.Status)
}
}
return
}
// Get get the current operation state
func (s *OperationState) Get() (interface{}, error) {
operation, _, err := s.Client.Operation.GetOperation(
context.Background(),
s.ID,
)
return operation, err
}
// OperationCount inventory state checker
type OperationCount struct {
ID string `json:"-"`
Client *c8y.Client `json:"-"`
Minimum int64 `json:"minimum,omitempty"`
Maximum int64 `json:"maximum,omitempty"`
FragmentType string `json:"fragmentType,omitempty"`
BulkOperationId string `json:"bulkOperationId,omitempty"`
Status string `json:"status,omitempty"`
DateFrom string `json:"dateFrom,omitempty"`
DateTo string `json:"dateTo,omitempty"`
}
type OperationCountParameters struct {
Minimum int64 `json:"minimum,omitempty"`
Maximum int64 `json:"maximum,omitempty"`
}
// Check check if inventory managed object exists or not
func (s *OperationCount) Check(m interface{}) (done bool, err error) {
if mo, ok := m.(*c8y.ManagedObject); ok {
var dateFrom, dateTo string
if s.DateFrom != "" {
if v, err := timestamp.TryGetTimestamp(s.DateFrom, false, false); err == nil {
dateFrom = v
}
}
if s.DateTo != "" {
if v, err := timestamp.TryGetTimestamp(s.DateTo, false, false); err == nil {
dateTo = v
}
}
col, _, apiErr := s.Client.Operation.GetOperations(context.Background(), &c8y.OperationCollectionOptions{
DeviceID: mo.ID,
Status: s.Status,
FragmentType: s.FragmentType,
BulkOperationId: s.BulkOperationId,
DateFrom: dateFrom,
DateTo: dateTo,
PaginationOptions: c8y.PaginationOptions{
PageSize: 1,
WithTotalPages: true,
},
})
if apiErr != nil {
err = cmderrors.NewAssertionError(&cmderrors.AssertionError{
Err: apiErr,
Type: cmderrors.ManagedObject,
Wanted: "Found",
Got: "NotFound",
Context: struct{ ID string }{ID: mo.ID},
})
// Don't treat value if value does not exist
return
}
count := 0
if col != nil && col.BaseResponse != nil {
if col.Statistics != nil && col.Statistics.TotalPages != nil {
count = *col.Statistics.TotalPages
}
}
done = CompareCount(int64(count), s.Minimum, s.Maximum)
if !done {
err = cmderrors.NewAssertionError(&cmderrors.AssertionError{
Type: cmderrors.OperationCount,
Wanted: OperationCountParameters{s.Minimum, s.Maximum},
Got: count,
Context: struct{ ID string }{ID: mo.ID},
})
}
if done {
return done, nil
}
}
return
}
func (s *OperationCount) SetValue(v interface{}) error {
if id, ok := v.(string); ok {
s.ID = id
}
return nil
}
// Get get current managed object state
func (s *OperationCount) Get() (interface{}, error) {
mo, _, err := s.Client.Inventory.GetManagedObject(
context.Background(),
s.ID,
nil,
)
return mo, err
}