-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
178 lines (149 loc) · 6.37 KB
/
main.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
package main
import (
"log"
"net/url"
"os"
"time"
"github.com/brian1917/illumioapi"
)
func main() {
// GET CONFIG
config, pce := parseConfig()
// SET UP LOGGING
if len(config.Logging.LogDirectory) > 0 && config.Logging.LogDirectory[len(config.Logging.LogDirectory)-1:] != string(os.PathSeparator) {
config.Logging.LogDirectory = config.Logging.LogDirectory + string(os.PathSeparator)
}
f, err := os.OpenFile(config.Logging.LogDirectory+"Illumio_ServiceNow_Sync_"+time.Now().Format("20060102_150405")+".log", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.SetOutput(f)
// LOG THE MODE
log.Printf("INFO - Log only mode set to %t \r\n", config.Logging.LogOnly)
if config.Logging.LogOnly == true {
log.Printf("INFO - THIS MEANS ALL CHANGES LOGGED TO THE PCE DID NOT ACTUALLY HAPPEN. THEY WILL HAPPEN IF YOU RUN AGAIN WITH LOG ONLY SET TO FALSE.\r\n")
}
log.Printf("INFO - Create unmanaged workloads set to %t\r\n", config.UnmanagedWorkloads.Enable)
// GET ALL EXISTING LABELS
if config.Logging.verbose == true {
log.Printf("DEBUG - Making API call to get all Labels...\r\n")
}
labelsAPI, apiResp, err := illumioapi.GetAllLabels(pce)
// DEBUG LOGGING BEFORE FATAL ERROR LOGGING
if config.Logging.verbose == true {
log.Printf("DEBUG - Get All Labels API HTTP Request: %s %v \r\n", apiResp.Request.Method, apiResp.Request.URL)
log.Printf("DEBUG - Get All Labels API HTTP Reqest Header: %v \r\n", apiResp.Request.Header)
log.Printf("DEBUG - Get All Labels API Response Status Code: %d \r\n", apiResp.StatusCode)
log.Printf("DEBUG - Get All Labels API Response Body: \r\n %s \r\n", apiResp.RespBody)
}
if err != nil {
log.Fatal(err)
}
accountLabelKeys := make(map[string]string)
accountLabelValues := make(map[string]string)
for _, l := range labelsAPI {
accountLabelKeys[l.Href] = l.Key
accountLabelValues[l.Href] = l.Value
}
// GET ALL EXISTING WORKLOADS
if config.Logging.verbose == true {
log.Printf("DEBUG - Making API call to get all Workloads...\r\n")
}
wlAPI, apiResp, err := illumioapi.GetAllWorkloads(pce)
// DEBUG LOGGING BEFORE FATAL ERROR LOGGING
if config.Logging.verbose == true {
log.Printf("DEBUG - Get All Workloads API HTTP Request: %s %v \r\n", apiResp.Request.Method, apiResp.Request.URL)
log.Printf("DEBUG - Get All Workloads API HTTP Reqest Header: %v \r\n", apiResp.Request.Header)
log.Printf("DEBUG - Get All Workloads API Response Status Code: %d \r\n", apiResp.StatusCode)
log.Printf("DEBUG - Get All Workloads API Response Body:\r\n %s \r\n", apiResp.RespBody)
}
if err != nil {
log.Fatal(err)
}
accountWorkloads := make(map[string]illumioapi.Workload)
for _, w := range wlAPI {
accountWorkloads[w.Href] = w
}
// GET DATA FROM SERVICENOW TABLE
snURL := config.ServiceNow.TableURL + "?CSV&sysparm_fields=" + url.QueryEscape(config.ServiceNow.MatchField) + "," + url.QueryEscape(config.LabelMapping.App) +
"," + url.QueryEscape(config.LabelMapping.Enviornment) + "," + url.QueryEscape(config.LabelMapping.Location) + "," + url.QueryEscape(config.LabelMapping.Role)
if config.UnmanagedWorkloads.Enable == true && config.UnmanagedWorkloads.Table == "cmdb_ci_server_list" {
snURL = snURL + ",ip_address,host_name"
}
data := snhttp(snURL)
// SET THE TOTAL MATCH VARIABLE AND COUNTER
counter := 0
totalMatch := 0
newUnmanagedWLs := 0
// ITERATE THROUGH EACH LINE OF THE CSV
for _, line := range data {
counter++
lineMatch := 0
updateLabelsArray := make([]illumioapi.Label, 0)
// CHECK IF WORKLOAD EXISTS
for _, wl := range accountWorkloads {
// SET SOME WORKLOAD SPECIFIC VARIABLES
updateRequired := false
updateLabelsArray = nil
wlLabels := make(map[string]string)
// SWITCH THE MATCH FIELD FROM HOSTNAME BASED ON CONFIG
illumioMatch := wl.Hostname
if config.Illumio.MatchField == "name" {
illumioMatch = wl.Name
}
// IF THE FIRST COL (MATCH) MATHCES THE ILLUMIO MATCH, TAKE ACTION
if line[0] == illumioMatch {
totalMatch++
lineMatch++
for _, l := range wl.Labels {
wlLabels[accountLabelKeys[l.Href]] = accountLabelValues[l.Href]
}
// CHECK EACH LABEL TYPE TO SEE IF IT NEEDS TO BE UPDATED
labelKeys := []string{"app", "env", "loc", "role"}
configFields := []string{config.LabelMapping.App, config.LabelMapping.Enviornment, config.LabelMapping.Location, config.LabelMapping.Role}
// ITERATE THROUGH EACH LABEL TYPE
for i := 0; i <= 3; i++ {
// CANNOT BE "csvPlaceHolderIllumio" (SKIPPING THAT COL) AND THE LABELS DON'T MATCH
if configFields[i] != "csvPlaceHolderIllumio" && wlLabels[labelKeys[i]] != line[i+1] {
log.Printf("INFO - %s - %s label updated from %s to %s\r\n", wl.Hostname, labelKeys[i], wlLabels[labelKeys[i]], line[i+1])
updateRequired = true
// IF THE NEW VALUE (FROM SN) IS BLANK, WE DON'T APPEND TO THE UPDATE ARRAY
if line[i+1] != "" {
updateLabelsArray = append(updateLabelsArray, illumioapi.Label{Key: labelKeys[i], Value: line[i+1]})
}
// ADD EXISTING LABEL IF IT EXISTS
} else if line[i+1] != "" {
updateLabelsArray = append(updateLabelsArray, illumioapi.Label{Key: labelKeys[i], Value: wlLabels[labelKeys[i]]})
}
}
// UPDATE THE WORKLOAD IF ANYTHING NEEDS TO CHANGE
if updateRequired == true {
if config.Logging.verbose == true {
log.Printf("DEBUG - Updating workload %s ...\r\n", wl.Hostname)
}
updateWorkload(updateLabelsArray, wl)
}
}
}
// IF THERE WERE NO MATCHES AND IT'S NOT THE HEADER FILE, CREATE THE UNMANAGED WORKLOAD
if lineMatch == 0 && counter != 1 && config.UnmanagedWorkloads.Enable == true {
interfaceList := []string{"eth0"}
ipAddressList := []string{line[5]}
if len(ipAddressList[0]) == 0 || len(line[0]) == 0 {
log.Printf("WARNING - Not enough information to create unmanaged workload for hostname %s\r\n", line[0])
} else {
err := createUnmanagedWorkload(interfaceList, ipAddressList, line[1], line[2], line[3], line[4], line[0])
if err == nil {
newUnmanagedWLs++
}
}
}
}
// SUMMARIZE ACTIONS FOR LOG
log.Printf("INFO - %d total servers in CMDB and %d matched to PCE workloads\r\n", len(data)-1, totalMatch)
if config.UnmanagedWorkloads.Enable == true {
log.Printf("INFO - %d new unmanaged workloads created\r\n", newUnmanagedWLs)
log.Printf("INFO - %d servers with not enough info for unmanaged workload.\r\n", len(data)-1-totalMatch-newUnmanagedWLs)
}
}