      1: <?php
      2: /**
      3:  * Copyright 2012-2014 Rackspace US, Inc.
      4:  *
      5:  * Licensed under the Apache License, Version 2.0 (the "License");
      6:  * you may not use this file except in compliance with the License.
      7:  * You may obtain a copy of the License at
      8:  *
      9:  * http://www.apache.org/licenses/LICENSE-2.0
     10:  *
     11:  * Unless required by applicable law or agreed to in writing, software
     12:  * distributed under the License is distributed on an "AS IS" BASIS,
     13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14:  * See the License for the specific language governing permissions and
     15:  * limitations under the License.
     16:  */
     17:
     18: namespace OpenCloud\DNS;
     19:
     20: use OpenCloud\Common\Http\Message\Formatter;
     21: use OpenCloud\Common\Service\CatalogService;
     22: use OpenCloud\Compute\Resource\Server;
     23: use OpenCloud\DNS\Collection\DnsIterator;
     24: use OpenCloud\DNS\Resource\HasPtrRecordsInterface;
     25:
     26: /**
     27:  * DNS Service.
     28:  */
     29: class Service extends CatalogService
     30: {
     31:     const DEFAULT_TYPE = 'rax:dns';
     32:     const DEFAULT_NAME = 'cloudDNS';
     33:
     34:     protected $regionless = true;
     35:
     36:     public function collection($class, $url = null, $parent = null, $data = null)
     37:     {
     38:         $options = $this->makeResourceIteratorOptions($this->resolveResourceClass($class));
     39:         $options['baseUrl'] = $url;
     40:
     41:         $parent = $parent ? : $this;
     42:
     43:         return DnsIterator::factory($parent, $options, $data);
     44:     }
     45:
     46:     /**
     47:      * Returns a domain
     48:      *
     49:      * @param mixed $info either the ID, an object, or array of parameters
     50:      * @return Resource\Domain
     51:      */
     52:     public function domain($info = null)
     53:     {
     54:         return $this->resource('Domain', $info);
     55:     }
     56:
     57:     /**
     58:      * Returns a collection of domains
     59:      *
     60:      * @param array $filter key/value pairs to use as query strings
     61:      * @return \OpenCloud\Common\Collection
     62:      */
     63:     public function domainList($filter = array())
     64:     {
     65:         $url = $this->getUrl(Resource\Domain::resourceName());
     66:         $url->setQuery($filter);
     67:
     68:         return $this->resourceList('Domain', $url);
     69:     }
     70:
     71:     /**
     72:      * returns a PtrRecord object for a server
     73:      *
     74:      * @param mixed $info ID, array, or object containing record data
     75:      * @return Resource\Record
     76:      */
     77:     public function ptrRecord($info = null)
     78:     {
     79:         return $this->resource('PtrRecord', $info);
     80:     }
     81:
     82:     /**
     83:      * returns a Collection of PTR records for a given Server
     84:      *
     85:      * @param \OpenCloud\Compute\Resource\Server $server the server for which to
     86:      *                                                   retrieve the PTR records
     87:      * @return \OpenCloud\Common\Collection
     88:      */
     89:     public function ptrRecordList(HasPtrRecordsInterface $parent)
     90:     {
     91:         $url = $this->getUrl()
     92:             ->addPath('rdns')
     93:             ->addPath($parent->getService()->getName())
     94:             ->setQuery(array('href' => (string) $parent->getUrl()));
     95:
     96:         return $this->resourceList('PtrRecord', $url);
     97:     }
     98:
     99:     /**
    100:      * retrieves an asynchronous response
    101:      *
    102:      * This method calls the provided `$url` and expects an asynchronous
    103:      * response. It checks for various HTTP error codes and returns
    104:      * an `AsyncResponse` object. This object can then be used to poll
    105:      * for the status or to retrieve the final data as needed.
    106:      *
    107:      * @param string $url     the URL of the request
    108:      * @param string $method  the HTTP method to use
    109:      * @param array  $headers key/value pairs for headers to include
    110:      * @param string $body    the body of the request (for PUT and POST)
    111:      * @return Resource\AsyncResponse
    112:      */
    113:     public function asyncRequest($url, $method = 'GET', $headers = array(), $body = null)
    114:     {
    115:         $response = $this->getClient()->createRequest($method, $url, $headers, $body)->send();
    116:
    117:         return new Resource\AsyncResponse($this, Formatter::decode($response));
    118:     }
    119:
    120:     /**
    121:      * Imports domain records
    122:      *
    123:      * Note that this function is called from the service (DNS) level, and
    124:      * not (as you might suspect) from the Domain object. Because the function
    125:      * return an AsyncResponse, the domain object will not actually exist
    126:      * until some point after the import has occurred.
    127:      *
    128:      * @param string $data the BIND_9 formatted data to import
    129:      * @return Resource\AsyncResponse
    130:      */
    131:     public function import($data)
    132:     {
    133:         $url = clone $this->getUrl();
    134:         $url->addPath('domains');
    135:         $url->addPath('import');
    136:
    137:         $object = (object) array(
    138:             'domains' => array(
    139:                 (object) array(
    140:                     'contents'    => $data,
    141:                     'contentType' => 'BIND_9'
    142:                 )
    143:             )
    144:         );
    145:
    146:         // encode it
    147:         $json = json_encode($object);
    148:
    149:         // perform the request
    150:         return $this->asyncRequest($url, 'POST', self::getJsonHeader(), $json);
    151:     }
    152:
    153:     /**
    154:      * returns a list of limits
    155:      */
    156:     public function limits($type = null)
    157:     {
    158:         $url = $this->getUrl('limits');
    159:
    160:         if ($type) {
    161:             $url->addPath($type);
    162:         }
    163:
    164:         $response = $this->getClient()->get($url)->send();
    165:         $body = Formatter::decode($response);
    166:
    167:         return isset($body->limits) ? $body->limits : $body;
    168:     }
    169:
    170:     /**
    171:      * returns an array of limit types
    172:      *
    173:      * @return array
    174:      */
    175:     public function limitTypes()
    176:     {
    177:         $response = $this->getClient()->get($this->getUrl('limits/types'))->send();
    178:         $body = Formatter::decode($response);
    179:
    180:         return $body->limitTypes;
    181:     }
    182: }
    183: