1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17
18 namespace OpenCloud\DNS;
19
20 use OpenCloud\Common\Http\Message\Formatter;
21 use OpenCloud\Common\Service\CatalogService;
22 use OpenCloud\DNS\Collection\DnsIterator;
23 use OpenCloud\DNS\Resource\AsyncResponse;
24 use OpenCloud\DNS\Resource\Domain;
25 use OpenCloud\DNS\Resource\HasPtrRecordsInterface;
26
27 28 29
30 class Service extends CatalogService
31 {
32 const DEFAULT_TYPE = 'rax:dns';
33 const DEFAULT_NAME = 'cloudDNS';
34
35 protected $regionless = true;
36
37 public function collection($class, $url = null, $parent = null, $data = null)
38 {
39 $options = $this->makeResourceIteratorOptions($this->resolveResourceClass($class));
40 $options['baseUrl'] = $url;
41
42 $parent = $parent ? : $this;
43
44 return DnsIterator::factory($parent, $options, $data);
45 }
46
47 48 49 50 51 52
53 public function domain($info = null)
54 {
55 return $this->resource('Domain', $info);
56 }
57
58 59 60 61 62 63
64 public function domainList($filter = array())
65 {
66 $url = $this->getUrl(Domain::resourceName());
67 $url->setQuery($filter);
68
69 return $this->resourceList('Domain', $url);
70 }
71
72 73 74 75 76 77
78 public function ptrRecord($info = null)
79 {
80 return $this->resource('PtrRecord', $info);
81 }
82
83 84 85 86 87 88 89
90 public function ptrRecordList(HasPtrRecordsInterface $parent)
91 {
92 $url = $this->getUrl()
93 ->addPath('rdns')
94 ->addPath($parent->getService()->getName())
95 ->setQuery(array('href' => (string) $parent->getUrl()));
96
97 return $this->resourceList('PtrRecord', $url);
98 }
99
100 101 102 103 104 105 106 107 108 109 110 111 112 113
114 public function asyncRequest($url, $method = 'GET', $headers = array(), $body = null)
115 {
116 $response = $this->getClient()->createRequest($method, $url, $headers, $body)->send();
117
118 return new AsyncResponse($this, Formatter::decode($response));
119 }
120
121 122 123 124 125 126 127 128 129 130 131
132 public function import($data)
133 {
134 $url = clone $this->getUrl();
135 $url->addPath('domains');
136 $url->addPath('import');
137
138 $object = (object) array(
139 'domains' => array(
140 (object) array(
141 'contents' => $data,
142 'contentType' => 'BIND_9'
143 )
144 )
145 );
146
147
148 $json = json_encode($object);
149
150
151 return $this->asyncRequest($url, 'POST', self::getJsonHeader(), $json);
152 }
153
154 155 156
157 public function limits($type = null)
158 {
159 $url = $this->getUrl('limits');
160
161 if ($type) {
162 $url->addPath($type);
163 }
164
165 $response = $this->getClient()->get($url)->send();
166 $body = Formatter::decode($response);
167
168 return isset($body->limits) ? $body->limits : $body;
169 }
170
171 172 173 174 175
176 public function limitTypes()
177 {
178 $response = $this->getClient()->get($this->getUrl('limits/types'))->send();
179 $body = Formatter::decode($response);
180
181 return $body->limitTypes;
182 }
183
184 public function listAsyncJobs(array $query = array())
185 {
186 $url = clone $this->getUrl();
187 $url->addPath('status');
188 $url->setQuery($query);
189
190 return DnsIterator::factory($this, array(
191 'baseUrl' => $url,
192 'resourceClass' => 'AsyncResponse',
193 'key.collection' => 'asyncResponses'
194 ));
195 }
196
197 public function getAsyncJob($jobId, $showDetails = true)
198 {
199 $url = clone $this->getUrl();
200 $url->addPath('status');
201 $url->addPath((string) $jobId);
202 $url->setQuery(array('showDetails' => ($showDetails) ? 'true' : 'false'));
203
204 $response = $this->getClient()->get($url)->send();
205
206 return new AsyncResponse($this, Formatter::decode($response));
207 }
208 }
209