Skip to content

Commit

Permalink
update marketplace construct to make eventManager optional
Browse files Browse the repository at this point in the history
  • Loading branch information
suhussai committed May 30, 2024
1 parent c0fbde5 commit a3cc4b3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
sns = boto3.client('sns')
topic_arn = os.environ.get('SupportSNSArn')

event_bus = boto3.client('events')
eventbus_name = os.environ['EVENTBUS_NAME']
control_plane_event_source = os.environ['EVENT_SOURCE']
offboarding_detail_type = os.environ['OFFBOARDING_DETAIL_TYPE']
events_client = boto3.client('events')
eventbus_name = os.environ.get('EVENTBUS_NAME')
control_plane_event_source = os.environ.get('EVENT_SOURCE')
offboarding_detail_type = os.environ.get('OFFBOARDING_DETAIL_TYPE')


@logger.inject_lambda_context
Expand Down Expand Up @@ -69,17 +69,18 @@ def lambda_handler(event: DynamoDBStreamEvent, context: LambdaContext):
elif revoke_access:
subject = 'AWS Marketplace customer end of subscription'
message = f'unsubscribe-success: {json.dumps(new_image)}'
response = event_bus.put_events(
Entries=[
{
'EventBusName': eventbus_name,
'Source': control_plane_event_source,
'DetailType': offboarding_detail_type,
'Detail': json.dumps(new_image),
}
]
)
logger.info(response)
if eventbus_name:
response = events_client.put_events(
Entries=[
{
'EventBusName': eventbus_name,
'Source': control_plane_event_source,
'DetailType': offboarding_detail_type,
'Detail': json.dumps(new_image),
}
]
)
logger.info(response)

elif entitlement_updated:
subject = 'AWS Marketplace customer change of subscription'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@
metering_marketplace = boto3.client("meteringmarketplace", region_name="us-east-1")
marketplace_entitlement = boto3.client('marketplace-entitlement', region_name="us-east-1")
sqs = boto3.client("sqs")
event_bus = boto3.client('events')
events_client = boto3.client('events')

new_subscribers_table = os.environ["NEW_SUBSCRIBERS_TABLE_NAME"]
entitlement_queue_url = os.environ.get("ENTITLEMENT_QUEUE_URL")
marketplace_seller_email = os.environ.get("MARKETPLACE_SELLER_EMAIL")
onboarding_detail_type = os.environ['ONBOARDING_DETAIL_TYPE']
eventbus_name = os.environ['EVENTBUS_NAME']
control_plane_event_source = os.environ['EVENT_SOURCE']
required_fields = os.environ.get('REQUIRED_FIELDS', '').split(',')

eventbus_name = os.environ.get('EVENTBUS_NAME')
onboarding_detail_type = os.environ.get('ONBOARDING_DETAIL_TYPE')
control_plane_event_source = os.environ.get('EVENT_SOURCE')

new_subscribers_table_handler = dynamodb.Table(new_subscribers_table)


Expand Down Expand Up @@ -120,24 +121,25 @@ def register_new_subscriber():
for page in page_iterator:
entitlements.append(page['Entitlements'])

response = event_bus.put_events(
Entries=[
{
'EventBusName': eventbus_name,
'Source': control_plane_event_source,
'DetailType': onboarding_detail_type,
'Detail': json.dumps(
# add entitlements to inform sbt-aws onboarding process
new_subscriber_item | {
"entitlements": entitlements,
},
# to avoid datetime serialization issues
default=str
),
}
]
)
logger.info(response)
if eventbus_name:
response = events_client.put_events(
Entries=[
{
'EventBusName': eventbus_name,
'Source': control_plane_event_source,
'DetailType': onboarding_detail_type,
'Detail': json.dumps(
# add entitlements to inform sbt-aws onboarding process
new_subscriber_item | {
"entitlements": entitlements,
},
# to avoid datetime serialization issues
default=str
),
}
]
)
logger.info(response)

if entitlement_queue_url:
sqs.send_message(
Expand Down
45 changes: 35 additions & 10 deletions src/control-plane/aws-marketplace/saas-product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ export interface AWSMarketplaceSaaSProductProps {
/** SNS topic ARN provided from AWS Marketplace. Must exist. */
readonly subscriptionSNSTopic: string;

/** The event manager for the AWS Marketplace SaaS product. */
readonly eventManager: IEventManager;
/**
* The EventManager for the AWS Marketplace SaaS product.
* This is used to enable integration with sbt-aws.
*/
readonly eventManager?: IEventManager;

/** Flag to disable API logging. */
readonly disableAPILogging?: boolean;
Expand Down Expand Up @@ -159,9 +162,6 @@ export class AWSMarketplaceSaaSProduct extends Construct {
layers: [powerToolsLayer],
environment: {
SupportSNSArn: supportTopic.topicArn,
EVENTBUS_NAME: props.eventManager.busName,
EVENT_SOURCE: props.eventManager.controlPlaneEventSource,
OFFBOARDING_DETAIL_TYPE: DetailType.OFFBOARDING_REQUEST,
},
events: [
new DynamoEventSource(this.subscribersTable, {
Expand All @@ -171,7 +171,20 @@ export class AWSMarketplaceSaaSProduct extends Construct {
],
}
);
props.eventManager.grantPutEventsTo(grantOrRevokeAccessFunctionPython);

if (props.eventManager) {
grantOrRevokeAccessFunctionPython.addEnvironment('EVENTBUS_NAME', props.eventManager.busName);
grantOrRevokeAccessFunctionPython.addEnvironment(
'EVENT_SOURCE',
props.eventManager.controlPlaneEventSource
);
grantOrRevokeAccessFunctionPython.addEnvironment(
'OFFBOARDING_DETAIL_TYPE',
DetailType.OFFBOARDING_REQUEST
);

props.eventManager.grantPutEventsTo(grantOrRevokeAccessFunctionPython);
}

const baseRequiredFieldsForRegistration = ['regToken'];
this.userProvidedRequiredFieldsForRegistration = ['contactEmail'];
Expand Down Expand Up @@ -199,14 +212,26 @@ export class AWSMarketplaceSaaSProduct extends Construct {
layers: [powerToolsLayer],
environment: {
NEW_SUBSCRIBERS_TABLE_NAME: this.subscribersTable.tableName,
EVENTBUS_NAME: props.eventManager.busName,
EVENT_SOURCE: props.eventManager.controlPlaneEventSource,
ONBOARDING_DETAIL_TYPE: DetailType.ONBOARDING_REQUEST,
REQUIRED_FIELDS: requiredFields.join(','),
},
}
);
props.eventManager.grantPutEventsTo(registerNewMarketplaceCustomerPython);

if (props.eventManager) {
registerNewMarketplaceCustomerPython.addEnvironment(
'EVENTBUS_NAME',
props.eventManager.busName
);
registerNewMarketplaceCustomerPython.addEnvironment(
'EVENT_SOURCE',
props.eventManager.controlPlaneEventSource
);
registerNewMarketplaceCustomerPython.addEnvironment(
'ONBOARDING_DETAIL_TYPE',
DetailType.ONBOARDING_REQUEST
);
props.eventManager.grantPutEventsTo(registerNewMarketplaceCustomerPython);
}

let options: any = {
defaultCorsPreflightOptions: {
Expand Down

0 comments on commit a3cc4b3

Please sign in to comment.