Skip to content

Commit fa33ee5

Browse files
authored
Merge pull request #1 from Think-Cube/develop
Develop
2 parents 50e6e7e + e8cad86 commit fa33ee5

File tree

7 files changed

+352
-0
lines changed

7 files changed

+352
-0
lines changed

backend.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_providers {
3+
azurerm = {
4+
source = "hashicorp/azurerm"
5+
version = "4.12.0"
6+
}
7+
}
8+
required_version = ">= 1.6.3"
9+
}

cosmos_db_database_container.tf

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
resource "azurerm_cosmosdb_sql_container" "main" {
2+
name = var.cosmosdb_sql_database_container_name
3+
resource_group_name = data.azurerm_cosmosdb_account.main.resource_group_name
4+
account_name = data.azurerm_cosmosdb_account.main.name
5+
database_name = var.cosmosdb_sql_database_name
6+
partition_key_paths = var.cosmosdb_sql_database_container_partition_key_paths
7+
partition_key_version = var.cosmosdb_sql_database_container_partition_key_version
8+
default_ttl = var.default_ttl != null ? var.default_ttl : null
9+
10+
dynamic "unique_key" {
11+
for_each = var.unique_keys != null ? var.unique_keys : []
12+
content {
13+
paths = unique_key.value["paths"]
14+
}
15+
}
16+
17+
dynamic "conflict_resolution_policy" {
18+
for_each = var.conflict_resolution_policy != null ? [var.conflict_resolution_policy] : []
19+
content {
20+
mode = conflict_resolution_policy.value["mode"]
21+
conflict_resolution_path = conflict_resolution_policy.value["conflict_resolution_path"]
22+
}
23+
}
24+
25+
dynamic "indexing_policy" {
26+
for_each = var.indexing_policy != null ? [var.indexing_policy] : []
27+
content {
28+
indexing_mode = indexing_policy.value["indexing_mode"]
29+
30+
dynamic "included_path" {
31+
for_each = indexing_policy.value["included_paths"] != null ? indexing_policy.value["included_paths"] : []
32+
content {
33+
path = included_path.value["path"]
34+
}
35+
}
36+
37+
dynamic "excluded_path" {
38+
for_each = indexing_policy.value["excluded_paths"] != null ? indexing_policy.value["excluded_paths"] : []
39+
content {
40+
path = excluded_path.value["path"]
41+
}
42+
}
43+
44+
}
45+
}
46+
}

main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
data "azurerm_client_config" "current" {}
2+
3+
data "azurerm_resource_group" "rg" {
4+
name = var.resource_group_name
5+
}
6+
7+
data "azurerm_cosmosdb_account" "main" {
8+
name = var.cosmosdb_account_name
9+
resource_group_name = data.azurerm_resource_group.rg.name
10+
}

output.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
output "id" {
2+
description = "The CosmosDB Database container ID."
3+
value = azurerm_cosmosdb_sql_container.main.id
4+
sensitive = false
5+
}

pipelines/azure-pipelines-pr.yaml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
name: $(MODULE_NAME)-$(date:yyyyMMdd)-$(rev:.r)
2+
trigger: none
3+
pr:
4+
branches:
5+
include:
6+
- refs/heads/master
7+
- refs/heads/main
8+
resources:
9+
repositories:
10+
- repository: self
11+
variables:
12+
- name: TERRAFORM_SEC_VERSION
13+
value: "v1.26.0"
14+
- name: GITHUB_REPO
15+
value: "Think-Cube/terraform-azure-cosmosdb-database-container"
16+
- group: GITHUB-PAT-TOKEN
17+
- name: VM_IMAGE
18+
value: ubuntu-latest
19+
- name: MODULE_NAME
20+
value: "terraform-azure-cosmosdb-database-container"
21+
pool:
22+
vmImage: $(VM_IMAGE)
23+
stages:
24+
- stage: Validate_Terraform_Module
25+
displayName: 'Validate Terraform Module'
26+
jobs:
27+
- job: Validate_Terraform_Module
28+
displayName: 'Validate Terraform Module'
29+
steps:
30+
- checkout: self
31+
displayName: 'Checkout Module'
32+
fetchDepth: 1
33+
- task: TerraformCLI@0
34+
displayName: 'Terraform Init'
35+
inputs:
36+
command: 'init'
37+
allowTelemetryCollection: false
38+
- task: TerraformCLI@0
39+
displayName: 'Terraform Validate'
40+
inputs:
41+
command: 'validate'
42+
allowTelemetryCollection: false
43+
- task: tfsec@1
44+
displayName: 'Terraform SEC check'
45+
inputs:
46+
version: '$(TERRAFORM_SEC_VERSION)'
47+
dir: '$(System.DefaultWorkingDirectory)'
48+
- script: |
49+
cd /tmp
50+
curl -sSLo /tmp/terraform-docs.tar.gz https://terraform-docs.io/dl/v0.19.0/terraform-docs-v0.19.0-$(uname)-amd64.tar.gz
51+
tar -xzf /tmp/terraform-docs.tar.gz
52+
chmod +x /tmp/terraform-docs
53+
displayName: 'Download terraform-docs'
54+
- script: |
55+
# Variables
56+
github_token="$(GITHUB_TOKEN)"
57+
# Generate or update README.md
58+
/tmp/terraform-docs markdown table . > README.md
59+
60+
# Check if README.md has been updated or created and add to PR
61+
if [ -f README.md ]; then
62+
echo "README.md file generated/updated."
63+
git config --global user.email "devops-bot@example.com"
64+
git config --global user.name "DevOps Bot"
65+
git add README.md
66+
git commit -m "Update README.md with module documentation"
67+
68+
# Set remote URL with authentication token
69+
git remote set-url origin https://$(GITHUB_TOKEN)@github.com/Think-Cube/terraform-azure-cosmosdb-database-container.git
70+
71+
# Pull the latest changes to avoid conflicts (source branch of the PR)
72+
git pull origin $(System.PullRequest.SourceBranch) --rebase
73+
74+
# Push changes
75+
git push origin HEAD:$(System.PullRequest.SourceBranch)
76+
else
77+
echo "Failed to generate README.md"
78+
exit 1
79+
fi
80+
displayName: 'Generate and Update README.md Documentation'

pipelines/azure-pipelines.yaml

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
name: $(MODULE_NAME)-$(date:yyyyMMdd)-$(rev:.r)
2+
parameters:
3+
- name: publish_module
4+
displayName: 'Publish Module ???'
5+
type: string
6+
default: 'false'
7+
values:
8+
- true
9+
- false
10+
trigger:
11+
branches:
12+
include:
13+
- refs/heads/master
14+
- refs/heads/main
15+
resources:
16+
repositories:
17+
- repository: self
18+
variables:
19+
- name: TERRAFORM_SEC_VERSION
20+
value: "v1.26.0"
21+
- name: GITHUB_REPO
22+
value: "Think-Cube/terraform-azure-cosmosdb-database-container"
23+
- group: GITHUB-PAT-TOKEN
24+
- name: PUBLISH_MODULE
25+
value: ${{parameters.publish_module}}
26+
- name: VM_IMAGE
27+
value: ubuntu-latest
28+
- name: MODULE_NAME
29+
value: "terraform-azure-cosmosdb-database-container"
30+
- name: MODULE_DESCRIPTION
31+
value: "Terraform module for azure cosmosdb database container"
32+
pool:
33+
vmImage: $(VM_IMAGE)
34+
stages:
35+
- stage: Validate_Terraform_Module
36+
displayName: 'Validate Terraform Module'
37+
jobs:
38+
- job: Validate_Terraform_Module
39+
displayName: 'Validate Terraform Module'
40+
steps:
41+
- checkout: self
42+
displayName: 'Checkout Module'
43+
fetchDepth: 1
44+
- task: CmdLine@2
45+
displayName: 'Terraform Init'
46+
inputs:
47+
script: |
48+
terraform init
49+
workingDirectory: '$(System.DefaultWorkingDirectory)'
50+
- task: CmdLine@2
51+
displayName: 'Terraform Validate'
52+
inputs:
53+
script: |
54+
terraform validate
55+
workingDirectory: '$(System.DefaultWorkingDirectory)'
56+
- task: tfsec@1
57+
displayName: 'Terraform SEC check'
58+
inputs:
59+
version: '$(TERRAFORM_SEC_VERSION)'
60+
dir: '$(System.DefaultWorkingDirectory)'
61+
- stage: Publish_Terraform_Module
62+
condition: eq('${{parameters.publish_module}}', 'true')
63+
displayName: 'Publish Terraform Module'
64+
jobs:
65+
- job: Publish_Terraform_Module
66+
displayName: 'Publish Terraform Module'
67+
steps:
68+
- checkout: self
69+
- task: CopyFiles@2
70+
displayName: 'Copy Terraform module files'
71+
inputs:
72+
SourceFolder: $(System.DefaultWorkingDirectory)
73+
Contents: '**.tf'
74+
TargetFolder: $(System.DefaultWorkingDirectory)/$(MODULE_NAME)
75+
- task: CmdLine@2
76+
displayName: 'Fetch latest version and increment'
77+
inputs:
78+
script: |
79+
# Variables
80+
module_path="$(System.DefaultWorkingDirectory)/$(MODULE_NAME)"
81+
github_repo="$(GITHUB_REPO)"
82+
github_token="$(GITHUB_TOKEN)"
83+
description="New $(MODULE_DESCRIPTION) release"
84+
85+
# Remove unnecessary files from the module directory
86+
find "$module_path" -name ".git" -type d -exec rm -rf {} +
87+
find "$module_path" -name ".github" -type d -exec rm -rf {} +
88+
89+
# Fetch the latest tag and increment version
90+
latest_tag=$(git tag --list "v*" | sort -V | tail -n1)
91+
new_version="0.0.1"
92+
if [ -n "$latest_tag" ]; then
93+
IFS='.' read -r major minor patch <<< "${latest_tag#v}"
94+
if (( patch < 999 )); then
95+
patch=$((patch + 1))
96+
else
97+
patch=0
98+
if (( minor < 999 )); then
99+
minor=$((minor + 1))
100+
else
101+
minor=0
102+
major=$((major + 1))
103+
fi
104+
fi
105+
new_version="$major.$minor.$patch"
106+
fi
107+
108+
# Create a tarball of the module
109+
tarball_name="$(MODULE_NAME)-v$new_version.tar.gz"
110+
tar -czf "$tarball_name" -C "$module_path" .
111+
112+
# Publish to GitHub Releases using gh CLI
113+
echo "$github_token" | gh auth login --with-token
114+
gh release create "v$new_version" "$tarball_name" \
115+
--repo "$github_repo" \
116+
--title "$(MODULE_NAME) v$new_version" \
117+
--notes "$description"

variables.tf

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
variable "environment" {
2+
description = "The environment name used for backend container naming (e.g., dev, staging, prod)."
3+
type = string
4+
default = "dev"
5+
}
6+
7+
variable "resource_group_location" {
8+
description = "The Azure location where the resource group is created. Changing this value forces the creation of a new resource."
9+
type = string
10+
default = "West Europe"
11+
}
12+
13+
variable "resource_group_name" {
14+
description = "The name of the resource group in which to create the Cosmos DB SQL container. Changing this value forces the creation of a new resource."
15+
type = string
16+
}
17+
18+
variable "cosmosdb_account_name" {
19+
description = "The name of the Cosmos DB account. Changing this value forces the creation of a new resource."
20+
type = string
21+
}
22+
23+
variable "cosmosdb_sql_database_name" {
24+
description = "The name of the Cosmos DB SQL database. Changing this value forces the creation of a new resource."
25+
type = string
26+
}
27+
28+
variable "cosmosdb_sql_database_container_name" {
29+
description = "The name of the Cosmos DB SQL container to be created."
30+
type = string
31+
}
32+
33+
variable "cosmosdb_sql_database_container_partition_key_paths" {
34+
description = "A list of partition key paths for the Cosmos DB SQL container. Partition keys are essential for scalable performance in Cosmos DB."
35+
type = list(string)
36+
default = ["/myPartitionKey"]
37+
}
38+
39+
variable "cosmosdb_sql_database_container_partition_key_version" {
40+
description = "The version of the partition key for the Cosmos DB SQL container. Defaults to 1."
41+
type = number
42+
default = 1
43+
}
44+
45+
variable "sql_database_container_paths" {
46+
description = "List of Cosmos DB SQL containers to create. Some parameters are inherited from the Cosmos DB account."
47+
type = string
48+
}
49+
50+
variable "conflict_resolution_policy" {
51+
description = "The conflict resolution policy for the Cosmos DB SQL container, which determines how conflicting changes are resolved."
52+
type = object({
53+
mode = string # E.g., 'LastWriterWins' or 'Custom'.
54+
conflict_resolution_path = string # Path used for resolving conflicts, applicable for 'LastWriterWins' mode.
55+
})
56+
default = null
57+
}
58+
59+
variable "unique_keys" {
60+
description = "A list of unique keys for the Cosmos DB SQL container to ensure uniqueness of specified paths."
61+
type = list(object({
62+
paths = list(string) # Paths defining the unique key constraints.
63+
}))
64+
default = null
65+
}
66+
67+
variable "indexing_policy" {
68+
description = "The indexing policy for the Cosmos DB SQL container, which specifies how items are indexed for queries."
69+
type = object({
70+
indexing_mode = string # Either 'consistent' or 'none'.
71+
included_paths = list(object({
72+
path = string # Paths explicitly included in the index.
73+
}))
74+
excluded_paths = list(object({
75+
path = string # Paths explicitly excluded from the index.
76+
}))
77+
})
78+
default = null
79+
}
80+
81+
variable "default_ttl" {
82+
description = "Default time-to-live (TTL) for the Cosmos DB SQL container, specified in seconds. If null, TTL is not configured."
83+
type = number
84+
default = null
85+
}

0 commit comments

Comments
 (0)