-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathOpenStack.php
More file actions
218 lines (186 loc) · 7.61 KB
/
OpenStack.php
File metadata and controls
218 lines (186 loc) · 7.61 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
<?php
declare(strict_types=1);
namespace OpenStack;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware as GuzzleMiddleware;
use OpenStack\Common\Service\Builder;
use OpenStack\Common\Transport\Utils;
use OpenStack\Identity\v3\Service;
/**
* This class is the primary entry point for working with the SDK. It allows for the easy creation
* of OpenStack services.
*/
class OpenStack
{
/** @var Builder */
private $builder;
/**
* @param array $options User-defined options
*
* $options['username'] = (string) Your OpenStack username [REQUIRED]
* ['password'] = (string) Your OpenStack password [REQUIRED]
* ['tenantId'] = (string) Your tenant ID [REQUIRED if tenantName omitted]
* ['tenantName'] = (string) Your tenant name [REQUIRED if tenantId omitted]
* ['authUrl'] = (string) The Keystone URL [REQUIRED]
* ['debugLog'] = (bool) Whether to enable HTTP logging [OPTIONAL]
* ['logger'] = (LoggerInterface) Must set if debugLog is true [OPTIONAL]
* ['messageFormatter'] = (MessageFormatter) Must set if debugLog is true [OPTIONAL]
* ['requestOptions'] = (array) Guzzle Http request options [OPTIONAL]
* ['cachedToken'] = (array) Cached token credential [OPTIONAL]
* @param Builder $builder
*/
public function __construct(array $options = [], Builder $builder = null)
{
if (!isset($options['identityService'])) {
$options['identityService'] = $this->getDefaultIdentityService($options);
}
$this->builder = $builder ?: new Builder($options, 'OpenStack');
}
/**
* @param array $options
*
* @return Service
*/
private function getDefaultIdentityService(array $options): Service
{
if (!isset($options['authUrl'])) {
throw new \InvalidArgumentException("'authUrl' is a required option");
}
$stack = HandlerStack::create();
if (!empty($options['debugLog'])
&& !empty($options['logger'])
&& !empty($options['messageFormatter'])
) {
$stack->push(GuzzleMiddleware::log($options['logger'], $options['messageFormatter']));
}
$clientOptions = [
'base_uri' => Utils::normalizeUrl($options['authUrl']),
'handler' => $stack,
];
if (isset($options['requestOptions'])) {
$clientOptions = array_merge($options['requestOptions'], $clientOptions);
}
return Service::factory(new Client($clientOptions));
}
/**
* Creates a new Compute v2 service.
*
* @param array $options options that will be used in configuring the service
*
* @return \OpenStack\Compute\v2\Service
*/
public function computeV2(array $options = []): \OpenStack\Compute\v2\Service
{
$defaults = ['catalogName' => 'nova', 'catalogType' => 'compute'];
return $this->builder->createService('Compute\\v2', array_merge($defaults, $options));
}
/**
* Creates a new Networking v2 service.
*
* @param array $options options that will be used in configuring the service
*
* @return \OpenStack\Networking\v2\Service
*/
public function networkingV2(array $options = []): \OpenStack\Networking\v2\Service
{
$defaults = ['catalogName' => 'neutron', 'catalogType' => 'network'];
return $this->builder->createService('Networking\\v2', array_merge($defaults, $options));
}
/**
* Creates a new Networking v2 Layer 3 service.
*
* @param array $options options that will be used in configuring the service
*
* @return \OpenStack\Networking\v2\Extensions\Layer3\Service
*/
public function networkingV2ExtLayer3(array $options = []): \OpenStack\Networking\v2\Extensions\Layer3\Service
{
$defaults = ['catalogName' => 'neutron', 'catalogType' => 'network'];
return $this->builder->createService('Networking\\v2\\Extensions\\Layer3', array_merge($defaults, $options));
}
/**
* Creates a new Networking v2 Layer 3 service.
*
* @param array $options options that will be used in configuring the service
*
* @return \OpenStack\Networking\v2\Extensions\SecurityGroups\Service
*/
public function networkingV2ExtSecGroups(array $options = []): \OpenStack\Networking\v2\Extensions\SecurityGroups\Service
{
$defaults = ['catalogName' => 'neutron', 'catalogType' => 'network'];
return $this->builder->createService('Networking\\v2\\Extensions\\SecurityGroups', array_merge($defaults, $options));
}
/**
* Creates a new Identity v2 service.
*
* @param array $options options that will be used in configuring the service
*
* @return \OpenStack\Identity\v2\Service
*/
public function identityV2(array $options = []): \OpenStack\Identity\v2\Service
{
$defaults = ['catalogName' => 'keystone', 'catalogType' => 'identity'];
return $this->builder->createService('Identity\\v2', array_merge($defaults, $options));
}
/**
* Creates a new Identity v3 service.
*
* @param array $options options that will be used in configuring the service
*
* @return \OpenStack\Identity\v3\Service
*/
public function identityV3(array $options = []): \OpenStack\Identity\v3\Service
{
$defaults = ['catalogName' => 'keystone', 'catalogType' => 'identity'];
return $this->builder->createService('Identity\\v3', array_merge($defaults, $options));
}
/**
* Creates a new Object Store v1 service.
*
* @param array $options options that will be used in configuring the service
*
* @return \OpenStack\ObjectStore\v1\Service
*/
public function objectStoreV1(array $options = []): \OpenStack\ObjectStore\v1\Service
{
$defaults = ['catalogName' => 'swift', 'catalogType' => 'object-store'];
return $this->builder->createService('ObjectStore\\v1', array_merge($defaults, $options));
}
/**
* Creates a new Block Storage v2 service.
*
* @param array $options options that will be used in configuring the service
*
* @return \OpenStack\BlockStorage\v2\Service
*/
public function blockStorageV2(array $options = []): \OpenStack\BlockStorage\v2\Service
{
$defaults = ['catalogName' => 'cinderv2', 'catalogType' => 'volumev2'];
return $this->builder->createService('BlockStorage\\v2', array_merge($defaults, $options));
}
/**
* Creates a new Images v2 service.
*
* @param array $options options that will be used in configuring the service
*
* @return \OpenStack\Images\v2\Service
*/
public function imagesV2(array $options = []): \OpenStack\Images\v2\Service
{
$defaults = ['catalogName' => 'glance', 'catalogType' => 'image'];
return $this->builder->createService('Images\\v2', array_merge($defaults, $options));
}
/**
* Creates a new Gnocchi Metric service v1.
*
* @param array $options
*
* @return \OpenStack\Metric\v1\Gnocchi\Service
*/
public function metricGnocchiV1(array $options = []): \OpenStack\Metric\v1\Gnocchi\Service
{
$defaults = ['catalogName' => 'gnocchi', 'catalogType' => 'metric'];
return $this->builder->createService('Metric\\v1\\Gnocchi', array_merge($defaults, $options));
}
}