/** * Authenticate a user * * Authentication is handled by SoftLayer's authentication system. Passwords * are not stored locally. * * @param string $username * @param string $password * @throws Exception * @return bool */ public static function authenticate($username, $password) { /* * Make sure the user exists locally first. */ $user = Model_DbTable_User::findByUsername($username); if ($user == null) { throw new Exception('Invalid login credentials provided.'); } /* * Attempt to authenticate to the SoftLayer API. API docs at * http://sldn.softlayer.com/wiki/index.php/SoftLayer_User_Customer::getPortalLoginToken */ $client = SoftLayer_SoapClient::getClient('SoftLayer_User_Customer'); try { $result = $client->getPortalLoginToken($username, $password); } catch (Exception $e) { throw new Exception($e->getMessage()); } return true; }
public function indexAction() { $client = SoftLayer_SoapClient::getClient('SoftLayer_Account'); $objectMask = new SoftLayer_ObjectMask(); $objectMask->hardwareCount; $objectMask->virtualGuestCount; /* * Bandwidth usage */ $objectMask->hardware->billingCyclePublicBandwidthUsage; $objectMask->hardware->billingItem->bandwidthAllocation; $objectMask->virtualGuests->billingCyclePublicBandwidthUsage; $objectMask->virtualGuests->billingItem->bandwidthAllocation; /* * Monitoring status */ $objectMask->networkMonitorUpHardwareCount; $objectMask->networkMonitorRecoveringHardwareCount; $objectMask->networkMonitorDownHardwareCount; $objectMask->networkMonitorUpVirtualGuestCount; $objectMask->networkMonitorRecoveringVirtualGuestCount; $objectMask->networkMonitorDownVirtualGuestCount; $client->setObjectMask($objectMask); try { $account = $client->getObject(); } catch (Exception $e) { $this->view->errorMessage = 'Unable to retrieve account information. ' . $e->getMessage(); } /* * Calculate servers and CCIs over 85% and 100% bandwidth. */ $serversOver85PercentBandwidth = 0; $serversOver100PercentBandwidth = 0; $instancesOver85PercentBandwidth = 0; $instancesOver100PercentBandwidth = 0; foreach ($account->hardware as $server) { if (isset($server->billingCyclePublicBandwidthUsage) && isset($server->billingItem) && isset($server->billingItem->bandwidthAllocation) && $server->billingItem->bandwidthAllocation->amount > 0) { $usagePercent = $server->billingCyclePublicBandwidthUsage->amountOut / $server->billingItem->bandwidthAllocation->amount; } else { $usagePercent = 0; } if ($usagePercent >= 0.85) { $serversOver85PercentBandwidth++; } elseif ($usagePercent >= 1) { $serversOver100PercentBandwidth++; } } foreach ($account->virtualGuests as $server) { if (isset($server->billingCyclePublicBandwidthUsage) && isset($server->billingItem) && isset($server->billingItem->bandwidthAllocation) && $server->billingItem->bandwidthAllocation->amount > 0) { $usagePercent = $server->billingCyclePublicBandwidthUsage->amountOut / $server->billingItem->bandwidthAllocation->amount; } else { $usagePercent = 0; } if ($usagePercent >= 0.85) { $instancesOver85PercentBandwidth++; } elseif ($usagePercent >= 1) { $instancesOver100PercentBandwidth++; } } $this->view->pageTitle = ucfirst($this->view->translate->_('Home')); $this->view->headTitle(ucfirst($this->view->translate->_('Home'))); $this->view->account = $account; $this->view->serversOver85PercentBandwidth = $serversOver85PercentBandwidth; $this->view->serversOver100PercentBandwidth = $serversOver100PercentBandwidth; $this->view->instancesOver85PercentBandwidth = $instancesOver85PercentBandwidth; $this->view->instancesOver100PercentBandwidth = $instancesOver100PercentBandwidth; }
/** * Make sure PHP is up to snuff, that we can write to the right places, and * make the first user. */ public function indexAction() { $phpCheck = array('PHP Version >= 5.2.3' => version_compare(PHP_VERSION, '5.2.3') >= 0, 'Standard Extension Loaded' => extension_loaded('standard'), 'SOAP Extension Loaded' => extension_loaded('soap'), 'PCRE Extension Loaded' => extension_loaded('pcre'), 'PDO Extension Loaded' => extension_loaded('pdo'), 'PDO SQLite Extension Loaded' => extension_loaded('pdo_sqlite'), 'SPL Extension Loaded' => extension_loaded('spl'), 'Session Extension Loaded' => extension_loaded('session'), 'Ctype Extension Loaded' => extension_loaded('ctype')); $systemCheck = array('Languages Directory (' . LANGUAGE_PATH . ') Writable' => is_writable(LANGUAGE_PATH), 'Skins Directory (' . SKIN_PATH . ') Writable' => is_writable(SKIN_PATH), 'Database Directory (' . APPLICATION_PATH . '/../data/db) Writable' => is_writable(APPLICATION_PATH . '/../data/db'), 'Configuration File (' . CONFIG_PATH . '/settings.ini' . ') Writable' => is_writable(CONFIG_PATH . '/settings.ini')); /* * Show an error if there are any PHP or system errors. */ $hasPhpErrors = false; $hasSystemErrors = false; foreach ($phpCheck as $check) { if (!$check) { $hasPhpErrors = true; break; } } foreach ($systemCheck as $check) { if (!$check) { $hasSystemErrors = true; break; } } if (!$hasPhpErrors && !$hasSystemErrors) { /* * Build the add user form. */ $config = Zend_Registry::get('config'); $skins = Model_Skin::getAllSkins(); $languages = Model_Language::getAllLanguages(); /* * Turn the skin and language lists into something more Zend_Form * friendly. */ foreach ($skins as $skin) { $skinList[$skin->name] = $skin->name; } foreach ($languages as $language) { $languageList[$language->name] = $language->name; } $form = new Zend_Form(); $form->setMethod('post'); $username = $form->createElement('text', 'username'); $username->setLabel(ucfirst($this->view->translate->_('username'))); $username->setRequired(true); $username->addValidator('alnum'); $apiKey = $form->createElement('text', 'apiKey'); $apiKey->setLabel(ucfirst($this->view->translate->_('API key'))); $apiKey->setRequired(true); $apiKey->addValidator('alnum'); $skin = $form->createElement('select', 'skin'); $skin->setLabel(ucfirst($this->view->translate->_('skin'))); $skin->addMultiOptions($skinList); $skin->setValue($config->defaults->skin); $skin->setRequired(true); $language = $form->createElement('select', 'language'); $language->setLabel(ucfirst($this->view->translate->_('language'))); $language->addMultiOptions($languageList); $language->setValue($config->defaults->language); $language->setRequired(true); $form->addElement($username); $form->addElement($apiKey); $form->addElement($skin); $form->addElement($language); $form->addElement('submit', 'submit', array('label' => $this->view->translate->_('Submit'))); /* * Process form submission. */ if ($this->getRequest()->isPost()) { $formData = $this->getRequest()->getPost(); if ($form->isValid($formData)) { /* * Try out the username and API key to make sure they * entered a good one. */ $account = null; $client = SoftLayer_SoapClient::getClient('SoftLayer_Account', null, $form->getValue('username'), $form->getValue('apiKey')); try { $account = $client->getObject(); } catch (Exception $e) { $this->view->errorMessage = $this->view->translate->_('Please enter a valid username and API key combination.'); } /* * Add the user. */ if ($account != null) { try { $user = Model_DbTable_User::addUser($form->getValue('username'), $form->getValue('apiKey'), $form->getValue('skin'), $form->getValue('language'), true); $this->_helper->_redirector->goToRouteAndExit(array('controller' => 'index', 'action' => null)); } catch (Exception $e) { $this->view->errorMessage = $this->view->translate->_('Unable to add user.') . ' ' . $e->getMessage(); } } } else { $this->view->errorMessage = $this->view->translate->_('Please completely fill out the configuration form.'); } $form->populate($formData); } $this->view->form = $form; } $this->view->pageTitle = 'Installation'; $this->view->headTitle('Installation'); $this->view->phpCheck = $phpCheck; $this->view->systemCheck = $systemCheck; $this->view->hasPhpErrors = $hasPhpErrors; $this->view->hasSystemErrors = $hasSystemErrors; }
* account and connectivity. */ try { print_r($client->getObject()); } catch (Exception $e) { die($e->getMessage()); } /** * For a more complex example we’ll retrieve a support ticket with id 123456 * along with the ticket’s updates, the user it’s assigned to, the servers * attached to it, and the datacenter those servers are in. We’ll retrieve our * extra information using a nested object mask. After we have the ticket we’ll * update it with the text ‘Hello!’. */ // Declare an API client to connect to the SoftLayer_Ticket API service. $client = SoftLayer_SoapClient::getClient('SoftLayer_Ticket', 123456, $apiUsername, $apiKey); // Assign an object mask to our API client: $objectMask = new SoftLayer_ObjectMask(); $objectMask->updates; $objectMask->assignedUser; $objectMask->attachedHardware->datacenter; $client->setObjectMask($objectMask); // Retrieve the ticket record. try { $ticket = $client->getObject(); print_r($ticket); } catch (Exception $e) { die('Unable to retrieve ticket record: ' . $e->getMessage()); } // Now update the ticket. $update = new stdClass();
/** * Reboot a server * * Allow user reboot via management card, power strip, or both. */ public function rebootAction() { $hardware = null; $rebootSuccessful = false; if ($this->_getParam('id') == null) { $this->view->errorMessage = $this->view->translate->_('Please provide a hardware id.'); } else { $client = SoftLayer_SoapClient::getClient('SoftLayer_Hardware_Server', $this->_getParam('id')); /* * Build the reboot form. */ $form = new Zend_Form(); $form->setMethod('post'); $rebootMethod = $form->createElement('select', 'rebootMethod'); $rebootMethod->setLabel(ucfirst($this->view->translate->_('reboot via'))); $rebootMethod->addMultiOptions(array('rebootDefault' => $this->view->translate->_('management card with powerstrip fallback'), 'rebootSoft' => $this->view->translate->_('management card only'), 'powerCycle' => $this->view->translate->_('power strip only'))); $rebootMethod->setRequired(true); $form->addElement($rebootMethod); $form->addElement('submit', 'reboot', array('label' => ucfirst($this->view->translate->_('reboot')))); /* * Get hardware info. */ $objectMask = new SoftLayer_ObjectMask(); $objectMask->recentRemoteManagementCommands; $client->setObjectMask($objectMask); try { $hardware = $client->getObject(); } catch (Exception $e) { $this->view->errorMessage = $this->translate->_('Error retrieving hardware record.') . ' ' . $e->getMessage(); } /* * Handle the reboot request. */ if ($this->getRequest()->isPost()) { $formData = $this->getRequest()->getPost(); if ($form->isValid($formData)) { try { switch ($form->getValue('rebootMethod')) { case 'rebootDefault': $result = $client->rebootDefault(); break; case 'rebootSoft': $result = $client->rebootSoft(); break; case 'powerCycle': $result = $client->powerCycle(); break; } $rebootSuccessful = true; } catch (Exception $e) { $this->view->errorMessage = $this->view->translate->_('Reboot failed.') . ' ' . $e->getMessage(); } } else { $this->view->errorMessage = $this->view->translate->_('Reboot failed.') . ' ' . $this->view->translate->_('Please completely fill out the reboot form.'); $form->populate($formData); } } } if ($hardware != null) { $this->view->pageTitle = ucfirst($this->view->translate->_('reboot')) . ' ' . $hardware->hostname . '.' . $hardware->domain; $this->view->headTitle(ucfirst($this->view->translate->_('reboot')) . ' ' . $hardware->hostname . '.' . $hardware->domain); $this->view->form = $form; } $this->view->rebootSuccessful = $rebootSuccessful; $this->view->hardware = $hardware; }
/** * Edit a local user. * * This does not affect the user's corresponding SoftLayer user account. */ public function edituserAction() { $user = null; /* * Get user info. */ try { $user = new Model_DbTable_User($this->_getParam('id')); } catch (Exception $e) { $this->view->errorMessage = $this->translate->_('Unable to locate user.') . ' ' . $e->getMessage(); } if ($user != null) { /* * Build the delete form. Only show it if the user isn't trying to * delete themselves. */ $deleteForm = null; if ($user->id != $this->view->currentUser->id) { $deleteForm = new Zend_Form(); $deleteForm->setMethod('post'); $hidden = $deleteForm->createElement('hidden', 'mode'); $hidden->setValue('delete'); $deleteForm->addElement($hidden); $deleteForm->addElement('submit', 'submit', array('label' => $this->view->translate->_('Submit'))); } /* * Build the edit form. */ $skins = Model_Skin::getAllSkins(); $languages = Model_Language::getAllLanguages(); /* * Turn the skin and language lists into something more Zend_Form * friendly. */ foreach ($skins as $skin) { $skinList[$skin->name] = $skin->name; } foreach ($languages as $language) { $languageList[$language->name] = $language->name; } $form = new Zend_Form(); $form->setMethod('post'); $username = $form->createElement('text', 'username'); $username->setLabel(ucfirst($this->view->translate->_('username'))); $username->setRequired(true); $username->addValidator('alnum'); $username->setValue($user->username); $apiKey = $form->createElement('text', 'apiKey'); $apiKey->setLabel(ucfirst($this->view->translate->_('API key'))); $apiKey->setRequired(true); $apiKey->addValidator('alnum'); $apiKey->setValue($user->apiKey); $skin = $form->createElement('select', 'skin'); $skin->setLabel(ucfirst($this->view->translate->_('skin'))); $skin->addMultiOptions($skinList); $skin->setValue($user->skin); $skin->setRequired(true); $language = $form->createElement('select', 'language'); $language->setLabel(ucfirst($this->view->translate->_('language'))); $language->addMultiOptions($languageList); $language->setValue($user->language); $language->setRequired(true); $isAdmin = $form->createElement('checkbox', 'isAdmin'); $isAdmin->setLabel(ucfirst($this->view->translate->_('administrator'))); $isAdmin->setChecked($user->isAdmin); $hidden = $form->createElement('hidden', 'mode'); $hidden->setValue('edit'); $form->addElement($username); $form->addElement($apiKey); $form->addElement($skin); $form->addElement($language); $form->addElement($isAdmin); $form->addElement($hidden); $form->addElement('submit', 'submit', array('label' => $this->view->translate->_('Submit'))); /* * Process form submission. */ if ($this->getRequest()->isPost()) { $formData = $this->getRequest()->getPost(); /* * Delete the user. */ if ($formData['mode'] == 'delete') { /* * Users may not delete themselves. */ if ($user->id == $this->view->currentUser->id) { $this->view->errorMessage = $this->view->translate->_('You may not delete your user account.'); } else { try { $user->deleteUser(); $this->_helper->_redirector->goToRouteAndExit(array('controller' => 'admin', 'action' => 'users', 'id' => null)); } catch (Exception $e) { $this->view->errorMessage = $this->view->translate->_('Unable to delete user.') . ' ' . $e->getMessage(); } } /* * Edit the user. */ } else { if ($form->isValid($formData)) { /* * Try out the username and API key to make sure they * entered a good one. */ $account = null; $client = SoftLayer_SoapClient::getClient('SoftLayer_Account', null, $form->getValue('username'), $form->getValue('apiKey')); try { $account = $client->getObject(); } catch (Exception $e) { $this->view->errorMessage = $this->view->translate->_('Please enter a valid username and API key combination.'); } /* * If the user is editing themself then make sure they * don't take away their own admin privileges. */ if ($user->id == $this->view->currentUser->id && $form->getValue('isAdmin') != $this->view->currentUser->isAdmin) { $account = null; $this->view->errorMessage = $this->view->translate->_('You may not change your administrative status.'); } /* * Update the user. */ if ($account != null) { try { $user->updateUser($form->getValue('username'), $form->getValue('apiKey'), $form->getValue('skin'), $form->getValue('language'), $form->getValue('isAdmin')); $this->view->statusMessage = $this->view->translate->_('User saved.'); } catch (Exception $e) { $this->view->errorMessage = $this->view->translate->_('Unable to save user.') . ' ' . $e->getMessage(); } } } else { $this->view->errorMessage = $this->view->translate->_('Please completely fill out the configuration form.'); } } $form->populate($formData); } $this->view->pageTitle = $this->view->translate->_('Edit') . ' ' . $user->username; $this->view->headTitle($this->view->translate->_('Edit') . ' ' . $user->username); $this->view->deleteForm = $deleteForm; $this->view->form = $form; } $this->view->user = $user; }