SARK V6 API

From sailpbx
Revision as of 11:35, 28 May 2020 by Adminwiki (talk | contribs) (Class of Service)
Jump to: navigation, search

back to SARK contents

sark6api

sark6api is fairly vanilla OAS API so it should be familiar to anyone who uses modern api's in their work. It allows you to programmatically do (almost) anything you can do at the SARK V6 browser. Why would you want to do that? Well maybe you have an idea for an extension to SARK, maybe you want to rewrite all or parts of the SARK browser and incorporate it into a package of your own. Maybe you want to control a remote SARK from a management layer. All of these things are possible with the API.

sark6api can be installed onto buster (Debian 10) or bionic (Ubuntu 18). Only 64 bit architectures are supported for both X86 and ARM.

It was tested with Postman 7.2 and developed on Vagrant Homestead running Ubuntu 18.04. sark6api uses the Laravel 7 API framework.

Requirements

  • PHP 7.2.5 or higher
  • PHP Composer
  • Laravel 7
  • GiT
  • SARK V6.0.1-50 or higher

Before you begin

If you haven't used Postman before then it would be a good idea to familiarise yourself with it before you begin. Postman is a general purpose API testing tool which is easy to use (at least for what we want to do). It runs as an app on OSX or Windows and there is also a version for Ubuntu desktop. It makes testing and exploring API's a breeze. In my view it's essential for this kind of work.


WARNING

The initial release of sark6api works as a "trusted" peer. It has no security of its own so you must use fixed IP's and secure firewall rules to keep it safe from baddies. So, for now, no dynamic IP clients. We are working on an OAuth security blanket and it will be ready as soon as we can get it out the door but you can test with it as-is as long as you observe point-to-point security using the host SARK firewall rules.

The srk6api runs at port 44300 (HTTPS).

Installation

You will need a SARKV6 host running on buster or bionic. Doesn't matter what the underlying platform is so you can run it on metal or in virtual as the muse takes you.

Update your existing deb packages

sudo apt update
sudo apt upgrade

NB - this will bring your SARK up to the latest V6 release.

GiT should already be installed as part of the sark6 install. If not then you can do

sudo apt install git

There are a few PHP extensions you may need over and above what gets dragged along with SARK. However, my Vagrant Homestead dev box doesn't appear to have any of them and chugs along OK so YMMV.

  • BCMath PHP Extension (php-bcmath)
  • Ctype PHP Extension (php-ctype)
  • Fileinfo PHP extension (php-fileinfo)
  • Tokenizer PHP Extension (php-tokenizer)
  • zip (php-zip)

I'm not sure about these because my Vagrant Homestead dev box doesn't appear to have any of them except php-zip yet it chugs along OK so YMMV. So, lets install zip

apt install php-zip


Next, you can install composer. Composer is the PHP package manager. You can think of it a bit like the Debian package manager but with a lot of stuff in it just for PHP. It has more or less become the tool of choice for GiT PHP developers to package their code and we need it to enable the Laravel API framework.

apt install composer

You will also need a regular user account on the box. It doesn't much matter what you choose, it's just an anchor to install the code.

adduser s6api

logout and back in and su to the new account

sudo su - s6api


OK, we're now in out new user and ready to install laravel.


Next we need to add the composer bin to our path

export PATH=$PATH:$HOME/.config/composer/vendor/bin

So far so good. Now we have to create a wrapper directory and clone the api into it

mkdir www
cd www
git clone https://github.com/aelintra/sark6api.git

Next we use composer to add the required Laravel dependencies (which aren't on the Git image)

cd sark6api
composer install

Next we have to add a .env file for Laravel. Create a file called .env in sark6api and paste this into it

APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=sqlite

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Now generate an encryption key

php artisan key:generate

Methods

Agents

GET /agents/{agent?}

POST /agents

Body
'pkey' = 'required|integer|min:1000|max:9999';
'cluster' = 'required|exists:cluster';
'name' = 'required|alpha_dash';
'passwd' = 'required|integer|min:1000|max:9999';
'queue1' => 'exists:queue|nullable',
'queue2' => 'exists:queue|nullable',
'queue3' => 'exists:queue|nullable',
'queue4' => 'exists:queue|nullable',
'queue5' => 'exists:queue|nullable',
'queue6' => 'exists:queue|nullable'

PUT /agents/{agent}

Body
'cluster' => 'exists:cluster,pkey',
'name' => 'alpha_dash',
'passwd' => 'integer',
'queue1' => 'exists:queue|nullable',
'queue2' => 'exists:queue|nullable',
'queue3' => 'exists:queue|nullable',
'queue4' => 'exists:queue|nullable',
'queue5' => 'exists:queue|nullable',
'queue6' => 'exists:queue|nullable'

DELETE /agents/{agent}

Asterisk AMI

Most of the AMI functions use GET. They return either a single instance or an instance list. AMI method Originate uses POST and two of the database functions use PUT. AMI methods are case sensitive for consistency with the Asterisk AMI documentation.

GET

Instance List

GET /astamis/

Agents
ConfbridgeListRooms
CoreShowChannels
ExtensionStateList
IAXpeers
IAXregistry
ListCommands
QueueStatus
QueueSummary
SIPpeers
SIPshowregistry
Status
VoicemailUsersList
Instance

GET /astamis/

CoreSettings
CoreStatus
DBget/{id}/{key}
ExtensionState/{id}{context?}
MailboxCount/{id}
MailboxStatus/{id}
QueueStatus/{id}
QueueSummary/{id}
Reload
SIPshowpeer/{id}
POST /astamis/originate
originate a new bridge
Body
  • target = 'required|integer';
  • ringback = 'required|numeric';
  • context= alpha_dash';
  • clid = 'numeric';
PUT /astamis/DBdel/{id}/{key}
PUT /astamis/DBPut/{id}/{key}(value)

Backups

GET /backups

return a list of available backups on the server

GET /backups/{backup}

Download an existing backup from the server

GET /backups/new

Request a new backup be taken and saved in the backup set

POST /backups

Upload a local zipped backup to the backup set on the server
Body
'uploadzip' => 'required|file|mimetypes:application/zip'

PUT /backups{backup}

Restore an existing backup from the backup set to the live system. Choose which elements of the backup set you want to restore by setting the corresponding element to true (N.B. this is JSON true - i.e. no quotes)
Body
'restoredb' => 'boolean',
'restoreasterisk' => 'boolean',
'restoreusergreeting' => 'boolean',
'restorevmail' => 'boolean',
'restoreldap' => 'boolean'

DELETE /backups/{backup}

Class of Service

There are three elements to CoS.

  • A rule (cosrule) which defines a particular dialplan set to be tested
  • A closed instance of an egress rule (cosclose) which applies to a particular endpoint(extension)
  • An open instance of an egress rule (cosopen) which applies to a particular endpoint(extension)

Class of service instances are accessed using the extension number as the key (NOT the Class of Service key)

coscloses

GET /coscloses

returns a list of the cosrule/extension intersections from the database

GET /coscloses/{cosclose}

POST /coscloses/{cosclose}

create a new cosclose instance
Body
'IPphone_pkey' => 'exists:ipphone,pkey',
'COS_pkey' => 'exists:cos,pkey'

DELETE/coscloses/{cosclose}

Route::get('coscloses', 'CosCloseController@index'); Route::get('coscloses/{cosclose}', 'CosCloseController@show'); Route::post('coscloses', 'CosCloseController@save'); Route::put('coscloses/{cosclose}', 'CosCloseController@update'); Route::delete('coscloses/{cosclose}', 'CosCloseController@delete');

Route::get('cosopens', 'CosOpenController@index'); Route::get('cosopens/{cosopen}', 'CosOpenController@show'); Route::post('cosopens', 'CosOpenController@save'); Route::put('cosopens/{cosopen}', 'CosOpenController@update'); Route::delete('cosopens/{cosopen}', 'CosOpenController@delete');

Route::get('cosrules', 'ClassOfServiceController@index'); Route::get('cosrules/{classofservice}', 'ClassOfServiceController@show'); Route::post('cosrules', 'ClassOfServiceController@save'); Route::put('cosrules/{classofservice}', 'ClassOfServiceController@update'); Route::delete('cosrules/{classofservice}', 'ClassOfServiceController@delete');

Route::get('customapps', 'CustomAppController@index'); Route::get('customapps/{customapp}', 'CustomAppController@show'); Route::post('customapps', 'CustomAppController@save'); Route::put('customapps/{customapp}', 'CustomAppController@update'); Route::delete('customapps/{customapp}', 'CustomAppController@delete');

Route::get('daytimers', 'DayTimerController@index'); Route::get('daytimers/{daytimer}', 'DayTimerController@show'); Route::post('daytimers', 'DayTimerController@save'); Route::put('daytimers/{daytimer}', 'DayTimerController@update'); Route::delete('daytimers/{daytimer}', 'DayTimerController@delete');

Route::get('destinations', 'DestinationController@index');

Route::get('extensions', 'ExtensionController@index'); Route::get('extensions/{extension}', 'ExtensionController@show'); Route::get('extensions/{extension}/runtime', 'ExtensionController@showruntime'); Route::post('extensions/mailbox', 'ExtensionController@mailbox'); Route::post('extensions/provisioned', 'ExtensionController@provisioned'); Route::post('extensions/vxt', 'ExtensionController@vxt'); Route::post('extensions/unprovisioned', 'ExtensionController@unprovisioned'); Route::put('extensions/{extension}', 'ExtensionController@update'); Route::put('extensions/{extension}/runtime', 'ExtensionController@updateruntime'); Route::delete('extensions/{extension}', 'ExtensionController@delete');

Route::get('firewall', 'FirewallController@index'); Route::get('firewall/ipv4', 'FirewallController@ipv4'); Route::get('firewall/ipv6', 'FirewallController@ipv6'); Route::post('firewall/ipv4', 'FirewallController@ipv4save'); Route::post('firewall/ipv6', 'FirewallController@ipv6save'); Route::put('firewall/ipv4', 'FirewallController@ipv4restart'); Route::put('firewall/ipv6', 'FirewallController@ipv6restart');

Route::get('greetings', 'GreetingController@index'); Route::get('greetings/{greeting}', 'GreetingController@download'); Route::post('greetings', 'GreetingController@save'); Route::delete('greetings/{greeting}', 'GreetingController@delete');

Route::get('holidaytimers', 'HolidayTimerController@index'); Route::get('holidaytimers/{holidaytimer}', 'HolidayTimerController@show'); Route::post('holidaytimers', 'HolidayTimerController@save'); Route::put('holidaytimers/{holidaytimer}', 'HolidayTimerController@update'); Route::delete('holidaytimers/{holidaytimer}', 'HolidayTimerController@delete');

Route::get('inboundroutes', 'InboundRouteController@index'); Route::get('inboundroutes/{inboundroute}', 'InboundRouteController@show'); Route::post('inboundroutes', 'InboundRouteController@save'); Route::put('inboundroutes/{inboundroute}', 'InboundRouteController@update'); Route::delete('inboundroutes/{inboundroute}', 'InboundRouteController@delete');

Route::get('ivrs', 'IvrController@index'); Route::get('ivrs/{ivr}', 'IvrController@show'); Route::post('ivrs', 'IvrController@save'); Route::put('ivrs/{ivr}', 'IvrController@update'); Route::delete('ivrs/{ivr}', 'IvrController@delete');

Route::get('logs', 'LogController@index'); Route::get('logs/cdrs/{limit?}', 'LogController@showcdr');

Route::get('queues', 'QueueController@index'); Route::get('queues/{queue}', 'QueueController@show'); Route::post('queues', 'QueueController@save'); Route::put('queues/{queue}', 'QueueController@update'); Route::delete('queues/{queue}', 'QueueController@delete');

Route::get('snapshots', 'SnapShotController@index'); Route::get('snapshots/new', 'SnapShotController@new'); Route::get('snapshots/{snapshot}', 'SnapShotController@download'); Route::post('snapshots', 'SnapShotController@save'); Route::put('snapshots/{snapshot}', 'SnapShotController@update'); Route::delete('snapshots/{snapshot}', 'SnapShotController@delete');

Route::get('ringgroups', 'RingGroupController@index'); Route::get('ringgroups/{ringgroup}', 'RingGroupController@show'); Route::post('ringgroups', 'RingGroupController@save'); Route::put('ringgroups/{ringgroup}', 'RingGroupController@update'); Route::delete('ringgroups/{ringgroup}', 'RingGroupController@delete');

Route::get('routes', 'RouteController@index'); Route::get('routes/{route}', 'RouteController@show'); Route::post('routes', 'RouteController@save'); Route::put('routes/{route}', 'RouteController@update'); Route::delete('routes/{route}', 'RouteController@delete');

Route::get('syscommands', 'SysCommandController@index'); Route::get('syscommands/commit', 'SysCommandController@commit'); Route::get('syscommands/reboot', 'SysCommandController@reboot'); Route::get('syscommands/pbxrunstate', 'SysCommandController@pbxrunstate'); Route::get('syscommands/pbxstart', 'SysCommandController@start'); Route::get('syscommands/pbxstop', 'SysCommandController@stop');

Route::get('sysglobals', 'SysglobalController@index'); Route::put('sysglobals', 'SysglobalController@update');

Route::get('templates', 'TemplateController@index'); Route::get('templates/{template}', 'TemplateController@show'); Route::post('templates', 'TemplateController@save'); Route::put('templates/{template}', 'TemplateController@update'); Route::delete('templates/{template}', 'TemplateController@delete');

Route::get('tenants', 'TenantController@index'); Route::get('tenants/{tenant}', 'TenantController@show'); Route::post('tenants', 'TenantController@save'); Route::put('tenants/{tenant}', 'TenantController@update'); Route::delete('tenants/{tenant}', 'TenantController@delete');

Route::get('trunks', 'TrunkController@index'); Route::get('trunks/{trunk}', 'TrunkController@show'); Route::post('trunks', 'TrunkController@save'); Route::put('trunks/{trunk}', 'TrunkController@update'); Route::delete('trunks/{trunk}', 'TrunkController@delete');