make snmp opt-in for terraform provider devices

change snmp_enabled default from true to false so devices like
resolver1 that only need ping/dns don't trigger unnecessary snmp
discovery. skip sending snmp_version/snmp_port to the api when
snmp_enabled is false.
This commit is contained in:
Graham McIntire 2026-03-17 11:13:07 -05:00
parent bc3dbfe419
commit 12206c16fe
No known key found for this signature in database
2 changed files with 82 additions and 66 deletions

View file

@ -55,6 +55,16 @@ resource "towerops_device" "secure_switch" {
} }
``` ```
### Ping/DNS-only Device (no SNMP)
```terraform
resource "towerops_device" "resolver" {
site_id = towerops_site.example.id
name = "resolver1"
ip_address = "204.110.191.240"
}
```
### Minimal Configuration ### Minimal Configuration
```terraform ```terraform
@ -77,7 +87,7 @@ resource "towerops_device" "switch" {
- `name` (String) - The name of the device. If not provided, will be auto-discovered from SNMP. - `name` (String) - The name of the device. If not provided, will be auto-discovered from SNMP.
- `description` (String) - A description of the device. - `description` (String) - A description of the device.
- `monitoring_enabled` (Boolean) - Whether monitoring is enabled for this device. Default: `true`. - `monitoring_enabled` (Boolean) - Whether monitoring is enabled for this device. Default: `true`.
- `snmp_enabled` (Boolean) - Whether SNMP polling is enabled for this device. Default: `true`. - `snmp_enabled` (Boolean) - Whether SNMP polling is enabled for this device. Default: `false`.
- `snmp_version` (String) - The SNMP version to use (`1`, `2c`, or `3`). Default: `"2c"`. - `snmp_version` (String) - The SNMP version to use (`1`, `2c`, or `3`). Default: `"2c"`.
- `snmp_port` (Number) - The SNMP port to use. Default: `161`. - `snmp_port` (Number) - The SNMP port to use. Default: `161`.

View file

@ -105,7 +105,7 @@ func (r *DeviceResource) Schema(ctx context.Context, req resource.SchemaRequest,
Description: "Whether SNMP polling is enabled for this device.", Description: "Whether SNMP polling is enabled for this device.",
Optional: true, Optional: true,
Computed: true, Computed: true,
Default: booldefault.StaticBool(true), Default: booldefault.StaticBool(false),
}, },
"snmp_version": schema.StringAttribute{ "snmp_version": schema.StringAttribute{
Description: "The SNMP version to use (1, 2c, or 3).", Description: "The SNMP version to use (1, 2c, or 3).",
@ -215,6 +215,8 @@ func (r *DeviceResource) Create(ctx context.Context, req resource.CreateRequest,
device.SNMPEnabled = &enabled device.SNMPEnabled = &enabled
} }
// Only send SNMP config when SNMP is enabled
if data.SNMPEnabled.ValueBool() {
if !data.SNMPVersion.IsNull() { if !data.SNMPVersion.IsNull() {
version := data.SNMPVersion.ValueString() version := data.SNMPVersion.ValueString()
device.SNMPVersion = &version device.SNMPVersion = &version
@ -255,6 +257,7 @@ func (r *DeviceResource) Create(ctx context.Context, req resource.CreateRequest,
password := data.SNMPv3PrivPassword.ValueString() password := data.SNMPv3PrivPassword.ValueString()
device.SNMPv3PrivPassword = &password device.SNMPv3PrivPassword = &password
} }
}
created, err := r.client.CreateDevice(device) created, err := r.client.CreateDevice(device)
if err != nil { if err != nil {
@ -435,6 +438,8 @@ func (r *DeviceResource) Update(ctx context.Context, req resource.UpdateRequest,
device.SNMPEnabled = &enabled device.SNMPEnabled = &enabled
} }
// Only send SNMP config when SNMP is enabled
if data.SNMPEnabled.ValueBool() {
if !data.SNMPVersion.IsNull() { if !data.SNMPVersion.IsNull() {
version := data.SNMPVersion.ValueString() version := data.SNMPVersion.ValueString()
device.SNMPVersion = &version device.SNMPVersion = &version
@ -475,6 +480,7 @@ func (r *DeviceResource) Update(ctx context.Context, req resource.UpdateRequest,
password := data.SNMPv3PrivPassword.ValueString() password := data.SNMPv3PrivPassword.ValueString()
device.SNMPv3PrivPassword = &password device.SNMPv3PrivPassword = &password
} }
}
updated, err := r.client.UpdateDevice(data.ID.ValueString(), device) updated, err := r.client.UpdateDevice(data.ID.ValueString(), device)
if err != nil { if err != nil {