Last active
November 5, 2020 10:01
-
-
Save changbowen/6c99780ba64d6010344f23b702e6b04a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let nsxServer = location.host; | |
let xsrfToken = await fetch(`https://${nsxServer}/api/v1/reverse-proxy/usersessionInfo`).then(r => r.text()).then(r => JSON.parse(r).xsrfToken); | |
let pageSize = 500; | |
// NSX groups with effective VM members | |
fetch(`https://${nsxServer}/policy/api/v1/search/aggregate?page_size=${pageSize}&cursor=0&sort_by=display_name&sort_ascending=true`, { | |
"headers": { | |
"accept": "application/json, text/plain, */*", | |
"accept-language": "en-US", | |
"content-type": "application/json;charset=UTF-8", | |
"x-xsrf-token": xsrfToken | |
}, | |
"body": "{\"primary\":{\"resource_type\":\"Group\"},\"related\":[{\"resource_type\":\"Domain\",\"join_condition\":\"path:parent_path\",\"alias\":\"domains\"}]}", | |
"method": "POST" | |
}).then(r=>r.text()).then(r=>{ | |
let vmInGroups = []; | |
let groupJson = JSON.parse(r); | |
let allProm = []; | |
for (const grp of groupJson.results) { | |
let grpObj = {groupId: grp.primary.id}; | |
let grpPath = grp.primary.path; | |
for (const expr of grp.primary.expression) { | |
switch (expr.resource_type) { | |
case 'NestedExpression': | |
expr.expressions.filter(e=>e.resource_type === 'Condition').map(e=>e.value.split('|')).forEach(e=>grpObj[e[0]] = e[1]); | |
break; | |
case 'IPAddressExpression': | |
grpObj['IpAddress'] = expr.ip_addresses.join(', '); | |
break; | |
} | |
} | |
allProm.push(fetch(`https://${nsxServer}/policy/api/v1${grpPath}/members/virtual-machines?page_size=${pageSize}&sort_ascending=true&sort_by=display_name`, { | |
"headers": { | |
"accept": "application/json, text/plain, */*", | |
"accept-language": "en-US", | |
"content-type": "application/json;charset=UTF-8", | |
"x-xsrf-token": xsrfToken | |
}, | |
"method": "GET" | |
}).then(r=> r.text()).then(r => { | |
let vmJson = JSON.parse(r).results; | |
grpObj.effVmMembers = vmJson.map(i => i.display_name).join(', '); | |
vmInGroups.push(grpObj); | |
})); | |
} | |
Promise.all(allProm).then(()=>console.log(JSON.stringify(vmInGroups))); | |
}); | |
// VM's with tag info | |
fetch(`https://${nsxServer}/policy/api/v1/infra/realized-state/enforcement-points/default/virtual-machines?page_size=${pageSize}&cursor=0&sort_by=display_name&sort_ascending=true&query=`, { | |
"headers": { | |
"accept": "application/json, text/plain, */*", | |
"accept-language": "en-US", | |
"content-type": "application/json;charset=UTF-8", | |
"x-xsrf-token": xsrfToken | |
}, | |
"method": "GET" | |
}).then(r=> r.text()).then(r => { | |
let vmAll = []; | |
let vmJson = JSON.parse(r).results; | |
for (const vm of vmJson) { | |
let vmObj = {displayName: vm.display_name}; | |
if (vm.tags) { | |
for (const tag of vm.tags) { | |
vmObj[tag.scope] = tag.tag; | |
} | |
} | |
vmAll.push(vmObj); | |
} | |
console.log(JSON.stringify(vmAll)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Login to NSX-T console, copy the content, past into Chrome's dev tools console (F12), run it, copy the console output (json format). And paste into some JSON - CSV conversion tools.