Skip to content

Instantly share code, notes, and snippets.

@changbowen
Last active November 5, 2020 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save changbowen/6c99780ba64d6010344f23b702e6b04a to your computer and use it in GitHub Desktop.
Save changbowen/6c99780ba64d6010344f23b702e6b04a to your computer and use it in GitHub Desktop.
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));
});
@changbowen
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment