Skip to content

Commit

Permalink
implement fixes requested for Connect Certification (2023-10-02) (#180)
Browse files Browse the repository at this point in the history
* improve subscription management in Connect deployment scripts
  • Loading branch information
andreshernandez-e2x authored Oct 2, 2023
1 parent 8afc5e4 commit 0bb5471
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 146 deletions.
86 changes: 86 additions & 0 deletions plugin/scripts/common/commercetools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { getApiRoot } from '../../src/infrastructure/driven/commercetools/ctService';
import { ByProjectKeyRequestBuilder } from '@commercetools/platform-sdk/dist/declarations/src/generated/client/by-project-key-request-builder';
import { Subscription, SubscriptionDraft } from '@commercetools/platform-sdk';

const ctApiRoot: ByProjectKeyRequestBuilder = getApiRoot();

const connect_env = process.env.CONNECT_ENV || 'connect_dev';

export const subscriptions = [
{
resource: 'product',
types: ['ProductPublished', 'ProductUnpublished'],
changes: ['product'],
},
{
resource: 'inventory-entry',
types: [],
changes: ['inventory-entry'],
},
{
resource: 'category',
types: ['CategoryCreated'],
changes: ['category'],
},
{
resource: 'order',
types: ['OrderCreated', 'OrderStateChanged', 'OrderImported', 'OrderCustomerSet'],
changes: ['order'],
},
{
resource: 'customer',
types: ['CustomerCreated'],
changes: ['customer'],
},
{
resource: 'payment',
types: ['PaymentTransactionAdded', 'PaymentTransactionStateChanged'],
changes: ['payment'],
},
];

export const getSubscriptions = async () => {
return (
await ctApiRoot
.subscriptions()
.get({
queryArgs: {
where: `key = "${subscriptions
.map((sub) => `connect-${connect_env}-${sub.resource}`)
.join('" or key = "')}"`,
},
})
.execute()
).body.results;
};

export const createSubscription = async (body: SubscriptionDraft) => {
await ctApiRoot
.subscriptions()
.post({
body,
})
.execute();
};

export const updateSubscription = async (key: string, body: SubscriptionDraft) => {
await ctApiRoot
.subscriptions()
.withKey({ key })
.post({
body,
})
.execute();
};

export const deleteSubscription = async (key: string, version: number) => {
await ctApiRoot
.subscriptions()
.withKey({ key })
.delete({
queryArgs: {
version,
},
})
.execute();
};
136 changes: 43 additions & 93 deletions plugin/scripts/post-deploy.ts
Original file line number Diff line number Diff line change
@@ -1,124 +1,74 @@
import { getApiRoot } from '../src/infrastructure/driven/commercetools/ctService';
import { ByProjectKeyRequestBuilder } from '@commercetools/platform-sdk/dist/declarations/src/generated/client/by-project-key-request-builder';
import { Subscription } from '@commercetools/platform-sdk';
import logger from '../src/utils/log';

const ctApiRoot: ByProjectKeyRequestBuilder = getApiRoot();
import { subscriptions, getSubscriptions, createSubscription, updateSubscription } from './common/commercetools';

const connect_env = process.env.CONNECT_ENV || 'connect_dev';

const subscriptions = [
{
resource: 'product',
types: ['ProductPublished', 'ProductUnpublished'],
changes: ['product'],
},
{
resource: 'inventory-entry',
types: [],
changes: ['inventory-entry'],
},
{
resource: 'category',
types: ['CategoryCreated'],
changes: ['category'],
},
{
resource: 'order',
types: ['OrderCreated', 'OrderStateChanged', 'OrderImported', 'OrderCustomerSet'],
changes: ['order'],
},
{
resource: 'customer',
types: ['CustomerCreated'],
changes: ['customer'],
},
{
resource: 'payment',
types: ['PaymentTransactionAdded', 'PaymentTransactionStateChanged'],
changes: ['payment'],
},
];

const run = async () => {
await configureSubscriptions();
};

const configureSubscriptions = async () => {
let ctSubscriptions: Subscription[] = (await ctApiRoot.subscriptions().get().execute()).body.results;
ctSubscriptions = ctSubscriptions.filter((sub) => sub.key.includes(`connect-${connect_env}`));
const ctSubscriptionKeys = ctSubscriptions.map((sub) => sub.key);
const ctSubscriptions: Subscription[] = await getSubscriptions();

subscriptions.forEach(async (sub) => {
const subscriptionExists = ctSubscriptionKeys.includes(`connect-${connect_env}-${sub.resource}`);
const existingSubscription = ctSubscriptions.find((s) => s.key === `connect-${connect_env}-${sub.resource}`);
try {
if (subscriptionExists) {
await ctApiRoot
.subscriptions()
.withKey({ key: `connect-${connect_env}-${sub.resource}` })
.post({
body: {
version: ctSubscriptions.find((s) => s.key === `connect-${connect_env}-${sub.resource}`)
.version,
actions: [
{
action: 'setMessages',
messages: [
{
resourceTypeId: sub.resource,
types: sub.types,
},
],
},
{
action: 'setChanges',
changes: sub.changes.map((c) => {
return {
resourceTypeId: c,
};
}),
},
{
action: 'changeDestination',
destination: {
type: 'GoogleCloudPubSub',
topic: process.env.CONNECT_GCP_TOPIC_NAME,
projectId: process.env.CONNECT_GCP_PROJECT_ID,
},
},
],
},
})
.execute();
} else {
await ctApiRoot
.subscriptions()
.post({
body: {
key: `connect-${connect_env}-${sub.resource}`,
destination: {
type: 'GoogleCloudPubSub',
topic: process.env.CONNECT_GCP_TOPIC_NAME,
projectId: process.env.CONNECT_GCP_PROJECT_ID,
},
if (existingSubscription) {
await updateSubscription(`connect-${connect_env}-${sub.resource}`, {
version: existingSubscription.version,
actions: [
{
action: 'setMessages',
messages: [
{
resourceTypeId: sub.resource,
types: sub.types,
},
],
},
{
action: 'setChanges',
changes: sub.changes.map((c) => {
return {
resourceTypeId: c,
};
}),
},
})
.execute();
{
action: 'changeDestination',
destination: {
type: 'GoogleCloudPubSub',
topic: process.env.CONNECT_GCP_TOPIC_NAME,
projectId: process.env.CONNECT_GCP_PROJECT_ID,
},
},
],
});
} else {
await createSubscription({
key: `connect-${connect_env}-${sub.resource}`,
destination: {
type: 'GoogleCloudPubSub',
topic: process.env.CONNECT_GCP_TOPIC_NAME,
projectId: process.env.CONNECT_GCP_PROJECT_ID,
},
messages: [
{
resourceTypeId: sub.resource,
types: sub.types,
},
],
changes: sub.changes.map((c) => {
return {
resourceTypeId: c,
};
}),
});
}
} catch (error) {
logger.error(
`PostDeploy: Failed to ${subscriptionExists ? 'update' : 'create'} connect-${
`PostDeploy: Failed to ${existingSubscription ? 'update' : 'create'} connect-${connect_env}-${
sub.resource
} subscription: ${error.message}`,
error,
Expand Down
59 changes: 6 additions & 53 deletions plugin/scripts/pre-undeploy.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,24 @@
import { getApiRoot } from '../src/infrastructure/driven/commercetools/ctService';
import { ByProjectKeyRequestBuilder } from '@commercetools/platform-sdk/dist/declarations/src/generated/client/by-project-key-request-builder';
import { Subscription } from '@commercetools/platform-sdk';
import logger from '../src/utils/log';

const ctApiRoot: ByProjectKeyRequestBuilder = getApiRoot();
import { subscriptions, getSubscriptions, deleteSubscription } from './common/commercetools';

const connect_env = process.env.CONNECT_ENV || 'connect_dev';

const subscriptions = [
{
resource: 'product',
types: ['ProductPublished', 'ProductUnpublished'],
changes: ['product'],
},
{
resource: 'inventory-entry',
types: [],
changes: ['inventory-entry'],
},
{
resource: 'category',
types: ['CategoryCreated'],
changes: ['category'],
},
{
resource: 'order',
types: ['OrderCreated', 'OrderStateChanged', 'OrderImported', 'OrderCustomerSet'],
changes: ['order'],
},
{
resource: 'customer',
types: ['CustomerCreated'],
changes: ['customer'],
},
{
resource: 'payment',
types: ['PaymentTransactionAdded', 'PaymentTransactionStateChanged'],
changes: ['payment'],
},
];

const run = async () => {
await deleteSubscriptions();
};

const deleteSubscriptions = async () => {
let ctSubscriptions: Subscription[] = (await ctApiRoot.subscriptions().get().execute()).body.results;
ctSubscriptions = ctSubscriptions.filter((sub) => sub.key.includes(`connect-${connect_env}`));
const ctSubscriptionKeys = ctSubscriptions.map((sub) => sub.key);
const ctSubscriptions: Subscription[] = await getSubscriptions();

subscriptions.forEach(async (sub) => {
const subscriptionExists = ctSubscriptionKeys.includes(`connect-${connect_env}-${sub.resource}`);
if (subscriptionExists) {
const existingSubscription = ctSubscriptions.find((s) => s.key === `connect-${connect_env}-${sub.resource}`);
if (existingSubscription) {
try {
await ctApiRoot
.subscriptions()
.withKey({ key: `connect-${connect_env}-${sub.resource}` })
.delete({
queryArgs: {
version: ctSubscriptions.find((s) => s.key === `connect-${connect_env}-${sub.resource}`)
.version,
},
})
.execute();
await deleteSubscription(`connect-${connect_env}-${sub.resource}`, existingSubscription.version);
} catch (error) {
logger.error(
`PreUndeploy: Failed to delete connect-${sub.resource} subscription: ${error.message}`,
`PreUndeploy: Failed to delete connect-${connect_env}-${sub.resource} subscription: ${error.message}`,
error,
);
}
Expand Down

0 comments on commit 0bb5471

Please sign in to comment.