Skip to content

Commit eabda46

Browse files
author
Alan Quillin
committed
Added new Extended Identity Tests: ChangePassword
1 parent b9996aa commit eabda46

22 files changed

Lines changed: 496 additions & 94 deletions

lib/SimpleRestServices.dll

16 KB
Binary file not shown.

lib/SimpleRestServices.pdb

32 KB
Binary file not shown.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace net.openstack.Core.Domain
7+
{
8+
public class ServerState
9+
{
10+
public static string ACTIVE { get { return "ACTIVE"; } }
11+
12+
public static string BUILD { get { return "BUILD"; } }
13+
14+
public static string DELETED { get { return "DELETED"; } }
15+
16+
public static string ERROR { get { return "ERROR"; } }
17+
18+
public static string HARD_REBOOT { get { return "HARD_REBOOT"; } }
19+
20+
public static string MIGRATING { get { return "MIGRATING"; } }
21+
22+
public static string PASSWORD { get { return "PASSWORD"; } }
23+
24+
public static string REBOOT { get { return "REBOOT"; } }
25+
26+
public static string REBUILD { get { return "REBUILD"; } }
27+
28+
public static string RESCUE { get { return "RESCUE"; } }
29+
30+
public static string RESIZE { get { return "RESIZE"; } }
31+
32+
public static string REVERT_RESIZE { get { return "REVERT_RESIZE"; } }
33+
34+
public static string SUSPENDED { get { return "SUSPENDED"; } }
35+
36+
public static string UNKNOWN { get { return "UNKNOWN"; } }
37+
38+
public static string VERIFY_RESIZE { get { return "VERIFY_RESIZE"; } }
39+
}
40+
41+
public class ImageState : ServerState{}
42+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace net.openstack.Core.Exceptions.Response
7+
{
8+
class MethodNotImplementedException : ResponseException
9+
{
10+
public MethodNotImplementedException(SimpleRestServices.Client.Response response) : base("The requested method is not implemented at the service.", response) { }
11+
12+
public MethodNotImplementedException(string message, SimpleRestServices.Client.Response response) : base(message, response) {}
13+
}
14+
}

src/corelib/Core/Exceptions/Response/ResponseException.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace net.openstack.Core.Exceptions.Response
44
{
5+
[Serializable]
56
public class ResponseException : Exception
67
{
78
public SimpleRestServices.Client.Response Response { get; private set; }

src/corelib/Core/Exceptions/Response/UserNotAuthorizedException.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using System;
2+
13
namespace net.openstack.Core.Exceptions.Response
24
{
5+
[Serializable]
36
public class UserNotAuthorizedException : ResponseException
47
{
58
public UserNotAuthorizedException(SimpleRestServices.Client.Response response) : base("Unable to authenticate user and retrieve authorized service endpoints", response){}

src/corelib/Core/IComputeProvider.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ public interface IComputeProvider
5757
string GetImageMetadataItem(CloudIdentity identity, string cloudServerId, string key, string region = null);
5858
bool SetImageMetadataItem(CloudIdentity identity, string cloudServerId, string key, string value, string region = null);
5959
bool DeleteImageMetadataItem(CloudIdentity identity, string cloudServerId, string key, string region = null);
60+
61+
ServerDetails WaitForServerState(CloudIdentity identity, string cloudServerId, string expectedState, string[] errorStates, string region = null, int refreshCount = 600, int refreshDelayInMS = 2400);
62+
ServerDetails WaitForServerActive(CloudIdentity identity, string cloudServerId, string region = null, int refreshCount = 600, int refreshDelayInMS = 2400);
63+
ServerDetails WaitForServerDeleted(CloudIdentity identity, string cloudServerId, string region = null, int refreshCount = 600, int refreshDelayInMS = 2400);
64+
ServerImageDetails WaitForImageState(CloudIdentity identity, string imageId, string expectedState, string[] errorStates, string region = null, int refreshCount = 600, int refreshDelayInMS = 2400);
65+
ServerImageDetails WaitForImageActive(CloudIdentity identity, string imageId, string region = null, int refreshCount = 600, int refreshDelayInMS = 2400);
66+
ServerImageDetails WaitForImageDeleted(CloudIdentity identity, string imageId, string region = null, int refreshCount = 600, int refreshDelayInMS = 2400);
6067
}
6168
}
6269

src/corelib/Core/IIdentityProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ public interface IIdentityProvider
2020
UserCredential[] ListUserCredentials(CloudIdentity identity, string userId);
2121

2222
Tenant[] ListTenants(CloudIdentity identity);
23+
UserAccess GetUserAccess(CloudIdentity identity, bool forceCacheRefresh = false);
2324
}
2425
}

src/corelib/Providers/Rackspace/ComputeProvider.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Threading;
45
using SimpleRestServices.Client;
56
using SimpleRestServices.Client.Json;
67
using net.openstack.Core;
@@ -116,6 +117,34 @@ public bool DeleteServer(CloudIdentity identity, string cloudServerId, string re
116117
return true;
117118
}
118119

120+
public ServerDetails WaitForServerState(CloudIdentity identity, string cloudServerId, string expectedState, string[] errorStates, string region = null, int refreshCount = 600, int refreshDelayInMS = 2400)
121+
{
122+
var serverDetails = GetDetails(identity, cloudServerId, region);
123+
124+
int count = 0;
125+
while (!serverDetails.Status.Equals(expectedState) && !errorStates.Contains(serverDetails.Status) && count < refreshCount)
126+
{
127+
Thread.Sleep(refreshDelayInMS);
128+
serverDetails = GetDetails(identity, cloudServerId, region);
129+
count++;
130+
}
131+
132+
if (errorStates.Contains(serverDetails.Status))
133+
throw new ServerEnteredErrorStateException(serverDetails.Status);
134+
135+
return serverDetails;
136+
}
137+
138+
public ServerDetails WaitForServerActive(CloudIdentity identity, string cloudServerId, string region = null, int refreshCount = 600, int refreshDelayInMS = 2400)
139+
{
140+
return WaitForServerState(identity, cloudServerId, ServerState.ACTIVE, new [] {ServerState.ERROR, ServerState.UNKNOWN, ServerState.SUSPENDED}, region, refreshCount, refreshDelayInMS);
141+
}
142+
143+
public ServerDetails WaitForServerDeleted(CloudIdentity identity, string cloudServerId, string region = null, int refreshCount = 600, int refreshDelayInMS = 2400)
144+
{
145+
return WaitForServerState(identity, cloudServerId, ServerState.DELETED, new[] { ServerState.ERROR, ServerState.UNKNOWN, ServerState.SUSPENDED }, region, refreshCount, refreshDelayInMS);
146+
}
147+
119148
#endregion
120149

121150
#region Server Addresses
@@ -394,6 +423,34 @@ public bool DeleteImage(CloudIdentity identity, string imageId, string region =
394423
return true;
395424
}
396425

426+
public ServerImageDetails WaitForImageState(CloudIdentity identity, string imageId, string expectedState, string[] errorStates, string region = null, int refreshCount = 600, int refreshDelayInMS = 2400)
427+
{
428+
var details = GetImage(identity, imageId, region);
429+
430+
int count = 0;
431+
while (!details.Status.Equals(expectedState) && !errorStates.Contains(details.Status) && count < refreshCount)
432+
{
433+
Thread.Sleep(refreshDelayInMS);
434+
details = GetImage(identity, imageId, region);
435+
count++;
436+
}
437+
438+
if (errorStates.Contains(details.Status))
439+
throw new ServerEnteredErrorStateException(details.Status);
440+
441+
return details;
442+
}
443+
444+
public ServerImageDetails WaitForImageActive(CloudIdentity identity, string imageId, string region = null, int refreshCount = 600, int refreshDelayInMS = 2400)
445+
{
446+
return WaitForImageState(identity, imageId, ImageState.ACTIVE, new[] { ImageState.ERROR, ImageState.UNKNOWN, ImageState.SUSPENDED }, region, refreshCount, refreshDelayInMS);
447+
}
448+
449+
public ServerImageDetails WaitForImageDeleted(CloudIdentity identity, string imageId, string region = null, int refreshCount = 600, int refreshDelayInMS = 2400)
450+
{
451+
return WaitForImageState(identity, imageId, ImageState.DELETED, new[] { ImageState.ERROR, ImageState.UNKNOWN, ImageState.SUSPENDED }, region, refreshCount, refreshDelayInMS);
452+
}
453+
397454
#endregion
398455

399456
#region Server Metadata
@@ -557,4 +614,14 @@ protected string GetServiceEndpoint(CloudIdentity identity, string region = null
557614

558615
#endregion
559616
}
617+
618+
public class ServerEnteredErrorStateException : Exception
619+
{
620+
public string Status { get; private set; }
621+
622+
public ServerEnteredErrorStateException(string status) : base(string.Format("The server entered an error state: '{0}'", status))
623+
{
624+
Status = status;
625+
}
626+
}
560627
}

src/corelib/Providers/Rackspace/GeographicalIdentityProvider.cs

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,22 @@ public bool SetUserPassword(CloudIdentity identity, string userId, string passwo
9999
{
100100
var user = GetUser(identity, userId);
101101

102+
return SetUserPassword(identity, user, password);
103+
}
104+
105+
public bool SetUserPassword(CloudIdentity identity, User user, string password)
106+
{
107+
return SetUserPassword(identity, user.Id, user.Username, password);
108+
}
109+
110+
public bool SetUserPassword(CloudIdentity identity, string userId, string username, string password)
111+
{
102112
var urlPath = string.Format("v2.0/users/{0}/OS-KSADM/credentials", userId);
103113
var request = new SetPasswordRequest
104-
{
105-
PasswordCredencial =
106-
new PasswordCredencial {Username = user.Username, Password = password}
107-
};
114+
{
115+
PasswordCredencial =
116+
new PasswordCredencial { Username = username, Password = password }
117+
};
108118
var response = ExecuteRESTRequest<PasswordCredencialResponse>(identity, urlPath, HttpMethod.POST, request);
109119

110120
if (response == null || response.StatusCode != 201 || response.Data == null)
@@ -128,8 +138,18 @@ public UserCredential UpdateUserCredentials(CloudIdentity identity, string userI
128138
{
129139
var user = GetUser(identity, userId);
130140

141+
return UpdateUserCredentials(identity, user, apiKey);
142+
}
143+
144+
public UserCredential UpdateUserCredentials(CloudIdentity identity, User user, string apiKey)
145+
{
146+
return UpdateUserCredentials(identity, user.Id, user.Username, apiKey);
147+
}
148+
149+
public UserCredential UpdateUserCredentials(CloudIdentity identity, string userId, string username, string apiKey)
150+
{
131151
var urlPath = string.Format("v2.0/users/{0}/OS-KSADM/credentials/RAX-KSKEY:apiKeyCredentials", userId);
132-
var request = new UpdateUserCredencialRequest { UserCredential = new UserCredential { Username = user.Username, APIKey = apiKey } };
152+
var request = new UpdateUserCredencialRequest { UserCredential = new UserCredential { Username = username, APIKey = apiKey } };
133153
var response = ExecuteRESTRequest<UserCredentialResponse>(identity, urlPath, HttpMethod.POST, request);
134154

135155
if (response == null || response.Data == null)
@@ -280,24 +300,27 @@ public IdentityToken GetTokenInfo(CloudIdentity identity, bool forceCacheRefresh
280300

281301
public UserAccess Authenticate(CloudIdentity identity)
282302
{
283-
var auth = AuthRequest.FromCloudIdentity(identity);
284-
var response = ExecuteRESTRequest<AuthenticationResponse>(identity, "/v2.0/tokens", HttpMethod.POST, auth, isTokenRequest: true);
285-
286-
287-
if (response == null || response.Data == null || response.Data.UserAccess == null || response.Data.UserAccess.Token == null)
288-
return null;
289-
290-
return response.Data.UserAccess;
303+
return GetUserAccess(identity, true);
291304
}
292305

293-
private UserAccess GetUserAccess(CloudIdentity identity, bool forceCacheRefresh = false)
306+
public UserAccess GetUserAccess(CloudIdentity identity, bool forceCacheRefresh = false)
294307
{
295308
var rackspaceCloudIdentity = identity as RackspaceCloudIdentity;
296309

297310
if (rackspaceCloudIdentity == null)
298311
throw new InvalidCloudIdentityException(string.Format("Invalid Identity object. Rackspace Identity service requires an instance of type: {0}", typeof(RackspaceCloudIdentity)));
299312

300-
var userAccess = _userAccessCache.Get(string.Format("{0}/{1}", rackspaceCloudIdentity.CloudInstance, rackspaceCloudIdentity.Username), () => Authenticate(rackspaceCloudIdentity), forceCacheRefresh);
313+
var userAccess = _userAccessCache.Get(string.Format("{0}/{1}", rackspaceCloudIdentity.CloudInstance, rackspaceCloudIdentity.Username), () =>
314+
{
315+
var auth = AuthRequest.FromCloudIdentity(identity);
316+
var response = ExecuteRESTRequest<AuthenticationResponse>(identity, "/v2.0/tokens", HttpMethod.POST, auth, isTokenRequest: true);
317+
318+
319+
if (response == null || response.Data == null || response.Data.UserAccess == null || response.Data.UserAccess.Token == null)
320+
return null;
321+
322+
return response.Data.UserAccess;
323+
}, forceCacheRefresh);
301324

302325
return userAccess;
303326
}

0 commit comments

Comments
 (0)