Skip to main content

Overview

The ToggleResourceAction 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:
netbirdService
NetbirdService
required
Service class for interacting with the NetBird API

Method Signature

public function execute(
    string $resourceId,
    array $resourceData,
    string $groupId,
    User $user
): array

Parameters

resourceId
string
required
The NetBird resource ID to toggle
resourceData
array
required
An array containing the current resource data:
  • name (string, required): The resource name
  • address (string, required): The resource address
  • description (string|null, optional): The resource description
  • enabled (bool, required): The current enabled status
groupId
string
required
The NetBird group ID to maintain the resource association
user
User
required
The user performing the toggle action (used for audit logging)

Return Value

return
array
Returns an array containing the updated resource data from NetBird:
id
string
The unique NetBird resource ID
name
string
The resource name
address
string
The resource network address
description
string|null
The resource description (nullable)
enabled
bool
The new enabled status (inverted from the input)

Toggle Logic

The action inverts the current enabled status:
$newEnabled = ! ($resourceData['enabled'] ?? true);
  • If current status is true (enabled), new status is false (disabled)
  • If current status is false (disabled), new status is true (enabled)
  • If status is missing, defaults to true and toggles to false

Side Effects

This action performs the following side effects:
  1. NetBird API Call: Updates the resource in NetBird via NetbirdService::updateResource() with the inverted enabled status
  2. Audit Log: Creates a ResourceLog entry with action type:
    • Enabled if the resource was toggled to enabled
    • Disabled if the resource was toggled to disabled

Example Usage

use App\Actions\ToggleResourceAction;
use App\Services\NetbirdService;
use App\Models\User;

// Instantiate the action with dependencies
$netbirdService = app(NetbirdService::class);
$action = new ToggleResourceAction($netbirdService);

// Prepare current resource data
$resourceData = [
    'name' => 'Production Database',
    'address' => '10.0.1.50',
    'description' => 'Main PostgreSQL database',
    'enabled' => true, // Currently enabled
];

// Execute the action
$user = User::find(1);
$result = $action->execute(
    resourceId: 'res_xyz789',
    resourceData: $resourceData,
    groupId: 'grp_abc123',
    user: $user
);

// Resource is now disabled
echo $result['enabled']; // false

Audit Logging

The action logs two different action types depending on the toggle direction:
// Action type: ResourceAction::Enabled
// Log entry shows: "Resource enabled"
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

If the NetBird API call fails (e.g., resource not found, invalid group ID, network error), the action will throw an exception. No audit log will be created in this case.
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?

Unlike UpdateResourceAction, this action requires passing the full resource data because:
  1. Preserves all fields: Name, address, description must be sent in the update
  2. Knows current state: The current enabled value determines the toggle direction
  3. Avoids extra API call: Caller has already fetched the resource

Source Reference

Implementation: /home/daytona/workspace/source/app/Actions/ToggleResourceAction.php:24

Build docs developers (and LLMs) love