Skip to main content

Overview

The CreateResourceAction class handles the creation of new resources directly in NetBird. It creates the resource via the NetBird API, stores a reference in the local database, and logs the action for audit purposes.

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 $name,
    string $address,
    ?string $description,
    bool $enabled,
    string $groupId,
    User $user
): array

Parameters

name
string
required
The name of the resource to create
address
string
required
The network address of the resource (e.g., IP address or hostname)
description
string|null
required
Optional description of the resource. Can be null
enabled
bool
required
Whether the resource should be enabled upon creation
groupId
string
required
The NetBird group ID to associate the resource with
user
User
required
The user performing the action (used for audit logging)

Return Value

return
array
Returns an array containing the created 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
Whether the resource is enabled

Side Effects

This action performs the following side effects:
  1. NetBird API Call: Creates a new resource in NetBird via NetbirdService::createResource()
  2. Database Insert: Creates a new Resource record with:
    • netbird_id: The ID returned from NetBird
    • user_id: The ID of the user creating the resource
    • created_by: The name of the user creating the resource
  3. Audit Log: Creates a ResourceLog entry with action type Created and tracks all resource attributes

Example Usage

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

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

// Execute the action
$user = User::find(1);
$result = $action->execute(
    name: 'Production Database',
    address: '10.0.1.50',
    description: 'Main PostgreSQL database server',
    enabled: true,
    groupId: 'grp_abc123',
    user: $user
);

// Result contains the created resource data
echo $result['id']; // e.g., "res_xyz789"
echo $result['name']; // "Production Database"

Error Scenarios

If the NetBird API call fails (e.g., invalid group ID, network error), the action will throw an exception before creating database records. The database transaction ensures no partial data is saved.
The action logs all resource attributes in the audit log, including the user who performed the action and when it occurred. This provides full traceability for compliance and debugging.

Source Reference

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

Build docs developers (and LLMs) love