Overview

Namespaces

  • OpenCloud
    • Autoscale
      • Resource
    • CDN
      • Resource
    • CloudMonitoring
      • Collection
      • Exception
      • Resource
    • Common
      • Collection
      • Constants
      • Exceptions
      • Http
        • Message
      • Log
      • Resource
      • Service
    • Compute
      • Constants
      • Exception
      • Resource
    • Database
      • Resource
    • DNS
      • Collection
      • Resource
    • Identity
      • Constants
      • Resource
    • Image
      • Enum
      • Resource
        • JsonPatch
        • Schema
    • LoadBalancer
      • Collection
      • Enum
      • Resource
    • Networking
      • Resource
    • ObjectStore
      • Constants
      • Enum
      • Exception
      • Resource
      • Upload
    • Orchestration
      • Resource
    • Queues
      • Collection
      • Exception
      • Resource
    • Volume
      • Resource
  • PHP

Classes

  • Service
  • Overview
  • Namespace
  • Class
  • Tree
  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\Common\Exceptions\DomainNotFoundException;
 23: use OpenCloud\DNS\Collection\DnsIterator;
 24: use OpenCloud\DNS\Resource\AsyncResponse;
 25: use OpenCloud\DNS\Resource\Domain;
 26: use OpenCloud\DNS\Resource\HasPtrRecordsInterface;
 27: 
 28: /**
 29:  * DNS Service.
 30:  */
 31: class Service extends CatalogService
 32: {
 33:     const DEFAULT_TYPE = 'rax:dns';
 34:     const DEFAULT_NAME = 'cloudDNS';
 35: 
 36:     protected $regionless = true;
 37: 
 38:     public function collection($class, $url = null, $parent = null, $data = null)
 39:     {
 40:         $options = $this->makeResourceIteratorOptions($this->resolveResourceClass($class));
 41:         $options['baseUrl'] = $url;
 42: 
 43:         $parent = $parent ? : $this;
 44: 
 45:         return DnsIterator::factory($parent, $options, $data);
 46:     }
 47: 
 48:     /**
 49:      * Returns a domain
 50:      *
 51:      * @param mixed $info either the ID, an object, or array of parameters
 52:      * @return Resource\Domain
 53:      */
 54:     public function domain($info = null)
 55:     {
 56:         return $this->resource('Domain', $info);
 57:     }
 58: 
 59:     /**
 60:      * Returns a domain, given a domain name
 61:      *
 62:      * @param string $domainName Domain name
 63:      * @return Domain the domain object
 64:      */
 65:     public function domainByName($domainName)
 66:     {
 67:         $domainList = $this->domainList(array("name" => $domainName));
 68: 
 69:         if (count($domainList) != 1) {
 70:             throw new DomainNotFoundException();
 71:         }
 72: 
 73:         return $this->resource('Domain', $domainList[0]);
 74:     }
 75: 
 76: 
 77:     /**
 78:      * Returns a collection of domains
 79:      *
 80:      * @param array $filter key/value pairs to use as query strings
 81:      * @return OpenCloud\DNS\Collection\DnsIterator
 82:      */
 83:     public function domainList($filter = array())
 84:     {
 85:         $url = $this->getUrl(Domain::resourceName());
 86:         $url->setQuery($filter);
 87: 
 88:         return $this->resourceList('Domain', $url);
 89:     }
 90: 
 91:     /**
 92:      * returns a PtrRecord object for a server
 93:      *
 94:      * @param mixed $info ID, array, or object containing record data
 95:      * @return Resource\Record
 96:      */
 97:     public function ptrRecord($info = null)
 98:     {
 99:         return $this->resource('PtrRecord', $info);
100:     }
101: 
102:     /**
103:      * returns a Collection of PTR records for a given Server
104:      *
105:      * @param \OpenCloud\Compute\Resource\Server $server the server for which to
106:      *                                                   retrieve the PTR records
107:      * @return OpenCloud\DNS\Collection\DnsIterator
108:      */
109:     public function ptrRecordList(HasPtrRecordsInterface $parent)
110:     {
111:         $url = $this->getUrl()
112:             ->addPath('rdns')
113:             ->addPath($parent->getService()->getName())
114:             ->setQuery(array('href' => (string) $parent->getUrl()));
115: 
116:         return $this->resourceList('PtrRecord', $url);
117:     }
118: 
119:     /**
120:      * retrieves an asynchronous response
121:      *
122:      * This method calls the provided `$url` and expects an asynchronous
123:      * response. It checks for various HTTP error codes and returns
124:      * an `AsyncResponse` object. This object can then be used to poll
125:      * for the status or to retrieve the final data as needed.
126:      *
127:      * @param string $url     the URL of the request
128:      * @param string $method  the HTTP method to use
129:      * @param array  $headers key/value pairs for headers to include
130:      * @param string $body    the body of the request (for PUT and POST)
131:      * @return Resource\AsyncResponse
132:      */
133:     public function asyncRequest($url, $method = 'GET', $headers = array(), $body = null)
134:     {
135:         $response = $this->getClient()->createRequest($method, $url, $headers, $body)->send();
136: 
137:         return new AsyncResponse($this, Formatter::decode($response));
138:     }
139: 
140:     /**
141:      * Imports domain records
142:      *
143:      * Note that this function is called from the service (DNS) level, and
144:      * not (as you might suspect) from the Domain object. Because the function
145:      * return an AsyncResponse, the domain object will not actually exist
146:      * until some point after the import has occurred.
147:      *
148:      * @param string $data the BIND_9 formatted data to import
149:      * @return Resource\AsyncResponse
150:      */
151:     public function import($data)
152:     {
153:         $url = clone $this->getUrl();
154:         $url->addPath('domains');
155:         $url->addPath('import');
156: 
157:         $object = (object) array(
158:             'domains' => array(
159:                 (object) array(
160:                     'contents'    => $data,
161:                     'contentType' => 'BIND_9'
162:                 )
163:             )
164:         );
165: 
166:         // encode it
167:         $json = json_encode($object);
168: 
169:         // perform the request
170:         return $this->asyncRequest($url, 'POST', self::getJsonHeader(), $json);
171:     }
172: 
173:     /**
174:      * returns a list of limits
175:      */
176:     public function limits($type = null)
177:     {
178:         $url = $this->getUrl('limits');
179: 
180:         if ($type) {
181:             $url->addPath($type);
182:         }
183: 
184:         $response = $this->getClient()->get($url)->send();
185:         $body = Formatter::decode($response);
186: 
187:         return isset($body->limits) ? $body->limits : $body;
188:     }
189: 
190:     /**
191:      * returns an array of limit types
192:      *
193:      * @return array
194:      */
195:     public function limitTypes()
196:     {
197:         $response = $this->getClient()->get($this->getUrl('limits/types'))->send();
198:         $body = Formatter::decode($response);
199: 
200:         return $body->limitTypes;
201:     }
202: 
203:     /**
204:      * List asynchronous responses' statuses.
205:      * @see http://docs.rackspace.com/cdns/api/v1.0/cdns-devguide/content/viewing_status_all_asynch_jobs.html
206:      *
207:      * @param array $query Any query parameters. Optional.
208:      * @return OpenCloud\DNS\Collection\DnsIterator
209:      */
210:     public function listAsyncJobs(array $query = array())
211:     {
212:         $url = clone $this->getUrl();
213:         $url->addPath('status');
214:         $url->setQuery($query);
215: 
216:         return DnsIterator::factory($this, array(
217:             'baseUrl'        => $url,
218:             'resourceClass'  => 'AsyncResponse',
219:             'key.collection' => 'asyncResponses'
220:         ));
221:     }
222: 
223:     public function getAsyncJob($jobId, $showDetails = true)
224:     {
225:         $url = clone $this->getUrl();
226:         $url->addPath('status');
227:         $url->addPath((string) $jobId);
228:         $url->setQuery(array('showDetails' => ($showDetails) ? 'true' : 'false'));
229: 
230:         $response = $this->getClient()->get($url)->send();
231: 
232:         return new AsyncResponse($this, Formatter::decode($response));
233:     }
234: }
235: 
API documentation generated by ApiGen 2.8.0