From 1b5114464994f1bfcc4168eabc23bfe52b4a198d Mon Sep 17 00:00:00 2001
From: Dipti Pai <diptipai89@outlook.com>
Date: Tue, 27 Aug 2024 16:20:34 +0000
Subject: [PATCH] Terraform files to create ado project and repository

Signed-off-by: Dipti Pai <diptipai89@outlook.com>
---
 tf-modules/azure/devops/README.md    | 40 ++++++++++++++++++++++++++++
 tf-modules/azure/devops/main.tf      | 16 +++++++++++
 tf-modules/azure/devops/outputs.tf   |  9 +++++++
 tf-modules/azure/devops/variables.tf | 15 +++++++++++
 tf-modules/azure/devops/versions.tf  |  8 ++++++
 5 files changed, 88 insertions(+)
 create mode 100644 tf-modules/azure/devops/README.md
 create mode 100644 tf-modules/azure/devops/main.tf
 create mode 100644 tf-modules/azure/devops/outputs.tf
 create mode 100644 tf-modules/azure/devops/variables.tf
 create mode 100644 tf-modules/azure/devops/versions.tf

diff --git a/tf-modules/azure/devops/README.md b/tf-modules/azure/devops/README.md
new file mode 100644
index 0000000..98b537b
--- /dev/null
+++ b/tf-modules/azure/devops/README.md
@@ -0,0 +1,40 @@
+# DevOps Module
+
+Configuration in this directory creates an Azure DevOps Project and repository.
+
+## Usage
+
+Legacy shared modules with their own provider configurations are not compatible
+with new features like for_each, count and depends_on as described
+[here](https://developer.hashicorp.com/terraform/language/modules/develop/providers#legacy-shared-modules-with-provider-configurations).
+To use these features by passing provider configuration to the legacy module,
+create version.tf file with the following content -
+
+```hcl
+terraform {
+  required_providers {
+    azuredevops = {
+      source = "microsoft/azuredevops"
+    }
+  }
+}
+```
+
+In main.tf, create the provider configuration and pass it to the devops module.
+
+```hcl
+provider "azuredevops" {
+  org_service_url       = "https://dev.azure.com/azuredevops_org"
+  personal_access_token = "azuredevops_pat"
+}
+
+module "devops" {
+  source = "git::https://github.com/fluxcd/test-infra.git//tf-modules/azure/devops"
+  providers = {
+    azuredevops = azuredevops
+  }
+
+  project_name = local.project_name
+  repository_name = local.repo_name
+}
+```
diff --git a/tf-modules/azure/devops/main.tf b/tf-modules/azure/devops/main.tf
new file mode 100644
index 0000000..998b847
--- /dev/null
+++ b/tf-modules/azure/devops/main.tf
@@ -0,0 +1,16 @@
+resource "azuredevops_project" "project" {
+  name               = var.project_name
+  visibility         = "private"
+  version_control    = "Git"
+  work_item_template = "Agile"
+  description        = var.project_description
+}
+
+resource "azuredevops_git_repository" "application" {
+  project_id     = azuredevops_project.project.id
+  name           = var.repository_name
+  default_branch = "refs/heads/main"
+  initialization {
+    init_type = "Clean"
+  }
+}
diff --git a/tf-modules/azure/devops/outputs.tf b/tf-modules/azure/devops/outputs.tf
new file mode 100644
index 0000000..6801fcd
--- /dev/null
+++ b/tf-modules/azure/devops/outputs.tf
@@ -0,0 +1,9 @@
+output "repo_url" {
+  description = "Azure Devops Git repository HTTPS url"
+  value       = azuredevops_git_repository.application.remote_url
+}
+
+output "project_id" {
+  description = "Azure Devops Project ID"
+  value       = azuredevops_project.project.id
+}
diff --git a/tf-modules/azure/devops/variables.tf b/tf-modules/azure/devops/variables.tf
new file mode 100644
index 0000000..e9ce15b
--- /dev/null
+++ b/tf-modules/azure/devops/variables.tf
@@ -0,0 +1,15 @@
+variable "project_name" {
+  description = "The name of the Azure DevOps project"
+  type        = string
+}
+
+variable "project_description" {
+  description = "The description of the Azure DevOps project"
+  type        = string
+  default     = "Test Project for Flux E2E test - Managed by Terraform"
+}
+
+variable "repository_name" {
+  description = "The name of the Azure DevOps repository"
+  type        = string
+}
diff --git a/tf-modules/azure/devops/versions.tf b/tf-modules/azure/devops/versions.tf
new file mode 100644
index 0000000..03d5ac9
--- /dev/null
+++ b/tf-modules/azure/devops/versions.tf
@@ -0,0 +1,8 @@
+terraform {
+  required_providers {
+    azuredevops = {
+      source  = "microsoft/azuredevops"
+      version = ">= 1.2.0"
+    }
+  }
+}