Service.php
5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* PHP OpenCloud library.
*
* @copyright 2014 Rackspace Hosting, Inc. See LICENSE for information.
* @license https://www.apache.org/licenses/LICENSE-2.0
* @author Glen Campbell <glen.campbell@rackspace.com>
* @author Jamie Hannaford <jamie.hannaford@rackspace.com>
*/
namespace OpenCloud\DNS;
use OpenCloud\Common\Service\CatalogService;
use OpenCloud\OpenStack;
use OpenCloud\Compute\Resource\Server;
use OpenCloud\Common\Http\Message\Formatter;
use OpenCloud\DNS\Collection\DnsIterator;
/**
* DNS Service.
*/
class Service extends CatalogService
{
const DEFAULT_TYPE = 'rax:dns';
const DEFAULT_NAME = 'cloudDNS';
protected $regionless = true;
public function collection($class, $url = null, $parent = null, $data = null)
{
$options = $this->makeResourceIteratorOptions($this->resolveResourceClass($class));
$options['baseUrl'] = $url;
$parent = $parent ?: $this;
return DnsIterator::factory($parent, $options, $data);
}
/**
* returns a DNS::Domain object
*
* @api
* @param mixed $info either the ID, an object, or array of parameters
* @return Resource\Domain
*/
public function domain($info = null)
{
return new Resource\Domain($this, $info);
}
/**
* returns a Collection of DNS::Domain objects
*
* @api
* @param array $filter key/value pairs to use as query strings
* @return \OpenCloud\Common\Collection
*/
public function domainList($filter = array())
{
$url = $this->getUrl(Resource\Domain::resourceName(), $filter);
return $this->collection('OpenCloud\DNS\Resource\Domain', $url);
}
/**
* returns a PtrRecord object for a server
*
* @param mixed $info ID, array, or object containing record data
* @return Resource\Record
*/
public function ptrRecord($info = null)
{
return new Resource\PtrRecord($this, $info);
}
/**
* returns a Collection of PTR records for a given Server
*
* @param \OpenCloud\Compute\Resource\Server $server the server for which to
* retrieve the PTR records
* @return \OpenCloud\Common\Collection
*/
public function ptrRecordList(Server $server)
{
$url = $this->getUrl()
->addPath('rdns')
->addPath($server->getService()->name())
->setQuery(array('href' => $server->url()));
return $this->collection('OpenCloud\DNS\Resource\PtrRecord', $url);
}
/**
* retrieves an asynchronous response
*
* This method calls the provided `$url` and expects an asynchronous
* response. It checks for various HTTP error codes and returns
* an `AsyncResponse` object. This object can then be used to poll
* for the status or to retrieve the final data as needed.
*
* @param string $url the URL of the request
* @param string $method the HTTP method to use
* @param array $headers key/value pairs for headers to include
* @param string $body the body of the request (for PUT and POST)
* @return Resource\AsyncResponse
*/
public function asyncRequest($url, $method = 'GET', $headers = array(), $body = null)
{
$response = $this->getClient()->createRequest($method, $url, $headers, $body)->send();
return new Resource\AsyncResponse($this, Formatter::decode($response));
}
/**
* imports domain records
*
* Note that this function is called from the service (DNS) level, and
* not (as you might suspect) from the Domain object. Because the function
* return an AsyncResponse, the domain object will not actually exist
* until some point after the import has occurred.
*
* @api
* @param string $data the BIND_9 formatted data to import
* @return Resource\AsyncResponse
*/
public function import($data)
{
// determine the URL
$url = $this->url('domains/import');
$object = (object) array(
'domains' => array(
(object) array(
'contents' => $data,
'contentType' => 'BIND_9'
)
)
);
// encode it
$json = json_encode($object);
// debug it
$this->getLogger()->info('Importing [{json}]', array('json' => $json));
// perform the request
return $this->asyncRequest($url, 'POST', array(), $json);
}
/**
* returns a list of limits
*/
public function limits($type = null)
{
$url = $this->url('limits') . ($type ? "/$type" : '');
$response = $this->getClient()->get($url)->send();
$body = Formatter::decode($response);
return ($body) ? $body : $body->limits;
}
/**
* returns an array of limit types
*
* @return array
*/
public function limitTypes()
{
$response = $this->getClient()->get($this->getUrl('limits/types'))->send();
$body = Formatter::decode($response);
return $body->limitTypes;
}
}