Skip to content

Instantly share code, notes, and snippets.

@chindit
Created June 23, 2018 07:55
Show Gist options
  • Save chindit/05f1309eab1f83fb264d0f6dacc08583 to your computer and use it in GitHub Desktop.
Save chindit/05f1309eab1f83fb264d0f6dacc08583 to your computer and use it in GitHub Desktop.
php-opencloud login with Identity V3
<?php
/**
* Login with php-opencloud and Identity v3 is really badly explained.
*
* Here is a bit of code that allows you to easily connect to your OpenStack using Keystone (Identity) v3
*/
use OpenStack\ObjectStore\v1\Service;
use OpenStack\OpenStack;
use Psr\Log\LoggerInterface;
/**
* Class StorageManager
* @package App\Service
*/
class StorageManager
{
/**
* @var OpenStack
*/
private $openStack
/**
* @var Service
*/
private $storageObject;
/**
* StorageManager constructor.
* @param string $endpoint
* @param string $user
* @param string $password
* @param string $tenantId
* @param string $region
* @param string $environment
* @param LoggerInterface $logger
* @throws \Exception
*/
public function __construct(string $endpoint, string $user, string $password, string $tenantId, string $region,
string $environment, LoggerInterface $logger)
{
$this->openStack = new OpenStack([
'authUrl' => $endpoint, // For example, for OVH, it's https://auth.cloud.ovh.net/v3
'type' => 'password', // We're using a password, not a token
'user' => [
'id' => 'Project name', // A random sting. I've put my project name
'name' => $user, // Openstack user name
'password' => $password, // Openstack password
'domain' => ['id' => 'default']
],
'scope' => [
'project' => [
'id' => $tenantId // Your tenant ID
]
],
'region' => $region // Your region, if needed (ex: SBG3 or GRA5)
]);
// That's all, it's working!
$this->storageObject = $this->openStack->objectStoreV1();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment