Overview

Namespaces

  • OpenCloud
    • Autoscale
      • Resource
    • CDN
      • Resource
    • CloudMonitoring
      • Collection
      • 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
      • Collection
      • Enum
      • Resource
    • Networking
      • Resource
    • ObjectStore
      • Constants
      • Enum
      • Exception
      • Resource
      • Upload
    • Orchestration
      • Resource
    • Queues
      • Collection
      • Exception
      • Resource
    • Volume
      • Resource
  • PHP

Classes

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