API: Default organization_id to authenticated org when empty

When creating devices via API, organization_id is now properly defaulted
to the authenticated organization's ID even when an empty string is sent.

This fixes Terraform provider compatibility where organization_id may be
sent as "" instead of omitted entirely.

Before: Only checked if key was missing (Map.has_key?)
After: Also handles nil and empty string values
This commit is contained in:
Graham McIntire 2026-02-04 16:03:20 -06:00
parent a97ae5ad09
commit 7a1317d0d0
No known key found for this signature in database

View file

@ -88,12 +88,12 @@ defmodule ToweropsWeb.Api.V1.DevicesController do
organization_id = conn.assigns.current_organization_id
current_user = conn.assigns[:current_user]
# Add organization_id to params if not provided
# Default organization_id to authenticated org if not provided or empty
device_params =
if Map.has_key?(device_params, "organization_id") do
device_params
else
Map.put(device_params, "organization_id", organization_id)
case Map.get(device_params, "organization_id") do
nil -> Map.put(device_params, "organization_id", organization_id)
"" -> Map.put(device_params, "organization_id", organization_id)
_provided_id -> device_params
end
# Verify site belongs to organization if site_id is provided