Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 87 additions & 1 deletion src/lib/azure/services/eventgrid/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import {
} from '@cdktf/provider-azurerm/lib/data-azurerm-eventgrid-topic'
import { EventgridTopic } from '@cdktf/provider-azurerm/lib/eventgrid-topic'
import { EventgridEventSubscription } from '@cdktf/provider-azurerm/lib/eventgrid-event-subscription'
import { EventgridSystemTopic } from '@cdktf/provider-azurerm/lib/eventgrid-system-topic'
import { EventgridSystemTopicEventSubscription } from '@cdktf/provider-azurerm/lib/eventgrid-system-topic-event-subscription/index.js'
import { CommonAzureConstruct } from '../../common'
import { createAzureTfOutput } from '../../utils'
import { EventgridTopicProps, EventgridEventSubscriptionProps } from './types'
import {
EventgridTopicProps,
EventgridEventSubscriptionProps,
EventgridSystemTopicProps,
EventgridSystemTopicEventSubscriptionProps,
} from './types'

/**
* @classdesc Provides operations on Azure Event Grid
Expand Down Expand Up @@ -122,4 +129,83 @@ export class AzureEventgridManager {

return eventgridSubscription
}

/**
* @summary Method to create a new eventgrid system topic
* @param id scoped id of the resource
* @param scope scope in which this resource is defined
* @param props eventgrid system topic properties
* @see [CDKTF Eventgrid System Topic Module]{@link https://github.com/cdktf/cdktf-provider-azurerm/blob/main/docs/eventgridSystemTopic.typescript.md}
*/
public createEventgridSystemTopic(id: string, scope: CommonAzureConstruct, props: EventgridSystemTopicProps) {
if (!props) throw `Props undefined for ${id}`

const resourceGroup = new DataAzurermResourceGroup(scope, `${id}-est-rg`, {
name: scope.props.resourceGroupName
? scope.resourceNameFormatter.format(scope.props.resourceGroupName)
: `${props.resourceGroupName}`,
})

if (!resourceGroup) throw `Resource group undefined for ${id}`

const eventgridSystemTopic = new EventgridSystemTopic(scope, `${id}-est`, {
...props,
name: scope.resourceNameFormatter.format(props.name, scope.props.resourceNameOptions?.eventGridSystemTopic),
location: resourceGroup.location,
resourceGroupName: resourceGroup.name,
tags: props.tags ?? {
environment: scope.props.stage,
},
})

createAzureTfOutput(`${id}-eventgridSystemTopicName`, scope, eventgridSystemTopic.name)
createAzureTfOutput(`${id}-eventgridSystemTopicFriendlyUniqueId`, scope, eventgridSystemTopic.friendlyUniqueId)
createAzureTfOutput(`${id}-eventgridSystemTopicId`, scope, eventgridSystemTopic.id)

return eventgridSystemTopic
}

/**
* @summary Method to create a new eventgrid system topic subscription
* @param id scoped id of the resource
* @param scope scope in which this resource is defined
* @param props eventgrid system topic subscription properties
* @see [CDKTF Eventgrid System Topic Subscription Module]{@link https://github.com/cdktf/cdktf-provider-azurerm/blob/main/docs/eventgridSystemTopicEventSubscription.typescript.md}
*/
public createEventgridSystemTopicEventSubscription(
id: string,
scope: CommonAzureConstruct,
props: EventgridSystemTopicEventSubscriptionProps,
systemTopic: EventgridSystemTopic
) {
if (!props) throw `Props undefined for ${id}`

const resourceGroup = new DataAzurermResourceGroup(scope, `${id}-ests-rg`, {
name: scope.props.resourceGroupName
? scope.resourceNameFormatter.format(scope.props.resourceGroupName)
: `${props.resourceGroupName}`,
})

if (!resourceGroup) throw `Resource group undefined for ${id}`

const eventgridSystemTopicSubscription = new EventgridSystemTopicEventSubscription(scope, `${id}-ests`, {
...props,
name: scope.resourceNameFormatter.format(
props.name,
scope.props.resourceNameOptions?.eventGridSystemTopicEventSubscription
),
systemTopic: systemTopic.name,
resourceGroupName: resourceGroup.name,
})

createAzureTfOutput(`${id}-eventgridSystemTopicEventSubscriptionName`, scope, eventgridSystemTopicSubscription.name)
createAzureTfOutput(
`${id}-eventgridSystemTopicEventSubscriptionFriendlyUniqueId`,
scope,
eventgridSystemTopicSubscription.friendlyUniqueId
)
createAzureTfOutput(`${id}-eventgridSystemTopicEventSubscriptionId`, scope, eventgridSystemTopicSubscription.id)

return eventgridSystemTopicSubscription
}
}
6 changes: 6 additions & 0 deletions src/lib/azure/services/eventgrid/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { EventgridTopicConfig } from '@cdktf/provider-azurerm/lib/eventgrid-topic'
import { EventgridEventSubscriptionConfig } from '@cdktf/provider-azurerm/lib/eventgrid-event-subscription'
import { EventgridSystemTopicConfig } from '@cdktf/provider-azurerm/lib/eventgrid-system-topic'
import { EventgridSystemTopicEventSubscriptionConfig } from '@cdktf/provider-azurerm/lib/eventgrid-system-topic-event-subscription/index.js'

export interface EventgridTopicProps extends EventgridTopicConfig {}

export interface EventgridEventSubscriptionProps extends EventgridEventSubscriptionConfig {}

export interface EventgridSystemTopicProps extends EventgridSystemTopicConfig {}

export interface EventgridSystemTopicEventSubscriptionProps extends EventgridSystemTopicEventSubscriptionConfig {}
7 changes: 7 additions & 0 deletions src/test/azure/common/cdkConfig/eventgrid.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@
},
"testEventgridEventSubscription": {
"name": "test-eventgrid-subscription"
},
"testEventgridSystemTopic": {
"name": "test-eventgrid-system-topic",
"topicType": "Microsoft.Storage.StorageAccounts"
},
"testEventgridSystemEventSubscription": {
"name": "test-eventgrid-system-subscription"
}
}
42 changes: 42 additions & 0 deletions src/test/azure/services/eventgrid-manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { EventgridTopic } from '@cdktf/provider-azurerm/lib/eventgrid-topic'
import { EventgridEventSubscription } from '@cdktf/provider-azurerm/lib/eventgrid-event-subscription'
import { EventgridSystemTopic } from '@cdktf/provider-azurerm/lib/eventgrid-system-topic'
import { EventgridSystemTopicEventSubscription } from '@cdktf/provider-azurerm/lib/eventgrid-system-topic-event-subscription/index.js'
import { App, Testing } from 'cdktf'
import 'cdktf/lib/testing/adapters/jest'
import { Construct } from 'constructs'
Expand All @@ -9,11 +11,15 @@ import {
CommonAzureStackProps,
EventgridTopicProps,
EventgridEventSubscriptionProps,
EventgridSystemTopicProps,
EventgridSystemTopicEventSubscriptionProps,
} from '../../../lib'

interface TestAzureStackProps extends CommonAzureStackProps {
testEventgridTopic: EventgridTopicProps
testEventgridEventSubscription: EventgridEventSubscriptionProps
testEventgridSystemTopic: EventgridSystemTopicProps
testEventgridSystemEventSubscription: EventgridSystemTopicEventSubscriptionProps
testAttribute?: string
}

Expand Down Expand Up @@ -42,6 +48,8 @@ class TestCommonStack extends CommonAzureStack {
testAttribute: this.node.tryGetContext('testAttribute'),
testEventgridTopic: this.node.tryGetContext('testEventgridTopic'),
testEventgridEventSubscription: this.node.tryGetContext('testEventgridEventSubscription'),
testEventgridSystemTopic: this.node.tryGetContext('testEventgridSystemTopic'),
testEventgridSystemEventSubscription: this.node.tryGetContext('testEventgridSystemEventSubscription'),
}
}
}
Expand Down Expand Up @@ -72,6 +80,19 @@ class TestCommonConstruct extends CommonAzureConstruct {
this,
this.props.testEventgridEventSubscription
)

const eventgridSystemTopic = this.eventgridManager.createEventgridSystemTopic(
`test-eventgrid-system-topic-${this.props.stage}`,
this,
this.props.testEventgridSystemTopic
)

this.eventgridManager.createEventgridSystemTopicEventSubscription(
`test-eventgrid-subscription-${this.props.stage}`,
this,
this.props.testEventgridSystemEventSubscription,
eventgridSystemTopic
)
}
}

Expand Down Expand Up @@ -152,3 +173,24 @@ describe('TestAzureEventgridConstruct', () => {
})
})
})

describe('TestAzureEventgridConstruct', () => {
test('provisions eventgrid topic as expected', () => {
expect(construct).toHaveResourceWithProperties(EventgridSystemTopic, {
location: '${data.azurerm_resource_group.test-eventgrid-system-topic-dev-est-rg.location}',
name: 'test-eventgrid-system-topic-dev',
resource_group_name: '${data.azurerm_resource_group.test-eventgrid-system-topic-dev-est-rg.name}',
tags: {
environment: 'dev',
},
})
})
})

describe('TestAzureEventgridConstruct', () => {
test('provisions eventgrid subscription as expected', () => {
expect(construct).toHaveResourceWithProperties(EventgridSystemTopicEventSubscription, {
name: 'test-eventgrid-system-subscription-dev',
})
})
})