All pastes #1291735 Raw Edit

SoftLayer DNS

public php v1 · immutable
#1291735 ·published 2008-12-22 21:18 UTC
rendered paste body
<?phpclass dns_softlayer {    protected $_config;        function __construct($config)    {        $this->_config = $config;    }        public function index()    {        $serverList = $this->_xmlrpcCall('SoftLayer_Account', 'getHardware', array());         // Now find out server CPU temperatures. Add temps to the serverList array.        foreach ($serverList as $key => $server)         {            $sensorData = $this->_xmlrpcCall('SoftLayer_Hardware_Server', 'getSensorData', array('id' => $server['id']));                     // Different IPMI cards return different sensor data. Find out which sensor            // to read.            $cpuKeys = array('CPU', 'CPU Temperature', 'CPU Temperature 1');            foreach ($sensorData as $sensor)            {                if (in_array($sensor['sensorId'], $cpuKeys))                {                	$serverList[$key]['cpuTemperature'] = $sensor['sensorReading'].' '.$sensor['sensorUnits'];                }            }        }                return $serverList;    }        public function showDomains()    {        return $this->_xmlrpcCall('SoftLayer_Account', 'getDomains', array(), array());    }		public function addDomain($params)	{				$domain = new stdClass(); // Init the class object				if (!isset($params['domain']))		{			return 'Missing domain parameter';		}				$domain->name = $params['domain']; // Add the domain name				if (!isset($params['a']))		{			return 'You need at least one a record';		}				$i = 0; // Object resource record key		foreach ($params as $name => $value)		{			switch ($name)			{				case 'a': // Ads a type DNS record					$domain->resourceRecords[$i]->type = 'a';					$domain->resourceRecords[$i]->data = $value;					$i++;				break;				case 'cname': // Ads cname type DNS record					$domain->resourceRecords[$i]->type = 'cname';					$domain->resourceRecords[$i]->data = $value;					$i++;				break;				case 'mx': // Ads mx type DNS record					$domain->resourceRecords[$i]->type = 'mx';					$domain->resourceRecords[$i]->data = $value;					$i++;				break;				case 'ns': // Ads ns type DNS record					$domain->resourceRecords[$i]->type = 'ns';					$domain->resourceRecords[$i]->data = $value;					$i++;				break;				default:					continue;				break;			}		}        $dns = $this->_xmlrpcCall('SoftLayer_Dns_Domain', 'createObject', array(), array($domain));     	}        protected function _xmlrpcCall($serviceName, $methodName, $initParameters = array(), $methodParameters = array())     {        // Build the XML-RPC request array. Everything goes into a "headers" array,        // with authentication and service initialization parameters in sub-arrays.        $headers = array();        $headers['authenticate'] = array();        $headers['authenticate']['username'] = $this->_config['API_USER'];        $headers['authenticate']['apiKey'] = $this->_config['API_KEY'];        $headers[$serviceName.'InitParameters'] = $initParameters;             $request = array();        $request[0] = array('headers' => $headers);             $request = array_merge($request, $methodParameters);             $encodedRequest = xmlrpc_encode_request($methodName, $request);             // Making the XML-RPC call interpreting the response is taken from the PHP         // manual:        // http://www.php.net/manual/en/function.xmlrpc-encode-request.php        $context = stream_context_create(array(            'http' => array(                'method' => "POST",                'header' => "Content-Type: text/xml",                'content' => $encodedRequest            )));             $file = file_get_contents('http://api.service.softlayer.com/xmlrpc/v3/'.$serviceName, false, $context);        $response = xmlrpc_decode($file);             if (xmlrpc_is_fault($response))         {            trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");			        }        else        {            return $response;        }    }}