# ServiceNow CMDB

> Create the ServiceNow CMDB Resource in Terraform to create and consume ServiceNow "CIs-as-a-Service"

---

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

---

To learn more about this OneFuse module, refer to [ServiceNow CMDB](/docs/onefuse/configure/modules/servicenow-cmdb/).

## 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 ServiceNow CI:
    1. `terraform init`
    1. `terraform plan`
    1. `terraform apply`
    1. See example apply output:

        ```text
        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.

        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_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment will be created
          + resource "onefuse_servicenow_cmdb_deployment" "my_servicenow_cmdb_deployment" {
              + configuration_items_info = (known after apply)
              + id                       = (known after apply)
              + policy_id                = 72
              + template_properties      = {
                  + "OneFuse_Suffix" = "company.com"
                  + "cpuCount"       = "2"
                  + "dnsSuffix"      = "company.com"
                  + "fqdn"           = "myhost.company.com"
                  + "hostname"       = "myhost"
                  + "ipAddress"      = "10.10.2.7"
                  + "memoryMB"       = "4"
                  + "platformUuid"   = "634f89cd-bcf8-421b-bfa3-65f09557abca"
                  + "powerState"     = "On"
                  + "totalStorageGB" = "30"
                }
              + workspace_url            = (known after apply)
            }

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

        Changes to Outputs:
          + servicenow-cmdb-response = (known after apply)

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

        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.


        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_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment will be created
          + resource "onefuse_servicenow_cmdb_deployment" "my_servicenow_cmdb_deployment" {
              + configuration_items_info = (known after apply)
              + id                       = (known after apply)
              + policy_id                = 72
              + template_properties      = {
                  + "OneFuse_Suffix" = "company.com"
                  + "cpuCount"       = "2"
                  + "dnsSuffix"      = "company.com"
                  + "fqdn"           = "myhost.company.com"
                  + "hostname"       = "myhost"
                  + "ipAddress"      = "10.10.2.7"
                  + "memoryMB"       = "4"
                  + "platformUuid"   = "634f89cd-bcf8-421b-bfa3-65f09557abca"
                  + "powerState"     = "On"
                  + "totalStorageGB" = "30"
                }
              + workspace_url            = (known after apply)
            }

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

        Changes to Outputs:
          + servicenow-cmdb-response = (known after apply)

        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_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment: Creating...
        onefuse_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment: Still creating... [10s elapsed]
        onefuse_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment: Creation complete after 14s [id=73]

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

        Outputs:

        servicenow-cmdb-response = tolist([
          tomap({
            "ciClassName" = "cmdb_ci_linux_server"
            "ciName" = "myhost-company.com"
          }),
        ])
        ```

1. Modify values, then run `terraform apply` again to update the ServiceNow CI:
    - In our example below, the **cpuCount** was updated from **2** to **4**.

        See example apply output:

        ```text
        An execution plan has been generated and is shown below.
        Resource actions are indicated with the following symbols:
          ~ update in-place

        Terraform will perform the following actions:

          # onefuse_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment will be updated in-place
          ~ resource "onefuse_servicenow_cmdb_deployment" "my_servicenow_cmdb_deployment" {
                id                       = "73"
              ~ template_properties      = {
                  ~ "cpuCount"       = "2" -> "4"
                    # (9 unchanged elements hidden)
                }
                # (3 unchanged attributes hidden)
            }

        Plan: 0 to add, 1 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_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment: Modifying... [id=73]
        onefuse_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment: Still modifying... [id=73, 10s elapsed]
        onefuse_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment: Modifications complete after 13s [id=73]

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

        Outputs:

        servicenow-cmdb-response = tolist([
          tomap({
            "ciClassName" = "cmdb_ci_linux_server"
            "ciName" = "myhost-company.com"
          }),
        ])
        ```

1. Run terraform command to delete the ServiceNow CI:
    - `terraform destroy`

        See example destroy output:

        ```text
        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_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment will be destroyed
          - resource "onefuse_servicenow_cmdb_deployment" "my_servicenow_cmdb_deployment" {
              - configuration_items_info = [
                  - {
                      - "ciClassName" = "cmdb_ci_linux_server"
                      - "ciName"      = "myhost-company.com"
                    },
                ] -> null
              - id                       = "73" -> null
              - policy_id                = 72 -> null
              - template_properties      = {
                  - "OneFuse_Suffix" = "company.com"
                  - "cpuCount"       = "4"
                  - "dnsSuffix"      = "company.com"
                  - "fqdn"           = "myhost.company.com"
                  - "hostname"       = "myhost"
                  - "ipAddress"      = "10.10.2.7"
                  - "memoryMB"       = "4"
                  - "platformUuid"   = "634f89cd-bcf8-421b-bfa3-65f09557abca"
                  - "powerState"     = "On"
                  - "totalStorageGB" = "30"
                } -> null
              - workspace_url            = "/api/v3/onefuse/workspaces/2/" -> null
            }

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

        Changes to Outputs:
          - servicenow-cmdb-response = [
              - {
                  - "ciClassName" = "cmdb_ci_linux_server"
                  - "ciName"      = "myhost-company.com"
                },
            ] -> null

        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_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment: Destroying... [id=73]
        onefuse_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment: Still destroying... [id=73, 10s elapsed]
        onefuse_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment: Destruction complete after 12s

        Destroy complete! Resources: 1 destroyed.
        ```

---

### 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 ServiceNow CMDB resource

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

#### Parameter references

The following defines the function of each ServiceNow CMDB specific parameter.

| 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. |

#### 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 ServiceNow CMDB Policy - lookup Policy ID by Policy Name
data "onefuse_servicenow_cmdb_policy" "servicenow_cmdb_policy" {
  name = "my_policy_name"  // Replace with Policy Name
}

// Resource for ServiceNow CMDB
resource "onefuse_servicenow_cmdb_deployment" "my_servicenow_cmdb_deployment" {
  policy_id           = data.onefuse_servicenow_cmdb_policy.servicenow_cmdb_policy.id  // Refers to Data Source to get Policy ID
  workspace_url       = ""
  template_properties = var.template_properties
}

// Outputs for Resource
output "servicenow_cmdb_response" {
  value = onefuse_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment
}
```

## OneFuse module: ServiceNow CMDB import

### Create main.tf for ServiceNow CMDB resource import

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

The following parameter is required to import a ServiceNow CMDB object.

| 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_servicenow_cmdb_deployment" "my_servicenow_cmdb_deployment" {
  policy_id    = 1
}
```

Run the following command to import the OneFuse ServiceNow CMDB resource:

```bash
terraform import <resource_type>.<resource_name> <resource_id>
terraform import onefuse_servicenow_cmdb_deployment.my_servicenow_cmdb_deployment 2
```
