Skip to main content

Overview

The UpdateResourceAction class handles updating existing resources in NetBird. It modifies the resource via the NetBird API and logs the changes 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 $resourceId,
    string $name,
    string $address,
    ?string $description,
    bool $enabled,
    string $groupId,
    User $user
): array

Parameters

resourceId
string
required
The NetBird resource ID to update
name
string
required
The updated name of the resource
address
string
required
The updated network address of the resource
description
string|null
required
Optional updated description. Can be null
enabled
bool
required
Whether the resource should be enabled
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 updated resource data from NetBird:
id
string
The unique NetBird resource ID
name
string
The updated resource name
address
string
The updated resource network address
description
string|null
The updated resource description (nullable)
enabled
bool
Whether the resource is enabled

Side Effects

This action performs the following side effects:
  1. NetBird API Call: Updates the resource in NetBird via NetbirdService::updateResource()
  2. Audit Log: Creates a ResourceLog entry with action type Updated and tracks all changed attributes
Unlike CreateResourceAction, this action does not modify the local Resource database table. The local table only stores the NetBird ID mapping and creation metadata.

Example Usage

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

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

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

// Result contains the updated resource data
echo $result['name']; // "Production Database - Updated"
echo $result['address']; // "10.0.1.55"

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.
All update operations are logged with the complete new state of the resource, not just the changed fields. This ensures the audit log provides a complete history.

Source Reference

Implementation: /home/daytona/workspace/source/app/Actions/UpdateResourceAction.php:23

Build docs developers (and LLMs) love