HomeGuidesAPI Reference
GuidesAPI ReferenceGitHubAirheads Developer CommunityLog In
Guides

Example using Terraform

Prerequisites

Terraform version >= v1.7.0 and 64-bit install terraform.
A Greenlake API client to authenticate against UXI configuration API.

See here for more information on generating client credentials.

  1. Open the previously generated file using nano uxi.tf and add client id and client secretof UXI from the GreenLake account.

Note: We recommend secrets (GreenLake credentials) be stored as environment variables, as shown here: https://registry.terraform.io/providers/HewlettPackard/hpeuxi/latest/docs#optional

terraform {
  required_providers {
    hpeuxi = {
      source = "HewlettPackard/hpeuxi"
      version = "0.1.0-alpha.1"
    }
  }
}
# ====================== PROVIDER CONFIGURATION =======================

provider "hpeuxi" {
  # Replace with actual provider configuration
  client_id     = <your client_id> (RATHER USE HPEUXI_CLIENT_ID ENV VAR)
  client_secret = <your client_secret> (RATHER USE HPEUXI_CLIENT_SECRET ENV VAR)
}
  1. Run first Terraform Execution Plan.

The best way to get started using Terraform is to run execution plans. All Terraform plans are written in HCL (Hashicorp Configuration Language). HCL is very similar to the JSON format.

Example: Create Parent Group and Child group in the hierarchy using Terraform

Open the file using the nano uxi.tfand copy the following code:

terraform {
  required_providers {
    hpeuxi = {
      source = "HewlettPackard/hpeuxi"
      version = "0.1.0-alpha.1"
    }
  }
}

# ============================== PROVIDER CONFIGURATION ============================================

provider "hpeuxi" {
  # Replace with actual provider configuration
  client_id     = <your client_id> (RATHER USE HPEUXI_CLIENT_ID ENV VAR)
  client_secret = <your client_secret> (RATHER USE HPEUXI_CLIENT_SECRET ENV VAR)
}

# ============================== GROUP RESOURCE CONFIGURATION ======================================

# Create level 1 group attached to the root node
resource "hpeuxi_group" "level_1" {
  name = "Parent Group"
}

# Create level 2 group attached to level 1 group
resource "hpeuxi_group" "level_2" {
  name            = "Child Group"
  parent_group_id = hpeuxi_group.level_1.id
}

# ============================== AGENT RESOURCE CONFIGURATION ======================================

# Declare an agent resource
resource "hpeuxi_agent" "my_agent" {
  name      = "HPE-YQ5G7FLWQ9"
  notes     = "notes"
  pcap_mode = "light"
}

# Import agent using its ID with an import block
import {
    to = hpeuxi_agent.my_agent
    id = "e9199fe1-d055-39bc-b691-593e645e6783"
}

# Assign an agent to a group
resource "hpeuxi_agent_group_assignment" "my_agent_group_assignment" {
  agent_id = hpeuxi_agent.my_agent.id
  group_id = hpeuxi_group.level_2.id
}

# ============================== SENSOR RESOURCE CONFIGURATION =====================================

# Declare a sensor resource
resource "hpeuxi_sensor" "my_sensor" {
  name         = "name"
  address_note = "Example 3rd Floor"
  notes        = "notes"
  pcap_mode    = "light"

  # Deleting a sensor is not supported
  lifecycle {
    prevent_destroy = true
  }
}

# Import sensor using its ID with an import block
import {
    to = hpeuxi_sensor.my_sensor
    id = <sensor_id>
}

# Assign a sensor to a group
resource "hpeuxi_sensor_group_assignment" "my_sensor_group_assignment" {
  sensor_id =  = hpeuxi_sensor.my_sensor.id
  group_id = hpeuxi_group.level_2.id
}

# ============================== WIRED NETWORK RESOURCE CONFIGURATION ==============================

# Declare a wired network resource
resource "hpeuxi_wired_network" "my_wired_network" {
  name = "tf-ethernet-0"

  # Deleting of wired networks is not supported yet
  lifecycle {
    prevent_destroy = true
  }
}

# Import a wired network using its ID with an import block
import {
    to = hpeuxi_wired_network.my_wired_network
    id = <wired_network_id>
}

# Assign a wired network to the parent group (group and group children sensors will test this
# network)
resource "hpeuxi_network_group_assignment" "my_wired_network_group_assignment" {
  network_id = hpeuxi_wired_network.my_wired_network.id
  group_id   = hpeuxi_group.level_1.id
}

# ============================== WIRELESS NETWORK RESOURCE CONFIGURATION ===========================

# Declare a wireless network resource
resource "hpeuxi_wireless_network" "my_wireless_network" {
  name = "tf-ssid-0"

  # Deleting of wireless networks is not supported yet
  lifecycle {
    prevent_destroy = true
  }
}

# Import a wireless network using its ID with an import block
import {
    to = hpeuxi_wireless_network.my_wireless_network
    id = <wireless_network_id>
}

# Assign a wireless network to the parent group (group and group children sensors will test this
# network)
resource "hpeuxi_network_group_assignment" "my_wireless_network_group_assignment" {
  network_id = hpeuxi_wireless_network.my_wireless_network.id
  group_id   = hpeuxi_group.level_1.id
}

# ============================== SERVICE TEST RESOURCE CONFIGURATION ===============================

# Declare a service test resource
resource "hpeuxi_service_test" "my_service_test" {
  name = "Google"

  # Deleting of service tests is not supported yet
  lifecycle {
    prevent_destroy = true
  }
}

# Import a service test using its ID with an import block
import {
    to = hpeuxi_service_test.my_service_test
    id = <service_test_id>
}

# Assign a service test to the parent group (group and group children sensors will test this
# service test)
resource "hpeuxi_service_test_group_assignment" "my_service_test_group_assignment" {
  service_test_id = hpeuxi_service_test.my_service_test.id
  group_id        = hpeuxi_group.level_1.id
}

  1. Run the plan using the following commands
terraform init
terraform plan
terraform apply

How to get the IDs of resources for import reasons?

To get the IDs, one could either use the configuration API GET Endpoint(s).

Using API GET Endpoint:

Example: To get sensor_id You can run the sensor GET endpoint and see the id field below:

{ 
"id": "06a2c5d6-ba8f-421c-b6b1-3aedc15ac08c",
"serial": "CNHJKLQ2M6", 
"name": "CNHJKLQ2M6",
"modelNumber": "UX-F5C", 
"wifiMacAddress": "20:4c:03:50:79:23",
"ethernetMacAddress": "20:4c:03:50:79:22",
"addressNote": "",
"longitude": -121.9880602,
"latitude": 37.3860927,
"notes": "", 
"pcapMode": "light", 
"type": "networking-uxi/sensor" 
}

Finding the right Terraform resource/data-source

We have all the resources that can help you manage the sensors, agents, groups and tests.

We have all the data sources that can help you retrieve information from sensors, agents, groups and tests.

Please follow this link to check out all the resources and data sources.