Skip to content

Commit

Permalink
feat: add post deploy for azure and gcp
Browse files Browse the repository at this point in the history
  • Loading branch information
koonweiteo-commercetools committed Sep 18, 2024
1 parent 5010723 commit 2192701
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 73 deletions.
54 changes: 25 additions & 29 deletions application-templates/javascript/event/src/connector/actions.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -52,6 +47,7 @@ export async function createCustomerCreateSubscription(
.execute();
}


export async function deleteCustomerCreateSubscription(apiRoot) {
const {
body: { results: subscriptions },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
58 changes: 29 additions & 29 deletions application-templates/typescript/event/src/connector/actions.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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<void> {
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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>): Promise<void> {
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<void> {
Expand Down

0 comments on commit 2192701

Please sign in to comment.