multicloud_rule_manager 1.0.30 → 1.0.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/commands/deploy.js +64 -53
- package/package.json +1 -1
package/commands/deploy.js
CHANGED
|
@@ -1,67 +1,78 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import path from "path";
|
|
3
|
+
import { getAlias } from "../utils/aliasManager.js";
|
|
3
4
|
|
|
5
|
+
export default {
|
|
6
|
+
command: "deploy <alias>",
|
|
7
|
+
describe: "Deploy dei file rules su API",
|
|
8
|
+
handler: async (argv) => {
|
|
9
|
+
try {
|
|
10
|
+
console.log("🔹 Recupero credenziali per alias:", argv.alias);
|
|
11
|
+
const cred = getAlias(argv.alias);
|
|
12
|
+
// --- PRIMA CHIAMATA: token ---
|
|
13
|
+
console.log("🔹 Inizio richiesta token...");
|
|
14
|
+
const loginResp = await fetch(`https://login-eon-no-prod.bit2win.cloud/auth/realms/${cred.realm}/protocol/openid-connect/token`, {
|
|
15
|
+
method: "POST",
|
|
4
16
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
17
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
18
|
+
body: new URLSearchParams({
|
|
19
|
+
client_id: cred.clientId,
|
|
20
|
+
client_secret: cred.clientSecret,
|
|
21
|
+
username: cred.username,
|
|
22
|
+
password: cred.password,
|
|
23
|
+
grant_type: "password"
|
|
24
|
+
})
|
|
25
|
+
});
|
|
8
26
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const loginResp = await fetch(`https://login-eon-no-prod.bit2win.cloud/auth/realms/${cred.realm}/protocol/openid-connect/token`, {
|
|
12
|
-
method: "POST",
|
|
13
|
-
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
14
|
-
body: new URLSearchParams({
|
|
15
|
-
client_id: cred.clientId,
|
|
16
|
-
client_secret: cred.clientSecret,
|
|
17
|
-
username: cred.username,
|
|
18
|
-
password: cred.password,
|
|
19
|
-
grant_type: "password"
|
|
20
|
-
})
|
|
21
|
-
});
|
|
27
|
+
const loginData = await loginResp.json(); // leggi direttamente JSON
|
|
28
|
+
const accessToken = loginData.access_token;
|
|
22
29
|
|
|
23
|
-
|
|
24
|
-
const
|
|
30
|
+
// cartella rules
|
|
31
|
+
const rulesDir = "./rules";
|
|
25
32
|
|
|
26
|
-
//
|
|
27
|
-
const
|
|
33
|
+
// leggi tutti i file js in rules/
|
|
34
|
+
const files = fs.readdirSync(rulesDir).filter(f => f.endsWith(".js"));
|
|
28
35
|
|
|
29
|
-
|
|
30
|
-
const
|
|
36
|
+
for (const file of files) {
|
|
37
|
+
const filePath = path.join(rulesDir, file);
|
|
38
|
+
const content = fs.readFileSync(filePath, "utf8");
|
|
31
39
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
40
|
+
// estrai guid dal nome file: name_guid.js
|
|
41
|
+
const match = file.match(/_(.+)\.js$/);
|
|
42
|
+
if (!match) {
|
|
43
|
+
console.warn(`⚠️ Nome file non valido, saltato: ${file}`);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const guid = match[1];
|
|
35
47
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
48
|
+
// --- POST API ---
|
|
49
|
+
try {
|
|
50
|
+
const resp = await fetch(`https://${cred.realm}.bit2win.cloud/api/data/v1/rules`, {
|
|
51
|
+
method: "PATCH",
|
|
52
|
+
headers: {
|
|
53
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
54
|
+
"Content-Type": "application/json"
|
|
55
|
+
},
|
|
56
|
+
body: JSON.stringify({
|
|
57
|
+
guid,
|
|
58
|
+
dsl_internal: content
|
|
59
|
+
})
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
if (!resp.ok) {
|
|
63
|
+
const text = await resp.text();
|
|
64
|
+
console.error(`❌ Errore deploy ${file}: ${resp.status} ${text}`);
|
|
65
|
+
} else {
|
|
66
|
+
console.log(`✅ Deploy riuscito per ${file}`);
|
|
67
|
+
}
|
|
68
|
+
} catch (err) {
|
|
69
|
+
console.error(`❌ Errore rete deploy ${file}:`, err.message);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
43
72
|
|
|
44
|
-
// --- POST API ---
|
|
45
|
-
try {
|
|
46
|
-
const resp = await fetch(`https://${cred.realm}.bit2win.cloud/api/data/v1/rules`, {
|
|
47
|
-
method: "PATCH",
|
|
48
|
-
headers: {
|
|
49
|
-
"Authorization": `Bearer ${accessToken}`,
|
|
50
|
-
"Content-Type": "application/json"
|
|
51
|
-
},
|
|
52
|
-
body: JSON.stringify({
|
|
53
|
-
guid,
|
|
54
|
-
dsl_internal: content
|
|
55
|
-
})
|
|
56
|
-
});
|
|
57
73
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
console.error(`❌ Errore deploy ${file}: ${resp.status} ${text}`);
|
|
61
|
-
} else {
|
|
62
|
-
console.log(`✅ Deploy riuscito per ${file}`);
|
|
74
|
+
} catch (err) {
|
|
75
|
+
console.error("❌ Errore:", err.message);
|
|
63
76
|
}
|
|
64
|
-
} catch (err) {
|
|
65
|
-
console.error(`❌ Errore rete deploy ${file}:`, err.message);
|
|
66
77
|
}
|
|
67
|
-
}
|
|
78
|
+
};
|