Skip to main content

Overview

The DeleteResourceAction class handles the deletion of resources from NetBird. It removes the resource via the NetBird API, logs the deletion, and cleans up the local database reference.

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,
    User $user
): void

Parameters

resourceId
string
required
The NetBird resource ID to delete
resourceData
array
required
An array containing the resource data for logging purposes:
  • name (string, required): The resource name
  • address (string, required): The resource address
  • description (string|null, optional): The resource description
user
User
required
The user performing the deletion (used for audit logging)

Return Value

This action returns void (no return value).

Side Effects

This action performs the following side effects:
  1. NetBird API Call: Deletes the resource from NetBird via NetbirdService::deleteResource()
  2. Audit Log: Creates a ResourceLog entry with action type Deleted and records the resource details
  3. Database Cleanup: Deletes the corresponding Resource record from the local database where netbird_id matches
Deletion is permanent and cannot be undone. The resource will be removed from NetBird and all local references will be deleted.

Example Usage

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

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

// Prepare resource data for logging
$resourceData = [
    'name' => 'Production Database',
    'address' => '10.0.1.50',
    'description' => 'Main PostgreSQL database server',
];

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

// Resource is now deleted from NetBird and local database

Operation Order

The action performs operations in this specific order:
  1. Delete from NetBird API
  2. Log the deletion
  3. Clean up local database
The local database cleanup happens last to ensure the audit log is created successfully. If the NetBird API call fails, no database changes will occur.

Error Scenarios

If the NetBird API call fails (e.g., resource not found, network error), the action will throw an exception before creating the audit log or cleaning up the local database. This ensures data consistency.
The resourceData parameter is required because the resource may not exist in the local database at deletion time. Passing the data explicitly ensures proper audit logging regardless of database state.

Source Reference

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

Build docs developers (and LLMs) love