Skip to content

Commit

Permalink
Merge pull request #19 from tago-io/feat/tdeploy-support
Browse files Browse the repository at this point in the history
Update region to extended TagoIO Deploy support
  • Loading branch information
vitorfdl authored Dec 17, 2024
2 parents 00bfe21 + abe892c commit 3b3872d
Show file tree
Hide file tree
Showing 24 changed files with 282 additions and 94 deletions.
20 changes: 10 additions & 10 deletions docs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
"description": "Email of the account",
"type": "string",
"format": "email"
},
"tagoAPIURL": {
"description": "URL to API for TagoDeploy server",
"type": "string",
"format": "uri-reference"
},
"tagoSSEURL": {
"description": "URL to SSE for TagoDeploy server",
"type": "string",
"format": "uri-reference"
}
}
}
Expand All @@ -50,16 +60,6 @@
"type": "string",
"format": "uri-reference"
},
"tagoDeployUrl": {
"description": "URL to API for TagoDeploy server",
"type": "string",
"format": "uri-reference"
},
"tagoDeploySse": {
"description": "URL to SSE for TagoDeploy server",
"type": "string",
"format": "uri-reference"
},
"buildPath": {
"description": "Path from the root of the project to the build folder",
"type": "string",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/analysis/analysis-console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async function connectAnalysisConsole(scriptName: string | void, options: { envi
return;
}

const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
const account = new Account({ token: config.profileToken, region: config.profileRegion });
const analysis_info = await account.analysis.info(scriptObj.id);
if (!analysis_info) {
errorHandler(`Analysis with ID: ${scriptObj.id} couldn't be found.`);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/analysis/analysis-set-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function analysisSetMode(userInputName: string | void, options: { environm
return;
}

const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
const account = new Account({ token: config.profileToken, region: config.profileRegion });
const analysisFilterName = userInputName ? `*${userInputName}*` : undefined;

// Get analysis list from TagoIO
Expand Down
2 changes: 1 addition & 1 deletion src/commands/analysis/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ async function deployAnalysis(cmdScriptName: string, options: { environment: str
return;
}

const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
const account = new Account({ token: config.profileToken, region: config.profileRegion });
for (const { id, fileName } of scriptList) {
await buildScript(account, fileName, id, config);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/analysis/run-analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async function runAnalysis(scriptName: string | undefined, options: { environmen
return process.exit();
}

const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
const account = new Account({ token: config.profileToken, region: config.profileRegion });

let { token: analysisToken, run_on, name } = await account.analysis.info(scriptToRun.id);
successMSG(`> Analysis found: ${highlightMSG(scriptToRun.fileName)} (${name}}) [${highlightMSG(analysisToken)}].`);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/analysis/trigger-analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function triggerAnalysis(scriptName: string | void, options: { environment
return;
}

const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
const account = new Account({ token: config.profileToken, region: config.profileRegion });
const analysisList = config.analysisList.filter((x) => x.fileName);

let script: IEnvironment["analysisList"][0] | undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/dashboard/copy-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async function copyTabWidgets(dashID: string, options: IOptions) {
return;
}

const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
const account = new Account({ token: config.profileToken, region: config.profileRegion });
if (!dashID) {
dashID = await pickDashboardIDFromTagoIO(account);
}
Expand Down
24 changes: 15 additions & 9 deletions src/commands/devices/change-bucket-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Account } from "@tago-io/sdk";
import axios from "axios";
import kleur from "kleur";

import { Account } from "@tago-io/sdk";

import { getEnvironmentConfig } from "../../lib/config-file";
import { errorHandler, infoMSG, successMSG } from "../../lib/messages";
import { chooseFromList } from "../../prompt/choose-from-list";
Expand All @@ -13,10 +15,12 @@ interface BucketSettings {
chunk_retention?: number;
}

type environmentConfigResponse = NonNullable<ReturnType<typeof getEnvironmentConfig>>;

const coloredBucketType = (type: string) => (type === "mutable" ? kleur.green(type) : type === "legacy" ? kleur.red(type) : kleur.blue(type));

async function convertDevice(deviceID: string, settings: BucketSettings, profileToken: string) {
const account = new Account({ token: profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
async function convertDevice(deviceID: string, settings: BucketSettings, config: environmentConfigResponse) {
const account = new Account({ token: config.profileToken, region: config?.profileRegion });
const deviceInfo = await account.devices.info(deviceID);
const bucketType = deviceInfo.type;

Expand All @@ -33,8 +37,10 @@ async function convertDevice(deviceID: string, settings: BucketSettings, profile
}
};

const url = `https://api.tago.io/device/${deviceInfo.id}/convert`;
const headers = { Authorization: `${profileToken}` };
const defaultBaseURL = "https://api.tago.io";
const userBaseURL = typeof config.profileRegion === "object" ? config.profileRegion.api : defaultBaseURL;

Check failure on line 41 in src/commands/devices/change-bucket-type.ts

View workflow job for this annotation

GitHub Actions / Build and Deploy

Property 'api' does not exist on type 'never'.
const url = `${userBaseURL}/device/${deviceInfo.id}/convert`;
const headers = { Authorization: `${config.profileToken}` };

try {
const response = await axios.post(url, settings, { headers });
Expand All @@ -50,8 +56,8 @@ async function convertDevice(deviceID: string, settings: BucketSettings, profile
// function to copy device data
// this function will copy device data from mutable bucket to immutable bucket

async function startBucketChange(profileToken: string, deviceID: string, settings: BucketSettings) {
await convertDevice(deviceID, settings, profileToken).catch((error) => {
async function startBucketChange(config: environmentConfigResponse, deviceID: string, settings: BucketSettings) {
await convertDevice(deviceID, settings, config).catch((error) => {
errorHandler(error);
throw false;
});
Expand Down Expand Up @@ -81,7 +87,7 @@ async function changeBucketType(id: string, options: { environment: string }) {
errorHandler("Environment not found");
return;
}
const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
const account = new Account({ token: config.profileToken, region: config.profileRegion });
const bucketList = id ? [id] : await chooseBucketsFromList(account);
if (id) {
const bucketInfo = await account.buckets.info(id);
Expand Down Expand Up @@ -109,7 +115,7 @@ async function changeBucketType(id: string, options: { environment: string }) {
}

for (const bucket of bucketList) {
await startBucketChange(config.profileToken, bucket, bucketTypeSettings);
await startBucketChange(config, bucket, bucketTypeSettings);
}
}

Expand Down
85 changes: 85 additions & 0 deletions src/commands/devices/change-network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import axios from "axios";
import kleur from "kleur";

import { Account } from "@tago-io/sdk";

import { getEnvironmentConfig } from "../../lib/config-file";
import { errorHandler, infoMSG, successMSG } from "../../lib/messages";
import { pickDeviceIDFromTagoIO } from "../../prompt/pick-device-id-from-tagoio";
import { promptTextToEnter } from "../../prompt/text-prompt";

interface BucketSettings {
network: string;
connector: string;
}

type environmentConfigResponse = NonNullable<ReturnType<typeof getEnvironmentConfig>>;


async function updateDevice(config: environmentConfigResponse, deviceID: string, settings: BucketSettings) {
const account = new Account({ token: config.profileToken, region: config.profileRegion });

const tokens = await account.devices.tokenList(deviceID);
const tokenList = tokens.map((token) => token.token);

if (tokenList) {
for (const token of tokenList) {
await account.devices.tokenDelete(token);
}
}

await account.devices.edit(deviceID, { network: settings.network, connector: settings.connector, active: true });

for (const token of tokens) {
const serieNumber = token.serie_number as string | undefined;
await account.devices.tokenCreate(deviceID, { serie_number: serieNumber, name: token.name, permission: "full" });
}

successMSG(`> ${deviceID} - ${settings.network} network and ${settings.connector} connector updated successfully`);
}

async function changeNetworkOrConnector(id: string, options: { environment: string; networkID: string; connectorID: string }) {
const config = getEnvironmentConfig(options.environment);
if (!config || !config.profileToken) {
errorHandler("Environment not found");
return;
}

let { networkID, connectorID } = options;

const account = new Account({ token: config.profileToken, region: "usa-1" });
const deviceID = id || (await pickDeviceIDFromTagoIO(account));
if (!deviceID) {
return;
}

const deviceInfo = await account.devices.info(deviceID);
infoMSG(`> ${deviceInfo.name} - ${kleur.blue(deviceID)}\n`);

if (!networkID) {
networkID = await promptTextToEnter("Enter the network ID");
}

if (!connectorID) {
connectorID = await promptTextToEnter("Enter the connector ID");
}

if (!networkID && !connectorID) {
errorHandler("Network or Connector ID is required");
return;
}

if (networkID === deviceInfo.network && connectorID === deviceInfo.connector) {
errorHandler("Network and Connector are already set to this device");
return;
}

const updateInfo = {
network: networkID || deviceInfo.network,
connector: connectorID || deviceInfo.connector,
};

await updateDevice(config, deviceID, updateInfo);
}

export { changeNetworkOrConnector }
4 changes: 2 additions & 2 deletions src/commands/devices/copy-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ async function copyDeviceData(options: IOptions) {
}

if (!options.from || !options.to) {
const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
const account = new Account({ token: config.profileToken, region: config.profileRegion });
options.from = await pickDeviceIDFromTagoIO(account, "Choose a device to copy the data from:");
options.to = await pickDeviceIDFromTagoIO(account, "Choose a device to copy the data to: ");
}

let deviceFrom: Device | undefined;
let deviceTo: Device | undefined;
if (options.from?.length === 24 || options.to?.length === 24) {
const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
const account = new Account({ token: config.profileToken, region: config.profileRegion });

if (options.from.length === 24) {
deviceFrom = await Utils.getDevice(account, options.from);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/devices/data-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async function getDeviceData(idOrToken: string, options: IOptions) {
errorHandler("Environment not found");
return;
}
const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
const account = new Account({ token: config.profileToken, region: config.profileRegion });
if (!idOrToken) {
idOrToken = await pickDeviceIDFromTagoIO(account);
}
Expand Down
3 changes: 2 additions & 1 deletion src/commands/devices/data-post.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Account, Device, Utils } from "@tago-io/sdk";

import { getEnvironmentConfig } from "../../lib/config-file";
import { errorHandler, successMSG } from "../../lib/messages";
import { pickDeviceIDFromTagoIO } from "../../prompt/pick-device-id-from-tagoio";
Expand All @@ -15,7 +16,7 @@ async function postDeviceData(idOrToken: string, options: IOptions) {
return;
}

const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
const account = new Account({ token: config.profileToken, region: config.profileRegion });
if (!idOrToken) {
idOrToken = await pickDeviceIDFromTagoIO(account);
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/devices/device-bkp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync, writeFileSync } from "fs";
import axios from "axios";
import { readFileSync, writeFileSync } from "fs";
import kleur from "kleur";
import { DateTime } from "luxon";

Expand Down Expand Up @@ -111,7 +111,7 @@ async function bkpDeviceData(idOrToken: string, options: IOptions) {
return;
}

const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
const account = new Account({ token: config.profileToken, region: config.profileRegion });
if (!idOrToken) {
idOrToken = await pickDeviceIDFromTagoIO(account);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/devices/device-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function deviceInfo(idOrToken: string, options: { environment: string; raw
return;
}

const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
const account = new Account({ token: config.profileToken, region: config.profileRegion });
if (!idOrToken) {
idOrToken = await pickDeviceIDFromTagoIO(account);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/devices/device-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async function deviceList(options: IOptions) {
return;
}

const account = new Account({ token: config.profileToken });
const account = new Account({ token: config.profileToken, region: config.profileRegion });
const filter: DeviceQuery = { amount: 50, fields: ["id", "name", "active", "last_input"], filter: { tags: [{}] } };
if (filter.filter && options.name) {
filter.filter.name = `*${options.name}*`;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/devices/device-live-inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async function inspectorConnection(deviceIdOrToken: string, options: IOptions) {
return;
}

const account = new Account({ token: config.profileToken, region: !process.env.TAGOIO_API ? "usa-1" : "env" });
const account = new Account({ token: config.profileToken, region: config.profileRegion });
if (!deviceIdOrToken) {
deviceIdOrToken = await pickDeviceIDFromTagoIO(account);
}
Expand Down
5 changes: 3 additions & 2 deletions src/commands/list-env.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Account } from "@tago-io/sdk";

import { getConfigFile, writeToConfigFile } from "../lib/config-file";
import { getConfigFile, getProfileRegion, writeToConfigFile } from "../lib/config-file";
import { infoMSG } from "../lib/messages";
import { readToken } from "../lib/token";

Expand All @@ -22,8 +22,9 @@ async function fixEnvironments(configFile: ReturnType<typeof getConfigFile>, env
if (!token) {
continue;
}
const region = getProfileRegion(environment);

const account = new Account({ token });
const account = new Account({ token, region });
const profile = await account.profiles.info("current").catch((error) => {
console.error(`Error getting profile info for ${env}: ${error.message}`);
return;
Expand Down
Loading

0 comments on commit 3b3872d

Please sign in to comment.