Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk update/create Resources from Excel #11

Merged
merged 3 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions TwingateApiClient.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,20 @@ export class TwingateApiClient {
isNode: true,
canCreate: true,
fields: [
{name: "name", type: "string", isLabel: true, canQuery: true},
{name: "createdAt", type: "datetime"},
{name: "updatedAt", type: "datetime"},
{name: "isActive", type: "boolean"},
{name: "name", type: "string", isLabel: true, canQuery: true},
{name: "address", type: "Object", typeName: "ResourceAddress"},
{name: "alias", type: "string"},
{name: "protocols", type: "Object", typeName: "ResourceProtocols"},
{name: "isActive", type: "boolean"},
{name: "remoteNetwork", type: "Node", typeName: "RemoteNetwork"},
{name: "groups", type: "Connection", typeName: "Group"}
{name: "groups", type: "Connection", typeName: "Group"}, // deprecated
{name: "serviceAccounts", type: "Connection", typeName: "ServiceAccount"}, // deprecated
//{name: "access", type: "Connection", typeName: "AccessConnection"}, // future to replace groups & serviceAccounts
{name: "isVisible", type: "boolean"},
{name: "isBrowserShortcutEnabled", type: "boolean"},
{name: "securityPolicy", type: "Node", typeName: "SecurityPolicy"}
]
},
"RemoteNetwork": {
Expand Down Expand Up @@ -907,13 +913,20 @@ export class TwingateApiClient {
}


async createResource(name, address, remoteNetworkId, protocols = null, groupIds = []) {
const createResourceQuery = "mutation CreateResource($name:String!,$address:String!,$remoteNetworkId:ID!,$protocols:ProtocolsInput,$groupIds:[ID]){result:resourceCreate(address:$address,groupIds:$groupIds,name:$name,protocols:$protocols,remoteNetworkId:$remoteNetworkId){error entity{id name address{value} remoteNetwork{name} groups{edges{node{id name}}}}}}";
let createResourceResponse = await this.exec(createResourceQuery, {name, address, remoteNetworkId, protocols, groupIds} );
async createResource(name, address, remoteNetworkId, protocols = null, groupIds = [], alias = null, isBrowserShortcutEnabled = null, isVisible = null, securityPolicyId = null) {
const createResourceQuery = "mutation CreateResource($address:String!,$alias:String,$groupIds:[ID],$isBrowserShortcutEnabled:Boolean,$isVisible:Boolean,$name:String!,$protocols:ProtocolsInput,$remoteNetworkId:ID!,$securityPolicyId:ID){result:resourceCreate(address:$address,alias:$alias,groupIds:$groupIds,isBrowserShortcutEnabled:$isBrowserShortcutEnabled,isVisible:$isVisible,name:$name,protocols:$protocols,remoteNetworkId:$remoteNetworkId,securityPolicyId:$securityPolicyId){ok error entity{id name address{value} remoteNetwork{name} groups{edges{node{id name}}}}}}";
let createResourceResponse = await this.exec(createResourceQuery, {address, alias, groupIds, isBrowserShortcutEnabled, isVisible, name, protocols, remoteNetworkId, securityPolicyId});
if ( createResourceResponse.result.error !== null ) throw new Error(`Error creating resource: '${createResourceResponse.result.error}'`)
return createResourceResponse.result.entity;
}

async updateResource(addedGroupIds = [], address, alias = null, groupIds = [], id, isActive, isBrowserShortcutEnabled = null, isVisible = null, name, protocols = null, remoteNetworkId, removedGroupIds = [], securityPolicyId = null) {
const updateResourceQuery = "mutation UpdateResource($addedGroupIds:[ID],$address:String,$alias:String,$groupIds:[ID],$id:ID!,$isActive:Boolean,$isBrowserShortcutEnabled:Boolean,$isVisible:Boolean,$name:String,$protocols:ProtocolsInput,$remoteNetworkId:ID,$removedGroupIds:[ID],$securityPolicyId:ID){result:resourceUpdate(addedGroupIds:$addedGroupIds,address:$address,alias:$alias,groupIds:$groupIds,id:$id,isActive:$isActive,isBrowserShortcutEnabled:$isBrowserShortcutEnabled,isVisible:$isVisible,name:$name,protocols:$protocols,remoteNetworkId:$remoteNetworkId,removedGroupIds:$removedGroupIds,securityPolicyId:$securityPolicyId){ok error entity{id name address{value} remoteNetwork{name} groups{edges{node{id name}}}}}}";
let updateResourceResponse = await this.exec(updateResourceQuery, {addedGroupIds, address, alias, groupIds, id, isActive, isBrowserShortcutEnabled, isVisible, name, protocols, remoteNetworkId, removedGroupIds, securityPolicyId} );
if ( updateResourceResponse.result.error !== null ) throw new Error(`Error updating resource: '${updateResourceResponse.result.error}'`);
return updateResourceResponse.result;
}

async removeGroup(id) {
const removeGroupQuery = "mutation RemoveGroup($id:ID!){result:groupDelete(id:$id){ok, error}}";
let removeGroupResponse = await this.exec(removeGroupQuery, {id});
Expand Down
22 changes: 16 additions & 6 deletions cliCmd/exportCmd.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,18 @@ async function outputDot(client, options) {
async function exportDot(client, options) {
let dot = await outputDot(client, options);
options.outputFile = options.outputFile || genFileNameFromNetworkName(options.accountName, options.format);
return await Deno.writeTextFile(`./${options.outputFile}`, dot);
let outputDir = `./output/${options.outputFile}_export`;
await Deno.mkdir(outputDir, {recursive: true});
return await Deno.writeTextFile(`${outputDir}/${options.outputFile}_export`, dot);
}


async function exportImage(client, options) {
let dot = await outputDot(client, options);
options.outputFile = options.outputFile || genFileNameFromNetworkName(options.accountName, options.format);
return await renderDot(dot, `./${options.outputFile}`, {format: options.format});
let outputDir = `./output/${options.outputFile}_export`;
await Deno.mkdir(outputDir, {recursive: true});
return await renderDot(dot, `${outputDir}/${options.outputFile}_export`, {format: options.format});
}


Expand All @@ -170,8 +174,10 @@ async function exportJson(client, options) {
const allNodes = await client.fetchAll(configForExport);

setLastConnectedOnUser(allNodes);
options.outputFile = options.outputFile || genFileNameFromNetworkName(options.accountName, "json");
await Deno.writeTextFile(`./${options.outputFile}`, JSON.stringify(allNodes));
options.outputFile = options.outputFile || genFileNameFromNetworkName(options.accountName, options.format);
let outputDir = `./output/${options.outputFile}_export`;
await Deno.mkdir(outputDir, {recursive: true});
await Deno.writeTextFile(`${outputDir}/${options.outputFile}_export.${options.format}`, JSON.stringify(allNodes));
}


Expand Down Expand Up @@ -203,8 +209,10 @@ async function exportExcel(client, options) {
ws['!autofilter'] = {ref: ws["!ref"]};
XLSX.utils.book_append_sheet(wb, ws, typeName);
}
options.outputFile = options.outputFile || genFileNameFromNetworkName(options.accountName);
await Deno.writeFile(`./${options.outputFile}`, new Uint8Array(XLSX.write(wb, {type: "array"})));
options.outputFile = options.outputFile || genFileNameFromNetworkName(options.accountName, "xlsx");
let outputDir = `./output/${options.outputFile}_export`;
await Deno.mkdir(outputDir, {recursive: true});
await Deno.writeFile(`${outputDir}/${options.outputFile}_export.${options.format}`, new Uint8Array(XLSX.write(wb, {type: "array"})));
}


Expand All @@ -223,6 +231,7 @@ export const exportCmd = new Command()
.option("-n, --remote-networks [boolean]", "Include Remote Networks")
.option("-r, --resources [boolean]", "Include Resources")
.option("-g, --groups [boolean]", "Include Groups")
.option("-p, --security-policies [boolean]", "Include Security Policies")
.option("-u, --users [boolean]", "Include Users")
.option("-d, --devices [boolean]", "Include Devices (trust)")
.description("Export from account to various formats")
Expand All @@ -236,6 +245,7 @@ export const exportCmd = new Command()
if ( options.groups === true ) options.typesToFetch.push("Group")
if ( options.users === true ) options.typesToFetch.push("User")
if ( options.devices === true ) options.typesToFetch.push("Device")
if ( options.securityPolicies === true ) options.typesToFetch.push("SecurityPolicy")

let outputFn = outputFnMap[options.format];
if (outputFn == null) {
Expand Down
Loading
Loading