diff --git a/components/scalr/.gitignore b/components/scalr/.gitignore deleted file mode 100644 index ec761ccab7595..0000000000000 --- a/components/scalr/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -*.js -*.mjs -dist \ No newline at end of file diff --git a/components/scalr/app/scalr.app.ts b/components/scalr/app/scalr.app.ts deleted file mode 100644 index 4bc5dc154ca0a..0000000000000 --- a/components/scalr/app/scalr.app.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { defineApp } from "@pipedream/types"; - -export default defineApp({ - type: "app", - app: "scalr", - propDefinitions: {}, - methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); - }, - }, -}); \ No newline at end of file diff --git a/components/scalr/package.json b/components/scalr/package.json index da6e1849fa52c..6910fb4e1274d 100644 --- a/components/scalr/package.json +++ b/components/scalr/package.json @@ -1,16 +1,18 @@ { "name": "@pipedream/scalr", - "version": "0.0.2", + "version": "0.1.0", "description": "Pipedream Scalr Components", - "main": "dist/app/scalr.app.mjs", + "main": "scalr.app.mjs", "keywords": [ "pipedream", "scalr" ], - "files": ["dist"], "homepage": "https://pipedream.com/apps/scalr", "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.3" } } diff --git a/components/scalr/scalr.app.mjs b/components/scalr/scalr.app.mjs new file mode 100644 index 0000000000000..db2328a2a9487 --- /dev/null +++ b/components/scalr/scalr.app.mjs @@ -0,0 +1,65 @@ +import { axios } from "@pipedream/platform"; + +export default { + type: "app", + app: "scalr", + propDefinitions: { + accountId: { + type: "string", + label: "Account ID", + description: "The ID of the account you wish to use.", + async options({ page }) { + const { data: accounts } = await this.getAccounts({ + params: { + "page[number]": page + 1, + }, + }); + return accounts.map((account) => ({ + label: account.attributes.name, + value: account.id, + })); + }, + }, + }, + methods: { + _baseUrl() { + return `https://${this.$auth.domain}.scalr.io/api/iacp/v3`; + }, + async _makeRequest(opts = {}) { + const { + $ = this, + path, + headers, + ...otherOpts + } = opts; + return axios($, { + ...otherOpts, + url: this._baseUrl() + path, + headers: { + "Authorization": `Bearer ${this.$auth.api_token}`, + "Content-type": "application/vnd.api+json", + ...headers, + }, + }); + }, + async createWebhook({ ...args }) { + return this._makeRequest({ + path: "/integrations/webhooks", + method: "post", + ...args, + }); + }, + async removeWebhook(webhookId) { + return this._makeRequest({ + path: `/integrations/webhooks/${webhookId}`, + method: "delete", + }); + }, + async getAccounts(args = {}) { + return this._makeRequest({ + path: "/accounts", + ...args, + }); + }, + }, +}; diff --git a/components/scalr/sources/common/common.mjs b/components/scalr/sources/common/common.mjs new file mode 100644 index 0000000000000..b09e019e3ca88 --- /dev/null +++ b/components/scalr/sources/common/common.mjs @@ -0,0 +1,70 @@ +import scalr from "../../scalr.app.mjs"; + +export default { + props: { + scalr, + db: "$.service.db", + http: "$.interface.http", + accountId: { + propDefinition: [ + scalr, + "accountId", + ], + }, + }, + methods: { + _getWebhookId() { + return this.db.get("webhookId"); + }, + _setWebhookId(webhookId) { + this.db.set("webhookId", webhookId); + }, + getWebhookEventType() { + throw new Error("getWebhookEventType is not implemented"); + }, + emitEvent(event) { + throw new Error("emitEvent is not implemented", event); + }, + }, + hooks: { + async activate() { + const { data } = await this.scalr.createWebhook({ + data: { + "data": { + "attributes": { + "max-attempts": 3, + "timeout": 15, + "name": "Webhook Pipedream - " + new Date().toISOString(), + "url": this.http.endpoint, + }, + "relationships": { + "account": { + "data": { + "type": "accounts", + "id": this.accountId, + }, + }, + "events": { + "data": [ + { + "type": "event-definitions", + "id": this.getWebhookEventType(), + }, + ], + }, + }, + "type": "webhook-integrations", + }, + }, + }); + this._setWebhookId(data.id); + }, + async deactivate() { + const webhookId = this._getWebhookId(); + await this.scalr.removeWebhook(webhookId); + }, + }, + async run(event) { + await this.emitEvent(event.body); + }, +}; diff --git a/components/scalr/sources/new-run-completed/new-run-completed.mjs b/components/scalr/sources/new-run-completed/new-run-completed.mjs new file mode 100644 index 0000000000000..1c291c5649f69 --- /dev/null +++ b/components/scalr/sources/new-run-completed/new-run-completed.mjs @@ -0,0 +1,28 @@ +import common from "../common/common.mjs"; + +export default { + ...common, + name: "New Run Completed (Instant)", + version: "0.0.1", + key: "scalr-new-run-completed", + description: "Emit new event on each new completed run. [See the documentation](https://docs.scalr.io/reference/create_webhook_integration)", + type: "source", + dedupe: "unique", + hooks: { + ...common.hooks, + }, + methods: { + ...common.methods, + getWebhookEventType() { + return "run:completed"; + }, + async emitEvent(data) { + + this.$emit(data, { + id: data.run.id, + summary: `New run completed with ID ${data.run.id}`, + ts: Date.parse(data.run["created-at"]), + }); + }, + }, +}; diff --git a/components/scalr/sources/new-run-errored/new-run-errored.mjs b/components/scalr/sources/new-run-errored/new-run-errored.mjs new file mode 100644 index 0000000000000..be2dccb41be4c --- /dev/null +++ b/components/scalr/sources/new-run-errored/new-run-errored.mjs @@ -0,0 +1,28 @@ +import common from "../common/common.mjs"; + +export default { + ...common, + name: "New Run Errored (Instant)", + version: "0.0.1", + key: "scalr-new-run-errored", + description: "Emit new event when a new run encountered an error. [See the documentation](https://docs.scalr.io/reference/create_webhook_integration)", + type: "source", + dedupe: "unique", + hooks: { + ...common.hooks, + }, + methods: { + ...common.methods, + getWebhookEventType() { + return "run:errored"; + }, + async emitEvent(data) { + + this.$emit(data, { + id: data.run.id, + summary: `New run completed with ID ${data.run.id}`, + ts: Date.parse(data.run["created-at"]), + }); + }, + }, +}; diff --git a/components/scalr/sources/new-run-needs-attention/new-run-needs-attention.mjs b/components/scalr/sources/new-run-needs-attention/new-run-needs-attention.mjs new file mode 100644 index 0000000000000..b72a3faa81a50 --- /dev/null +++ b/components/scalr/sources/new-run-needs-attention/new-run-needs-attention.mjs @@ -0,0 +1,28 @@ +import common from "../common/common.mjs"; + +export default { + ...common, + name: "New Run Needs Attention (Instant)", + version: "0.0.1", + key: "scalr-new-run-needs-attention", + description: "Emit new event when a new run needs attention. [See the documentation](https://docs.scalr.io/reference/create_webhook_integration)", + type: "source", + dedupe: "unique", + hooks: { + ...common.hooks, + }, + methods: { + ...common.methods, + getWebhookEventType() { + return "run:needs_attention"; + }, + async emitEvent(data) { + + this.$emit(data, { + id: data.run.id, + summary: `New run with ID ${data.run.id} needs attention`, + ts: Date.parse(data.run["created-at"]), + }); + }, + }, +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b50f9ff381732..8865de6dcea7d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11443,7 +11443,11 @@ importers: specifier: ^1.5.1 version: 1.6.6 - components/scalr: {} + components/scalr: + dependencies: + '@pipedream/platform': + specifier: ^3.0.3 + version: 3.0.3 components/schedule: dependencies: