/* * Copyright © 2020 Atomist, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import * as gcp from "@pulumi/gcp"; import * as pulumi from "@pulumi/pulumi"; export function createTopic( gcpProject: string, prefix: string, overrides: gcp.pubsub.TopicState = {}, ): gcp.pubsub.Topic { return new gcp.pubsub.Topic(`${prefix}-topic`, { project: gcpProject, name: prefix, ...overrides, labels: { managed_by: "pulumi", type: prefix.replace(/-/g, "_"), ...overrides?.labels, }, }); } export function createSubscription( gcpProject: string, prefix: string, topicId: pulumi.Output, overrides: gcp.pubsub.SubscriptionState = {}, ): gcp.pubsub.Subscription { return new gcp.pubsub.Subscription(`${prefix}-subscription`, { project: gcpProject, name: prefix, ackDeadlineSeconds: 120, messageRetentionDuration: "604800s", topic: topicId, ...overrides, expirationPolicy: { ttl: "", ...overrides?.expirationPolicy, }, labels: { managed_by: "pulumi", type: prefix.replace(/-/g, "_"), ...overrides?.labels, }, }); } export function createTopicAndSubscription( gcpProject: string, prefix: string, overrides?: { ackDeadlineSeconds?: number; precreatedTopic?: gcp.pubsub.Topic; topic?: gcp.pubsub.TopicState; subscription?: gcp.pubsub.SubscriptionState; }, ): { topic: gcp.pubsub.Topic; subscription: gcp.pubsub.Subscription; } { const topic = overrides?.precreatedTopic || createTopic(gcpProject, prefix, overrides?.topic); const subscription = createSubscription(gcpProject, prefix, topic.id, { ackDeadlineSeconds: overrides?.ackDeadlineSeconds, ...overrides?.subscription, }); return { topic, subscription, }; } export function createDeadLetteredTopicAndSubscription( gcpProject: string, pubsubServiceIdentity: gcp.projects.ServiceIdentity, prefix: string, overrides?: { topic?: gcp.pubsub.TopicState; subscription?: gcp.pubsub.SubscriptionState; }, ): { topic: gcp.pubsub.Topic; subscription: gcp.pubsub.Subscription } { const deadLetterTopic = new gcp.pubsub.Topic( `${prefix}-dead-letter-topic`, { project: gcpProject, name: `${prefix}-dead-letter`, labels: { managed_by: "pulumi", }, }, ); new gcp.pubsub.Subscription(`${prefix}-dead-letter-subscription`, { project: gcpProject, name: `${prefix}-dead-letter`, ackDeadlineSeconds: 10, expirationPolicy: { ttl: "", }, labels: { managed_by: "pulumi", }, messageRetentionDuration: "21600s", topic: deadLetterTopic.id, }); new gcp.pubsub.TopicIAMMember( `${prefix}-pubsub-dead-letter-topic-publish-member`, { project: gcpProject, topic: deadLetterTopic.id, member: pulumi.interpolate`serviceAccount:${pubsubServiceIdentity.email}`, role: "roles/pubsub.publisher", }, ); const { topic, subscription } = createTopicAndSubscription( gcpProject, prefix, { ...overrides, subscription: { ...overrides?.subscription, deadLetterPolicy: { deadLetterTopic: deadLetterTopic.id, maxDeliveryAttempts: 20, ...overrides?.subscription?.deadLetterPolicy, }, }, }, ); new gcp.pubsub.SubscriptionIAMMember( `${prefix}-pubsub-dead-letter-subscription-subscriber-member`, { project: gcpProject, subscription: subscription.id, member: pulumi.interpolate`serviceAccount:${pubsubServiceIdentity.email}`, role: "roles/pubsub.subscriber", }, ); return { topic, subscription }; }