~ubuntu-branches/ubuntu/vivid/php-opencloud/vivid

« back to all changes in this revision

Viewing changes to lib/OpenCloud/DNS/Service.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-07-20 13:48:45 UTC
  • mfrom: (2.1.7 experimental)
  • Revision ID: package-import@ubuntu.com-20140720134845-0dsedgwivztyrjow
Tags: 1.10.0-2
* Make dir_to_symlink Wheezy-compatible
* Upload to unstable as needed by latest owncloud version

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
2
/**
3
 
 * PHP OpenCloud library.
4
 
 * 
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
7
 
 * @version   1.6.0
8
 
 * @author    Glen Campbell <glen.campbell@rackspace.com>
9
 
 * @author    Jamie Hannaford <jamie.hannaford@rackspace.com>
 
3
 * Copyright 2012-2014 Rackspace US, Inc.
 
4
 *
 
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
 
8
 *
 
9
 * http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
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.
10
16
 */
11
17
 
12
18
namespace OpenCloud\DNS;
13
19
 
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;
19
25
 
20
26
/**
21
27
 * DNS Service.
22
28
 */
23
 
class Service extends AbstractService
 
29
class Service extends CatalogService
24
30
{
25
 
 
26
 
    /**
27
 
     * creates a new DNS object
28
 
     *
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
33
 
     */
34
 
    public function __construct(
35
 
        OpenStack $connection,
36
 
        $serviceName,
37
 
        $serviceRegion,
38
 
        $urltype
39
 
    ) {
40
 
        
41
 
        $this->getLogger()->info('Initializing DNS...');
42
 
        
43
 
        parent::__construct(
44
 
            $connection,
45
 
            'rax:dns',
46
 
            $serviceName,
47
 
            $serviceRegion,
48
 
            $urltype
49
 
        );
 
31
    const DEFAULT_TYPE = 'rax:dns';
 
32
    const DEFAULT_NAME = 'cloudDNS';
 
33
 
 
34
    protected $regionless = true;
 
35
 
 
36
    public function collection($class, $url = null, $parent = null, $data = null)
 
37
    {
 
38
        $options = $this->makeResourceIteratorOptions($this->resolveResourceClass($class));
 
39
        $options['baseUrl'] = $url;
 
40
 
 
41
        $parent = $parent ? : $this;
 
42
 
 
43
        return DnsIterator::factory($parent, $options, $data);
50
44
    }
51
45
 
52
46
    /**
53
 
     * returns a DNS::Domain object
 
47
     * Returns a domain
54
48
     *
55
 
     * @api
56
49
     * @param mixed $info either the ID, an object, or array of parameters
57
 
     * @return DNS\Domain
 
50
     * @return Resource\Domain
58
51
     */
59
52
    public function domain($info = null)
60
53
    {
61
 
        return new Domain($this, $info);
 
54
        return $this->resource('Domain', $info);
62
55
    }
63
56
 
64
57
    /**
65
 
     * returns a Collection of DNS::Domain objects
 
58
     * Returns a collection of domains
66
59
     *
67
 
     * @api
68
60
     * @param array $filter key/value pairs to use as query strings
69
 
     * @return \OpenCloud\Collection
 
61
     * @return \OpenCloud\Common\Collection
70
62
     */
71
63
    public function domainList($filter = array())
72
64
    {
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);
 
67
 
 
68
        return $this->resourceList('Domain', $url);
75
69
    }
76
70
 
77
71
    /**
78
72
     * returns a PtrRecord object for a server
79
73
     *
80
74
     * @param mixed $info ID, array, or object containing record data
81
 
     * @return Record
 
75
     * @return Resource\Record
82
76
     */
83
77
    public function ptrRecord($info = null)
84
78
    {
85
 
        return new PtrRecord($this, $info);
 
79
        return $this->resource('PtrRecord', $info);
86
80
    }
87
81
 
88
82
    /**
89
83
     * returns a Collection of PTR records for a given Server
90
84
     *
91
 
     * @param \OpenCloud\Compute\Server $server the server for which to
92
 
     *      retrieve the PTR records
93
 
     * @return Collection
 
85
     * @param \OpenCloud\Compute\Resource\Server $server the server for which to
 
86
     *                                                   retrieve the PTR records
 
87
     * @return \OpenCloud\Common\Collection
94
88
     */
95
 
    public function ptrRecordList(Server $server)
 
89
    public function ptrRecordList(HasPtrRecordsInterface $parent)
96
90
    {
97
 
        $url = $this->url('rdns/' . $server->getService()->name(), array(
98
 
            'href' => $server->url()
99
 
        ));
100
 
        return $this->collection('\OpenCloud\DNS\PtrRecord', $url);
101
 
    }
 
91
        $url = $this->getUrl()
 
92
            ->addPath('rdns')
 
93
            ->addPath($parent->getService()->getName())
 
94
            ->setQuery(array('href' => (string) $parent->getUrl()));
102
95
 
103
 
    /**
104
 
     * performs a HTTP request
105
 
     *
106
 
     * This method overrides the request with JSON content type
107
 
     *
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
113
 
     */
114
 
    public function request(
115
 
        $url,
116
 
        $method = 'GET',
117
 
        array $headers = array(),
118
 
        $body = null
119
 
    ) {
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);
123
97
    }
124
98
 
125
99
    /**
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.
132
106
     *
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
138
112
     */
139
113
    public function asyncRequest($url, $method = 'GET', $headers = array(), $body = null)
140
114
    {
141
 
        // perform the initial request
142
 
        $resp = $this->request($url, $method, $headers, $body);
143
 
 
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]'),
148
 
                $url,
149
 
                $method,
150
 
                $resp->HttpStatus(),
151
 
                $resp->HttpBody()
152
 
            ));
153
 
        }
154
 
        // @codeCoverageIgnoreEnd
155
 
 
156
 
        // debug
157
 
        $this->getLogger()->info('AsyncResponse [{body}]', array(
158
 
            'body' => $resp->httpBody()
159
 
        ));
160
 
 
161
 
        // return an AsyncResponse object
162
 
        return new AsyncResponse($this, $resp->httpBody());
 
115
        $response = $this->getClient()->createRequest($method, $url, $headers, $body)->send();
 
116
 
 
117
        return new Resource\AsyncResponse($this, Formatter::decode($response));
163
118
    }
164
119
 
165
120
    /**
166
 
     * imports domain records
 
121
     * Imports domain records
167
122
     *
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.
172
127
     *
173
 
     * @api
174
128
     * @param string $data the BIND_9 formatted data to import
175
 
     * @return DNS\AsyncResponse
 
129
     * @return Resource\AsyncResponse
176
130
     */
177
131
    public function import($data)
178
132
    {
179
 
        // determine the URL
180
 
        $url = $this->url('domains/import');
 
133
        $url = clone $this->getUrl();
 
134
        $url->addPath('domains');
 
135
        $url->addPath('import');
181
136
 
182
137
        $object = (object) array(
183
138
            'domains' => array(
191
146
        // encode it
192
147
        $json = json_encode($object);
193
148
 
194
 
        // debug it
195
 
        $this->getLogger()->info('Importing [{json}]', array('json' => $json));
196
 
 
197
149
        // perform the request
198
 
        return $this->asyncRequest($url, 'POST', array(), $json);
 
150
        return $this->asyncRequest($url, 'POST', self::getJsonHeader(), $json);
199
151
    }
200
152
 
201
153
    /**
202
154
     * returns a list of limits
203
 
     *
204
155
     */
205
156
    public function limits($type = null)
206
157
    {
207
 
        $url = $this->url('limits') . ($type ? "/$type" : '');
208
 
        $object = $this->simpleRequest($url);
209
 
        return ($type) ? $object : $object->limits;
 
158
        $url = $this->getUrl('limits');
 
159
 
 
160
        if ($type) {
 
161
            $url->addPath($type);
 
162
        }
 
163
 
 
164
        $response = $this->getClient()->get($url)->send();
 
165
        $body = Formatter::decode($response);
 
166
 
 
167
        return isset($body->limits) ? $body->limits : $body;
210
168
    }
211
169
 
212
170
    /**
216
174
     */
217
175
    public function limitTypes()
218
176
    {
219
 
        $object = $this->simpleRequest($this->url('limits/types'));
220
 
        return $object->limitTypes;
221
 
    }
222
 
 
223
 
    /**
224
 
     * Performs a simple request and returns the JSON as an object
225
 
     *
226
 
     * @param string $url the URL to GET
227
 
     */
228
 
    public function simpleRequest($url)
229
 
    {
230
 
        // Perform the request
231
 
        $response = $this->request($url);
232
 
 
233
 
        // Check for errors
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(),
239
 
                $url,
240
 
                $response->HttpBody()
241
 
            ));
242
 
        }
243
 
        // @codeCoverageIgnoreEnd
244
 
 
245
 
        // Decode the JSON
246
 
        $json = $response->httpBody();
247
 
        $this->getLogger()->info('Limit Types JSON [{json}]', array('json' => $json));
248
 
 
249
 
        $object = json_decode($json);
250
 
 
251
 
        $this->checkJsonError();
252
 
 
253
 
        return $object;
254
 
    }
255
 
 
 
177
        $response = $this->getClient()->get($this->getUrl('limits/types'))->send();
 
178
        $body = Formatter::decode($response);
 
179
 
 
180
        return $body->limitTypes;
 
181
    }
256
182
}