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\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: 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: 50: 51: 52: 53:
54: public function domain($info = null)
55: {
56: return $this->resource('Domain', $info);
57: }
58:
59: 60: 61: 62: 63: 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: 79: 80: 81: 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: 93: 94: 95: 96:
97: public function ptrRecord($info = null)
98: {
99: return $this->resource('PtrRecord', $info);
100: }
101:
102: 103: 104: 105: 106: 107: 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: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 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: 142: 143: 144: 145: 146: 147: 148: 149: 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:
167: $json = json_encode($object);
168:
169:
170: return $this->asyncRequest($url, 'POST', self::getJsonHeader(), $json);
171: }
172:
173: 174: 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: 192: 193: 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: 205: 206: 207: 208: 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: