-
Notifications
You must be signed in to change notification settings - Fork 11
/
integrations.go
297 lines (274 loc) · 11.5 KB
/
integrations.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package morpheus
import (
"fmt"
)
var (
// IntegrationsPath is the API endpoint for integrations
IntegrationsPath = "/api/integrations"
)
// Integration structures for use in request and response payloads
type Integration struct {
ID int64 `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
Type string `json:"type"`
Username string `json:"username"`
Password string `json:"password"`
PasswordHash string `json:"passwordHash"`
Port string `json:"port"`
Version string `json:"version"`
IntegrationType struct {
ID int64 `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
}
URL string `json:"url"`
WindowsVersion string `json:"windowsVersion"`
RefType string `json:"refType"`
RefId string `json:"refId"`
ServiceUrl string `json:"serviceUrl"`
ServiceUsername string `json:"serviceUsername"`
ServicePassword string `json:"servicePassword"`
Token string `json:"token"`
TokenHash string `json:"tokenHash"`
AuthType string `json:"authType"`
AuthId string `json:"authId"`
Config struct {
Inventory string `json:"inventory"`
// Git Integration
DefaultBranch string `json:"defaultBranch"`
CacheEnabled interface{} `json:"cacheEnabled"`
// Ansible Integration
AnsiblePlaybooks string `json:"ansiblePlaybooks"`
AnsibleRoles string `json:"ansibleRoles"`
AnsibleGroupVars string `json:"ansibleGroupVars"`
AnsibleHostVars string `json:"ansibleHostVars"`
AnsibleCommandBus interface{} `json:"ansibleCommandBus"`
AnsibleVerbose interface{} `json:"ansibleVerbose"`
AnsibleGalaxyEnabled interface{} `json:"ansibleGalaxyEnabled"`
AnsibleDefaultBranch string `json:"ansibleDefaultBranch"`
Plugin interface{} `json:"plugin"`
IncidentAccess bool `json:"incidentAccess"`
RequestAccess bool `json:"requestAccess"`
ServiceNowCMDBMode string `json:"cmdbMode"`
ServiceNowIgnoreCertErrors bool `json:"ignoreCertErrors"`
ServiceNowCMDBBusinessObject string `json:"serviceNowCMDBBusinessObject"`
ServiceNowCustomCmdbMapping string `json:"serviceNowCustomCmdbMapping"`
ServiceNowCmdbClassMapping []serviceNowCmdbClassMappingEntry `json:"serviceNowCmdbClassMapping"`
ServiceNowCmdbClassMappingInput []string `json:"serviceNowCmdbClassMapping.input"`
PreparedForSync bool `json:"preparedForSync"`
PreparedForSyncv2 bool `json:"preparedForSync-v2"`
PreparedForSyncv3 bool `json:"preparedForSync-v3"`
PreparedForSyncv4 bool `json:"preparedForSync-v4"`
WebServiceImportUrl string `json:"webServiceImportUrl"`
WebServiceImportSysId string `json:"webServiceImportSysId"`
WebServiceOperationUrl string `json:"webServiceOperationUrl"`
ApprovalUser string `json:"approvalUser"`
Company string `json:"company"`
AppID string `json:"appId"`
InventoryExisting string `json:"inventoryExisting"`
ExtraAttributes string `json:"extraAttributes"`
EngineMount string `json:"engineMount"`
SecretPath string `json:"secretPath"`
SecretEngine string `json:"secretEngine"`
SecretPathHash string `json:"secretPathHash"`
SecretEngineHash string `json:"secretEngineHash"`
// Chef Integration
Databags []chefDatabagEntry `json:"databag"`
Endpoint string `json:"endpoint"`
Org string `json:"org"`
ChefUser string `json:"chefUser"`
UseFqdn bool `json:"useFqdn"`
ChefUseFQDN bool `json:"chefUseFqdn"`
UserKey string `json:"userKey"`
OrgKey string `json:"orgKey"`
Version string `json:"version"`
WindowsVersion string `json:"windowsVersion"`
WindowsInstallURL string `json:"windowsInstallUrl"`
UserKeyHash string `json:"userKeyHash"`
OrgKeyHash string `json:"orgKeyHash"`
PuppetMaster string `json:"puppetMaster"`
PuppetFireNow string `json:"puppetFireNow"`
PuppetSshUser string `json:"puppetSshUser"`
PuppetSshPassword string `json:"puppetSshPassword"`
PuppetSshPasswordHash string `json:"puppetSshPasswordHash"`
// Cherwell integration
CherwellCustomCmdbMapping string `json:"cherwellCustomCmdbMapping"`
CherwellClientKey string `json:"cherwellClientKey"`
CherwellCreatedBy string `json:"cherwellCreatedBy"`
CherwellStartDate string `json:"cherwellStartDate"`
CherwellEndDate string `json:"cherwellEndDate"`
CherwellIgnoreSSLErrors string `json:"cherwellIgnoreSSLErrors"`
CherwellBusinessObject string `json:"cherwellBusinessObject"`
ZoneFilter string `json:"zoneFilter"`
}
Status string `json:"status"`
StatusDate string `json:"statusDate"`
StatusMessage string `json:"statusMessage"`
LastSync string `json:"lastSync"`
LastSyncDuration string `json:"lastSyncDuration"`
IsPlugin bool `json:"isPlugin"`
ServiceKey struct {
ID int64 `json:"id"`
Name string `json:"name"`
}
ServiceMode string `json:"serviceMode"`
ServiceFlag bool `json:"serviceFlag"`
Credential struct {
ID int64 `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Types []string `json:"types"`
}
}
type serviceNowCmdbClassMappingEntry struct {
ID string `json:"id"`
Name string `json:"name"`
Code string `json:"code"`
NowClass string `json:"nowClass"`
}
type chefDatabagEntry struct {
Path string `json:"path"`
Key string `json:"key"`
}
// ListIntegrationsResult structure parses the list integrations response payload
type ListIntegrationsResult struct {
Integrations *[]Integration `json:"integrations"`
Meta *MetaResult `json:"meta"`
}
type ListIntegrationObjectsResult struct {
Objects []struct {
ID int64 `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
RefType string `json:"refType"`
RefID int64 `json:"refId"`
CatalogItemType struct {
ID int64 `json:"id"`
Name string `json:"name"`
}
} `json:"objects"`
Meta *MetaResult `json:"meta"`
}
type GetIntegrationResult struct {
Integration *Integration `json:"integration"`
}
type CreateIntegrationResult struct {
Success bool `json:"success"`
Message string `json:"msg"`
Errors map[string]string `json:"errors"`
Integration *Integration `json:"integration"`
}
type CreateIntegrationObjectResult struct {
Success bool `json:"success"`
Message string `json:"msg"`
Errors map[string]string `json:"errors"`
Object struct {
ID int64 `json:"id"`
} `json:"object"`
}
type UpdateIntegrationResult struct {
CreateIntegrationResult
}
type DeleteIntegrationResult struct {
DeleteResult
}
// Client request methods
func (client *Client) ListIntegrations(req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "GET",
Path: IntegrationsPath,
QueryParams: req.QueryParams,
Result: &ListIntegrationsResult{},
})
}
func (client *Client) ListIntegrationObjects(id int64, req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "GET",
Path: fmt.Sprintf("%s/%d/objects", IntegrationsPath, id),
QueryParams: req.QueryParams,
Result: &ListIntegrationsResult{},
})
}
func (client *Client) GetIntegration(id int64, req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "GET",
Path: fmt.Sprintf("%s/%d", IntegrationsPath, id),
QueryParams: req.QueryParams,
Result: &GetIntegrationResult{},
})
}
func (client *Client) CreateIntegration(req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "POST",
Path: IntegrationsPath,
QueryParams: req.QueryParams,
Body: req.Body,
Result: &CreateIntegrationResult{},
})
}
func (client *Client) RefreshIntegration(id int64, req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "POST",
Path: fmt.Sprintf("%s/%d/refresh", IntegrationsPath, id),
QueryParams: req.QueryParams,
Body: req.Body,
Result: &UpdateIntegrationResult{},
})
}
func (client *Client) CreateIntegrationObject(id int64, objectId int64, req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "POST",
Path: fmt.Sprintf("%s/%d/objects/%d", IntegrationsPath, id, objectId),
QueryParams: req.QueryParams,
Body: req.Body,
Result: &CreateIntegrationResult{},
})
}
func (client *Client) UpdateIntegration(id int64, req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "PUT",
Path: fmt.Sprintf("%s/%d", IntegrationsPath, id),
QueryParams: req.QueryParams,
Body: req.Body,
Result: &UpdateIntegrationResult{},
})
}
func (client *Client) DeleteIntegration(id int64, req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "DELETE",
Path: fmt.Sprintf("%s/%d", IntegrationsPath, id),
QueryParams: req.QueryParams,
Body: req.Body,
Result: &DeleteIntegrationResult{},
})
}
func (client *Client) DeleteIntegrationObject(id int64, objectId int64, req *Request) (*Response, error) {
return client.Execute(&Request{
Method: "DELETE",
Path: fmt.Sprintf("%s/%d/objects/%d", IntegrationsPath, id, objectId),
QueryParams: req.QueryParams,
Body: req.Body,
Result: &DeleteIntegrationResult{},
})
}
// helper functions
func (client *Client) FindIntegrationByName(name string) (*Response, error) {
// Find by name, then get by ID
resp, err := client.ListIntegrations(&Request{
QueryParams: map[string]string{
"name": name,
},
})
if err != nil {
return resp, err
}
listResult := resp.Result.(*ListIntegrationsResult)
integrationCount := len(*listResult.Integrations)
if integrationCount != 1 {
return resp, fmt.Errorf("found %d integrations named %v", integrationCount, name)
}
firstRecord := (*listResult.Integrations)[0]
integrationID := firstRecord.ID
return client.GetIntegration(integrationID, &Request{})
}