axsor/laravel-phpipam

PhpIPAM wrapper for laravel

1.1.1 2021-06-07 09:03 UTC

README

Latest Stable Version Build Status StyleCI Scrutinizer Code Quality

PhpIPAM wrapper for laravel

Attention: Lastest stable version is from this repository. Actual developement is not finished and posted to packagist yet.

Index

Installation

Install it via composer:

composer require axsor/laravel-phpipam

If you are using Laravel 5.4 or lower you must add PhpIPAMServiceProvider to your config/app.php:

'providers' => [
    Axsor\PhpIPAM\PhpIPAMServiceProvider::class,
],

Higher versions will auto-discover it.

Configuration

Edit your .env file and add your PhpIPAM credentials:

PHPIPAM_URL=https://your-phpipam-server/api
PHPIPAM_USER=username
PHPIPAM_PASSWORD=password
PHPIPAM_APP="phpipam application"
PHPIPAM_TOKEN="phpipam application token"
PHPIPAM_VERIFY_CERT=false    # Optional (Default: true)

And run php artisan config:cache to reload config cache.

If you want to use different PhpIPAM connections, you have to publish the config file using the next command and configure as connections as you want:

'default' => [
    'url' => env('PHPIPAM_URL'),
    'user' => env('PHPIPAM_USER'),
    'pass' => env('PHPIPAM_PASSWORD'),
    'app' => env('PHPIPAM_APP'),
    'token' => env('PHPIPAM_TOKEN'),
    'verify_cert' => env('PHPIPAM_VERIFY_CERT', true),
],

'second_connection' => [
    'url' => env('PHPIPAM_2_URL'),
    'user' => env('PHPIPAM_2_USER'),
    'pass' => env('PHPIPAM_2_PASSWORD'),
    'app' => env('PHPIPAM_2_APP'),
    'token' => env('PHPIPAM_2_TOKEN'),
    'verify_cert' => env('PHPIPAM_2_VERIFY_CERT', true),
],

php artisan vendor:publish --provider="Axsor\\PhpIPAM\\PhpIPAMServiceProvider" --tag="config"

If in some moment you want to use other different configuration you can set it:

$config = [
    'url' => 'https://phpipam.net/api',
    'user' => 'axsor',
    'pass' => 'secure',
    'app' => 'api-client',
    'token' => 'my_awesome_token',
    'verify_cert' => false
];

PhpIPAM::use($config)->address(22); // Sets custom configuration before execute method

PhpIPAM::ping(22); // Keeps custom configuration when execute method

PhpIPAM::useDefaultConfig(); // Discard custom configuration and uses default configuration

How to use

Object parameters nomenclature and data type must agree on PhpPIAM API Documentation.

Most of methods returns Models (Address, Subnet, ...), Collections of models or action results as boolean ('created', 'updated', ...).

When you call PhpIPAM model you can pass the ID or the model that contains the id. ex. PhpIPAM::subnet($subnetId) will return same than PhpIPAM::subnet($subnetObject).

use Axsor\PhpIPAM\Facades\PhpIPAM;

class MyController extends Controller
{
    public function ping(Request $request, $addressId)
    {
        return PhpIPAM::ping($addressId); // Returns true if address is online
    }

    public function address(Request $request, $addressId)
    {
        $user = Auth::user();

        $config = config('phpipam');
        $config['user'] = $user->phpipam_user;
        $config['pass'] = $user->phpipam_pass;

        return PhpIPAM::use($config)->address($addressId); // Returns address using custom config
    }

    public function addressUpdate(Request $request, $addressId)
    {
        $address = PhpIPAM::address($addressId);

        $address->hostname = 'New Hostname';

        $address->update();

        // Or
        PhpIPAM::addressUpdate($addressId);

        // Or
        $address = PhpIPAM::address($addressId);
        // Do some actions with address
        $newData = [...];
        PhpIPAM::addressUpdate($address, $newData);
    }
}

Connections

You can set multiple instances of PhpIPAM.

PhpIPAM::tags(); // Return tags from 'default' phpipam instance

PhpIPAM::connect('second_instance')->tags(); // Return tags from 'second_instance' phpipam instance
PhpIPAM::tags(); // Return tags from 'second_instance' phpipam instance

PhpIPAM::resetConnection()->tags(); // Return tags from 'default' phpipam instance

Available methods

All api calls are wrapped by the controllers into Models, Collections or simple data types.

(Pending:) If you want to get the response content without wrapping you can use the same command adding "Raw" as suffix. ex. PhpIPAM::addressRaw($address). That will return you an associative array with the response content.

The model methods will call Global methods;

Section

Global methods

PhpIPAM::sections();
PhpIPAM::section($section);
PhpIPAM::sectionSubnets($section);
PhpIPAM::sectionByName($section);
PhpIPAM::sectionCreate($section);
PhpIPAM::sectionUpdate($section, $newData);
PhpIPAM::sectionDrop($section);

Model methods

$section->update();
$section->drop();
$section->subnets();

Device

Global methods

PhpIPAM::devices();
PhpIPAM::device($device);
PhpIPAM::deviceAddresses($device);
PhpIPAM::deviceSubnets($device);
PhpIPAM::deviceCreate($data);
PhpIPAM::deviceUpdate($device, $data);
PhpIPAM::deviceDrop($device);

Model methods

$device->subnets();
$device->addresses();
$device->deviceTypes();
$device->update();
$device->drop();
$device->ping();

Subnet

Global methods

PhpIPAM::subnet($subnet);
PhpIPAM::subnetUsage($subnet);
PhpIPAM::subnetFreeAddress($subnet);
PhpIPAM::subnetSlaves($subnet);
PhpIPAM::subnetSlavesRecursive($subnet);
PhpIPAM::subnetAddresses($subnet);
PhpIPAM::subnetIp($subnet, "192.168.1.4");
PhpIPAM::subnetFreeSubnet($subnet, $mask);
PhpIPAM::subnetFreeSubnets($subnet, $mask);
PhpIPAM::subnetCustomFields();
PhpIPAM::subnetByCidr("192.168.1.0/24");
PhpIPAM::subnetCreate($subnetData);
PhpIPAM::subnetCreateInSubnet($subnet, $subnetData);
PhpIPAM::subnetUpdate($subnet, $subnetData);
PhpIPAM::subnetResize($subnet, $mask);
PhpIPAM::subnetSplit($subnet, $number);
PhpIPAM::subnetDrop($subnet);
PhpIPAM::subnetTruncate($subnet);

Model methods

$subnet->update();
$subnet->drop();
$subnet->usage();
$subnet->freeAddress();
$subnet->slaves();
$subnet->slavesRecursive();
$subnet->addresses();
$subnet->ip("192.168.168.4");
$subnet->freeSubnet($mask);
$subnet->freeSubnet($mask);
$subnet->resize($mask);
$subnet->split($number);
$subnet->truncate();

Address

Global methods

PhpIPAM::address($address);
PhpIPAM::ping($address);
PhpIPAM::addressByIp("10.140.128.1");
PhpIPAM::addressByHostname("phpipam.net");
PhpIPAM::addressCustomFields();
PhpIPAM::addressTags();
PhpIPAM::addressTag($tag);
PhpIPAM::tagAddresses($tag);
PhpIPAM::addressCreate($data);
PhpIPAM::addressCreateFirstFree($subnet);
PhpIPAM::addressUpdate($address, $data);
PhpIPAM::addressDrop($address);

Model methods

$address->update();
$address->drop();
$address->ping();

Circuit

Global methods

PhpIPAM::circuits();
PhpIPAM::circuit($circuit);
PhpIPAM::circuitCreate($circuit);
PhpIPAM::circuitUpdate($circuit, $newData);
PhpIPAM::circuitDrop($circuit);

Model methods

$circuit->update();
$circuit->drop();
$circuit->subnets();

Tools

Global methods

PhpIPAM::locations();
PhpIPAM::location($location);
PhpIPAM::locationCreate($location);
PhpIPAM::locationUpdate($location, $newData);
PhpIPAM::locationDrop($location);

PhpIPAM::tags();
PhpIPAM::tag($tag);
PhpIPAM::tagCreate($tag);
PhpIPAM::tagUpdate($tag, $newData);
PhpIPAM::tagDrop($tag);

PhpIPAM::deviceTypes();

Model methods

$location->update();
$location->drop();

$tag->update();
$tag->drop();

License

GPL-3.0