From ed2c9f649321dcf3eecb2703c68c3647f6c7f4cf Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Fri, 15 Mar 2024 15:22:56 +0800 Subject: [PATCH] Update Signed-off-by: SpiritZhou --- terraform/main.tf | 36 ++++++++++++++++++ terraform/modules/azure/event-grid/main.tf | 37 +++++++++++++++++++ terraform/modules/azure/event-grid/outputs.tf | 7 ++++ terraform/modules/azure/event-grid/vars.tf | 25 +++++++++++++ terraform/modules/azure/service-bus/main.tf | 6 +++ .../modules/azure/service-bus/outputs.tf | 8 ++++ 6 files changed, 119 insertions(+) create mode 100644 terraform/modules/azure/event-grid/main.tf create mode 100644 terraform/modules/azure/event-grid/outputs.tf create mode 100644 terraform/modules/azure/event-grid/vars.tf diff --git a/terraform/main.tf b/terraform/main.tf index 52b8dc5..99c0d2c 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -213,6 +213,26 @@ module "azure_servicebus_namespace_alternative" { tags = local.tags } +module "azure_servicebus_namespace_event_grid" { + source = "./modules/azure/service-bus" + resource_group_name = var.azure_resource_group_name + unique_project_name = var.unique_project_name + service_bus_suffix = "-event-grid" + service_bus_admin_identities = [ + module.azuread_applications.identity_1 + ] + tags = local.tags +} + +module "azurerm_eventgrid_topic" { + source = "./modules/azure/event-grid" + resource_group_name = var.azure_resource_group_name + unique_project_name = var.unique_project_name + service_bus_topic_id = module.azure_servicebus_namespace_event_grid.event_grid_receive_topic_id + + tags = local.tags +} + module "azure_storage_account" { source = "./modules/azure/storage-account" resource_group_name = var.azure_resource_group_name @@ -378,6 +398,22 @@ module "github_secrets" { name = "TF_AZURE_RABBIT_API_APPLICATION_ID" value = module.azure_rabbitmq_app_registration.application_id }, + { + name = "TF_AZURE_SERVICE_BUS_EVENTGRID_CONNECTION_STRING" + value = module.azure_servicebus_namespace_event_grid.connection_string + }, + { + name = "TF_AZURE_EVENT_GRID_ENDPOINT" + value = module.azurerm_eventgrid_topic.endpoint + }, + { + name = "TF_AZURE_EVENT_GRID_KEY" + value = module.azurerm_eventgrid_topic.key + }, + { + name = "TF_AZURE_SB_EVENT_GRID_RECEIVE_TOPIC" + value = module.azure_servicebus_namespace_event_grid.event_grid_receive_topic + }, ] } diff --git a/terraform/modules/azure/event-grid/main.tf b/terraform/modules/azure/event-grid/main.tf new file mode 100644 index 0000000..96e14f4 --- /dev/null +++ b/terraform/modules/azure/event-grid/main.tf @@ -0,0 +1,37 @@ +provider "azurerm" { + features {} + skip_provider_registration = true +} + +locals { + event_grid_name = "${var.unique_project_name}-e2e-event-grid" + event_grid_subscription_name = "${var.unique_project_name}-e2e-event-grid-subscription" +} + +data "azurerm_resource_group" "rg" { + name = var.resource_group_name +} + +resource "azurerm_eventgrid_topic" "eventgrid" { + name = local.event_grid_name + location = data.azurerm_resource_group.rg.location + resource_group_name = data.azurerm_resource_group.rg.name + input_schema = "CloudEventSchemaV1_0" + + tags = var.tags +} + +resource "azurerm_eventgrid_event_subscription" "eventsubscription" { + name = local.event_grid_subscription_name + scope = azurerm_eventgrid_topic.eventgrid.id + event_delivery_schema = "CloudEventSchemaV1_0" + + service_bus_topic_endpoint_id = var.service_bus_topic_id +} + +resource "azurerm_role_assignment" "roles" { + count = length(var.event_grid_admin_identities) + scope = azurerm_eventgrid_topic.eventgrid.id + role_definition_name = "Azure Event Grid Owner" + principal_id = var.event_grid_admin_identities[count.index].principal_id +} \ No newline at end of file diff --git a/terraform/modules/azure/event-grid/outputs.tf b/terraform/modules/azure/event-grid/outputs.tf new file mode 100644 index 0000000..827c577 --- /dev/null +++ b/terraform/modules/azure/event-grid/outputs.tf @@ -0,0 +1,7 @@ +output "endpoint" { + value = azurerm_eventgrid_topic.eventgrid.endpoint +} + +output "key" { + value = azurerm_eventgrid_topic.eventgrid.primary_access_key +} \ No newline at end of file diff --git a/terraform/modules/azure/event-grid/vars.tf b/terraform/modules/azure/event-grid/vars.tf new file mode 100644 index 0000000..5d9730a --- /dev/null +++ b/terraform/modules/azure/event-grid/vars.tf @@ -0,0 +1,25 @@ +variable "resource_group_name" { + type = string + description = "Resource group name where event hub will be placed" +} + +variable "unique_project_name" { + type = string + description = "Value to make unique every resource name generated" +} + +variable "service_bus_topic_id" { + type = string + description = "Service bus topic to subscription events" +} + +variable "tags" { + type = map(any) + description = "Tags to apply on every resource" +} + +variable "event_grid_admin_identities" { + type = list(any) + description = "Azure Service Bus Data Owner identities" + default = [] +} \ No newline at end of file diff --git a/terraform/modules/azure/service-bus/main.tf b/terraform/modules/azure/service-bus/main.tf index 08ecaa0..2185384 100644 --- a/terraform/modules/azure/service-bus/main.tf +++ b/terraform/modules/azure/service-bus/main.tf @@ -6,6 +6,7 @@ provider "azurerm" { locals { service_bus_namespace_name = "${var.unique_project_name}-e2e-servicebus-namespace${var.service_bus_suffix}" service_bus_authorization_rule = "${var.unique_project_name}-e2e-manage" + service_bus_topic_name = "${var.unique_project_name}-e2e-receive-event-grid-topic" } data "azurerm_resource_group" "rg" { @@ -20,6 +21,11 @@ resource "azurerm_servicebus_namespace" "namespace" { tags = var.tags } +resource "azurerm_servicebus_topic" "topic" { + name = local.service_bus_topic_name + namespace_id = azurerm_servicebus_namespace.namespace.id +} + resource "azurerm_servicebus_namespace_authorization_rule" "manage" { name = local.service_bus_authorization_rule namespace_id = azurerm_servicebus_namespace.namespace.id diff --git a/terraform/modules/azure/service-bus/outputs.tf b/terraform/modules/azure/service-bus/outputs.tf index 88aca17..eabd677 100644 --- a/terraform/modules/azure/service-bus/outputs.tf +++ b/terraform/modules/azure/service-bus/outputs.tf @@ -1,3 +1,11 @@ output "connection_string" { value = azurerm_servicebus_namespace_authorization_rule.manage.primary_connection_string +} + +output "event_grid_receive_topic_id" { + value = azurerm_servicebus_topic.topic.id +} + +output "event_grid_receive_topic" { + value = azurerm_servicebus_topic.topic.name } \ No newline at end of file