1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\HandlerStack; |
7
|
|
|
use GuzzleHttp\Middleware as GuzzleMiddleware; |
8
|
|
|
use OpenStack\Common\Service\Builder; |
9
|
|
|
use OpenStack\Common\Transport\Utils; |
10
|
|
|
use OpenStack\Identity\v3\Service; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This class is the primary entry point for working with the SDK. It allows for the easy creation |
14
|
|
|
* of OpenStack services. |
15
|
|
|
* |
16
|
|
|
* @package OpenStack |
17
|
|
|
*/ |
18
|
|
|
class OpenStack |
19
|
|
|
{ |
20
|
|
|
/** @var Builder */ |
21
|
|
|
private $builder; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param array $options User-defined options |
25
|
|
|
* |
26
|
|
|
* $options['username'] = (string) Your OpenStack username [REQUIRED] |
27
|
|
|
* ['password'] = (string) Your OpenStack password [REQUIRED] |
28
|
7 |
|
* ['tenantId'] = (string) Your tenant ID [REQUIRED if tenantName omitted] |
29
|
|
|
* ['tenantName'] = (string) Your tenant name [REQUIRED if tenantId omitted] |
30
|
7 |
|
* ['authUrl'] = (string) The Keystone URL [REQUIRED] |
31
|
7 |
|
* ['debugLog'] = (bool) Whether to enable HTTP logging [OPTIONAL] |
32
|
|
|
* ['logger'] = (LoggerInterface) Must set if debugLog is true [OPTIONAL] |
33
|
|
|
* ['messageFormatter'] = (MessageFormatter) Must set if debugLog is true [OPTIONAL] |
34
|
|
|
* ['requestOptions'] = (array) Guzzle Http request options [OPTIONAL] |
35
|
|
|
* |
36
|
|
|
* @param Builder $builder |
37
|
|
|
*/ |
38
|
|
|
public function __construct(array $options = [], Builder $builder = null) |
39
|
|
|
{ |
40
|
1 |
|
if (!isset($options['identityService'])) { |
41
|
|
|
$options['identityService'] = $this->getDefaultIdentityService($options); |
42
|
1 |
|
} |
43
|
1 |
|
|
44
|
|
|
$this->builder = $builder ?: new Builder($options, 'OpenStack'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $options |
49
|
|
|
* |
50
|
|
|
* @return Service |
51
|
|
|
*/ |
52
|
|
|
private function getDefaultIdentityService(array $options): Service |
53
|
1 |
|
{ |
54
|
|
|
if (!isset($options['authUrl'])) { |
55
|
1 |
|
throw new \InvalidArgumentException("'authUrl' is a required option"); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
$stack = HandlerStack::create(); |
59
|
|
|
|
60
|
|
View Code Duplication |
if (!empty($options['debugLog']) |
|
|
|
|
61
|
|
|
&& !empty($options['logger']) |
62
|
|
|
&& !empty($options['messageFormatter']) |
63
|
|
|
) { |
64
|
|
|
$stack->push(GuzzleMiddleware::log($options['logger'], $options['messageFormatter'])); |
65
|
|
|
} |
66
|
1 |
|
|
67
|
|
|
$clientOptions = [ |
68
|
1 |
|
'base_uri' => Utils::normalizeUrl($options['authUrl']), |
69
|
1 |
|
'handler' => $stack |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
if (isset($options['requestOptions'])) { |
73
|
|
|
$clientOptions = array_merge($options['requestOptions'], $clientOptions); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return Service::factory(new Client($clientOptions)); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
/** |
80
|
|
|
* Creates a new Compute v2 service. |
81
|
1 |
|
* |
82
|
1 |
|
* @param array $options Options that will be used in configuring the service. |
83
|
|
|
* |
84
|
|
|
* @return \OpenStack\Compute\v2\Service |
85
|
|
|
*/ |
86
|
|
|
public function computeV2(array $options = []): \OpenStack\Compute\v2\Service |
87
|
|
|
{ |
88
|
|
|
$defaults = ['catalogName' => 'nova', 'catalogType' => 'compute']; |
89
|
|
|
return $this->builder->createService('Compute\\v2', array_merge($defaults, $options)); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
/** |
93
|
|
|
* Creates a new Networking v2 service. |
94
|
1 |
|
* |
95
|
1 |
|
* @param array $options Options that will be used in configuring the service. |
96
|
|
|
* |
97
|
|
|
* @return \OpenStack\Networking\v2\Service |
98
|
|
|
*/ |
99
|
|
|
public function networkingV2(array $options = []): \OpenStack\Networking\v2\Service |
100
|
|
|
{ |
101
|
|
|
$defaults = ['catalogName' => 'neutron', 'catalogType' => 'network']; |
102
|
|
|
return $this->builder->createService('Networking\\v2', array_merge($defaults, $options)); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
/** |
106
|
|
|
* Creates a new Networking v2 Layer 3 service. |
107
|
1 |
|
* |
108
|
1 |
|
* @param array $options Options that will be used in configuring the service. |
109
|
|
|
* |
110
|
|
|
* @return \OpenStack\Networking\v2\Extensions\Layer3\Service |
111
|
|
|
*/ |
112
|
|
|
public function networkingV2ExtLayer3(array $options = []): \OpenStack\Networking\v2\Extensions\Layer3\Service |
113
|
|
|
{ |
114
|
|
|
$defaults = ['catalogName' => 'neutron', 'catalogType' => 'network']; |
115
|
|
|
return $this->builder->createService('Networking\\v2\\Extensions\\Layer3', array_merge($defaults, $options)); |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
/** |
119
|
|
|
* Creates a new Networking v2 Layer 3 service. |
120
|
1 |
|
* |
121
|
1 |
|
* @param array $options Options that will be used in configuring the service. |
122
|
|
|
* |
123
|
|
|
* @return \OpenStack\Networking\v2\Extensions\SecurityGroups\Service |
124
|
|
|
*/ |
125
|
|
|
public function networkingV2ExtSecGroups(array $options = []): \OpenStack\Networking\v2\Extensions\SecurityGroups\Service |
126
|
|
|
{ |
127
|
|
|
$defaults = ['catalogName' => 'neutron', 'catalogType' => 'network']; |
128
|
|
|
return $this->builder->createService('Networking\\v2\\Extensions\\SecurityGroups', array_merge($defaults, $options)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Creates a new Identity v2 service. |
133
|
|
|
* |
134
|
|
|
* @param array $options Options that will be used in configuring the service. |
135
|
|
|
* |
136
|
|
|
* @return \OpenStack\Identity\v2\Service |
137
|
|
|
*/ |
138
|
|
|
public function identityV2(array $options = []): \OpenStack\Identity\v2\Service |
139
|
|
|
{ |
140
|
|
|
$defaults = ['catalogName' => 'keystone', 'catalogType' => 'identity']; |
141
|
|
|
return $this->builder->createService('Identity\\v2', array_merge($defaults, $options)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Creates a new Identity v3 service. |
146
|
|
|
* |
147
|
|
|
* @param array $options Options that will be used in configuring the service. |
148
|
|
|
* |
149
|
|
|
* @return \OpenStack\Identity\v3\Service |
150
|
|
|
*/ |
151
|
|
|
public function identityV3(array $options = []): \OpenStack\Identity\v3\Service |
152
|
|
|
{ |
153
|
|
|
$defaults = ['catalogName' => 'keystone', 'catalogType' => 'identity']; |
154
|
|
|
return $this->builder->createService('Identity\\v3', array_merge($defaults, $options)); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Creates a new Object Store v1 service. |
159
|
|
|
* |
160
|
|
|
* @param array $options Options that will be used in configuring the service. |
161
|
|
|
* |
162
|
|
|
* @return \OpenStack\ObjectStore\v1\Service |
163
|
|
|
*/ |
164
|
|
|
public function objectStoreV1(array $options = []): \OpenStack\ObjectStore\v1\Service |
165
|
|
|
{ |
166
|
|
|
$defaults = ['catalogName' => 'swift', 'catalogType' => 'object-store']; |
167
|
|
|
return $this->builder->createService('ObjectStore\\v1', array_merge($defaults, $options)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Creates a new Block Storage v2 service. |
172
|
|
|
* |
173
|
|
|
* @param array $options Options that will be used in configuring the service. |
174
|
|
|
* |
175
|
|
|
* @return \OpenStack\BlockStorage\v2\Service |
176
|
|
|
*/ |
177
|
|
|
public function blockStorageV2(array $options = []): \OpenStack\BlockStorage\v2\Service |
178
|
|
|
{ |
179
|
|
|
$defaults = ['catalogName' => 'cinderv2', 'catalogType' => 'volumev2']; |
180
|
|
|
return $this->builder->createService('BlockStorage\\v2', array_merge($defaults, $options)); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Creates a new Images v2 service. |
185
|
|
|
* |
186
|
|
|
* @param array $options Options that will be used in configuring the service. |
187
|
|
|
* |
188
|
|
|
* @return \OpenStack\Images\v2\Service |
189
|
|
|
*/ |
190
|
|
|
public function imagesV2(array $options = []): \OpenStack\Images\v2\Service |
191
|
|
|
{ |
192
|
|
|
$defaults = ['catalogName' => 'glance', 'catalogType' => 'image']; |
193
|
|
|
return $this->builder->createService('Images\\v2', array_merge($defaults, $options)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Creates a new Gnocchi Metric service v1 |
198
|
|
|
* |
199
|
|
|
* @param array $options |
200
|
|
|
* |
201
|
|
|
* @return \OpenStack\Metric\v1\Gnocchi\Service |
202
|
|
|
*/ |
203
|
|
|
public function metricGnocchiV1(array $options = []): \OpenStack\Metric\v1\Gnocchi\Service |
204
|
|
|
{ |
205
|
|
|
$defaults = ['catalogName' => 'gnocchi', 'catalogType' => 'metric']; |
206
|
|
|
|
207
|
|
|
return $this->builder->createService('Metric\\v1\\Gnocchi', array_merge($defaults, $options)); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.