Skip to content

Commit 2e7abea

Browse files
committed
updated the code with required fields
1 parent 8e95bb1 commit 2e7abea

File tree

6 files changed

+186
-108
lines changed

6 files changed

+186
-108
lines changed

example/complete/azure/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Mongodb Example
2+
![squareops_avatar]
3+
4+
[squareops_avatar]: https://squareops.com/wp-content/uploads/2022/12/squareops-logo.png
5+
6+
### [SquareOps Technologies](https://squareops.com/) Your DevOps Partner for Accelerating cloud journey.
7+
<br>
8+
This example will be very useful for users who are new to a module and want to quickly learn how to use it. By reviewing the examples, users can gain a better understanding of how the module works, what features it supports, and how to customize it to their specific needs.
9+
10+
## Requirements
11+
12+
No requirements.
13+
14+
## Providers
15+
16+
| Name | Version |
17+
|------|---------|
18+
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | 3.70.0 |
19+
20+
## Modules
21+
22+
| Name | Source | Version |
23+
|------|--------|---------|
24+
| <a name="module_azure"></a> [azure](#module\_azure) | squareops/mongodb/kubernetes//provider/azure | n/a |
25+
| <a name="module_mongodb"></a> [mongodb](#module\_mongodb) | squareops/mongodb/kubernetes | n/a |
26+
27+
## Resources
28+
29+
| Name | Type |
30+
|------|------|
31+
| [azurerm_kubernetes_cluster.primary](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/kubernetes_cluster) | data source |
32+
33+
## Inputs
34+
35+
No inputs.
36+
37+
## Outputs
38+
39+
| Name | Description |
40+
|------|-------------|
41+
| <a name="output_mongodb_credential"></a> [mongodb\_credential](#output\_mongodb\_credential) | MongoDB credentials used for accessing the MongoDB database. |
42+
| <a name="output_mongodb_endpoints"></a> [mongodb\_endpoints](#output\_mongodb\_endpoints) | MongoDB endpoints in the Kubernetes cluster. |

example/complete/azure/main.tf

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,27 @@ locals {
1616
metric_exporter_password = "nvAHhm1uGQNYWVw6ZyAH"
1717
}
1818

19-
azure_storage_account_name = "skaftest"
20-
azure_container_name = "mongodb-backup-conatiner"
19+
azure_storage_account_name = ""
20+
azure_container_name = ""
2121
}
2222

2323
module "azure" {
24-
source = "../../../provider/azure"
25-
resource_group_name = "prod-skaf-rg"
26-
resource_group_location = "eastus"
24+
source = "squareops/mongodb/kubernetes//provider/azure"
25+
resource_group_name = ""
26+
resource_group_location = ""
2727
name = local.name
2828
environment = local.environment
2929
mongodb_custom_credentials_enabled = local.mongodb_custom_credentials_enabled
3030
mongodb_custom_credentials_config = local.mongodb_custom_credentials_config
3131
store_password_to_secret_manager = local.store_password_to_secret_manager
32+
storage_account_name = local.azure_storage_account_name
3233
}
3334

3435
module "mongodb" {
3536
source = "squareops/mongodb/kubernetes"
36-
cluster_name = "prod-skaf-aks"
37-
resource_group_name = "prod-skaf-rg"
38-
resource_group_location = "eastus"
37+
cluster_name = ""
38+
resource_group_name = ""
39+
resource_group_location = ""
3940
mongodb_config = {
4041
name = local.name
4142
values_yaml = file("./helm/values.yaml")
@@ -48,10 +49,10 @@ module "mongodb" {
4849
}
4950
mongodb_custom_credentials_enabled = local.mongodb_custom_credentials_enabled
5051
mongodb_custom_credentials_config = local.mongodb_custom_credentials_config
51-
root_password = local.mongodb_custom_credentials_enabled ? "" : module.azure.root_password
52-
metric_exporter_pasword = local.mongodb_custom_credentials_enabled ? "" : module.azure.metric_exporter_pasword
53-
bucket_provider_type = "azure"
54-
mongodb_backup_enabled = false
52+
root_password = local.mongodb_custom_credentials_enabled ? "" : module.azure.root_password
53+
metric_exporter_pasword = local.mongodb_custom_credentials_enabled ? "" : module.azure.metric_exporter_pasword
54+
bucket_provider_type = "azure"
55+
mongodb_backup_enabled = false
5556
mongodb_backup_config = {
5657
bucket_uri = "https://${local.azure_storage_account_name}.blob.core.windows.net/${local.azure_container_name}"
5758
azure_storage_account_name = local.azure_storage_account_name

provider/azure/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Azure Mongodb Kubernetes Module
2+
## Requirements
3+
4+
No requirements.
5+
6+
## Providers
7+
8+
| Name | Version |
9+
|------|---------|
10+
| <a name="provider_azurerm"></a> [azurerm](#provider\_azurerm) | n/a |
11+
| <a name="provider_random"></a> [random](#provider\_random) | n/a |
12+
13+
## Modules
14+
15+
No modules.
16+
17+
## Resources
18+
19+
| Name | Type |
20+
|------|------|
21+
| [azurerm_key_vault.mongo-secret](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault) | resource |
22+
| [azurerm_key_vault_secret.mongo-secret](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault_secret) | resource |
23+
| [azurerm_role_assignment.pod_identity_assignment_backup](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) | resource |
24+
| [azurerm_role_assignment.secretadmin_backup](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) | resource |
25+
| [azurerm_role_assignment.secretadmin_restore](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) | resource |
26+
| [azurerm_role_assignment.service_account_token_creator_backup](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) | resource |
27+
| [azurerm_role_assignment.service_account_token_creator_restore](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) | resource |
28+
| [azurerm_user_assigned_identity.mongo_backup_identity](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/user_assigned_identity) | resource |
29+
| [azurerm_user_assigned_identity.mongo_restore_identity](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/user_assigned_identity) | resource |
30+
| [azurerm_user_assigned_identity.pod_identity_backup](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/user_assigned_identity) | resource |
31+
| [random_password.mongodb_exporter_password](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) | resource |
32+
| [random_password.mongodb_root_password](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) | resource |
33+
| [azurerm_client_config.current](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/client_config) | data source |
34+
| [azurerm_subscription.current](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/subscription) | data source |
35+
| [azurerm_subscription.primary](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/subscription) | data source |
36+
37+
## Inputs
38+
39+
| Name | Description | Type | Default | Required |
40+
|------|-------------|------|---------|:--------:|
41+
| <a name="input_azure_uai_backup_name"></a> [azure\_uai\_backup\_name](#input\_azure\_uai\_backup\_name) | Azure User Assigned Identity name for backup | `string` | `"mongo-backup"` | no |
42+
| <a name="input_azure_uai_pod_identity_backup_name"></a> [azure\_uai\_pod\_identity\_backup\_name](#input\_azure\_uai\_pod\_identity\_backup\_name) | Azure User Assigned Identity name for pod identity backup | `string` | `"pod-identity-backup"` | no |
43+
| <a name="input_azure_uai_pod_identity_restore_name"></a> [azure\_uai\_pod\_identity\_restore\_name](#input\_azure\_uai\_pod\_identity\_restore\_name) | Azure User Assigned Identity name for pod identity restore | `string` | `"pod-identity-restore"` | no |
44+
| <a name="input_azure_uai_restore_name"></a> [azure\_uai\_restore\_name](#input\_azure\_uai\_restore\_name) | Azure User Assigned Identity name for restore | `string` | `"mongo-restore"` | no |
45+
| <a name="input_environment"></a> [environment](#input\_environment) | Environment in which the infrastructure is being deployed (e.g., production, staging, development) | `string` | `"test"` | no |
46+
| <a name="input_mongodb_config"></a> [mongodb\_config](#input\_mongodb\_config) | Specify the configuration settings for Mongodb, including the name, environment, storage options, replication settings, and custom YAML values. | `any` | <pre>{<br> "architecture": "",<br> "environment": "",<br> "name": "",<br> "replica_count": 2,<br> "storage_class_name": "",<br> "store_password_to_secret_manager": true,<br> "values_yaml": "",<br> "volume_size": ""<br>}</pre> | no |
47+
| <a name="input_mongodb_custom_credentials_config"></a> [mongodb\_custom\_credentials\_config](#input\_mongodb\_custom\_credentials\_config) | Specify the configuration settings for Mongodb to pass custom credentials during creation. | `any` | <pre>{<br> "metric_exporter_password": "",<br> "metric_exporter_user": "",<br> "root_password": "",<br> "root_user": ""<br>}</pre> | no |
48+
| <a name="input_mongodb_custom_credentials_enabled"></a> [mongodb\_custom\_credentials\_enabled](#input\_mongodb\_custom\_credentials\_enabled) | Specifies whether to enable custom credentials for MongoDB database. | `bool` | `false` | no |
49+
| <a name="input_name"></a> [name](#input\_name) | Name of all the resources | `string` | `""` | no |
50+
| <a name="input_resource_group_location"></a> [resource\_group\_location](#input\_resource\_group\_location) | Azure region | `string` | `"East US"` | no |
51+
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | Azure Resource Group name | `string` | `""` | no |
52+
| <a name="input_storage_account_name"></a> [storage\_account\_name](#input\_storage\_account\_name) | Azure storage account name | `string` | `""` | no |
53+
| <a name="input_store_password_to_secret_manager"></a> [store\_password\_to\_secret\_manager](#input\_store\_password\_to\_secret\_manager) | Specifies whether to store the credentials in GCP secret manager. | `bool` | `false` | no |
54+
55+
## Outputs
56+
57+
| Name | Description |
58+
|------|-------------|
59+
| <a name="output_az_account_backup"></a> [az\_account\_backup](#output\_az\_account\_backup) | Azure User Assigned Identity for backup |
60+
| <a name="output_az_account_restore"></a> [az\_account\_restore](#output\_az\_account\_restore) | Azure User Assigned Identity for restore |
61+
| <a name="output_metric_exporter_pasword"></a> [metric\_exporter\_pasword](#output\_metric\_exporter\_pasword) | mongodb\_exporter user's password of MongoDB |
62+
| <a name="output_root_password"></a> [root\_password](#output\_root\_password) | Root user's password of MongoDB |

provider/azure/main.tf

Lines changed: 62 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,31 @@ data "azurerm_subscription" "current" {}
44

55
data "azurerm_subscription" "primary" {}
66

7-
resource "azurerm_role_definition" "blob_storage_access" {
8-
name = "BlobStorageAccess"
9-
description = "Role definition for accessing Azure Blob Storage"
10-
scope = "/subscriptions/${data.azurerm_subscription.current.subscription_id}"
11-
12-
permissions {
13-
actions = [
14-
"Microsoft.Storage/storageAccounts/blobServices/containers/read",
15-
"Microsoft.Storage/storageAccounts/blobServices/containers/write",
16-
"Microsoft.Storage/storageAccounts/blobServices/containers/delete",
17-
]
18-
19-
not_actions = []
20-
}
21-
22-
assignable_scopes = [
23-
"/subscriptions/${data.azurerm_subscription.current.subscription_id}",
24-
]
25-
}
26-
27-
resource "azurerm_user_assigned_identity" "mongo_backup_identity" {
28-
name = format("%s-%s-%s", var.environment, var.name, "backup-identity")
29-
location = var.resource_group_location # Specify the appropriate location
30-
resource_group_name = var.resource_group_name
31-
}
32-
33-
resource "azurerm_role_assignment" "blob_storage_access_assignment" {
34-
principal_id = azurerm_user_assigned_identity.mongo_backup_identity.principal_id
35-
role_definition_name = azurerm_role_definition.blob_storage_access.name
36-
scope = "/subscriptions/${data.azurerm_subscription.current.subscription_id}"
37-
}
7+
# resource "azurerm_role_definition" "blob_storage_access" {
8+
# name = "BlobStorageAccess"
9+
# description = "Role definition for accessing Azure Blob Storage"
10+
# scope = "/subscriptions/${data.azurerm_subscription.current.subscription_id}"
11+
12+
# permissions {
13+
# actions = [
14+
# "Microsoft.Storage/storageAccounts/blobServices/containers/read",
15+
# "Microsoft.Storage/storageAccounts/blobServices/containers/write",
16+
# "Microsoft.Storage/storageAccounts/blobServices/containers/delete",
17+
# ]
18+
19+
# not_actions = []
20+
# }
21+
22+
# assignable_scopes = [
23+
# "/subscriptions/${data.azurerm_subscription.current.subscription_id}",
24+
# ]
25+
# }
26+
27+
# resource "azurerm_role_assignment" "blob_storage_access_assignment" {
28+
# principal_id = azurerm_user_assigned_identity.mongo_backup_identity.principal_id
29+
# role_definition_name = azurerm_role_definition.blob_storage_access.name
30+
# scope = "/subscriptions/${data.azurerm_subscription.current.subscription_id}"
31+
# }
3832

3933
resource "random_password" "mongodb_root_password" {
4034
count = var.mongodb_custom_credentials_enabled ? 0 : 1
@@ -49,7 +43,7 @@ resource "random_password" "mongodb_exporter_password" {
4943
}
5044

5145
resource "azurerm_key_vault" "mongo-secret" {
52-
count = var.store_password_to_secret_manager ? 1 : 0
46+
count = var.store_password_to_secret_manager ? 1 : 0
5347
name = format("%s-%s-%s", var.environment, var.name, "mongodb")
5448
resource_group_name = var.resource_group_name
5549
location = var.resource_group_location
@@ -77,7 +71,7 @@ resource "azurerm_key_vault" "mongo-secret" {
7771

7872
resource "azurerm_key_vault_secret" "mongo-secret" {
7973
depends_on = [azurerm_key_vault.mongo-secret[0]]
80-
name = format("%s-%s-%s", var.environment, var.name, "secret")
74+
name = format("%s-%s-%s", var.environment, var.name, "secret")
8175
value = var.mongodb_custom_credentials_enabled ? jsonencode(
8276
{
8377
"root_user" : "${var.mongodb_custom_credentials_config.root_user}",
@@ -95,74 +89,57 @@ resource "azurerm_key_vault_secret" "mongo-secret" {
9589
key_vault_id = azurerm_key_vault.mongo-secret[0].id
9690
}
9791

98-
resource "azurerm_user_assigned_identity" "mongo_backup" {
99-
name = "mongo-backup"
92+
# Create a service principal for mongo backup
93+
resource "azurerm_user_assigned_identity" "mongo_backup_identity" {
94+
name = format("%s-%s-%s", var.environment, var.name, "mongo_backup_identity")
10095
resource_group_name = var.resource_group_name
10196
location = var.resource_group_location
10297
}
10398

104-
resource "azurerm_key_vault_access_policy" "secretadmin_backup" {
105-
key_vault_id = azurerm_key_vault.mongo-secret[0].id
106-
tenant_id = data.azurerm_client_config.current.tenant_id
107-
object_id = azurerm_user_assigned_identity.mongo_backup.principal_id
108-
109-
secret_permissions = [
110-
"Get",
111-
"List",
112-
"Delete",
113-
]
114-
}
115-
116-
resource "azurerm_user_assigned_identity" "pod_identity_backup" {
117-
name = format("%s-%s-%s", var.environment, var.name, "pod-identity-backup")
118-
resource_group_name = var.resource_group_name
119-
location = var.resource_group_location
99+
# Grant the storage blob contributor role to the backup service principal
100+
resource "azurerm_role_assignment" "secretadmin_backup" {
101+
principal_id = azurerm_user_assigned_identity.mongo_backup_identity.principal_id
102+
role_definition_name = "Storage Blob Data Contributor"
103+
scope = "/subscriptions/${data.azurerm_subscription.current.subscription_id}/resourceGroups/test-skaf-tfstate-rg/providers/Microsoft.Storage/storageAccounts/${var.storage_account_name}"
120104
}
121105

122-
resource "azurerm_key_vault_access_policy" "pod_identity_backup" {
123-
key_vault_id = azurerm_key_vault.mongo-secret[0].id
124-
tenant_id = data.azurerm_client_config.current.tenant_id
125-
object_id = azurerm_user_assigned_identity.pod_identity_backup.principal_id
126-
127-
secret_permissions = [
128-
"Get",
129-
"List",
130-
"Delete",
131-
]
106+
# Grant the "Managed Identity Token Creator" role to the backup service principal
107+
resource "azurerm_role_assignment" "service_account_token_creator_backup" {
108+
principal_id = azurerm_user_assigned_identity.mongo_backup_identity.principal_id
109+
role_definition_name = "Role Based Access Control Administrator (Preview)"
110+
scope = "/subscriptions/${data.azurerm_subscription.current.subscription_id}/resourceGroups/test-skaf-tfstate-rg"
132111
}
133112

134-
resource "azurerm_user_assigned_identity" "mongo_restore" {
135-
name = format("%s-%s-%s", var.environment, var.name, "mongo-restore")
113+
# Create a service principal for mongo restore
114+
resource "azurerm_user_assigned_identity" "mongo_restore_identity" {
115+
name = format("%s-%s-%s", var.environment, var.name, "mongo_restore_identity")
136116
resource_group_name = var.resource_group_name
137117
location = var.resource_group_location
138118
}
139119

140-
resource "azurerm_key_vault_access_policy" "secretadmin_restore" {
141-
key_vault_id = azurerm_key_vault.mongo-secret[0].id
142-
tenant_id = data.azurerm_client_config.current.tenant_id
143-
object_id = azurerm_user_assigned_identity.mongo_restore.principal_id
144-
145-
secret_permissions = [
146-
"Get",
147-
"List",
148-
"Delete",
149-
]
120+
# Grant the storage blob contributor role to the restore service principal
121+
resource "azurerm_role_assignment" "secretadmin_restore" {
122+
principal_id = azurerm_user_assigned_identity.mongo_restore_identity.principal_id
123+
role_definition_name = "Storage Blob Data Contributor"
124+
scope = "/subscriptions/${data.azurerm_subscription.current.subscription_id}/resourceGroups/test-skaf-tfstate-rg/providers/Microsoft.Storage/storageAccounts/${var.storage_account_name}"
125+
}
126+
127+
# Grant the "Managed Identity Token Creator" role to the restore service principal
128+
resource "azurerm_role_assignment" "service_account_token_creator_restore" {
129+
principal_id = azurerm_user_assigned_identity.mongo_restore_identity.principal_id
130+
role_definition_name = "Role Based Access Control Administrator (Preview)"
131+
scope = "/subscriptions/${data.azurerm_subscription.current.subscription_id}/resourceGroups/test-skaf-tfstate-rg"
150132
}
151133

152-
resource "azurerm_user_assigned_identity" "pod_identity_restore" {
153-
name = format("%s-%s-%s", var.environment, var.name, "pod-identity-restore")
134+
# Configure workload identity for mongo backup
135+
resource "azurerm_user_assigned_identity" "pod_identity_backup" {
136+
name = format("%s-%s-%s", var.environment, var.name, "pod_identity_backup")
154137
resource_group_name = var.resource_group_name
155138
location = var.resource_group_location
156139
}
157140

158-
resource "azurerm_key_vault_access_policy" "pod_identity_restore" {
159-
key_vault_id = azurerm_key_vault.mongo-secret[0].id
160-
tenant_id = data.azurerm_client_config.current.tenant_id
161-
object_id = azurerm_user_assigned_identity.pod_identity_restore.principal_id
162-
163-
secret_permissions = [
164-
"Get",
165-
"List",
166-
"Delete",
167-
]
141+
resource "azurerm_role_assignment" "pod_identity_assignment_backup" {
142+
principal_id = azurerm_user_assigned_identity.pod_identity_backup.principal_id
143+
role_definition_name = "Managed Identity Operator"
144+
scope = "/subscriptions/${data.azurerm_subscription.current.subscription_id}/resourceGroups/${var.resource_group_name}"
168145
}

provider/azure/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ output "az_account_backup" {
44
}
55

66
output "az_account_restore" {
7-
value = azurerm_user_assigned_identity.mongo_backup_identity.client_id
7+
value = azurerm_user_assigned_identity.mongo_restore_identity.client_id
88
description = "Azure User Assigned Identity for restore"
99
}
1010

0 commit comments

Comments
 (0)