|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sausin\LaravelOvh; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use OpenStack\OpenStack; |
|
7
|
|
|
use BadMethodCallException; |
|
8
|
|
|
use GuzzleHttp\HandlerStack; |
|
9
|
|
|
use League\Flysystem\Filesystem; |
|
10
|
|
|
use OpenStack\Identity\v2\Service; |
|
11
|
|
|
use Illuminate\Support\Facades\Storage; |
|
12
|
|
|
use Illuminate\Support\ServiceProvider; |
|
13
|
|
|
use OpenStack\Common\Transport\Utils as TransportUtils; |
|
14
|
|
|
|
|
15
|
|
|
class OVHServiceProvider extends ServiceProvider |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Bootstrap the application services. |
|
19
|
|
|
* |
|
20
|
|
|
* @return void |
|
21
|
|
|
*/ |
|
22
|
|
|
public function boot() |
|
23
|
|
|
{ |
|
24
|
|
|
Storage::extend('ovh', function ($app, $config) { |
|
25
|
|
|
// check if the config is complete |
|
26
|
|
|
$this->checkConfig($config); |
|
27
|
|
|
|
|
28
|
|
|
// create the client |
|
29
|
|
|
$client = $this->makeClient($config); |
|
30
|
|
|
|
|
31
|
|
|
// get the container |
|
32
|
|
|
$container = $client->objectStoreV1()->getContainer($config['container']); |
|
33
|
|
|
|
|
34
|
|
|
return new Filesystem(new OVHSwiftAdapter($container, $this->getVars($config))); |
|
35
|
|
|
}); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Check that the config is properly setup. |
|
40
|
|
|
* |
|
41
|
|
|
* @param array &$config |
|
42
|
|
|
* @return void|BadMethodCallException |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function checkConfig(&$config) |
|
45
|
|
|
{ |
|
46
|
|
|
// needed keys |
|
47
|
|
|
$needKeys = ['server', 'region', 'user', 'pass', 'tenantName', 'projectId', 'container']; |
|
48
|
|
|
|
|
49
|
|
|
if (sizeof(array_intersect($needKeys, array_keys($config))) === sizeof($needKeys)) { |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// if the configuration wasn't complete, throw an exception |
|
54
|
|
|
throw new BadMethodCallException('Need following keys '.implode($needKeys, ', ')); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Make the client needed for interaction with OVH OpenStack. |
|
59
|
|
|
* |
|
60
|
|
|
* @param array &$config |
|
61
|
|
|
* @return \OpenStack\OpenStack |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function makeClient(&$config) |
|
64
|
|
|
{ |
|
65
|
|
|
// this is needed because default setup of openstack leads to authentication |
|
66
|
|
|
// going to wrong path of the auth url as OVH uses deprecated version |
|
67
|
|
|
$httpClient = new Client([ |
|
68
|
|
|
'base_uri' => TransportUtils::normalizeUrl($config['server']), |
|
69
|
|
|
'handler' => HandlerStack::create(), |
|
70
|
|
|
]); |
|
71
|
|
|
|
|
72
|
|
|
// setup the client for OpenStack v1 |
|
73
|
|
|
return new OpenStack([ |
|
74
|
|
|
'authUrl' => $config['server'], |
|
75
|
|
|
'region' => $config['region'], |
|
76
|
|
|
'username' => $config['user'], |
|
77
|
|
|
'password' => $config['pass'], |
|
78
|
|
|
'tenantName' => $config['tenantName'], |
|
79
|
|
|
'identityService' => Service::factory($httpClient), |
|
80
|
|
|
]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Return the config variables required by the adapter. |
|
85
|
|
|
* |
|
86
|
|
|
* @param array &$config |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function getVars(&$config) |
|
90
|
|
|
{ |
|
91
|
|
|
return [ |
|
92
|
|
|
$config['region'], |
|
93
|
|
|
$config['projectId'], |
|
94
|
|
|
$config['container'], |
|
95
|
|
|
isset($config['urlKey']) ? $config['urlKey'] : null, |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|