Skip to content

Commit 6be06d9

Browse files
committed
Added new command 'ztm attach'
1 parent 163d01c commit 6be06d9

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

agent/api.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ function allHubs(mesh) {
104104
return m.discoverHubs()
105105
}
106106

107+
function setHub(mesh, id, hub) {
108+
var m = meshes[mesh]
109+
if (m && hub.connected === true) {
110+
return m.attachHub(id)
111+
} else {
112+
return Promise.resolve()
113+
}
114+
}
115+
107116
function getHub(mesh, id) {
108117
var m = meshes[mesh]
109118
if (!m) return Promise.resolve(null)
@@ -306,6 +315,7 @@ export default {
306315
delMesh,
307316
getPermit,
308317
allHubs,
318+
setHub,
309319
getHub,
310320
getHubLog,
311321
allEndpoints,

agent/main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ function main(listen) {
245245
ret => ret ? response(200, ret) : response(404)
246246
)
247247
},
248+
249+
'POST': function ({ mesh, id }, req) {
250+
mesh = URL.decodeComponent(mesh)
251+
return api.setHub(mesh, id, JSON.decode(req.body)).then(
252+
() => response(201)
253+
)
254+
}
248255
},
249256

250257
'/api/meshes/{mesh}/hubs/{id}/log': {

agent/mesh.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,22 @@ export default function (rootDir, listen, config, onConfigUpdate) {
12851285
}
12861286
}
12871287

1288+
function attachHub(id) {
1289+
if (id === hubActive[0]?.id) {
1290+
return Promise.resolve()
1291+
} else {
1292+
return findHub(id).then(
1293+
hub => {
1294+
if (hub) {
1295+
var port = hub.ports.reduce((a, b) => a.ping < b ? a : b)
1296+
hubActive.forEach(h => h.leave())
1297+
hubActive[0] = Hub(hub.id, hub.zone, port.name)
1298+
}
1299+
}
1300+
)
1301+
}
1302+
}
1303+
12881304
function discoverHubs() {
12891305
return listAllHubs(config.bootstraps).then(
12901306
all => {
@@ -2180,6 +2196,7 @@ export default function (rootDir, listen, config, onConfigUpdate) {
21802196
findEndpoint,
21812197
findFile,
21822198
findApp,
2199+
attachHub,
21832200
discoverHubs,
21842201
discoverEndpoints,
21852202
discoverUsers,

cli/main.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,16 @@ function doCommand(meshName, epName, argv, program) {
321321
}
322322
},
323323

324+
{
325+
title: `Attach to a specified hub`,
326+
usage: 'attach <hub name>',
327+
notes: `
328+
Only hubs in the current mesh can be attached.
329+
Type 'ztm get hub' to see the list of available hubs.
330+
`,
331+
action: (args) => selectMesh(meshName).then(mesh => attachHub(args['<hub name>'], mesh))
332+
},
333+
324334
{
325335
title: `List objects of a certain type`,
326336
usage: 'get <object type> [object name]',
@@ -1111,6 +1121,19 @@ function disableApp(name, mesh, ep) {
11111121
})
11121122
}
11131123

1124+
//
1125+
// Command: attach
1126+
//
1127+
1128+
function attachHub(name, mesh) {
1129+
return selectHub(name, mesh).then(
1130+
id => client.post(
1131+
`/api/meshes/${uri(mesh.name)}/hubs/${id}`,
1132+
JSON.encode({ connected: true })
1133+
)
1134+
)
1135+
}
1136+
11141137
//
11151138
// Command: get
11161139
//

0 commit comments

Comments
 (0)