Skip to content

Commit 1a5b405

Browse files
committed
Merge pull request #182 from alanquillin/ImplementGetAllUsersInRole
Implemented the Get All Users In A Role method
2 parents 8e2b119 + cc1cd86 commit 1a5b405

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

src/corelib/Providers/Rackspace/CloudIdentityProvider.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ public IEnumerable<Role> ListRoles(string serviceId = null, string markerId = nu
131131
return provider.ListRoles(serviceId, markerId, limit, identity);
132132
}
133133

134+
/// <inheritdoc/>
135+
public IEnumerable<User> ListUsersByRole(string roleId, bool? enabled = null, string markerId = null, int? limit = null, CloudIdentity identity = null)
136+
{
137+
if (limit < 0 || limit > 1000)
138+
throw new ArgumentOutOfRangeException("limit");
139+
140+
var provider = GetProvider(identity);
141+
return provider.ListUsersByRole(roleId, enabled, markerId, limit, identity);
142+
}
143+
134144
/// <inheritdoc/>
135145
public Role AddRole(string name, string description, CloudIdentity identity)
136146
{

src/corelib/Providers/Rackspace/GeographicalCloudIdentityProvider.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,41 @@ public bool DeleteRoleFromUser(string userId, string roleId, CloudIdentity ident
155155
return false;
156156
}
157157

158+
/// <inheritdoc/>
159+
public IEnumerable<User> ListUsersByRole(string roleId, bool? enabled = null, string markerId = null, int? limit = null, CloudIdentity identity = null)
160+
{
161+
if (limit < 0 || limit > 1000)
162+
throw new ArgumentOutOfRangeException("limit");
163+
164+
CheckIdentity(identity);
165+
166+
var parameters = BuildOptionalParameterList(new Dictionary<string, string>
167+
{
168+
{"enabled", !enabled.HasValue ? null : enabled.Value ? "true" : "false"},
169+
{"marker", markerId},
170+
{"limit", !limit.HasValue ? null : limit.Value.ToString()},
171+
});
172+
173+
var urlPath = string.Format("/v2.0/OS-KSADM/roles/{0}/RAX-AUTH/users", roleId);
174+
var response = ExecuteRESTRequest<UsersResponse>(identity, urlPath, HttpMethod.GET, queryStringParameter: parameters);
175+
176+
if (response == null || response.Data == null)
177+
return null;
178+
// Due to the fact the sometimes the API returns a JSON array of users and sometimes it returns a single JSON user object.
179+
// Therefore if we get a null data object (which indicates that the deserializer could not parse to an array) we need to try and parse as a single User object.
180+
if (response.Data.Users == null)
181+
{
182+
var userResponse = JsonConvert.DeserializeObject<UserResponse>(response.RawBody);
183+
184+
if (response == null || response.Data == null)
185+
return null;
186+
187+
return new[] { userResponse.User };
188+
}
189+
190+
return response.Data.Users;
191+
}
192+
158193
#endregion
159194

160195
#region Credentials

src/corelib/Providers/Rackspace/IExtendedCloudIdentityProvider.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ public interface IExtendedCloudIdentityProvider : IIdentityProvider
3030
/// <seealso href="http://docs.rackspace.com/openstack-extensions/auth/OS-KSADM-admin-devguide/content/GET_listRoles_v2.0_OS-KSADM_roles_Admin_API_Service_Developer_Operations-d1e1357.html">List Roles (Rackspace OS-KSADM Extension - API v2.0)</seealso>
3131
IEnumerable<Role> ListRoles(string serviceId = null, string markerId = null, int? limit = null, CloudIdentity identity = null);
3232

33+
/// <summary>
34+
/// Lists all roles.
35+
/// <note type="warning">The behavior of this API method is not defined. Do not use.</note>
36+
/// </summary>
37+
/// <param name="roleId">The role ID. The behavior is unspecified if this is not obtained from <see cref="Role.Id"/>.</param>
38+
/// <param name="enabled">Allows you to filter enabled or un-enabled users. If the value is <c>null</c>, a provider-specific default value is used.</param>
39+
/// <param name="markerId">The <see cref="Role.Id"/> of the last item in the previous list. Used for pagination. If the value is <c>null</c>, the list starts at the beginning.</param>
40+
/// <param name="limit">Indicates the maximum number of items to return. Used for pagination. If the value is <c>null</c>, a provider-specific default value is used.</param>
41+
/// <param name="identity">The cloud identity to use for this request. If not specified, the default identity for the current provider instance will be used.</param>
42+
/// <returns>A collection of <see cref="Role"/> objects describing the requested roles.</returns>
43+
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="limit"/> is less than 0 or greater than 1000.</exception>
44+
/// <exception cref="NotSupportedException">If the provider does not support the given <paramref name="identity"/> type.</exception>
45+
/// <exception cref="InvalidOperationException">If <paramref name="identity"/> is <c>null</c> and no default identity is available for the provider.</exception>
46+
/// <exception cref="ResponseException">If the REST API request failed.</exception>
47+
IEnumerable<User> ListUsersByRole(string roleId, bool? enabled = null, string markerId = null, int? limit = null, CloudIdentity identity = null);
48+
3349
/// <summary>
3450
/// Create a new role.
3551
/// </summary>

0 commit comments

Comments
 (0)