Ejemplo n.º 1
0
 /**
  * Returns the endpoint URL with a version in it
  *
  * @param string $url Endpoint URL
  * @param string $supportedServiceVersion Service version supported by the SDK
  * @param OpenCloud\OpenStack $client OpenStack client
  * @return Guzzle/Http/Url Endpoint URL with version in it
  */
 private function getVersionedUrl($url, $supportedServiceVersion, OpenStack $client)
 {
     $versionRegex = '/\\/[vV][0-9][0-9\\.]*/';
     if (1 === preg_match($versionRegex, $url)) {
         // URL has version in it; use it as-is
         return Url::factory($url);
     }
     // If there is no version in $url but no $supportedServiceVersion
     // is specified, just return $url as-is but log a warning
     if (is_null($supportedServiceVersion)) {
         $client->getLogger()->warning('Service version supported by SDK not specified. Using versionless service URL as-is, without negotiating version.');
         return Url::factory($url);
     }
     // Make GET request to URL
     $response = Formatter::decode($client->get($url)->send());
     // Attempt to parse response and determine URL for given $version
     if (!isset($response->versions) || !is_array($response->versions)) {
         throw new UnsupportedVersionError('Could not negotiate version with service.');
     }
     foreach ($response->versions as $version) {
         if (($version->status == 'CURRENT' || $version->status == 'SUPPORTED') && $version->id == $supportedServiceVersion) {
             foreach ($version->links as $link) {
                 if ($link->rel == 'self') {
                     return Url::factory($link->href);
                 }
             }
         }
     }
     // If we've reached this point, we could not find a versioned
     // URL in the response; throw an error
     throw new UnsupportedVersionError(sprintf('SDK supports version %s which is not currently provided by service.', $supportedServiceVersion));
 }