Merge pull request #11 from smos/master

Add a few more function for setting site specific parameters. It was …
This commit is contained in:
malle-pietje 2018-03-17 14:05:45 +01:00 committed by GitHub
commit 546334c88e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 657 additions and 1 deletions

View File

@ -12,4 +12,10 @@ If you would like to share your own example file(s), please open an issue and in
## Important Disclaimer
Use these examples at your own risk!
Use these examples at your own risk!
## Provisioning script
The example provision script easily creates a large number of sites with comparable network configuration. It sets the LAN config
to site specific configuration. Copy the settings.template.php and config.template.php to their respective files for testing. This is meant as a
basic provisioning only system. Currently used against controller 5.6.30.

336
examples/provision.php Normal file
View File

@ -0,0 +1,336 @@
<?php
/* include important files */
require_once('UniFi-API-client/src/Client.php');
/* Set the default timezone */
date_default_timezone_set('Europe/Amsterdam');
// Example array with site information, includes numeric reference
$fil_array = array();
$fil_array[600]['aktief'] = 1; // Active
$fil_array[600]['kassa_aantal'] = 1; // Cash registers
$fil_array[600]['divisie_code'] = "D"; // Brand
$fil_array[600]['corr_woonplaats'] = "Amsterdam"; // City
echo "<pre>";
// Import the controller auth config
include("config.php");
/**
* set to true (without quotes) to enable debug output to the browser and the PHP error log
*/
$debug = true;
$site_id = "default";
/**
* initialize the Unifi API connection class, log in to the controller and request the alarms collection
* (this example assumes you have already assigned the correct values to the variables used)
*/
$unifi_connection = new UniFi_API\Client($controller_user, $controller_password, $controller_url, $site_id, $controller_version, false);
$login = $unifi_connection->login();
if($login > 400) {
echo "Failed to log into controller";
die;
}
// $sites = $unifi_connection->list_sites(); // returns a PHP array containing sites
$open_shops = array();
// Mogrify shop info into useable arrays
foreach($fil_array as $filnr => $shop) {
if(floatval($shop['aktief']) == 0)
continue;
if($shop['divisie_code'] != "D")
continue;
if((floatval($shop['kassa_aantal']) > 0))
$open_shops[$filnr] = ucfirst(strtolower($shop['corr_woonplaats']));
}
// If debug, create Fake open shops array, otherwise unset test shops
if($debug === true) {
$open_shops = array();
$open_shops[943] = "Test 1";
$open_shops[965] = "Test 2";
} else {
unset($open_shops[943]);
unset($open_shops[965]);
unset($close_shops[943]);
unset($close_shops[965]);
}
// Check if we can find all our shop sites, otherwise add to todo list for creation, close list for deletion
$todo_shops = $open_shops;
$active_shops = array();
$close_shops = array();
foreach($unifi_connection->list_sites() as $site){
$desc = $site->desc;
// Does it look like a shop?
if(preg_match("/([0-9][0-9][0-9]+)/", $desc, $matches)) {
// echo "Found site {$desc}\n";
unset($todo_shops[floatval($matches[1])]);
$active_shops[floatval($matches[1])] = $site->name;
if(!$open_shops[floatval($matches[1])]) {
// echo "Shop {$matches[1]} does not have hardware\n";
$close_shops[floatval($matches[1])] = $site->name;
}
}
}
// If debug, create Fake site entries array, otherwise unset test shops
if($debug === true) {
$active_shops = array();
$active_shops[965] = "j103b83q";
$active_shops[943] = "winkels";
} else {
unset($active_shops[943]);
unset($active_shops[965]);
unset($close_shops[943]);
unset($close_shops[965]);
}
// Any sites we need to create before we can continue?
foreach($todo_shops as $filnr => $city){
$filnr = sprintf("%04d", $filnr);
$desc = "{$filnr} {$city}";
echo "Create site for {$filnr}\n";
$createsite = $unifi_connection->create_site($desc);
if($createsite === false) {
echo "Failed to create site for {$filnr}, id {$siteid}\n";
break;
}
}
// Refresh site list
if(count($todo_shops > 0)) {
foreach($unifi_connection->list_sites() as $site){
$desc = $site->desc;
// Does it look like a shop?
if(preg_match("/([0-9][0-9][0-9]+)/", $desc, $matches)) {
// echo "Found site {$desc}\n";
unset($todo_shops[floatval($matches[1])]);
$active_shops[floatval($matches[1])] = $site->name;
}
}
}
// We should have 0 todo shops now
// print_r($todo_shops);
/*
echo "Open\n";
print_r($open_shops);
echo "Active\n";
print_r($active_shops);
echo "Close\n";
print_r($close_shops);
die();
*/
// Foreach shop, select the site.
foreach($active_shops as $filnr => $siteid) {
$filnr = sprintf("%04d", $filnr);
$select = $unifi_connection->set_site($siteid);
// fetch configured group settings, we need those later, we only use the Default group.
$wlangroups = $unifi_connection->list_wlan_groups($siteid);
$usergroups = $unifi_connection->list_usergroups($siteid);
if(isset($close_shops[floatval($filnr)])) {
echo "Delete site {$siteid} with id ". $usergroups[0]->site_id ." for shop {$filnr}\n";
$delete = $unifi_connection->delete_site($usergroups[0]->site_id);
if($delete === false) {
echo "Failed to delete site for {$filnr}, id {$siteid}\n";
}
continue;
}
// fetch configured group settings, we need those later, we only use the Default group.
$wlangroups = $unifi_connection->list_wlan_groups($siteid);
$usergroups = $unifi_connection->list_usergroups($siteid);
if($debug===true) {
//var_export ($wlangroups);
//var_export ($usergroups);
}
foreach($wlangroups as $group){
// Check if template networks exist
if($group->name == "Default") {
$shawlangroup_id = $group->_id;
}
}
foreach($usergroups as $group){
// Check if template networks exist
if($group->name == "Default") {
$shausergroup_id = $group->_id;
}
}
// Include each time so site specific settings based on shop number are picked up
unset($wirednetworks);
unset($wlannetworks);
unset($siteconf);
include("settings.php");
refresh_networks();
refresh_wlans();
fetch_site_conf();
if($debug===true) {
// var_export ($siteconf);
//var_export ($wlanconf);
// var_export ($networkconf);
//print_r($wlannetworks);
}
foreach($sitesettings as $key => $values) {
echo "Update site setting {$key} id {$setting_id[$key]} for {$filnr}, id {$siteid}\n";
switch($key){
case "country":
$update_site[$key] = $unifi_connection->set_site_country($setting_id[$key], $sitesettings[$key]);
break;
case "locale":
$update_site[$key] = $unifi_connection->set_site_locale($setting_id[$key], $sitesettings[$key]);
break;
case "connectivity ":
$update_site[$key] = $unifi_connection->set_site_connectivity($setting_id[$key], $sitesettings[$key]);
break;
case "mgmt":
$update_site[$key] = $unifi_connection->set_site_mgmt($setting_id[$key], $sitesettings[$key]);
break;
case "guest_access":
$update_site[$key] = $unifi_connection->set_site_guest_access($setting_id[$key], $sitesettings[$key]);
break;
case "snmp":
$update_site[$key] = $unifi_connection->set_site_snmp($setting_id[$key], $sitesettings[$key]);
break;
case "ntp":
$update_site[$key] = $unifi_connection->set_site_ntp($setting_id[$key], $sitesettings[$key]);
break;
default:
break;
}
if($update_site[$key] === false)
echo "Failed to update setting {$key} for {$filnr}, id {$siteid} ". print_r($sitesettings[$key], true) ."\n";
}
foreach($wirednetworks as $key => $values) {
// Template network didn't exist, create
if($wired[$key] === false) {
echo "Create new vlan {$key} for {$filnr}, id {$siteid}\n";
$addnetwork[$key] = $unifi_connection->create_network($wirednetworks[$key]);
// echo json_encode($addvlan, JSON_PRETTY_PRINT);
}
if($addnetwork[$key] === false)
echo "Failed to add network {$key} for {$filnr}, id {$siteid}\n";
// Do we need to update?
if($wired[$key] === true) {
echo "Update network {$key} id {$wired_id[$key]} for {$filnr}, id {$siteid}\n";
$updatenetwork[$key] = $unifi_connection->set_networksettings_base($wired_id[$key], $wirednetworks[$key]);
}
if($updatenetwork[$key] === false)
echo "Failed to update network {$key} for {$filnr}, id {$siteid} ". print_r($wirednetworks[$key], true) . print_r($wired_id, true) ."\n";
}
foreach($wlannetworks as $key => $values) {
// Template network didn't exist, create
if($wlan[$key] === false) {
echo "Create new disabled wlan {$key} for {$filnr}, id {$siteid}\n";
$addwlan[$key] = $unifi_connection->create_wlan($wlannetworks[$key]['name'], $wlannetworks[$key]['x_passphrase'], $wlannetworks[$key]['usergroup_id'], $wlannetworks[$key]['wlangroup_id'], false);
}
if($addwlan[$key] === false)
echo "Failed to add wlan {$key} for {$filnr}, id {$siteid} ". print_r($wlannetworks[$key], true) ."\n";
else
refresh_wlans();
// Do we need to update?
if($wlan[$key] === true) {
echo "Update wlan {$key} id {$wlan_id[$key]} for {$filnr}, id {$siteid}\n";
$updatewlan[$key] = $unifi_connection->set_wlansettings_base($wlan_id[$key], $wlannetworks[$key]);
}
if($updatewlan[$key] === false)
echo "Failed to update wlan {$key} for {$filnr}, id {$siteid} ". print_r($wlannetworks[$key], true) . print_r($wlan_id, true) ."\n";
}
if($debug===true)
break;
}
$logout = $unifi_connection->logout();
function refresh_networks() {
global $unifi_connection;
global $networkconf;
global $wired;
global $wired_id;
global $shasite_id;
global $wirednetworks;
// Fetch configured wired networks
$networkconf = $unifi_connection->list_networkconf();
foreach($wirednetworks as $key => $values) {
$wired[$key] = false;
}
// Lan netwerken
foreach($networkconf as $network){
// Check if template networks exist
foreach($wirednetworks as $key => $values) {
if(($network->name == "$key")) {
$wired[$key] = true;
$wired_id[$key] = $network->_id;
$shasite_id = $network->site_id;
}
}
}
}
function refresh_wlans() {
global $unifi_connection;
global $wlanconf;
global $wlan;
global $wlan_id;
global $shasite_id;
global $wlannetworks;
// Fetch Wireless networks
$wlanconf = $unifi_connection->list_wlanconf();
foreach($wlannetworks as $key => $values)
$wlan[$key] = false;
foreach($wlanconf as $network){
// Check if template networks exist
foreach($wlannetworks as $key => $values) {
if($network->name == "$key") {
$wlan[$key] = true;
$wlan_id[$key] = $network->_id;
$shasite_id = $network->site_id;
}
}
}
}
function fetch_site_conf() {
global $unifi_connection;
global $siteconf;
global $siteid;
global $setting;
global $setting_id;
// Fetch site settings
$siteconf = $unifi_connection->list_settings($siteid);
foreach($sitesettings as $key => $values)
$sitesetting[$key] = false;
$setting = array();
foreach($siteconf as $arr) {
$setting[$arr->key] = $arr;
$setting_id[$arr->key] = $arr->_id;
}
}
?>

View File

@ -0,0 +1,195 @@
<?php
// Extract shop IP addressing from numeric shop number, you could use something else, static or using a database lookup.
$octet1 = 10;
if(strlen($filnr) == 3) {
$octet2 = floatval(substr($filnr,0,1));
$octet3 = floatval(substr($filnr,1,2));
} else {
$octet2 = floatval(substr($filnr,0,2));
$octet3 = floatval(substr($filnr,2,2));
}
// Wired networks
$wirednetworks['LAN'] = array(
'dhcpd_enabled' => true,
'dhcpd_start' => "{$octet1}.{$octet2}.{$octet3}.100",
'dhcpd_stop' => "{$octet1}.{$octet2}.{$octet3}.150",
'domain_name' => 'foo.bar.nl',
'dhcpd_dns_1' => '10.56.154.13',
'dhcpd_dns_2' => '10.34.234.66',
'dhcpd_ip_1' => "{$octet1}.{$octet2}.{$octet3}.254",
'dhcpguard_enabled' => true,
'ip_subnet' => "{$octet1}.{$octet2}.{$octet3}.254/24",
'is_nat' => true,
'name' => 'LAN',
'networkgroup' => 'LAN',
'purpose' => 'corporate',
'site_id' => $shasite_id,
'vlan_enabled' => false,
);
$wirednetworks['shop-wifi'] = array(
'enabled' => true,
'is_nat' => true,
'dhcpd_ip_1' => '192.168.2.254',
'dhcpguard_enabled' => true,
'name' => 'shop-wifi',
'purpose' => 'vlan-only',
'site_id' => $shasite_id,
'vlan_enabled' => true,
'vlan' => 10,
);
// Wireless networks
$wlannetworks["UBNT-{$filnr}"] = array(
'enabled' => true,
'is_guest' => true,
'mac_filter_enabled' => false,
'mac_filter_list' => array (),
'mac_filter_policy' => 'allow',
'name' => "UBNT-{$filnr}",
'usergroup_id' => $shausergroup_id,
'wlangroup_id' => $shawlangroup_id,
'schedule' =>
array (
0 => 'mon|0800-1800',
1 => 'tue|0800-1800',
2 => 'wed|0800-1800',
3 => 'thu|0800-1800',
4 => 'fri|0800-1800',
5 => 'sat|0800-1800',
6 => 'sun|0800-1800',
),
'schedule_enabled' => true,
'security' => 'wpapsk',
'site_id' => $shasite_id,
'vlan' => '10',
'vlan_enabled' => true,
'wep_idx' => 1,
'wpa_enc' => 'ccmp',
'wpa_mode' => 'wpa2',
'x_passphrase' => 'datisgeheim',
);
$wlannetworks['CorporateWifi'] = array(
'enabled' => true,
'is_guest' => false,
'mac_filter_enabled' => false,
'mac_filter_list' => array (),
'mac_filter_policy' => 'allow',
'name' => "CorporateWifi",
'usergroup_id' => $shausergroup_id,
'wlangroup_id' => $shawlangroup_id,
'schedule' =>
array (
0 => 'mon|0800-1800',
1 => 'tue|0800-1800',
2 => 'wed|0800-1800',
3 => 'thu|0800-1800',
4 => 'fri|0800-1800',
5 => 'sat|0800-1800',
6 => 'sun|0800-1800',
),
'schedule_enabled' => true,
'security' => 'wpapsk',
'site_id' => $shasite_id,
'wep_idx' => 1,
'wpa_enc' => 'ccmp',
'wpa_mode' => 'wpa2',
'x_passphrase' => 'SuperSecretPassword',
);
// Unset this network for test shops
if(preg_match("/[0-9][9][0-9]+)/si", $filnr))
unset($wlannetworks['CorporateWifi']);
// Site settings template
$sitesettings['connectivity'] = array(
'enabled' => true,
'key' => 'connectivity',
'site_id' => $shasite_id,
'uplink_type' => 'gateway',
);
$sitesettings['guest_access'] = array(
'auth' => 'none',
'key' => 'guest_access',
'redirect_https' => true,
'redirect_to_https' => false,
'restricted_subnet_1' => '192.168.0.0/16',
'restricted_subnet_2' => '172.16.0.0/12',
'restricted_subnet_3' => '10.0.0.0/8',
'site_id' => $shasite_id,
);
$sitesettings['country'] = array(
'code' => '528',
'key' => 'country',
'site_id' => $shasite_id,
);
$sitesettings['locale'] = array(
'key' => 'locale',
'site_id' => $shasite_id,
'timezone' => 'Europe/Amsterdam',
);/*
$sitesettings['porta'] = array(
'key' => 'porta',
'site_id' => $shasite_id,
'ugw3_wan2_enabled' => false,
);*/
$sitesettings['snmp'] = array(
'community' => 'esenempee',
'key' => 'snmp',
'site_id' => $shasite_id,
);
$sitesettings['rsyslogd'] = array(
'key' => 'rsyslogd',
'port' => '514',
'site_id' => $shasite_id,
);/*
$sitesettings['auto_speedtest'] = array(
'enabled' => false,
'interval' => 20,
'key' => 'auto_speedtest',
'site_id' => $shasite_id,
);*/
$sitesettings['ntp'] = array(
'key' => 'ntp',
'ntp_server_1' => 'ntp.xs4all.nl',
'ntp_server_2' => '0.ubnt.pool.ntp.org',
'site_id' => $shasite_id,
);
/*
$sitesettings['usg'] = array(
'broadcast_ping' => false,
'ftp_module' => true,
'gre_module' => true,
'h323_module' => true,
'key' => 'usg',
'mdns_enabled' => false,
'mss_clamp' => 'auto',
'offload_accounting' => true,
'offload_l2_blocking' => true,
'offload_sch' => true,
'pptp_module' => true,
'receive_redirects' => false,
'send_redirects' => true,
'sip_module' => true,
'site_id' => $shasite_id,
'syn_cookies' => true,
'tftp_module' => true,
'upnp_enabled' => false,
'upnp_nat_pmp_enabled' => true,
'upnp_secure_mode' => true,
);*/
$sitesettings['mgmt'] = array(
'advanced_feature_enabled' => false,
'alert_enabled' => true,
'auto_upgrade' => true,
'key' => 'mgmt',
'led_enabled' => true,
'site_id' => $shasite_id,
'unifi_idp_enabled' => true,
'x_ssh_auth_password_enabled' => true,
'x_ssh_bind_wildcard' => false,
'x_ssh_enabled' => true,
);
?>

119
src/Client.php Executable file → Normal file
View File

@ -897,6 +897,125 @@ class Client
return $this->process_response_boolean($response);
}
/**
* Set site country
* -----------
* required parameter <settings> = stdClass object or associative array containing the configuration to apply to the network, must be a (partial)
* object structured in the same manner as is returned by list_networkconf() for the specific network type.
* Do not include the _id property, it will be assigned by the controller and returned upon success.
* return true on success
*/
public function set_site_country($country_id, $setting)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode($setting);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/setting/country/'.trim($country_id), 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Set site locale
* -----------
* required parameter <settings> = stdClass object or associative array containing the configuration to apply to the network, must be a (partial)
* object structured in the same manner as is returned by list_networkconf() for the specific network type.
* Do not include the _id property, it will be assigned by the controller and returned upon success.
* return true on success
*/
public function set_site_locale($locale_id, $setting)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode($setting);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/setting/locale/'.trim($locale_id), 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Set site snmp
* -----------
* required parameter <settings> = stdClass object or associative array containing the configuration to apply to the network, must be a (partial)
* object structured in the same manner as is returned by list_networkconf() for the specific network type.
* Do not include the _id property, it will be assigned by the controller and returned upon success.
* return true on success
*/
public function set_site_snmp($snmp_id, $setting)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode($setting);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/setting/snmp/'.trim($snmp_id), 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Set site mgmt
* -----------
* required parameter <settings> = stdClass object or associative array containing the configuration to apply to the network, must be a (partial)
* object structured in the same manner as is returned by list_networkconf() for the specific network type.
* Do not include the _id property, it will be assigned by the controller and returned upon success.
* return true on success
*/
public function set_site_mgmt($mgmt_id, $setting)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode($setting);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/setting/mgmt/'.trim($mgmt_id), 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Set site guest access
* -----------
* required parameter <settings> = stdClass object or associative array containing the configuration to apply to the network, must be a (partial)
* object structured in the same manner as is returned by list_networkconf() for the specific network type.
* Do not include the _id property, it will be assigned by the controller and returned upon success.
* return true on success
*/
public function set_site_guest_access($guest_access_id, $setting)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode($setting);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/setting/guest_access/'.trim($guest_access_id), 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Set site ntp
* -----------
* required parameter <settings> = stdClass object or associative array containing the configuration to apply to the network, must be a (partial)
* object structured in the same manner as is returned by list_networkconf() for the specific network type.
* Do not include the _id property, it will be assigned by the controller and returned upon success.
* return true on success
*/
public function set_site_ntp($ntp_id, $setting)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode($setting);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/setting/ntp/'.trim($ntp_id), 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Set site connectivity
* -----------
* required parameter <settings> = stdClass object or associative array containing the configuration to apply to the network, must be a (partial)
* object structured in the same manner as is returned by list_networkconf() for the specific network type.
* Do not include the _id property, it will be assigned by the controller and returned upon success.
* return true on success
*/
public function set_site_connectivity($connectivity_id, $setting)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode($setting);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/setting/connectivity/'.trim($connectivity_id), 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* List admins
* -----------