Overview

Namespaces

  • OpenCloud
    • Autoscale
      • Resource
    • CloudMonitoring
      • Exception
      • Resource
    • Common
      • Collection
      • Constants
      • Exceptions
      • Http
        • Message
      • Log
      • Resource
      • Service
    • Compute
      • Constants
      • Exception
      • Resource
    • Database
      • Resource
    • DNS
      • Collection
      • Resource
    • Identity
      • Constants
      • Resource
    • Image
      • Enum
      • Resource
        • JsonPatch
        • Schema
    • LoadBalancer
      • Enum
      • Resource
    • ObjectStore
      • Constants
      • Exception
      • Resource
      • Upload
    • Orchestration
    • Queues
      • Exception
      • Resource
    • Volume
      • Resource
  • PHP

Classes

  • Service
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
  1: <?php
  2: /**
  3:  * Copyright 2012-2014 Rackspace US, Inc.
  4:  *
  5:  * Licensed under the Apache License, Version 2.0 (the "License");
  6:  * you may not use this file except in compliance with the License.
  7:  * You may obtain a copy of the License at
  8:  *
  9:  * http://www.apache.org/licenses/LICENSE-2.0
 10:  *
 11:  * Unless required by applicable law or agreed to in writing, software
 12:  * distributed under the License is distributed on an "AS IS" BASIS,
 13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14:  * See the License for the specific language governing permissions and
 15:  * limitations under the License.
 16:  */
 17: 
 18: namespace OpenCloud\Identity;
 19: 
 20: use Guzzle\Http\ClientInterface;
 21: use OpenCloud\Common\Collection\PaginatedIterator;
 22: use OpenCloud\Common\Collection\ResourceIterator;
 23: use OpenCloud\Common\Http\Message\Formatter;
 24: use OpenCloud\Common\Service\AbstractService;
 25: use OpenCloud\Identity\Constants\User as UserConst;
 26: 
 27: /**
 28:  * Class responsible for working with Rackspace's Cloud Identity service.
 29:  *
 30:  * @package OpenCloud\Identity
 31:  */
 32: class Service extends AbstractService
 33: {
 34: 
 35:     /**
 36:      * Factory method which allows for easy service creation
 37:      *
 38:      * @param  ClientInterface $client
 39:      * @return self
 40:      */
 41:     public static function factory(ClientInterface $client)
 42:     {
 43:         $identity = new self();
 44:         $identity->setClient($client);
 45:         $identity->setEndpoint(clone $client->getAuthUrl());
 46: 
 47:         return $identity;
 48:     }
 49: 
 50:     /**
 51:      * Get this service's URL, with appended path if necessary.
 52:      *
 53:      * @return \Guzzle\Http\Url
 54:      */
 55:     public function getUrl($path = null)
 56:     {
 57:         $url = clone $this->getEndpoint();
 58: 
 59:         if ($path) {
 60:             $url->addPath($path);
 61:         }
 62: 
 63:         return $url;
 64:     }
 65: 
 66:     /**
 67:      * Get all users for the current tenant.
 68:      *
 69:      * @return \OpenCloud\Common\Collection\ResourceIterator
 70:      */
 71:     public function getUsers()
 72:     {
 73:         $response = $this->getClient()->get($this->getUrl('users'))->send();
 74: 
 75:         if ($body = Formatter::decode($response)) {
 76:             return ResourceIterator::factory($this, array(
 77:                 'resourceClass'  => 'User',
 78:                 'key.collection' => 'users'
 79:             ), $body->users);
 80:         }
 81:     }
 82: 
 83:     /**
 84:      * Used for iterator resource instantation.
 85:      */
 86:     public function user($info = null)
 87:     {
 88:         return $this->resource('User', $info);
 89:     }
 90: 
 91:     /**
 92:      * Get a user based on a particular keyword and a certain search mode.
 93:      *
 94:      * @param $search string Keyword
 95:      * @param $mode   string Either 'name', 'userId' or 'email'
 96:      * @return \OpenCloud\Identity\Resource\User
 97:      */
 98:     public function getUser($search, $mode = UserConst::MODE_NAME)
 99:     {
100:         $url = $this->getUrl('users');
101: 
102:         switch ($mode) {
103:             default:
104:             case UserConst::MODE_NAME:
105:                 $url->setQuery(array('name' => $search));
106:                 break;
107:             case UserConst::MODE_ID:
108:                 $url->addPath($search);
109:                 break;
110:             case UserConst::MODE_EMAIL:
111:                 $url->setQuery(array('email' => $search));
112:                 break;
113:         }
114: 
115:         $user = $this->resource('User');
116:         $user->refreshFromLocationUrl($url);
117: 
118:         return $user;
119:     }
120: 
121:     /**
122:      * Create a new user with provided params.
123:      *
124:      * @param  $params array User data
125:      * @return \OpenCloud\Identity\Resource\User
126:      */
127:     public function createUser(array $params)
128:     {
129:         $user = $this->resource('User');
130:         $user->create($params);
131: 
132:         return $user;
133:     }
134: 
135:     /**
136:      * Get all possible roles.
137:      *
138:      * @return \OpenCloud\Common\Collection\PaginatedIterator
139:      */
140:     public function getRoles()
141:     {
142:         return PaginatedIterator::factory($this, array(
143:             'resourceClass'  => 'Role',
144:             'baseUrl'        => $this->getUrl()->addPath('OS-KSADM')->addPath('roles'),
145:             'key.marker'     => 'id',
146:             'key.collection' => 'roles'
147:         ));
148:     }
149: 
150:     /**
151:      * Get a specific role.
152:      *
153:      * @param $roleId string The ID of the role you're looking for
154:      * @return \OpenCloud\Identity\Resource\Role
155:      */
156:     public function getRole($roleId)
157:     {
158:         return $this->resource('Role', $roleId);
159:     }
160: 
161:     /**
162:      * Generate a new token for a given user.
163:      *
164:      * @param   $json    string The JSON data-structure used in the HTTP entity body when POSTing to the API
165:      * @headers $headers array  Additional headers to send (optional)
166:      * @return  \Guzzle\Http\Message\Response
167:      */
168:     public function generateToken($json, array $headers = array())
169:     {
170:         $url = $this->getUrl();
171:         $url->addPath('tokens');
172: 
173:         $headers += self::getJsonHeader();
174: 
175:         return $this->getClient()->post($url, $headers, $json)->send();
176:     }
177: 
178:     /**
179:      * Revoke a given token based on its ID
180:      *
181:      * @param $tokenId string Token ID
182:      * @return \Guzzle\Http\Message\Response
183:      */
184:     public function revokeToken($tokenId)
185:     {
186:         $token = $this->resource('Token');
187:         $token->setId($tokenId);
188: 
189:         return $token->delete();
190:     }
191: 
192:     /**
193:      * List over all the tenants for this cloud account.
194:      *
195:      * @return \OpenCloud\Common\Collection\ResourceIterator
196:      */
197:     public function getTenants()
198:     {
199:         $url = $this->getUrl();
200:         $url->addPath('tenants');
201: 
202:         $response = $this->getClient()->get($url)->send();
203: 
204:         if ($body = Formatter::decode($response)) {
205:             return ResourceIterator::factory($this, array(
206:                 'resourceClass'  => 'Tenant',
207:                 'key.collection' => 'tenants'
208:             ), $body->tenants);
209:         }
210:     }
211: }
212: 
PHP OpenCloud API API documentation generated by ApiGen 2.8.0