Skip to content

Commit

Permalink
feat: added the example list
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-calabrese committed Oct 16, 2024
1 parent 9d2c460 commit 0c4c0a8
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 56 deletions.
8 changes: 7 additions & 1 deletion src/_modules/data_factory_storage_account/data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ data "azurerm_storage_account" "target" {
}

data "azurerm_storage_containers" "this" {
for_each = var.what_to_migrate.blob.enabled && length(var.what_to_migrate.blob.containers) == 0 ? [1] : []
count = var.what_to_migrate.blob.enabled && length(var.what_to_migrate.blob.containers) == 0 ? 1 : 0
storage_account_id = data.azurerm_storage_account.source.id
}

data "azapi_resource_list" "tables" {
type = "Microsoft.Storage/storageAccounts/tableServices/tables@2021-09-01"
parent_id = "${data.azurerm_storage_account.source.id}/tableServices/default"
response_export_values = ["*"]
}
12 changes: 6 additions & 6 deletions src/_modules/data_factory_storage_account/datasets_containers.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
resource "azurerm_data_factory_custom_dataset" "source_dataset_container" {
for_each = local.containers
name = "${module.naming_convention.prefix}-adf-${var.storage_accounts.source.name}-${each.value.name}-blob-${module.naming_convention.suffix}"
data_factory_id = var.data_factory.id
data_factory_id = var.data_factory_id
type = "AzureBlob"

linked_service {
name = azurerm_data_factory_linked_service_azure_blob_storage.source_linked_service_blob.name
name = azurerm_data_factory_linked_service_azure_blob_storage.source_linked_service_blob[0].name
}

type_properties_json = jsonencode({
linkedServiceName = {
referenceName = azurerm_data_factory_linked_service_azure_blob_storage.source_linked_service_blob.name
referenceName = azurerm_data_factory_linked_service_azure_blob_storage.source_linked_service_blob[0].name
type = "LinkedServiceReference"
}
type = "AzureBlob"
Expand All @@ -21,16 +21,16 @@ resource "azurerm_data_factory_custom_dataset" "source_dataset_container" {
resource "azurerm_data_factory_custom_dataset" "target_dataset_container" {
for_each = local.containers
name = "${module.naming_convention.prefix}-adf-${var.storage_accounts.target.name}-${each.value.name}-blob-${module.naming_convention.suffix}"
data_factory_id = var.data_factory.id
data_factory_id = var.data_factory_id
type = "AzureBlob"

linked_service {
name = azurerm_data_factory_linked_service_azure_blob_storage.target_linked_service_blob.name
name = azurerm_data_factory_linked_service_azure_blob_storage.target_linked_service_blob[0].name
}

type_properties_json = jsonencode({
linkedServiceName = {
referenceName = azurerm_data_factory_linked_service_azure_blob_storage.target_linked_service_blob.name
referenceName = azurerm_data_factory_linked_service_azure_blob_storage.target_linked_service_blob[0].name
type = "LinkedServiceReference"
}
type = "AzureBlob"
Expand Down
8 changes: 4 additions & 4 deletions src/_modules/data_factory_storage_account/datasets_tables.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
resource "azurerm_data_factory_custom_dataset" "source_dataset_table" {
for_each = local.tables
name = "${module.naming_convention.prefix}-adf-${var.storage_accounts.source.name}-${each.value.name}-table-${module.naming_convention.suffix}"
data_factory_id = var.data_factory.id
data_factory_id = var.data_factory_id
type = "AzureTable"

linked_service {
name = azurerm_data_factory_linked_service_azure_blob_storage.source_linked_service_table.name
name = azurerm_data_factory_linked_service_azure_table_storage.source_linked_service_table[0].name
}

type_properties_json = jsonencode({
Expand All @@ -16,11 +16,11 @@ resource "azurerm_data_factory_custom_dataset" "source_dataset_table" {
resource "azurerm_data_factory_custom_dataset" "target_dataset_table" {
for_each = local.tables
name = "${module.naming_convention.prefix}-adf-${var.storage_accounts.target.name}-${each.value.name}-table-${module.naming_convention.suffix}"
data_factory_id = var.data_factory.id
data_factory_id = var.data_factory_id
type = "AzureTable"

linked_service {
name = azurerm_data_factory_linked_service_azure_blob_storage.target_linked_service_table.name
name = azurerm_data_factory_linked_service_azure_table_storage.target_linked_service_table[0].name
}

type_properties_json = jsonencode({
Expand Down
2 changes: 1 addition & 1 deletion src/_modules/data_factory_storage_account/iam.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module "roles" {
source = "github.com/pagopa/dx//infra/modules/azure_role_assignments?ref=main"
principal_id = var.function_app.user_func_02.principal_id
principal_id = var.data_factory_principal_id

storage_blob = var.what_to_migrate.blob.enabled ? [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
resource "azurerm_data_factory_linked_service_azure_blob_storage" "source_linked_service_blob" {
for_each = var.what_to_migrate.blob.enabled ? [1] : []
count = var.what_to_migrate.blob.enabled ? 1 : 0
name = "${module.naming_convention.prefix}-adf-${var.storage_accounts.source.name}-blob-${module.naming_convention.suffix}"
data_factory_id = var.data_factory.id
data_factory_id = var.data_factory_id

service_endpoint = "https://${data.azurerm_storage_account.source.id}.blob.core.windows.net"

use_managed_identity = true
}

resource "azurerm_data_factory_linked_service_azure_blob_storage" "target_linked_service_blob" {
for_each = var.what_to_migrate.blob.enabled ? [1] : []
count = var.what_to_migrate.blob.enabled ? 1 : 0
name = "${module.naming_convention.prefix}-adf-${var.storage_accounts.target.name}-blob-${module.naming_convention.suffix}"
data_factory_id = var.data_factory.id
data_factory_id = var.data_factory_id

service_endpoint = "https://${data.azurerm_storage_account.target.id}.blob.core.windows.net"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
resource "azurerm_data_factory_linked_service_azure_table_storage" "source_linked_service_table" {
for_each = var.what_to_migrate.table.enabled ? [1] : []
count = var.what_to_migrate.table.enabled ? 1 : 0
name = "${module.naming_convention.prefix}-adf-${var.storage_accounts.source.name}-table-${module.naming_convention.suffix}"
data_factory_id = var.data_factory.id
data_factory_id = var.data_factory_id

connection_string = data.azurerm_storage_account.source.primary_table_endpoint
}

resource "azurerm_data_factory_linked_service_azure_table_storage" "target_linked_service_table" {
for_each = var.what_to_migrate.table.enabled ? [1] : []
count = var.what_to_migrate.table.enabled ? 1 : 0
name = "${module.naming_convention.prefix}-adf-${var.storage_accounts.target.name}-table-${module.naming_convention.suffix}"
data_factory_id = var.data_factory.id
data_factory_id = var.data_factory_id

connection_string = data.azurerm_storage_account.target.primary_table_endpoint
}
4 changes: 2 additions & 2 deletions src/_modules/data_factory_storage_account/locals.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
locals {
containers = var.what_to_migrate.blob.enabled ? length(var.what_to_migrate.blob.containers) > 0 ? var.what_to_migrate.blob.containers : [for container in data.azurerm_storage_containers.this[0].containers : container.name] : []
tables = var.what_to_migrate.table.enabled ? var.what_to_migrate.table.tables : []
containers = var.what_to_migrate.blob.enabled ? (length(var.what_to_migrate.blob.containers) > 0 ? var.what_to_migrate.blob.containers : [for container in data.azurerm_storage_containers.this[0].containers : container.name]) : []
tables = var.what_to_migrate.table.enabled ? (length(var.what_to_migrate.table.tables) > 0 ? var.what_to_migrate.table.tables : [for table in jsondecode(data.azapi_resource_list.tables.output).value : table.TableName]) : []
}
9 changes: 9 additions & 0 deletions src/_modules/data_factory_storage_account/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
terraform {
required_providers {
azapi = {
source = "Azure/azapi"
version = "<= 1.15.0"
}
}
}

module "naming_convention" {
source = "github.com/pagopa/dx//infra/modules/azure_naming_convention/?ref=main"

Expand Down
27 changes: 27 additions & 0 deletions src/_modules/data_factory_storage_account/network.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
resource "azurerm_data_factory_managed_private_endpoint" "blob_source" {
name = "${module.naming_convention.prefix}-adf-${var.storage_accounts.source.name}-blob-${module.naming_convention.suffix}"
data_factory_id = var.data_factory_id
target_resource_id = data.azurerm_storage_account.source.id
subresource_name = "blob"
}

resource "azurerm_data_factory_managed_private_endpoint" "blob_target" {
name = "${module.naming_convention.prefix}-adf-${var.storage_accounts.target.name}-blob-${module.naming_convention.suffix}"
data_factory_id = var.data_factory_id
target_resource_id = data.azurerm_storage_account.target.id
subresource_name = "blob"
}

resource "azurerm_data_factory_managed_private_endpoint" "table_source" {
name = "${module.naming_convention.prefix}-adf-${var.storage_accounts.source.name}-table-${module.naming_convention.suffix}"
data_factory_id = var.data_factory_id
target_resource_id = data.azurerm_storage_account.source.id
subresource_name = "table"
}

resource "azurerm_data_factory_managed_private_endpoint" "table_target" {
name = "${module.naming_convention.prefix}-adf-${var.storage_accounts.target.name}-table-${module.naming_convention.suffix}"
data_factory_id = var.data_factory_id
target_resource_id = data.azurerm_storage_account.target.id
subresource_name = "table"
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
resource "azurerm_data_factory_pipeline" "pipeline_container" {
for_each = local.containers
name = "${module.naming_convention.prefix}-adf-${var.storage_accounts.source.name}-${each.value.name}-blob-${module.naming_convention.suffix}"
data_factory_id = var.data_factory.id

depends_on = [
azurerm_data_factory_custom_dataset.dataset_container
]
data_factory_id = var.data_factory_id

activities_json = jsonencode(
[
Expand Down
6 changes: 1 addition & 5 deletions src/_modules/data_factory_storage_account/pipeline_tables.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
resource "azurerm_data_factory_pipeline" "pipeline_table" {
for_each = local.tables
name = "${module.naming_convention.prefix}-adf-${var.storage_accounts.source.name}-${each.value.name}-table-${module.naming_convention.suffix}"
data_factory_id = var.data_factory.id

depends_on = [
azurerm_data_factory_custom_dataset.dataset_table
]
data_factory_id = var.data_factory_id

activities_json = jsonencode(
[
Expand Down
15 changes: 7 additions & 8 deletions src/_modules/data_factory_storage_account/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ variable "data_factory_id" {
type = string
}

variable "data_factory_principal_id" {
description = "Data Factory principal id to grant access to."
type = string
}

variable "storage_accounts" {
type = object({
source = object({
Expand All @@ -42,7 +47,7 @@ variable "what_to_migrate" {
table = optional(object(
{
enabled = bool
tables = list(string)
tables = optional(list(string), [])
}),
{ enabled = false }
)
Expand All @@ -54,11 +59,5 @@ variable "what_to_migrate" {
error_message = "At least one between blob and table should be enabled."
}

# validate that if table is enabled, at least one table is specified
validation {
condition = !(var.what_to_migrate.table.enabled && length(var.what_to_migrate.table.tables) == 0)
error_message = "If table is enabled, at least one table should be specified."
}

description = "List of databases, file shares, containers and tables to migrate."
description = "List of storage account containers and tables to migrate."
}
42 changes: 42 additions & 0 deletions src/migration/prod/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/migration/prod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

| Name | Version |
|------|---------|
| <a name="requirement_azapi"></a> [azapi](#requirement\_azapi) | <= 1.15.0 |
| <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) | <= 3.112.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | <= 3.112.0 |
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | 3.112.0 |

## Modules

Expand Down
24 changes: 9 additions & 15 deletions src/migration/prod/italynorth.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "azurerm_resource_group" "migration" {
name = "${local.project_itn}-migration-rg-01"
name = "${local.project_itn}-${local.environment.domain}-${local.environment.app_name}-rg-01"
location = "italynorth"

tags = local.tags
Expand All @@ -8,7 +8,7 @@ resource "azurerm_resource_group" "migration" {
# Create Azure Data Factory instances
# Enables system-assigned managed identity for secure access to resources
resource "azurerm_data_factory" "this" {
name = "${local.project_itn}-migration-adf-01"
name = "${local.project_itn}-${local.environment.domain}-${local.environment.app_name}-adf-01"
location = "italynorth"
resource_group_name = azurerm_resource_group.migration.name

Expand All @@ -20,25 +20,19 @@ resource "azurerm_data_factory" "this" {
}

resource "azurerm_data_factory_integration_runtime_azure" "azure_runtime" {
name = "${local.project_itn}-migration-adfir-01"
name = "${local.project_itn}-${local.environment.domain}-${local.environment.app_name}-adfir-01"
location = "italynorth"
data_factory_id = azurerm_data_factory.this.id
}

module "migrate_storage_accounts" {
for_each = local.storage_accounts
for_each = { for migration in local.storage_accounts : "${migration.source.name}|${migration.target.name}" => migration }
source = "../../_modules/data_factory_storage_account"

environment = {
prefix = local.prefix
env_short = local.env_short
location = "italynorth"
domain = "eng"
app_name = "mig"
instance_number = "01"
}
environment = local.environment

data_factory_id = azurerm_data_factory.this.id
data_factory_id = azurerm_data_factory.this.id
data_factory_principal_id = azurerm_data_factory.this.identity[0].principal_id

storage_accounts = {
source = each.value.source
Expand All @@ -48,12 +42,12 @@ module "migrate_storage_accounts" {
what_to_migrate = {
blob = {
enabled = each.value.blob.enabled
containers = each.value.blob.containers
containers = try(each.value.blob.containers, [])
}

table = {
enabled = each.value.table.enabled
tables = each.value.table.tables
tables = try(each.value.table.tables, [])
}
}
}
18 changes: 18 additions & 0 deletions src/migration/prod/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,29 @@ locals {
project_weu_legacy = "${local.prefix}-${local.env_short}"
secondary_project = "${local.prefix}-${local.env_short}-${local.location_short.germanywestcentral}"

environment = {
prefix = local.prefix
env_short = local.env_short
location = "italynorth"
domain = "eng"
app_name = "migitn"
instance_number = "01"
}

tags = {
CostCenter = "TS310 - PAGAMENTI & SERVIZI"
CreatedBy = "Terraform"
Environment = "Prod"
Owner = "IO"
Source = "https://github.com/pagopa/io-infra/blob/main/src/migration/prod"
}

storage_accounts = [
{
source = { name = "iopweuabc", resource_group_name = "abc" }
target = { name = "iopitnabc", resource_group_name = "abc" }
blob = { enabled = true }
table = { enabled = true }
}
]
}
Loading

0 comments on commit 0c4c0a8

Please sign in to comment.