From 21927016c7a40e3b0e85d16fda2048b0492b9a67 Mon Sep 17 00:00:00 2001 From: Koon Wei Teo Date: Wed, 18 Sep 2024 12:26:10 +0800 Subject: [PATCH] feat: add post deploy for azure and gcp --- .../javascript/event/src/connector/actions.js | 54 ++++++++--------- .../event/src/connector/post-deploy.js | 40 ++++++++++--- .../typescript/event/src/connector/actions.ts | 58 +++++++++---------- .../event/src/connector/post-deploy.ts | 42 +++++++++++--- 4 files changed, 121 insertions(+), 73 deletions(-) diff --git a/application-templates/javascript/event/src/connector/actions.js b/application-templates/javascript/event/src/connector/actions.js index ae728ee..bfc41bb 100644 --- a/application-templates/javascript/event/src/connector/actions.js +++ b/application-templates/javascript/event/src/connector/actions.js @@ -1,46 +1,41 @@ const CUSTOMER_CREATE_SUBSCRIPTION_KEY = 'myconnector-customerCreateSubscription'; -export async function createCustomerCreateSubscription( +export async function createGcpPubSubCustomerCreateSubscription( apiRoot, topicName, projectId ) { - const { - body: { results: subscriptions }, - } = await apiRoot - .subscriptions() - .get({ - queryArgs: { - where: `key = "${CUSTOMER_CREATE_SUBSCRIPTION_KEY}"`, - }, - }) - .execute(); - - if (subscriptions.length > 0) { - const subscription = subscriptions[0]; + const destination = { + type: 'GoogleCloudPubSub', + topic: topicName, + projectId, + }; + await createSubscription(apiRoot, destination); +} - await apiRoot - .subscriptions() - .withKey({ key: CUSTOMER_CREATE_SUBSCRIPTION_KEY }) - .delete({ - queryArgs: { - version: subscription.version, - }, - }) - .execute(); - } +export async function createAzureServiceBusCustomerCreateSubscription( + apiRoot, + connectionString +) { + const destination = { + type: 'AzureServiceBus', + connectionString: connectionString, + }; + await createSubscription(apiRoot, destination); +} +async function createSubscription( + apiRoot, + destination +) { + await deleteCustomerCreateSubscription(apiRoot); await apiRoot .subscriptions() .post({ body: { key: CUSTOMER_CREATE_SUBSCRIPTION_KEY, - destination: { - type: 'GoogleCloudPubSub', - topic: topicName, - projectId, - }, + destination, messages: [ { resourceTypeId: 'customer', @@ -52,6 +47,7 @@ export async function createCustomerCreateSubscription( .execute(); } + export async function deleteCustomerCreateSubscription(apiRoot) { const { body: { results: subscriptions }, diff --git a/application-templates/javascript/event/src/connector/post-deploy.js b/application-templates/javascript/event/src/connector/post-deploy.js index cf53931..fe93fa0 100644 --- a/application-templates/javascript/event/src/connector/post-deploy.js +++ b/application-templates/javascript/event/src/connector/post-deploy.js @@ -3,22 +3,46 @@ dotenv.config(); import { createApiRoot } from '../client/create.client.js'; import { assertError, assertString } from '../utils/assert.utils.js'; -import { createCustomerCreateSubscription } from './actions.js'; +import { createGcpPubSubCustomerCreateSubscription, createAzureServiceBusCustomerCreateSubscription } from './actions.js'; const CONNECT_GCP_TOPIC_NAME_KEY = 'CONNECT_GCP_TOPIC_NAME'; const CONNECT_GCP_PROJECT_ID_KEY = 'CONNECT_GCP_PROJECT_ID'; +const CONNECT_PROVIDER_KEY = 'CONNECT_PROVIDER'; +const CONNECT_AZURE_CONNECTION_STRING_KEY = 'CONNECT_AZURE_CONNECTION_STRING'; async function postDeploy(properties) { - const topicName = properties.get(CONNECT_GCP_TOPIC_NAME_KEY); - const projectId = properties.get(CONNECT_GCP_PROJECT_ID_KEY); + const connectprovider = properties.get(CONNECT_PROVIDER_KEY); + assertString(connectprovider, CONNECT_PROVIDER_KEY); + const apiRoot = createApiRoot(); - assertString(topicName, CONNECT_GCP_TOPIC_NAME_KEY); - assertString(projectId, CONNECT_GCP_PROJECT_ID_KEY); + switch (connectprovider) { + case 'AZURE': { + const connectionString = properties.get( + CONNECT_AZURE_CONNECTION_STRING_KEY + ); + assertString(connectionString, CONNECT_AZURE_CONNECTION_STRING_KEY); - const apiRoot = createApiRoot(); - await createCustomerCreateSubscription(apiRoot, topicName, projectId); -} + await createAzureServiceBusCustomerCreateSubscription( + apiRoot, + connectionString + ); + break; + } + default: { + const topicName = properties.get(CONNECT_GCP_TOPIC_NAME_KEY); + const projectId = properties.get(CONNECT_GCP_PROJECT_ID_KEY); + assertString(topicName, CONNECT_GCP_TOPIC_NAME_KEY); + assertString(projectId, CONNECT_GCP_PROJECT_ID_KEY); + + await createGcpPubSubCustomerCreateSubscription( + apiRoot, + topicName, + projectId + ); + } + } +} async function run() { try { const properties = new Map(Object.entries(process.env)); diff --git a/application-templates/typescript/event/src/connector/actions.ts b/application-templates/typescript/event/src/connector/actions.ts index 22ccb7b..78cf9cc 100644 --- a/application-templates/typescript/event/src/connector/actions.ts +++ b/application-templates/typescript/event/src/connector/actions.ts @@ -1,48 +1,48 @@ +import { + AzureServiceBusDestination, + Destination, + GoogleCloudPubSubDestination, +} from '@commercetools/platform-sdk'; import { ByProjectKeyRequestBuilder } from '@commercetools/platform-sdk/dist/declarations/src/generated/client/by-project-key-request-builder'; const CUSTOMER_CREATE_SUBSCRIPTION_KEY = 'myconnector-customerCreateSubscription'; -export async function createCustomerCreateSubscription( +export async function createGcpPubSubCustomerCreateSubscription( apiRoot: ByProjectKeyRequestBuilder, topicName: string, projectId: string ): Promise { - const { - body: { results: subscriptions }, - } = await apiRoot - .subscriptions() - .get({ - queryArgs: { - where: `key = "${CUSTOMER_CREATE_SUBSCRIPTION_KEY}"`, - }, - }) - .execute(); - - if (subscriptions.length > 0) { - const subscription = subscriptions[0]; + const destination: GoogleCloudPubSubDestination = { + type: 'GoogleCloudPubSub', + topic: topicName, + projectId, + }; + await createSubscription(apiRoot, destination); +} - await apiRoot - .subscriptions() - .withKey({ key: CUSTOMER_CREATE_SUBSCRIPTION_KEY }) - .delete({ - queryArgs: { - version: subscription.version, - }, - }) - .execute(); - } +export async function createAzureServiceBusCustomerCreateSubscription( + apiRoot: ByProjectKeyRequestBuilder, + connectionString: string +): Promise { + const destination: AzureServiceBusDestination = { + type: 'AzureServiceBus', + connectionString: connectionString, + }; + await createSubscription(apiRoot, destination); +} +async function createSubscription( + apiRoot: ByProjectKeyRequestBuilder, + destination: Destination +) { + await deleteCustomerCreateSubscription(apiRoot); await apiRoot .subscriptions() .post({ body: { key: CUSTOMER_CREATE_SUBSCRIPTION_KEY, - destination: { - type: 'GoogleCloudPubSub', - topic: topicName, - projectId, - }, + destination, messages: [ { resourceTypeId: 'customer', diff --git a/application-templates/typescript/event/src/connector/post-deploy.ts b/application-templates/typescript/event/src/connector/post-deploy.ts index 98beee6..5c15a58 100644 --- a/application-templates/typescript/event/src/connector/post-deploy.ts +++ b/application-templates/typescript/event/src/connector/post-deploy.ts @@ -3,20 +3,48 @@ dotenv.config(); import { createApiRoot } from '../client/create.client'; import { assertError, assertString } from '../utils/assert.utils'; -import { createCustomerCreateSubscription } from './actions'; +import { + createAzureServiceBusCustomerCreateSubscription, + createGcpPubSubCustomerCreateSubscription, +} from './actions'; const CONNECT_GCP_TOPIC_NAME_KEY = 'CONNECT_GCP_TOPIC_NAME'; const CONNECT_GCP_PROJECT_ID_KEY = 'CONNECT_GCP_PROJECT_ID'; +const CONNECT_PROVIDER_KEY = 'CONNECT_PROVIDER'; +const CONNECT_AZURE_CONNECTION_STRING_KEY = 'CONNECT_AZURE_CONNECTION_STRING'; async function postDeploy(properties: Map): Promise { - const topicName = properties.get(CONNECT_GCP_TOPIC_NAME_KEY); - const projectId = properties.get(CONNECT_GCP_PROJECT_ID_KEY); + const connectprovider = properties.get(CONNECT_PROVIDER_KEY); + assertString(connectprovider, CONNECT_PROVIDER_KEY); + const apiRoot = createApiRoot(); - assertString(topicName, CONNECT_GCP_TOPIC_NAME_KEY); - assertString(projectId, CONNECT_GCP_PROJECT_ID_KEY); + switch (connectprovider) { + case 'AZURE': { + const connectionString = properties.get( + CONNECT_AZURE_CONNECTION_STRING_KEY + ); + assertString(connectionString, CONNECT_AZURE_CONNECTION_STRING_KEY); - const apiRoot = createApiRoot(); - await createCustomerCreateSubscription(apiRoot, topicName, projectId); + await createAzureServiceBusCustomerCreateSubscription( + apiRoot, + connectionString + ); + break; + } + default: { + const topicName = properties.get(CONNECT_GCP_TOPIC_NAME_KEY); + const projectId = properties.get(CONNECT_GCP_PROJECT_ID_KEY); + + assertString(topicName, CONNECT_GCP_TOPIC_NAME_KEY); + assertString(projectId, CONNECT_GCP_PROJECT_ID_KEY); + + await createGcpPubSubCustomerCreateSubscription( + apiRoot, + topicName, + projectId + ); + } + } } async function run(): Promise {