In this article we are going to walk through using OneFuse to generate a name for use in a Terraform configuration. To do this we will create a new Terraform configuration that uses the OneFuse provider data source and resource for the naming policy we created as part of Creating a Naming Policy with OneFuse.
By the end of this article we will have created a Terraform configuration that calls OneFuse and returns a name. While this will be a simple example we will build upon this in later articles to showcase the advanced capabilities offered by OneFuse as a platform.
Before we begin there are prerequisites you will want to have ready.
Prerequisites
- The OneFuse appliance should be deployed and configured, see the following articles if you need to walk through the OneFuse deployment and configuration.
- Terraform 0.13 or higher
Creating the Terraform Configuration
To begin we will need to initialize the OneFuse Terraform provider. To do this we will need the following statement:
Provider Declaration
The OneFuse Terraform provider is available in the Terraform Registry. By point to the source “CloudBoltSoftware/onefuse” Terraform will automatically download the OneFuse provider based on the required_version specified. In the example above that will be v1.20.0 or higher.
terraform {
required_providers {
onefuse = {
source = "CloudBoltSoftware/onefuse"
version = ">= 1.20.0"
}
}
required_version = ">= 0.13"
}
provider "onefuse" {
scheme = "https"
address = "onefuse_fqdn"
port = "443"
user = "admin"
password = "admin"
verify_ssl = "false"
}
Data Source
Next we need to leverage the OneFuse provider Data Source to lookup the Naming policy we would like to use. This will allow us to determine which policy we will be using by it’s name.
In the above example I am using the “data” source type “onefuse_naming_policy” to lookup the policy by the name “default” and store it as “policy” We will then be able to refer to the results as data.onefuse_naming_policy.policy.
// OneFuse Data Source for Naming Policy to lookup policy ID
data "onefuse_naming_policy" "policy" {
name = "default"
}
Resource
Next we need to create a resource that will trigger Terraform to contact OneFuse to create the name and hold the name for use. To do this we will need the following declaration:
resource "onefuse_naming" "name" {
naming_policy_id = data.onefuse_naming_policy.policy.id
template_properties = {
"nameEnvironment" = "p"
"nameOS" = "w"
"nameApplication" = "ap"
"nameLocation" = "atl"
"nameCompliance" = "pci"
"dnsSuffix" = "company.com"
}
}
Here we are creating a resource that uses “onefuse_naming” with the name “name. We need to tell the resource the id for the policy we want to use. To do this we simple reference the data source we looked up. To get the id we reference “data.onefuse_naming_policy.policy.id”
In the article “Creating a Naming Policy with OneFuse” we created a naming policy with a naming template as follow:
{{nameEnvironment}}{{nameLocation}}{{nameApplication}}{{nameOS}}{{nameCompliance}}
When calling the resource “onefuse_nameing” we need to tell OneFuse what to use for the values for each of the variables that are used to build out the name based on the above template. This is done through the “tempalate_proeprties” input. In this case we need to pass all the values for the naming template as well as dnsSuffix which we added to the DNS Suffix field in the policy.
Output
If we want to see the results of the name that was generated we can use the Terraform “output” declaration to output the name after we run the plan. To do this we add the following declaration to the configuration.
output "name" {
value = onefuse_naming.name.name
}
Putting it all together
Our completed plan will look like the following:
terraform {
required_providers {
onefuse = {
source = "CloudBoltSoftware/onefuse"
version = ">= 1.20.0"
}
}
required_version = ">= 0.13"
}
// Inititalize OneFuse Provider
provider "onefuse" {
scheme = "https"
address = "onefuse12bp.company.com"
port = "443"
user = "admin"
password = "admin"
verify_ssl = "false"
}
// OneFuse Data Source for Naming Policy to lookup policy ID
data "onefuse_naming_policy" "policy" {
name = "default"
}
resource "onefuse_naming" "name" {
count = 1
naming_policy_id = data.onefuse_naming_policy.policy.id
template_properties = {
"nameEnvironment" = "p"
"nameOS" = "w"
"nameApplication" = "ap"
"nameLocation" = "atl"
"nameCompliance" = "pci"
"dns_suffix" = "company.com"
}
}
output "hostname" {
value = onefuse_naming.name.name
}
Applying the configuration
To apply this configuration we can perform the following:
- Init
terraform init
- Plan
terraform plan
- Apply
terraform apply
- If you login to the OneFuse web ui, navigate to modules, naming and scroll down to Managed Names you will see your newly generated name in the list.
- Destroy
terraform destroy
Once destroyed you will see the Managed Name in OneFuse has been removed and the name no longer exists.
For example, terraform configurations visit our onefuse-examples in our github repo.
Want to try OneFuse with Terraform for yourself? Check out the WWT HOL Accelerating Terraform with OneFuse.