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