Overview
TheToggleResourceAction class handles toggling the enabled/disabled state of resources in NetBird. It inverts the current status, updates the resource via the NetBird API, and logs the state change.
Constructor Dependencies
The action is instantiated with the following dependency:Service class for interacting with the NetBird API
Method Signature
Parameters
The NetBird resource ID to toggle
An array containing the current resource data:
name(string, required): The resource nameaddress(string, required): The resource addressdescription(string|null, optional): The resource descriptionenabled(bool, required): The current enabled status
The NetBird group ID to maintain the resource association
The user performing the toggle action (used for audit logging)
Return Value
Toggle Logic
The action inverts the currentenabled status:
- If current status is
true(enabled), new status isfalse(disabled) - If current status is
false(disabled), new status istrue(enabled) - If status is missing, defaults to
trueand toggles tofalse
Side Effects
This action performs the following side effects:- NetBird API Call: Updates the resource in NetBird via
NetbirdService::updateResource()with the inverted enabled status - Audit Log: Creates a
ResourceLogentry with action type:Enabledif the resource was toggled to enabledDisabledif the resource was toggled to disabled
Example Usage
Audit Logging
The action logs two different action types depending on the toggle direction:The audit log only records the
enabled field change, not all resource attributes. This keeps the log focused on the state change.Use Cases
Common scenarios for toggling resources:- Temporary maintenance: Disable a resource during maintenance windows
- Access control: Quickly enable/disable access to resources
- Emergency response: Rapidly disable compromised resources
- Testing: Toggle resources during testing without deleting them
Error Scenarios
The action requires the current resource state to be passed in. If the
resourceData is stale, the toggle may produce unexpected results. Always fetch current state before toggling.Why Pass Resource Data?
UnlikeUpdateResourceAction, this action requires passing the full resource data because:
- Preserves all fields: Name, address, description must be sent in the update
- Knows current state: The current
enabledvalue determines the toggle direction - Avoids extra API call: Caller has already fetched the resource
Source Reference
Implementation:/home/daytona/workspace/source/app/Actions/ToggleResourceAction.php:24