forked from stackforge/openstack-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootstrap.php
More file actions
298 lines (274 loc) · 10.5 KB
/
Bootstrap.php
File metadata and controls
298 lines (274 loc) · 10.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/*
* (c) Copyright 2012-2014 Hewlett-Packard Development Company, L.P.
* (c) Copyright 2014 Rackspace US, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
namespace OpenStack;
use OpenStack\Identity\v2\IdentityService;
use OpenStack\ObjectStore\v1\Resource\StreamWrapper;
use OpenStack\ObjectStore\v1\Resource\StreamWrapperFS;
/**
* Bootstrapping services.
*
* There is no requirement that this class be used. OpenStack is
* built to be flexible, and any individual component can be
* used directly, with one caveat: No explicit `require` or
* `include` calls are made.
*
* This class provides the following services:
*
* - Configuration: "global" settings are set here.
* See the setConfiguration() method to see how they
* can be set, and the config() and hasConfig() methods to see
* how configuration might be checked.
* - Stream Wrappers: This class can initialize a set of stream
* wrappers which will make certain OpenStack services available
* through the core PHP stream support.
*
* Configuration
*
* Configuration directives can be merged into the existing confiuration
* using the setConfiguration method.
*
* <?php
* $config = array(
* // We use Guzzle, which defaults to CURL, for a transport layer.
* 'transport' => 'OpenStack\Common\Transport\Guzzle\GuzzleAdapter',
* // Set the HTTP max wait time to 500 seconds.
* 'transport.timeout' => 500,
* );
* Bootstrap::setConfiguration($config);
*
* // Check and get params.
* if (Bootstrap::hasConf('transport.timeout') {
* $to = Bootstrap::conf('transport.timeout');
* }
*
* // Or get a param with a default value:
* $val = Bootstrap::conf('someval', 'default value');
*
* // $val will be set to 'default value' because there
* // is no 'someval' configuration param.
*
* ?>
*
* STREAM WRAPPERS
*
* Stream wrappers allow you to use the built-in file manipulation
* functions in PHP to interact with other services. Specifically,
* the OpenStack stream wrappers allow you to use built-in file commands
* to access Object Storage (Swift) and other OpenStack services using
* commands like file_get_contents() and fopen().
*
* It's awesome. Trust me.
*/
class Bootstrap
{
const VERSION = '0.0.1';
public static $config = [
// The transport implementation. By default, we use the Guzzle Client
'transport' => 'OpenStack\Common\Transport\Guzzle\GuzzleAdapter',
];
/**
* @var \OpenStack\Identity\v2\IdentityService An identity services object
* created from the global settings.
*/
public static $identity = null;
/**
* @var \OpenStack\Common\Transport\ClientInterface A transport client for requests.
*/
public static $transport = null;
/**
* Register stream wrappers for OpenStack.
*
* This registers the ObjectStorage stream wrappers, which allow you to access
* ObjectStorage through standard file access mechanisms.
*
* // Enable stream wrapper.
* Bootstrap::useStreamWrappers();
*
* // Create a context resource.
* $cxt = stream_context_create(array(
* 'tenantid' => '12de21',
* 'username' => 'foobar',
* 'password' => 'f78saf7hhlll',
* 'endpoint' => 'https://identity.hpcloud.com' // <-- not real URL!
* ));
*
* // Get the contents of a Swift object.
* $content = file_get_contents('swift://public/notes.txt', 'r', false, $cxt);
*/
public static function useStreamWrappers()
{
self::enableStreamWrapper(
StreamWrapper::DEFAULT_SCHEME,
'OpenStack\ObjectStore\v1\Resource\StreamWrapper'
);
self::enableStreamWrapper(
StreamWrapperFS::DEFAULT_SCHEME,
'OpenStack\ObjectStore\v1\Resource\StreamWrapperFS'
);
}
/**
* Register a stream wrapper according to its scheme and class
*
* @param $scheme Stream wrapper's scheme
* @param $class The class that contains stream wrapper functionality
*/
private static function enableStreamWrapper($scheme, $class)
{
if (in_array($scheme, stream_get_wrappers())) {
stream_wrapper_unregister($scheme);
}
stream_wrapper_register($scheme, $class);
}
/**
* Set configuration directives for OpenStack.
*
* This merges the provided associative array into the existing
* configuration parameters (Bootstrap::$config).
*
* All of the OpenStack classes share the same configuration. This
* ensures that a stable runtime environment is maintained.
*
* Common configuration directives:
*
* - 'transport': The namespaced classname for the transport that
* should be used. Example: \OpenStack\Common\Transport\Guzzle\GuzzleAdapter
* - 'transport.debug': The integer 1 for enabling debug, 0 for
* disabling. Enabling will turn on verbose debugging output
* for any transport that supports it.
* - 'transport.timeout': An integer value indicating how long
* the transport layer should wait for an HTTP request. A
* transport MAY ignore this parameter, but the ones included
* with the library honor it.
* - 'transport.ssl_verify': Set this to false to turn off SSL certificate
* verification. This is NOT recommended, but is sometimes necessary for
* certain proxy configurations.
* - 'transport.proxy': Set the proxy as a string.
* - 'username' and 'password'
* - 'tenantid'
* - 'endpoint': The full URL to identity services. This is used by stream
* wrappers.
*
* @param array $array An associative array of configuration directives.
*/
public static function setConfiguration($array)
{
self::$config = $array + self::$config;
}
/**
* Get a configuration option.
*
* Get a configuration option by name, with an optional default.
*
* @param string $name The name of the configuration option to get.
* @param mixed $default The default value to return if the name is not found.
*
* @return mixed The value, if found; or the default, if set; or null.
*/
public static function config($name = null, $default = null)
{
// If no name is specified, return the entire config array.
if (empty($name)) {
return self::$config;
}
// If the config value exists, return that.
if (isset(self::$config[$name])) {
return self::$config[$name];
}
// Otherwise, just return the default value.
return $default;
}
/**
* Check whether the given configuration option is set.
*
* if (Bootstrap::hasConfig('transport')) {
* syslog(LOG_INFO, 'An alternate transport is supplied.');
* }
*
* @param string $name The name of the item to check for.
*
* @return boolean true if the named option is set, false otherwise. Note that
* the value may be falsey (false, 0, etc.), but if the value is null, this
* will return false.
*/
public static function hasConfig($name)
{
return isset(self::$config[$name]);
}
/**
* Get a \OpenStack\Identity\v2\IdentityService object from the bootstrap config.
*
* A factory helper function that uses the bootstrap configuration to create
* a ready to use \OpenStack\Identity\v2\IdentityService object.
*
* @param bool $force Whether to force the generation of a new object even if
* one is already cached.
*
* @return \OpenStack\Identity\v2\IdentityService An authenticated ready to use
* \OpenStack\Identity\v2\IdentityService object.
* @throws \OpenStack\Common\Exception When the needed configuration to authenticate
* is not available.
*/
public static function identity($force = false)
{
$transport = self::transport();
// If we already have an identity make sure the token is not expired.
if ($force || is_null(self::$identity) || self::$identity->isExpired()) {
// Make sure we have an endpoint to use
if (!self::hasConfig('endpoint')) {
throw new Exception('Unable to authenticate. No endpoint supplied.');
}
// User cannot be an empty string, so we need
// to do more checking than self::hasConfig(), which returns true
// if an item exists and is an empty string.
$user = self::config('username', null);
// Check if we have a username/password
if (!empty($user) && self::hasConfig('password')) {
$is = new IdentityService(self::config('endpoint'), $transport);
$is->authenticateAsUser($user, self::config('password'), self::config('tenantid', null), self::config('tenantname', null));
self::$identity = $is;
} else {
throw new Exception('Unable to authenticate. No user credentials supplied.');
}
}
return self::$identity;
}
/**
* Get a transport client.
*
* @param boolean $reset Whether to recreate the transport client if one already exists.
*
* @return \OpenStack\Common\Transport\ClientInterface A transport client.
*/
public static function transport($reset = false)
{
if (is_null(self::$transport) || $reset == true) {
$options = [
'ssl_verify' => self::config('ssl_verify', true),
'timeout' => self::config('timeout', 0), // 0 is no timeout.
'debug' => self::config('debug', 0),
];
$proxy = self::config('proxy', false);
if ($proxy) {
$options['proxy'] = $proxy;
}
$class = self::config('transport');
self::$transport = $class::create($options);
}
return self::$transport;
}
}