# Microsoft Active Directory

> Create the Microsoft Active Directory Resource in Terraform to create and delete Computer Accounts

---

Site [llms.txt](/llms.txt)

---

To learn more about this OneFuse module, refer to [Microsoft Active Directory](/docs/onefuse/configure/modules/microsoft-active-directory/).

## Prerequisites

1. [Configure the OneFuse Provider](/docs/onefuse/consume/hashicorp-terraform/getting-started/#configure-the-onefuse-provider)
1. Review an overview on [OneFuse Modules](/docs/onefuse/consume/hashicorp-terraform/modules/) with Terraform

## Steps

1. Create `variables.tf` file
1. Create `main.tf` file
1. Run terraform commands to create a Computer Account in AD:
   1. `terraform init`
   1. `terraform plan`
   1. `terraform apply`
1. Run terraform command to delete the Computer Account from AD:
   - `terraform destroy`

### Create variables.tf

Add the following in the `variables.tf` file. Update the values as needed.

<div class="alert alert-info" role="alert">


It is a best practice to separate configurations into identifiable files, refer to [Best practices for OneFuse modules](/docs/onefuse/consume/hashicorp-terraform/modules/#best-practices-for-onefuse-modules).

The sample below shows all configurations in one `variables.tf` file for simplicity.
</div>


```hcl
// SAMPLE VARIABLES.TF FILE CONFIGURATIONS

variable "onefuse_scheme" {
  type    = string
  default = "https"
}

variable "onefuse_address" {
  type = string
}

variable "onefuse_port" {
  type    = string
  default = "443"
}

variable "onefuse_user" {
  type    = string
  default = "admin"
}

variable "onefuse_password" {
  type    = string
  default = "admin"
}

variable "onefuse_verify_ssl" {
  type    = bool
  default = false
}

variable "workspace_url" {
  type    = string
  default = ""              // Default
}

variable "template_properties" {
  type    = map
  default = {
    "Environment" = "d"     //p for production or d for development
    "Location"    = "w"     //e for East or w for West
    "Application" = "wp"    //wp for wordpress or iis for IIS
    "OS"          = "l"     //l for Linux or w for Windows
  }
}
```

### Create main.tf for Microsoft Active Directory Resource

Add the following in the `main.tf` file. Update the values as needed.

#### Parameter references

The following defines the function of each Microsoft Active Directory specific parameters.

| Parameter Name | Required? | Notes |
| --- | --- | --- |
| `policy_id` | Required | OneFuse Module Policy ID (integer). |
| `workspace_url` | *Optional* | OneFuse Workspace URL path. Use `""` for the default workspace. |
| `template_properties` | *Optional* | Additional properties that are referenced within the Policy. |

**Microsoft Active Directory specific parameters:**

| Parameter Name | Required? | Notes |
| --- | --- | --- |
| `name` | **Required** | Computer Account Name |

#### Sample main.tf

```hcl
// Terraform OneFuse Provider
terraform {
  required_providers {
    onefuse = {
      source  = "CloudBoltSoftware/onefuse"
      version = ">= 1.50.2"
   }
  }
  required_version = ">= 0.13"
}

// Inititalize OneFuse Provider
provider "onefuse" {
  scheme     = var.onefuse_scheme
  address    = var.onefuse_address
  port       = var.onefuse_port
  user       = var.onefuse_user
  password   = var.onefuse_password
  verify_ssl = var.onefuse_verify_ssl
}

// Data Source for Microsoft Active Directory Policy - lookup Policy ID by Policy Name
data "onefuse_ad_policy" "ad_policy" {
  name = "my_policy_name"  // Replace with Policy Name
}

// Resource for Microsoft Active Directory
resource "onefuse_microsoft_ad_computer_account" "my_ad_computer_account" {
  policy_id           = data.onefuse_ad_policy.ad_policy.id        // Refers to Data Source to get Policy ID
  workspace_url       = ""
  template_properties = var.template_properties
  name                = "computer_name"
}

// Outputs for Resource
output "ad_ou" {
  value = onefuse_microsoft_ad_computer_account.my_ad_computer_account.final_ou
}
```

### OneFuse Module: Active Directory Import

#### Create main.tf for AD Resource Import

Add the following in the `main.tf` file.

The following parameter is required to import an AD object.

**Parameter references:**

| Parameter Name | Required? | Notes |
| --- | --- | --- |
| `policy_id` | *Required* | OneFuse Module Policy ID (integer). |

#### Sample main.tf

```hcl
// Terraform OneFuse Provider
terraform {
  required_providers {
    onefuse = {
      source  = "CloudBoltSoftware/onefuse"
      version = ">= 1.50.2"
   }
  }
  required_version = ">= 0.13"
}


// Inititalize OneFuse Provider
// Create the separate variables.tf file or hard code the values.
provider "onefuse" {
  scheme     = var.onefuse_scheme
  address    = var.onefuse_address
  port       = var.onefuse_port
  user       = var.onefuse_user
  password   = var.onefuse_password
  verify_ssl = var.onefuse_verify_ssl
}

resource "onefuse_microsoft_ad_computer_account" "my_ad_record" {
  policy_id           = 1
}
```

Run the following command to import the OneFuse AD Resource:

```bash
terraform import <resource_type>.<resource_name> <resource_id>
terraform import onefuse_microsoft_ad_computer_account.my_ad_record 2
```
