protected static function createIdentityService() { $username = self::conf('openstack.identity.username'); $password = self::conf('openstack.identity.password'); $url = self::conf('openstack.identity.url'); $tenantId = self::conf('openstack.identity.tenantId'); $service = new IdentityService($url); $service->authenticateAsUser($username, $password, $tenantId); return $service; }
/** * Given an IdentityService instance, create an ObjectStorage instance. * * This constructs a new ObjectStorage from an authenticated instance * of an \OpenStack\Identity\v2\IdentityService object. * * @param \OpenStack\Identity\v2\IdentityService $identity An identity services object that already * has a valid token and a service catalog. * @param string $region The Object Storage region * @param \OpenStack\Common\Transport\ClientInterface $client The HTTP client * * @return \OpenStack\ObjectStore\v1\ObjectStorage A new ObjectStorage instance. */ public static function newFromIdentity($identity, $region, \OpenStack\Common\Transport\ClientInterface $client = null) { $cat = $identity->serviceCatalog(); $tok = $identity->token(); return self::newFromServiceCatalog($cat, $tok, $region, $client); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use OpenStack\Identity\v2\IdentityService; use OpenStack\ObjectStore\v1\ObjectStorage; use OpenStack\ObjectStore\v1\ObjectStorage\Object; // Load these from an ini file. $ini = parse_ini_file(getenv('HOME') . '/.OpenStack.ini'); $username = $ini['username']; $password = $ini['password']; $tenantId = $ini['tenantId']; $endpoint = $ini['url']; $idService = new IdentityService($endpoint); $token = $idService->authenticateAsUser($username, $password, $tenantId); $catalog = $idService->serviceCatalog(); $store = ObjectStorage::newFromServiceCatalog($catalog, $token); $store->createContainer('Example'); $container = $store->container('Example'); $name = 'hello.txt'; $content = 'Hello World'; $mime = 'text/plain'; $localObject = new Object($name, $content, $mime); $container->save($localObject); $object = $container->object('hello.txt'); printf("Name: %s \n", $object->name()); printf("Size: %d \n", $object->contentLength()); printf("Type: %s \n", $object->contentType()); print $object->content() . PHP_EOL;
/** * @group tenant * @depends testTenants */ public function testRescopeByTenantName() { $service = new IdentityService(self::conf('openstack.identity.url'), $this->getTransportClient()); $user = self::conf('openstack.identity.username'); $pass = self::conf('openstack.identity.password'); $tenantName = self::conf('openstack.identity.tenantName'); // Authenticate without a tenant ID. $token = $service->authenticateAsUser($user, $pass); $this->assertNotEmpty($token); $details = $service->tokenDetails(); $this->assertFalse(isset($details['tenant'])); $service->rescopeUsingTenantName($tenantName); $details = $service->tokenDetails(); $this->assertEquals($tenantName, $details['tenant']['name']); // Test unscoping $service->rescopeUsingTenantName(''); $details = $service->tokenDetails(); $this->assertFalse(isset($details['tenant'])); }
/** * 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; }