# Naming

> Create the Naming Resource in Terraform to generate and destroy Names

---

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

---

To learn more about this OneFuse module, refer to [Naming](/docs/onefuse/configure/modules/naming/).

## 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 generate a Name:
    1. `terraform init`
    1. `terraform plan`
    1. `terraform apply`
    1. Example apply output:

        ```bash
        $ terraform init

        Initializing the backend...

        Initializing provider plugins...

        Terraform has been successfully initialized!

        You may now begin working with Terraform. Try running "terraform plan" to see
        any changes that are required for your infrastructure. All Terraform commands
        should now work.

        If you ever set or change modules or backend configuration for Terraform,
        rerun this command to reinitialize your working directory. If you forget, other
        commands will detect it and remind you to do so if necessary.

        $ terraform plan
        Refreshing Terraform state in-memory prior to plan...
        The refreshed state will be used to calculate this plan, but will not be
        persisted to local or remote state storage.


        ------------------------------------------------------------------------

        An execution plan has been generated and is shown below.
        Resource actions are indicated with the following symbols:
          + create

        Terraform will perform the following actions:

          # onefuse_naming.my_name will be created
          + resource "onefuse_naming" "my_name" {
              + custom_name_id      = (known after apply)
              + dns_suffix          = "company.com"
              + id                  = (known after apply)
              + name                = (known after apply)
              + naming_policy_id    = "2"
              + template_properties = {
                  + "Application" = "Web Servers"
                  + "env"         = "prod"
                  + "location"    = "atl"
                  + "os"          = "linux"
                  + "suffix"      = "company.com"
            }

        Plan: 1 to add, 0 to change, 0 to destroy.

        ------------------------------------------------------------------------

        Note: You didn't specify an "-out" parameter to save this plan, so Terraform
        can't guarantee that exactly these actions will be performed if
        "terraform apply" is subsequently run.

        $ terraform apply

        An execution plan has been generated and is shown below.
        Resource actions are indicated with the following symbols:
          + create

        Terraform will perform the following actions:

          # onefuse_naming.my_name will be created
          + resource "onefuse_naming" "my_name" {
              + custom_name_id      = (known after apply)
              + dns_suffix          = "company.com"
              + id                  = (known after apply)
              + name                = (known after apply)
              + naming_policy_id    = "2"
              + template_properties = {
                  + "Application" = "Web Servers"
                  + "env"         = "prod"
                  + "location"    = "atl"
                  + "os"          = "linux"
                  + "suffix"      = "company.com"
                }
            }

        Plan: 1 to add, 0 to change, 0 to destroy.

        Do you want to perform these actions?
          Terraform will perform the actions described above.
          Only 'yes' will be accepted to approve.

          Enter a value: yes

        onefuse_naming.my_name: Creating...
        onefuse_naming.my_name: Creation complete after 1s [id=prodlinuxatl2.company.com]

        Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

        $ terraform show
        # onefuse_naming.my_name:
        resource "onefuse_naming" "my_name" {
            custom_name_id      = 31
            dns_suffix          = "company.com"
            id                  = "prodlinuxatl2.company.com"
            name                = "prodlinuxatl2"
            naming_policy_id    = "2"
            template_properties = {
                "Application" = "Web Servers"
                "env"         = "prod"
                "location"    = "atl"
                "os"          = "linux"
                "suffix"      = "company.com"
            }
        }
        ```

1. Run terraform command to destroy a generated Name:
    - `terraform destroy`
    - Example destroy output:

        ```bash
        $ terraform destroy
        onefuse_naming.my_name: Refreshing state... [id=prodlinuxatl2.company.com]

        An execution plan has been generated and is shown below.
        Resource actions are indicated with the following symbols:
          - destroy

        Terraform will perform the following actions:

          # onefuse_naming.my_name will be destroyed
          - resource "onefuse_naming" "my_name" {
              - custom_name_id      = 31 -> null
              - dns_suffix          = "company.com" -> null
              - id                  = "prodlinuxatl2.company.com" -> null
              - name                = "prodlinuxatl2" -> null
              - naming_policy_id    = "2" -> null
              - template_properties = {
                  - "Application" = "Web Servers"
                  - "env"         = "prod"
                  - "location"    = "atl"
                  - "os"          = "linux"
                  - "suffix"      = "company.com"
                } -> null
            }

        Plan: 0 to add, 0 to change, 1 to destroy.

        Do you really want to destroy all resources?
          Terraform will destroy all your managed infrastructure, as shown above.
          There is no undo. Only 'yes' will be accepted to confirm.

          Enter a value: yes

        onefuse_naming.my_name: Destroying... [id=prodlinuxatl2.company.com]
        onefuse_naming.my_name: Destruction complete after 0s

        Destroy complete! Resources: 1 destroyed.
        $ terraform show

        $
        ```

---

### 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 Naming Resource

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

#### Parameter references

The following defines the function of each Naming-specific parameter.

| Parameter name | Required? | Notes |
| --- | --- | --- |
| `naming_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. |

**Naming-specific parameters:**

| Parameter name | Required? | Notes |
| --- | --- | --- |
| `dns_suffix` | *Optional* | DNS Suffix to append to the hostname. |

#### 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 Naming Policy - lookup Policy ID by Policy Name
data "onefuse_naming_policy" "naming_policy" {
  name = "my_policy_name"  // Replace with Policy Name
}

// Resource for Naming
resource "onefuse_naming" "my_name" {
  naming_policy_id        = data.onefuse_naming_policy.naming_policy.id      // Refers to Data Source to get Policy ID
  workspace_id            = ""
  template_properties     = var.template_properties
  dns_suffix              = "example.com"
}

// Outputs for Resource
output "name" {
  value = onefuse_naming.my_name.name
}

output "dns_suffix" {
  value = onefuse_naming.my_name.dns_suffix
}
```

---

## OneFuse module: Naming Import

### Create main.tf for Naming Resource Import

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

#### Parameter references

The following parameter is required to import a Naming object.

| Parameter name | Required? | Notes |
| --- | --- | --- |
| `naming_policy_id` | Required | OneFuse Module Policy ID (integer). |

```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
}

resource "onefuse_naming" "testabc001" {
   naming_policy_id        = 1
}
```

Run the following command to import the OneFuse Naming Resource:

```bash
terraform import <resource_type>.<resource_name> <resource_id>
terraform import onefuse_naming.testabc001 1
```
