3
* PHP OpenCloud library.
5
* @copyright Copyright 2013 Rackspace US, Inc. See COPYING for licensing information.
6
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache 2.0
8
* @author Glen Campbell <glen.campbell@rackspace.com>
9
* @author Jamie Hannaford <jamie.hannaford@rackspace.com>
3
* Copyright 2012-2014 Rackspace US, Inc.
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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
12
18
namespace OpenCloud\DNS;
14
use OpenCloud\Common\Service as AbstractService;
15
use OpenCloud\Common\Lang;
16
use OpenCloud\Common\Exceptions;
17
use OpenCloud\OpenStack;
18
use OpenCloud\Compute\Server;
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;
23
class Service extends AbstractService
29
class Service extends CatalogService
27
* creates a new DNS object
29
* @param \OpenCloud\OpenStack $conn connection object
30
* @param string $serviceName the name of the service
31
* @param string $serviceRegion (not currently used; DNS is regionless)
32
* @param string $urltype the type of URL
34
public function __construct(
35
OpenStack $connection,
41
$this->getLogger()->info('Initializing DNS...');
31
const DEFAULT_TYPE = 'rax:dns';
32
const DEFAULT_NAME = 'cloudDNS';
34
protected $regionless = true;
36
public function collection($class, $url = null, $parent = null, $data = null)
38
$options = $this->makeResourceIteratorOptions($this->resolveResourceClass($class));
39
$options['baseUrl'] = $url;
41
$parent = $parent ? : $this;
43
return DnsIterator::factory($parent, $options, $data);
53
* returns a DNS::Domain object
56
49
* @param mixed $info either the ID, an object, or array of parameters
50
* @return Resource\Domain
59
52
public function domain($info = null)
61
return new Domain($this, $info);
54
return $this->resource('Domain', $info);
65
* returns a Collection of DNS::Domain objects
58
* Returns a collection of domains
68
60
* @param array $filter key/value pairs to use as query strings
69
* @return \OpenCloud\Collection
61
* @return \OpenCloud\Common\Collection
71
63
public function domainList($filter = array())
73
$url = $this->url(Domain::ResourceName(), $filter);
74
return $this->collection('OpenCloud\DNS\Domain', $url);
65
$url = $this->getUrl(Resource\Domain::resourceName());
66
$url->setQuery($filter);
68
return $this->resourceList('Domain', $url);
78
72
* returns a PtrRecord object for a server
80
74
* @param mixed $info ID, array, or object containing record data
75
* @return Resource\Record
83
77
public function ptrRecord($info = null)
85
return new PtrRecord($this, $info);
79
return $this->resource('PtrRecord', $info);
89
83
* returns a Collection of PTR records for a given Server
91
* @param \OpenCloud\Compute\Server $server the server for which to
92
* retrieve the PTR records
85
* @param \OpenCloud\Compute\Resource\Server $server the server for which to
86
* retrieve the PTR records
87
* @return \OpenCloud\Common\Collection
95
public function ptrRecordList(Server $server)
89
public function ptrRecordList(HasPtrRecordsInterface $parent)
97
$url = $this->url('rdns/' . $server->getService()->name(), array(
98
'href' => $server->url()
100
return $this->collection('\OpenCloud\DNS\PtrRecord', $url);
91
$url = $this->getUrl()
93
->addPath($parent->getService()->getName())
94
->setQuery(array('href' => (string) $parent->getUrl()));
104
* performs a HTTP request
106
* This method overrides the request with JSON content type
108
* @param string $url the URL to target
109
* @param string $method the HTTP method to use
110
* @param array $headers key/value pairs for headers to include
111
* @param string $body the body of the request (for PUT and POST)
112
* @return \OpenCloud\HttpResponse
114
public function request(
117
array $headers = array(),
120
$headers['Accept'] = RAXSDK_CONTENT_TYPE_JSON;
121
$headers['Content-Type'] = RAXSDK_CONTENT_TYPE_JSON;
122
return parent::request($url, $method, $headers, $body);
96
return $this->resourceList('PtrRecord', $url);
130
104
* an `AsyncResponse` object. This object can then be used to poll
131
105
* for the status or to retrieve the final data as needed.
133
* @param string $url the URL of the request
134
* @param string $method the HTTP method to use
135
* @param array $headers key/value pairs for headers to include
136
* @param string $body the body of the request (for PUT and POST)
137
* @return DNS\AsyncResponse
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
139
113
public function asyncRequest($url, $method = 'GET', $headers = array(), $body = null)
141
// perform the initial request
142
$resp = $this->request($url, $method, $headers, $body);
144
// @codeCoverageIgnoreStart
145
if ($resp->HttpStatus() > 204) {
146
throw new Exceptions\AsyncHttpError(sprintf(
147
Lang::translate('Unexpected HTTP status for async request: URL [%s] method [%s] status [%s] response [%s]'),
154
// @codeCoverageIgnoreEnd
157
$this->getLogger()->info('AsyncResponse [{body}]', array(
158
'body' => $resp->httpBody()
161
// return an AsyncResponse object
162
return new AsyncResponse($this, $resp->httpBody());
115
$response = $this->getClient()->createRequest($method, $url, $headers, $body)->send();
117
return new Resource\AsyncResponse($this, Formatter::decode($response));
166
* imports domain records
121
* Imports domain records
168
123
* Note that this function is called from the service (DNS) level, and
169
124
* not (as you might suspect) from the Domain object. Because the function
170
125
* return an AsyncResponse, the domain object will not actually exist
171
126
* until some point after the import has occurred.
174
128
* @param string $data the BIND_9 formatted data to import
175
* @return DNS\AsyncResponse
129
* @return Resource\AsyncResponse
177
131
public function import($data)
180
$url = $this->url('domains/import');
133
$url = clone $this->getUrl();
134
$url->addPath('domains');
135
$url->addPath('import');
182
137
$object = (object) array(
183
138
'domains' => array(
217
175
public function limitTypes()
219
$object = $this->simpleRequest($this->url('limits/types'));
220
return $object->limitTypes;
224
* Performs a simple request and returns the JSON as an object
226
* @param string $url the URL to GET
228
public function simpleRequest($url)
230
// Perform the request
231
$response = $this->request($url);
234
// @codeCoverageIgnoreStart
235
if ($response->HttpStatus() > 202) {
236
throw new Exceptions\HttpError(sprintf(
237
Lang::translate('Unexpected status [%s] for URL [%s], body [%s]'),
238
$response->HttpStatus(),
240
$response->HttpBody()
243
// @codeCoverageIgnoreEnd
246
$json = $response->httpBody();
247
$this->getLogger()->info('Limit Types JSON [{json}]', array('json' => $json));
249
$object = json_decode($json);
251
$this->checkJsonError();
177
$response = $this->getClient()->get($this->getUrl('limits/types'))->send();
178
$body = Formatter::decode($response);
180
return $body->limitTypes;