/// <summary> /// The process record. /// </summary> protected override void ProcessRecord() { base.ProcessRecord(); ComputeServiceConnection newCloudComputeConnection = null; WriteDebug("Trying to login to the REST API"); try { newCloudComputeConnection = LoginTask().Result; if (newCloudComputeConnection != null) { Status status = ResetPasswordTask(newCloudComputeConnection).Result; } } catch (AggregateException ae) { ae.Handle( e => { ThrowTerminatingError(new ErrorRecord(e, "-1", ErrorCategory.AuthenticationError, newCloudComputeConnection)); return(true); }); } }
/// <summary> /// Process the record /// </summary> protected override void ProcessRecord() { base.ProcessRecord(); ComputeServiceConnection newCloudComputeConnection = null; WriteDebug("Trying to login to the REST API"); try { newCloudComputeConnection = LoginTask().Result; if (newCloudComputeConnection != null) { WriteDebug(String.Format("CaaS connection created successfully: {0}", newCloudComputeConnection)); SessionState.AddComputeServiceConnection(newCloudComputeConnection); WriteObject(newCloudComputeConnection); } } catch (AggregateException ae) { ae.Handle( e => { ThrowTerminatingError(new ErrorRecord(e, "-1", ErrorCategory.AuthenticationError, newCloudComputeConnection)); return(true); }); } }
/// <summary> /// Try to login into the account using the credentials. /// If succeed, it will return the account details. /// </summary> /// <returns>The CaaS connection</returns> private async Task <ComputeServiceConnection> LoginTask() { var newCloudComputeConnection = new ComputeServiceConnection(new ComputeApiClient(ApiBaseUri));; WriteDebug("Trying to login into the CaaS"); await newCloudComputeConnection.ApiClient.LoginAsync(ApiCredentials.GetNetworkCredential()); return(newCloudComputeConnection); }
/// <summary> /// Add the specified CaaS connection to the current session. /// </summary> /// <param name="sessionState"> /// The current PowerShell session state. /// </param> /// <param name="connectionName"> /// The connection Name. /// </param> /// <param name="connection"> /// A <see cref="ComputeServiceConnection"/> representing the CaaS connection. /// </param> /// <returns> /// The <paramref name="connection"/> (enables inline use / method-chaining). /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="connection"/> is <c>null</c>. /// </exception> public static ComputeServiceConnection AddComputeServiceConnection(this SessionState sessionState, string connectionName, ComputeServiceConnection connection) { if (sessionState == null) { throw new ArgumentNullException("sessionState"); } if (connection == null) { throw new ArgumentNullException("connection"); } if (string.IsNullOrEmpty(connectionName)) { throw new ArgumentNullException("connectionName"); } Dictionary <string, ComputeServiceConnection> connections; PSVariable connectionsVariable = sessionState.PSVariable.Get(VariableNames.ComputeSessions); if (connectionsVariable == null) { connectionsVariable = new PSVariable( VariableNames.ComputeSessions, connections = new Dictionary <string, ComputeServiceConnection>(), ScopedItemOptions.AllScope ); sessionState.PSVariable.Set(connectionsVariable); } else { connections = (Dictionary <string, ComputeServiceConnection>)connectionsVariable.Value; if (connections == null) { connectionsVariable.Value = connections = new Dictionary <string, ComputeServiceConnection>(); sessionState.PSVariable.Set(connectionsVariable); } } if (!connections.ContainsKey(connectionName)) { connections.Add(connectionName, connection); } else { connections[connectionName] = connection; } if (string.IsNullOrEmpty(_defaultComputeServiceConnectionName) || connections.Count().Equals(1)) { _defaultComputeServiceConnectionName = connectionName; } return(connection); }
/// <summary> /// Try to login into the account using the credentials. /// If succeed, it will return the account details. /// </summary> /// <param name="connection"> /// The connection. /// </param> /// <returns> /// The CaaS connection /// </returns> private async Task <Status> ResetPasswordTask(ComputeServiceConnection connection) { var account = new AccountWithPhoneNumber { userName = connection.User.UserName, password = NewPassword.ToPlainString() }; return(await connection.ApiClient.Account.UpdateAdministratorAccount(account)); }
/// <summary> /// Try to login into the account using the credentials. /// If succeed, it will return the account details. /// </summary> /// <returns> /// The CaaS connection /// </returns> private async Task <ComputeServiceConnection> LoginTask() { ComputeApiClient apiClient = ComputeApiClient.GetComputeApiClient(Vendor, Region, ApiCredentials.GetNetworkCredential()); var newCloudComputeConnection = new ComputeServiceConnection(apiClient); WriteDebug("Trying to login into the CaaS"); await newCloudComputeConnection.ApiClient.Login(); return(newCloudComputeConnection); }
/// <summary> /// Process the record /// </summary> protected override void ProcessRecord() { base.ProcessRecord(); ComputeServiceConnection newCloudComputeConnection = null; WriteDebug("Trying to login to the REST API"); try { newCloudComputeConnection = LoginTask().Result; if (newCloudComputeConnection != null) { WriteDebug(string.Format("CaaS connection created successfully: {0}", newCloudComputeConnection)); if (string.IsNullOrEmpty(Name)) { Name = Guid.NewGuid().ToString(); WriteWarning( string.Format("Connection name not specified. Therefore this connection name will be a random GUID: {0}", Name)); } if (!SessionState.GetComputeServiceConnections().Any()) { WriteDebug("This is the first connection and will be the default connection."); } SessionState.AddComputeServiceConnection(Name, newCloudComputeConnection); if (SessionState.GetComputeServiceConnections().Count > 1) { WriteWarning( "You have created more than one connection on this session, please use the cmdlet Set-CaasActiveConnection -Name <name> to change the active/default connection"); } SessionState.AddComputeServiceConnection(Name, newCloudComputeConnection); WriteObject(newCloudComputeConnection); } } catch (AggregateException ae) { ae.Handle( e => { ThrowTerminatingError(new ErrorRecord(e, "-1", ErrorCategory.AuthenticationError, newCloudComputeConnection)); return(true); }); } }
/// <summary> /// The begin processing. /// </summary> protected override void BeginProcessing() { base.BeginProcessing(); // If CaaS connection is NOT set via parameter, get it from the PS session if (Connection == null) { Connection = SessionState.GetDefaultComputeServiceConnection(); if (Connection == null) ThrowTerminatingError( new ErrorRecord( new AuthenticationException( "Cannot find a valid connection. Use New-CaasConnection to create or Set-CaasActiveConnection to set a valid connection"), "-1", ErrorCategory.AuthenticationError, this)); } }
/// <summary> /// The begin processing. /// </summary> protected override void BeginProcessing() { base.BeginProcessing(); // If CaaS connection is NOT set via parameter, get it from the PS session if (Connection == null) { Connection = SessionState.GetDefaultComputeServiceConnection(); if (Connection == null) { ThrowTerminatingError( new ErrorRecord( new AuthenticationException( "Cannot find a valid connection. Use New-CaasConnection to create or Set-CaasActiveConnection to set a valid connection"), "-1", ErrorCategory.AuthenticationError, this)); } } }
protected override void BeginProcessing() { base.BeginProcessing(); // If CaaS connection is NOT set via parameter, get it from the PS session if (CaaS == null) { // TODO: Choose the correct connection in case of more than one connections in one session CaaS = SessionState.GetComputeServiceConnections().FirstOrDefault(); if (CaaS == null) { this.ThrowTerminatingError( new ErrorRecord( new AuthenticationException("Cannot find a valid CaaS connection"), "-1", ErrorCategory.AuthenticationError, this)); } } }
/// <summary> /// Try to login into the account using the credentials. /// If succeed, it will return the account details. /// </summary> /// <returns> /// The CaaS connection /// </returns> private async Task <ComputeServiceConnection> LoginTask() { var messageHandler = GetMessageHandler(ApiCredentials.GetNetworkCredential()); var baseUri = KnownApiUri.Instance.GetBaseUri(Vendor, Region); var httpClient = new HttpClientAdapter( new HttpClient(messageHandler, disposeHandler: true) { BaseAddress = baseUri, Timeout = TimeSpan.FromMinutes(5), }); // we will not try to login again, assuming the clientId remains the same accross the regions var apiClient = new ComputeApiClient(httpClient); var newCloudComputeConnection = new ComputeServiceConnection(apiClient, messageHandler); WriteDebug("Trying to login into the CaaS"); await apiClient.LoginAsync(); return(newCloudComputeConnection); }
/// <summary> /// Try to login into the account using the credentials. /// If succeed, it will return the account details. /// </summary> /// <returns> /// The CaaS connection /// </returns> private async Task <ComputeServiceConnection> LoginTask() { var ftpHost = string.Empty; ComputeApiClient apiClient = null; if (ParameterSetName == "KnownApiUri") { apiClient = ComputeApiClient.GetComputeApiClient(Vendor, Region, ApiCredentials.GetNetworkCredential()); ftpHost = ComputeApiClient.GetFtpHost(Vendor, Region); } if (ParameterSetName == "ApiDomainName") { var baseUri = new Uri(ApiDomainName); WriteWarning("This parameter is obselete and will not work for MCP2.0 commands, use Vendor and Region"); apiClient = ComputeApiClient.GetComputeApiClient(baseUri, ApiCredentials.GetNetworkCredential()); ftpHost = ApiDomainName; } var newCloudComputeConnection = new ComputeServiceConnection(apiClient); WriteDebug("Trying to login into the CaaS"); newCloudComputeConnection.Account = await newCloudComputeConnection.ApiClient.Login(); // Right now we dont need to do a connect, as ftp is used in only a few commands newCloudComputeConnection.FtpClient = new FtpClient { Host = ftpHost, EncryptionMode = FtpEncryptionMode.Explicit, DataConnectionEncryption = true, Credentials = ApiCredentials.GetNetworkCredential() .GetCredential(new Uri(string.Format("ftp://{0}", ftpHost)), "Basic") }; return(newCloudComputeConnection); }
/// <summary> /// Try to login into the account using the credentials. /// If succeed, it will return the account details. /// </summary> /// <returns> /// The CaaS connection /// </returns> private async Task<ComputeServiceConnection> LoginTask() { var ftpHost = string.Empty; ComputeApiClient apiClient = null; if (ParameterSetName == "KnownApiUri") { apiClient = ComputeApiClient.GetComputeApiClient(Vendor, Region, ApiCredentials.GetNetworkCredential()); ftpHost = ComputeApiClient.GetFtpHost(Vendor, Region); } if (ParameterSetName == "ApiDomainName") { var baseUri = new Uri(ApiDomainName); WriteWarning("This parameter is obselete and will not work for MCP2.0 commands, use Vendor and Region"); apiClient = ComputeApiClient.GetComputeApiClient(baseUri, ApiCredentials.GetNetworkCredential()); ftpHost = ApiDomainName; } var newCloudComputeConnection = new ComputeServiceConnection(apiClient); WriteDebug("Trying to login into the CaaS"); newCloudComputeConnection.Account = await newCloudComputeConnection.ApiClient.Login(); // Right now we dont need to do a connect, as ftp is used in only a few commands newCloudComputeConnection.FtpClient = new FtpClient { Host = ftpHost, EncryptionMode = FtpEncryptionMode.Explicit, DataConnectionEncryption = true, Credentials = ApiCredentials.GetNetworkCredential() .GetCredential(new Uri(string.Format("ftp://{0}", ftpHost)), "Basic") }; return newCloudComputeConnection; }
/// <summary> /// Try to login into the account using the credentials. /// If succeed, it will return the account details. /// </summary> /// <returns> /// The CaaS connection /// </returns> private async Task <ComputeServiceConnection> LoginTask() { string ftpHost = string.Empty; IComputeApiClient apiClient = null; var messageHandler = GetMessageHandler(ApiCredentials.GetNetworkCredential()); _logger = new HttpTraceLogger(this); messageHandler.LogEventHandler += _logger.LogRequestHandler; if (ParameterSetName == "KnownApiUri") { var baseUri = KnownApiUri.Instance.GetBaseUri(Vendor, Region); apiClient = GetComputeApiClient(baseUri, messageHandler); ftpHost = ComputeApiClient.GetFtpHost(Vendor, Region); } if (ParameterSetName == "ApiDomainName") { Uri baseUri; // Support ApiDomainName containing https:// if (Uri.TryCreate(ApiDomainName, UriKind.Absolute, out baseUri)) { ftpHost = baseUri.Host; } else { // Support ApiDomainName as in just the domainName baseUri = new Uri(string.Format("https://{0}/", ApiDomainName)); ftpHost = ApiDomainName; } // Handle explicit FTP host name if (!string.IsNullOrWhiteSpace(FtpDomainName)) { ftpHost = FtpDomainName; Uri ftpUri; if (Uri.TryCreate(FtpDomainName, UriKind.Absolute, out ftpUri)) { ftpHost = ftpUri.Host; } } apiClient = GetComputeApiClient(baseUri, messageHandler); } if (ParameterSetName == "HttpClient") { apiClient = new ComputeApiClient(new HttpClientAdapter(HttpClient)); } var newCloudComputeConnection = new ComputeServiceConnection(apiClient, messageHandler); WriteDebug("Trying to login into the CaaS"); newCloudComputeConnection.User = await apiClient.LoginAsync(); // await newCloudComputeConnection.ApiClient.LoginAsync(); if (!string.IsNullOrWhiteSpace(ftpHost)) { // Right now we dont need to do a connect, as ftp is used in only a few commands newCloudComputeConnection.FtpClient = new FtpClient { Host = ftpHost, EncryptionMode = FtpEncryptionMode.Explicit, DataConnectionEncryption = true, Credentials = ApiCredentials.GetNetworkCredential() .GetCredential(new Uri(string.Format("ftp://{0}", ftpHost)), "Basic") }; } messageHandler.LogEventHandler -= _logger.LogRequestHandler; return(newCloudComputeConnection); }
/// <summary> /// Try to login into the account using the credentials. /// If succeed, it will return the account details. /// </summary> /// <returns> /// The CaaS connection /// </returns> private async Task<ComputeServiceConnection> LoginTask() { ComputeApiClient apiClient = null; if (ParameterSetName == "ApiBaseUri") { WriteWarning("This parameter is obselete and will not work for MCP2.0 commands"); apiClient = new ComputeApiClient(ApiBaseUri); } if (ParameterSetName == "KnownApiUri") apiClient = new ComputeApiClient(Vendor, Region); if (ParameterSetName == "ApiDomainName") { WriteWarning("This parameter is obselete and will not work for MCP2.0 commands"); apiClient = new ComputeApiClient(ApiDomainName); } var newCloudComputeConnection = new ComputeServiceConnection(apiClient); WriteDebug("Trying to login into the CaaS"); await newCloudComputeConnection.ApiClient.LoginAsync(ApiCredentials.GetNetworkCredential()); return newCloudComputeConnection; }
/// <summary> /// Try to login into the account using the credentials. /// If succeed, it will return the account details. /// </summary> /// <returns> /// The CaaS connection /// </returns> private async Task<ComputeServiceConnection> LoginTask() { ComputeApiClient apiClient = ComputeApiClient.GetComputeApiClient(Vendor, Region, ApiCredentials.GetNetworkCredential()); var newCloudComputeConnection = new ComputeServiceConnection(apiClient); WriteDebug("Trying to login into the CaaS"); await newCloudComputeConnection.ApiClient.Login(); return newCloudComputeConnection; }
/// <summary> /// Add the specified CaaS connection to the current session. /// </summary> /// <param name="sessionState"> /// The current PowerShell session state. /// </param> /// <param name="connection"> /// A <see cref="ComputeServiceConnection"/> representing the CaaS connection. /// </param> /// <returns> /// The <paramref name="connection"/> (enables inline use / method-chaining). /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="connection"/> is <c>null</c>. /// </exception> public static ComputeServiceConnection AddComputeServiceConnection(this SessionState sessionState, ComputeServiceConnection connection) { if (sessionState == null) { throw new ArgumentNullException("sessionState"); } if (connection == null) { throw new ArgumentNullException("connection"); } List <ComputeServiceConnection> connections; PSVariable connectionsVariable = sessionState.PSVariable.Get(VariableNames.ComputeSessions); if (connectionsVariable == null) { connectionsVariable = new PSVariable( VariableNames.ComputeSessions, value: connections = new List <ComputeServiceConnection>(), options: ScopedItemOptions.AllScope ); sessionState.PSVariable.Set(connectionsVariable); // AF: If this is getting serialised (can't remember), then you need to call Set() AFTER updating the collection. } else { connections = (List <ComputeServiceConnection>)connectionsVariable.Value; if (connections == null) { connectionsVariable.Value = connections = new List <ComputeServiceConnection>(); sessionState.PSVariable.Set(connectionsVariable); // AF: If this is getting serialised (can't remember), then you need to call Set() AFTER updating the collection. } } if (!connections.Contains(connection)) { connections.Add(connection); } return(connection); }
/// <summary> /// Remove the specified CaaS connection from the current session. /// </summary> /// <param name="sessionState"> /// The current PowerShell session state. /// </param> /// <param name="connection"> /// A <see cref="ComputeServiceConnection"/> representing the CaaS connection. /// </param> /// <returns> /// <c>true</c>, if the connection was removed from the session; <c>false</c>, if the connection wasn't present in the session. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="connection"/> is <c>null</c>. /// </exception> public static bool RemoveComputeServiceConnection(this SessionState sessionState, ComputeServiceConnection connection) { if (sessionState == null) { throw new ArgumentNullException("sessionState"); } if (connection == null) { throw new ArgumentNullException("connection"); } PSVariable connectionsVariable = sessionState.PSVariable.Get(VariableNames.ComputeSessions); if (connectionsVariable == null) { return(false); } List <ComputeServiceConnection> connections = (List <ComputeServiceConnection>)connectionsVariable.Value; if (connections == null) { return(false); } return(connections.Remove(connection)); }
/// <summary> /// Try to login into the account using the credentials. /// If succeed, it will return the account details. /// </summary> /// <param name="connection"> /// The connection. /// </param> /// <returns> /// The CaaS connection /// </returns> private async Task<Status> ResetPasswordTask(ComputeServiceConnection connection) { var account = new AccountWithPhoneNumber { userName = connection.Account.UserName, password = NewPassword.ToPlainString() }; return await connection.ApiClient.UpdateAdministratorAccount(account); }
/// <summary> /// Try to login into the account using the credentials. /// If succeed, it will return the account details. /// </summary> /// <returns> /// The CaaS connection /// </returns> private async Task<ComputeServiceConnection> LoginTask() { ComputeApiClient apiClient = null; if (ParameterSetName == "ApiBaseUri") apiClient = new ComputeApiClient(ApiBaseUri); if (ParameterSetName == "KnownApiUri") apiClient = new ComputeApiClient(Vendor, Region); var newCloudComputeConnection = new ComputeServiceConnection(apiClient); WriteDebug("Trying to login into the CaaS"); await newCloudComputeConnection.ApiClient.LoginAsync(ApiCredentials.GetNetworkCredential()); return newCloudComputeConnection; }
/// <summary> /// Add the specified CaaS connection to the current session. /// </summary> /// <param name="sessionState"> /// The current PowerShell session state. /// </param> /// <param name="connectionName"> /// The connection Name. /// </param> /// <param name="connection"> /// A <see cref="ComputeServiceConnection"/> representing the CaaS connection. /// </param> /// <returns> /// The <paramref name="connection"/> (enables inline use / method-chaining). /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="connection"/> is <c>null</c>. /// </exception> public static ComputeServiceConnection AddComputeServiceConnection(this SessionState sessionState, string connectionName, ComputeServiceConnection connection) { if (sessionState == null) throw new ArgumentNullException("sessionState"); if (connection == null) throw new ArgumentNullException("connection"); if (string.IsNullOrEmpty(connectionName)) throw new ArgumentNullException("connectionName"); Dictionary<string, ComputeServiceConnection> connections; PSVariable connectionsVariable = sessionState.PSVariable.Get(VariableNames.ComputeSessions); if (connectionsVariable == null) { connectionsVariable = new PSVariable( VariableNames.ComputeSessions, connections = new Dictionary<string, ComputeServiceConnection>(), ScopedItemOptions.AllScope ); sessionState.PSVariable.Set(connectionsVariable); } else { connections = (Dictionary<string, ComputeServiceConnection>) connectionsVariable.Value; if (connections == null) { connectionsVariable.Value = connections = new Dictionary<string, ComputeServiceConnection>(); sessionState.PSVariable.Set(connectionsVariable); } } if (!connections.ContainsKey(connectionName)) connections.Add(connectionName, connection); else connections[connectionName] = connection; if (string.IsNullOrEmpty(_defaultComputeServiceConnectionName) || connections.Count().Equals(1)) _defaultComputeServiceConnectionName = connectionName; return connection; }