public static void Main(string[] args) { if (args.Length < 2 || args.Length > 3) { Console.WriteLine("Usage: {0} username api_key [region (US|UK)]", Environment.CommandLine); Environment.Exit(1); } RackspaceImpersonationIdentity auth = new RackspaceImpersonationIdentity(); auth.Username = args[0]; auth.APIKey = args[1]; if (args.Length == 3) { if (args[2] != "UK" && args[2] != "US") { Console.WriteLine("region must be either US or UK", Environment.CommandLine); Environment.Exit(1); } switch (args[2]) { case "UK": { auth.CloudInstance = CloudInstance.UK; }; break; case "US": { auth.CloudInstance = CloudInstance.Default; }; break; } } else { auth.CloudInstance = CloudInstance.Default; } try { IIdentityProvider identityProvider = new CloudIdentityProvider(); var userAccess = identityProvider.Authenticate(auth); } catch (ResponseException ex2) { Console.WriteLine("Authentication failed with the following message: {0}", ex2.Message); Environment.Exit(1); } var cloudServers = new CloudServersProvider(auth); var servers = cloudServers.ListServers(); foreach (Server serv in servers) { var date = System.DateTime.Now; var success = serv.CreateSnapshot(serv.Name + "_" + date.Year + "-" + date.Month + "-" + date.Day); if (success) { Console.WriteLine("Image for server {0} has been created successfully.", serv.Name); } else { Console.WriteLine("Image for server {0} could not be created.", serv.Name); } } }
public string GetToken(RackspaceImpersonationIdentity identity, bool forceCacheRefresh = false) { var auth = GetUserAccess(identity, forceCacheRefresh); if (auth == null || auth.Token == null) { return(null); } return(auth.Token.Id); }
public UserAccess Authenticate(RackspaceImpersonationIdentity identity, bool forceCacheRefresh = false) { var impToken = _userAccessCache.Get(string.Format("imp/{0}/{1}", identity.UserToImpersonate.CloudInstance, identity.UserToImpersonate.Username), () => { const string urlPath = "/v2.0/RAX-AUTH/impersonation-tokens"; var request = BuildImpersonationRequestJson(urlPath, identity.UserToImpersonate.Username, 600); var response = ExecuteRESTRequest <UserImpersonationResponse>(identity, urlPath, HttpMethod.POST, request); if (response == null || response.Data == null || response.Data.UserAccess == null) { return(null); } return(response.Data.UserAccess); }, forceCacheRefresh); return(impToken); }
/// <summary> /// Gets the authentication token for the specified impersonation identity. If necessary, the /// identity is authenticated on the server to obtain a token. /// </summary> /// <remarks> /// If <paramref name="forceCacheRefresh"/> is <see langword="false"/> and a cached <see cref="IdentityToken"/> /// is available for the specified <paramref name="identity"/>, this method may return the cached /// value without performing an authentication against the server. If <paramref name="forceCacheRefresh"/> /// is <see langword="true"/>, this method always authenticates the identity with the server. /// </remarks> /// <param name="identity">The identity of the user to authenticate. If this value is <see langword="null"/>, the authentication is performed with the <see cref="DefaultIdentity"/>.</param> /// <param name="forceCacheRefresh">If <see langword="true"/>, the user is always authenticated against the server; otherwise a cached <see cref="IdentityToken"/> may be returned.</param> /// <returns>The user's authentication token.</returns> /// <exception cref="ArgumentNullException">If <paramref name="identity"/> is <see langword="null"/>.</exception> /// <exception cref="NotSupportedException">If the provider does not support the given <paramref name="identity"/> type.</exception> /// <exception cref="InvalidOperationException">If <paramref name="identity"/> is <see langword="null"/> and no default identity is available for the provider.</exception> /// <exception cref="ResponseException">If the authentication request failed.</exception> protected virtual UserAccess Impersonate(RackspaceImpersonationIdentity identity, bool forceCacheRefresh) { if (identity == null) { throw new ArgumentNullException("identity"); } var impToken = TokenCache.Get(string.Format("{0}:{1}/imp/{2}/{3}", UrlBase, identity.Username, identity.UserToImpersonate.Domain == null ? "none" : identity.UserToImpersonate.Domain.Name, identity.UserToImpersonate.Username), () => { const string urlPath = "/v2.0/RAX-AUTH/impersonation-tokens"; var request = BuildImpersonationRequestJson(identity.UserToImpersonate.Username, 600); var parentIdentity = new RackspaceCloudIdentity(identity); var response = ExecuteRESTRequest <UserImpersonationResponse>(parentIdentity, new Uri(UrlBase, urlPath), HttpMethod.POST, request); if (response == null || response.Data == null || response.Data.UserAccess == null) { return(null); } IdentityToken impersonationToken = response.Data.UserAccess.Token; if (impersonationToken == null) { return(null); } var userAccess = ValidateToken(impersonationToken.Id, identity: parentIdentity); if (userAccess == null) { return(null); } var endpoints = ListEndpoints(impersonationToken.Id, parentIdentity); var serviceCatalog = BuildServiceCatalog(endpoints); return(new UserAccess(userAccess.Token, userAccess.User, serviceCatalog)); }, forceCacheRefresh); return(impToken); }