Skip to content

Commit

Permalink
feat: IM-1016 add post deploy for azure and gcp (#90)
Browse files Browse the repository at this point in the history
* feat: add post deploy for azure and gcp

* chore: remove spaces

* chore: rename variable to be camelCase
  • Loading branch information
koonweiteo-commercetools authored Oct 2, 2024
1 parent e12d8a7 commit dca3413
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 75 deletions.
50 changes: 21 additions & 29 deletions application-templates/javascript/event/src/connector/actions.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,38 @@
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 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);

assertString(topicName, CONNECT_GCP_TOPIC_NAME_KEY);
assertString(projectId, CONNECT_GCP_PROJECT_ID_KEY);

const connectProvider = properties.get(CONNECT_PROVIDER_KEY);
assertString(connectProvider, CONNECT_PROVIDER_KEY);
const apiRoot = createApiRoot();
await createCustomerCreateSubscription(apiRoot, topicName, projectId);
}

switch (connectProvider) {
case 'AZURE': {
const connectionString = properties.get(
CONNECT_AZURE_CONNECTION_STRING_KEY
);
assertString(connectionString, CONNECT_AZURE_CONNECTION_STRING_KEY);
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,45 @@ 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);

assertString(topicName, CONNECT_GCP_TOPIC_NAME_KEY);
assertString(projectId, CONNECT_GCP_PROJECT_ID_KEY);

const connectProvider = properties.get(CONNECT_PROVIDER_KEY);
assertString(connectProvider, CONNECT_PROVIDER_KEY);
const apiRoot = createApiRoot();
await createCustomerCreateSubscription(apiRoot, topicName, projectId);

switch (connectProvider) {
case 'AZURE': {
const connectionString = properties.get(
CONNECT_AZURE_CONNECTION_STRING_KEY
);
assertString(connectionString, CONNECT_AZURE_CONNECTION_STRING_KEY);
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 dca3413

Please sign in to comment.