/// <summary> /// Perform Cmdlet processing. /// </summary> protected override void ProcessRecord() { WriteVerbose($"Remove CloudControl connection '{Name}'..."); SessionState.ReadConnections(); CloudControlClient client; if (SessionState.Clients().TryGetValue(Name, out client)) { WriteVerbose($"Closing connection '{Name}'..."); client.Dispose(); WriteVerbose($"Closed connection '{Name}'."); } if (!SessionState.Connections().Remove(Name)) { WriteError( Errors.ConnectionDoesNotExist(Name) ); return; } SessionState.WriteConnections(); }
/// <summary> /// Asynchronously perform Cmdlet processing. /// </summary> /// <param name="cancellationToken"> /// A <see cref="CancellationToken"/> that can be used to cancel the asynchronous operation. /// </param> /// <returns> /// A <see cref="Task"/> representing the asynchronous operation. /// </returns> protected override void ProcessRecord() { CloudControlClient client; if (SessionState.Clients().TryGetValue(Name, out client)) { WriteVerbose($"Closing CloudControl connection '{Name}'..."); client.Dispose(); WriteVerbose($"Closed CloudControl connection '{Name}'."); } else { WriteVerbose($"No open connection named '{Name}'."); } }
/// <summary> /// Get or create a <see cref="CloudControlClient"/> for Cmdlet. /// </summary> /// <returns> /// The <see cref="CloudControlClient"/>. /// </returns> protected CloudControlClient GetClient() { SessionState.ReadConnections(); if (String.IsNullOrWhiteSpace(ConnectionName)) { ConnectionName = SessionState.GetDefaultCloudControlConnectionName(); } if (String.IsNullOrWhiteSpace(ConnectionName)) { ThrowTerminatingError( Errors.ConnectionRequired(this) ); return(null); } CloudControlClient client; if (!SessionState.Clients().TryGetValue(ConnectionName, out client)) { ConnectionSettings connection; if (!SessionState.Connections().TryGetValue(ConnectionName, out connection)) { ThrowTerminatingError( Errors.ConnectionDoesNotExist(ConnectionName) ); return(null); } client = CloudControlClient.Create( new Uri($"https://api-{connection.Region.ToLower()}.dimensiondata.com/"), connection.CreateNetworkCredential() ); SessionState.Clients().Add(ConnectionName, client); } return(client); }