-
Notifications
You must be signed in to change notification settings - Fork 12
/
diff.go
182 lines (172 loc) · 4.81 KB
/
diff.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
182
package main
import (
"encoding/json"
"fmt"
)
// Diff represents a set of bulk changes to be made on the same Component
// associated with the same BaseObject (e.g. Ethernet, Memory). These changes
// are grouped in three categories: Create, Update and Delete accounting for
// respective CRUD operations. You can think of Diff as a kind of container for
// DiffComponents.
type Diff struct {
Create []*DiffComponent
Update []*DiffComponent
Delete []*DiffComponent
}
func (d Diff) String() string {
return fmt.Sprintf("Diff{Create: %s, Update: %s, Delete: %s}",
d.Create, d.Update, d.Delete)
}
// IsEmpty returns true if a given Diff is empty or false otherwise.
func (d Diff) IsEmpty() bool {
if len(d.Create) == 0 && len(d.Update) == 0 && len(d.Delete) == 0 {
return true
}
return false
}
// DiffComponent represents a single piece of changes that will be send to
// Ralph. It's main part is the Data field, holding JSON-ised payload that will
// be send to Ralph. Other fields provide convenience shourtcuts for some
// functions/methods (e.g. Component field holds a reference to the original
// object, which frees us from unmarshaling contents of Data field).
type DiffComponent struct {
ID int // ID of the object held in Component field
Name string // name of the component (e.g. Ethernet, Memory)
Data []byte // JSON-ed Component field
Component Component // reference to the original object
}
// NewDiffComponent creates a DiffComponent based on a given component. Since
// component is an interface type, it can hold both object or pointer, but
// NewDiffComponent handles both of these cases.
func NewDiffComponent(component Component) (*DiffComponent, error) {
var id int
var name string
var data []byte
var err error
switch v := component.(type) {
case *Ethernet:
id = v.ID
name = "Ethernet"
data, err = json.Marshal(v)
if err != nil {
return nil, err
}
case Ethernet:
return NewDiffComponent(&v)
case *Memory:
id = v.ID
name = "Memory"
data, err = json.Marshal(v)
if err != nil {
return nil, err
}
case Memory:
return NewDiffComponent(&v)
case *FibreChannelCard:
id = v.ID
name = "FibreChannelCard"
data, err = json.Marshal(v)
if err != nil {
return nil, err
}
case FibreChannelCard:
return NewDiffComponent(&v)
case *Processor:
id = v.ID
name = "Processor"
data, err = json.Marshal(v)
if err != nil {
return nil, err
}
case Processor:
return NewDiffComponent(&v)
case *Disk:
id = v.ID
name = "Disk"
data, err = json.Marshal(v)
if err != nil {
return nil, err
}
case Disk:
return NewDiffComponent(&v)
case *DataCenterAsset:
id = *v.ID
name = "DataCenterAsset"
data, err = json.Marshal(v)
if err != nil {
return nil, err
}
case DataCenterAsset:
return NewDiffComponent(&v)
default:
return nil, fmt.Errorf("unknown component: %+v", v)
}
return &DiffComponent{
ID: id,
Name: name,
Data: data,
Component: component,
}, nil
}
func (d DiffComponent) String() string {
return fmt.Sprintf("DiffComponent{ID: %d, Name: %s, Data: %s, Component: %s}",
d.ID, d.Name, string(d.Data), d.Component)
}
// SendDiffToRalph sends a given Diff to Ralph. If dryRun is set to true, then
// no changes will be sent to Ralph. If noOutput is set to true, then all the
// output from this function will be silenced (this is mostly for tests).
// Returned statusCodes slice is meant only to facilitate tests, so don't be
// surprised if you see it ignored somewhere in the source code.
func SendDiffToRalph(client *Client, diff *Diff, dryRun bool, noOutput bool) (statusCodes []int, err error) {
var send = func(d *DiffComponent, method, endpoint, msg string) (int, error) {
var code int
var data []byte
switch {
case method == "DELETE":
data = nil
default:
data = d.Data
}
if !dryRun {
code, err = client.SendToRalph(method, endpoint, data)
}
if err != nil {
return code, err
}
if !noOutput {
fmt.Printf("%s %s successfully.\n", d.Component, msg) // TODO(xor-xor): Use logger instead.
}
return code, nil
}
for _, d := range diff.Create {
endpoint := APIEndpoints[d.Name]
code, err := send(d, "POST", endpoint, "created")
if err != nil {
return statusCodes, err
}
if code != 0 {
statusCodes = append(statusCodes, code)
}
}
for _, d := range diff.Update {
endpoint := fmt.Sprintf("%s/%d", APIEndpoints[d.Name], d.ID)
code, err := send(d, "PATCH", endpoint, "updated")
if err != nil {
return statusCodes, err
}
if code != 0 {
statusCodes = append(statusCodes, code)
}
}
for _, d := range diff.Delete {
endpoint := fmt.Sprintf("%s/%d", APIEndpoints[d.Name], d.ID)
code, err := send(d, "DELETE", endpoint, "deleted")
if err != nil {
return statusCodes, err
}
if code != 0 {
statusCodes = append(statusCodes, code)
}
}
return statusCodes, nil
}