-
Notifications
You must be signed in to change notification settings - Fork 1
/
autocomplete.js
143 lines (133 loc) · 4.93 KB
/
autocomplete.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
const parsers = require("./parsers");
const GKEService = require("./gke.service");
// auto complete helper methods
const MAX_RESULTS = 10;
function mapAutoParams(autoParams) {
const params = {};
autoParams.forEach((param) => {
params[param.name] = parsers.autocomplete(param.value);
});
return params;
}
/** *
* @returns {[{id, value}]} filtered result items
** */
function handleResult(result, query, parseFunc) {
const items = result.map(parseFunc || getParseFromParam("id", "name"));
return filterItems(items, query);
}
function getAutoResult(id, value) {
return {
id: id || value,
value: value || id,
};
}
function getParseFromParam(idParamName, valParamName) {
if (valParamName) {
return (item) => getAutoResult(item[idParamName], item[valParamName]);
}
return (item) => getAutoResult(item[idParamName]);
}
function filterItems(items, query) {
let resolvedItems = items;
if (query) {
const qWords = query.split(/[. ]/g).map((word) => word.toLowerCase()); // split by '.' or ' ' and make lower case
resolvedItems = resolvedItems.filter((item) => (
qWords.every((word) => item.value.toLowerCase().includes(word))
));
resolvedItems = resolvedItems.sort((word1, word2) => (
word1.value.toLowerCase().indexOf(qWords[0]) - word2.value.toLowerCase().indexOf(qWords[0])
));
}
return resolvedItems.splice(0, MAX_RESULTS);
}
function listAuto(listFunc, fields, paging, noProject, parseFunc) {
let resolvedFields = fields;
let resolvedParseFunc = parseFunc;
if (!resolvedFields) {
resolvedFields = ["id", "name"];
}
if (!resolvedParseFunc && resolvedFields) {
resolvedParseFunc = getParseFromParam(...resolvedFields);
}
return async (query, pluginSettings, actionParams) => {
const resolvedQuery = (query || "").trim();
try {
const settings = mapAutoParams(pluginSettings);
const params = mapAutoParams(actionParams);
const client = GKEService.from(params, settings, noProject);
const items = [];
let nextPageToken;
params.query = resolvedQuery;
// TODO: Remove awaits in loop
// eslint-disable-next-line
while (true) {
// eslint-disable-next-line
const result = (await client[listFunc](params, resolvedFields, nextPageToken)) || [];
items.push(...handleResult(result.items || result, resolvedQuery, resolvedParseFunc));
if (!paging || !resolvedQuery || !result.nextPageToken || items.length >= MAX_RESULTS) {
return items;
}
const exactMatch = items.find((item) => (
item.value.toLowerCase() === resolvedQuery.toLowerCase()
|| item.id.toLowerCase() === resolvedQuery.toLowerCase()
));
if (exactMatch) {
return [exactMatch];
}
nextPageToken = result.nextPageToken;
}
} catch (err) {
throw new Error(`Problem with '${listFunc}': ${err.message || err}`);
}
};
}
async function listMachineTypesWithCustomAuto(query, pluginSettings, triggerParameters) {
let resolvedQuery = query;
try {
const settings = mapAutoParams(pluginSettings); const
params = mapAutoParams(triggerParameters);
const client = GKEService.from(params, settings);
let nextPageToken;
resolvedQuery = (resolvedQuery || "").trim();
const items = filterItems([{ id: "custom", value: "Custom N1(Default Custom)" },
{ id: "n2-custom", value: "Custom N2" },
{ id: "n2d-custom", value: "Custom N2D" },
{ id: "e2-custom", value: "Custom E2" }], resolvedQuery);
if (resolvedQuery.toLowerCase().includes("custom")) {
return items;
}
// TODO: Remove awaits in loop
// eslint-disable-next-line
while (true) {
// eslint-disable-next-line
const result = await client.listMachineTypes(params, ["name"], nextPageToken);
items.push(...handleResult(result.items, resolvedQuery, getParseFromParam("name")));
if (!result.nextPageToken || !resolvedQuery || items.length >= MAX_RESULTS) {
return items;
}
const exactMatch = items.find((item) => (
item.value.toLowerCase() === resolvedQuery.toLowerCase()
|| item.id.toLowerCase() === resolvedQuery.toLowerCase()
));
if (exactMatch) {
return [exactMatch];
}
nextPageToken = result.nextPageToken;
}
} catch (err) {
throw new Error(`Problem with 'listMachineTypesAuto': ${err.message || err}`);
}
}
module.exports = {
listClustersAuto: listAuto("listClusters"),
listNodePoolsAuto: listAuto("listNodePools"),
listProjectsAuto: listAuto("listProjects", ["projectId", "name"], false, true),
listRegionsAuto: listAuto("listRegions", ["name"]),
listZonesAuto: listAuto("listZones", ["name"]),
listServiceAccountsAuto: listAuto("listServiceAccounts", ["email", "displayName"]),
listMachineTypesAuto: listAuto("listMachineTypes", ["name"]),
listMachineTypesWithCustomAuto,
listNetworks: listAuto("listNetworks"),
listSubnetworks: listAuto("listSubnetworks"),
};