/
index.js
160 lines (147 loc) · 4 KB
/
index.js
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
import createLogger from '@xen-orchestra/log'
const log = createLogger('xo:web-hooks')
function handleHook(type, data) {
const hooks = this._hooks[data.method]?.[type]
if (hooks !== undefined) {
return Promise.all(
hooks.map(({ url }) =>
this._makeRequest(url, type, data).catch(error => {
log.error('web hook failed', {
error,
webHook: { ...data, url, type },
})
})
)
)
}
}
class XoServerHooks {
constructor({ xo }) {
this._xo = xo
// Defined in configure().
this._hooks = null
this._handlePreHook = handleHook.bind(this, 'pre')
this._handlePostHook = handleHook.bind(this, 'post')
}
_makeRequest(url, type, data) {
return this._xo.httpRequest(url, {
body: JSON.stringify({ ...data, type }),
headers: { 'Content-Type': 'application/json' },
method: 'POST',
onRequest: req => {
req.setTimeout(1e4)
req.on('timeout', req.abort)
},
})
}
configure(configuration) {
// this._hooks = {
// 'vm.start': {
// pre: [
// {
// method: 'vm.start',
// type: 'pre',
// url: 'https://my-domain.net/xo-hooks?action=vm.start'
// },
// ...
// ],
// post: [
// ...
// ]
// },
// ...
// }
const hooks = {}
for (const hook of configuration.hooks) {
if (hooks[hook.method] === undefined) {
hooks[hook.method] = {}
}
hook.type.split('/').forEach(type => {
if (hooks[hook.method][type] === undefined) {
hooks[hook.method][type] = []
}
hooks[hook.method][type].push(hook)
})
}
this._hooks = hooks
}
load() {
this._xo.on('xo:preCall', this._handlePreHook)
this._xo.on('xo:postCall', this._handlePostHook)
}
unload() {
this._xo.removeListener('xo:preCall', this._handlePreHook)
this._xo.removeListener('xo:postCall', this._handlePostHook)
}
async test({ url }) {
await this._makeRequest(url, 'pre', {
callId: '0',
userId: 'b4tm4n',
userName: 'bruce.wayne@waynecorp.com',
method: 'vm.start',
params: { id: '67aac198-0174-11ea-8d71-362b9e155667' },
timestamp: 0,
})
await this._makeRequest(url, 'post', {
callId: '0',
userId: 'b4tm4n',
userName: 'bruce.wayne@waynecorp.com',
method: 'vm.start',
result: '',
timestamp: 500,
duration: 500,
})
}
}
export const configurationSchema = ({ xo: { apiMethods } }) => ({
description: 'Bind XO API calls to HTTP requests.',
type: 'object',
properties: {
hooks: {
type: 'array',
title: 'Hooks',
items: {
type: 'object',
title: 'Hook',
properties: {
method: {
description: 'The method to be bound to',
enum: Object.keys(apiMethods).sort(),
title: 'Method',
type: 'string',
},
type: {
description:
'Right before the API call *or* right after the action has been completed',
enum: ['pre', 'post', 'pre/post'],
title: 'Type',
type: 'string',
},
url: {
description: 'The full URL you wish the request to be sent to',
// It would be more convenient to configure 1 URL for multiple
// triggers but the UI implementation is not ideal for such a deep
// configuration schema: https://i.imgur.com/CpvAwPM.png
title: 'URL',
type: 'string',
},
},
required: ['method', 'type', 'url'],
},
},
},
required: ['hooks'],
})
export const testSchema = {
type: 'object',
description:
'The test will simulate a hook on `vm.start` (both "pre" and "post" hooks)',
properties: {
url: {
title: 'URL',
type: 'string',
description: 'The URL the test request will be sent to',
},
},
}
export default opts => new XoServerHooks(opts)