Skip to content

Commit bf64708

Browse files
committed
Cleanups.
1 parent 0366607 commit bf64708

File tree

3 files changed

+50
-51
lines changed

3 files changed

+50
-51
lines changed

dotnetv4/Redshift/Actions/HelloRedshift.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ public static async Task Main(string[] args)
2626
{
2727
var redshiftClient = new AmazonRedshiftClient();
2828

29-
logger = LoggerFactory.Create(builder => { builder.AddConsole(); })
30-
.CreateLogger<HelloRedshift>();
31-
32-
Console.Clear();
3329
Console.WriteLine("Hello, Amazon Redshift! Let's list available clusters:");
34-
Console.WriteLine();
3530

3631
var clusters = new List<Cluster>();
3732

@@ -55,12 +50,10 @@ public static async Task Main(string[] args)
5550
}
5651
catch (AmazonRedshiftException ex)
5752
{
58-
logger.LogError("Error listing clusters: {Message}", ex.Message);
59-
Console.WriteLine($"Couldn't list clusters. Error: {ex.Message}");
53+
Console.WriteLine($"Couldn't list clusters. Here's why: {ex.Message}");
6054
}
6155
catch (Exception ex)
6256
{
63-
logger.LogError("An error occurred: {Message}", ex.Message);
6457
Console.WriteLine($"An error occurred: {ex.Message}");
6558
}
6659
}

dotnetv4/Redshift/Actions/RedshiftWrapper.cs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,31 @@ public async Task<List<Cluster>> DescribeClustersAsync(string? clusterIdentifier
8484
{
8585
try
8686
{
87+
var clusters = new List<Cluster>();
8788
var request = new DescribeClustersRequest();
8889
if (!string.IsNullOrEmpty(clusterIdentifier))
8990
{
9091
request.ClusterIdentifier = clusterIdentifier;
9192
}
9293

93-
var response = await _redshiftClient.DescribeClustersAsync(request);
94-
return response.Clusters;
94+
var clustersPaginator = _redshiftClient.Paginators.DescribeClusters(request);
95+
await foreach (var response in clustersPaginator.Responses)
96+
{
97+
if (response.Clusters != null)
98+
clusters.AddRange(response.Clusters);
99+
}
100+
101+
Console.WriteLine($"{clusters.Count} cluster(s) retrieved.");
102+
foreach (var cluster in clusters)
103+
{
104+
Console.WriteLine($"\t{cluster.ClusterIdentifier} (Status: {cluster.ClusterStatus})");
105+
}
106+
107+
return clusters;
95108
}
96109
catch (ClusterNotFoundException ex)
97110
{
98-
Console.WriteLine($"Cluster not found: {ex.Message}");
111+
Console.WriteLine($"Cluster {clusterIdentifier} not found: {ex.Message}");
99112
throw;
100113
}
101114
catch (Exception ex)
@@ -112,8 +125,8 @@ public async Task<List<Cluster>> DescribeClustersAsync(string? clusterIdentifier
112125
/// </summary>
113126
/// <param name="clusterIdentifier">The identifier for the cluster.</param>
114127
/// <param name="preferredMaintenanceWindow">The preferred maintenance window.</param>
115-
/// <returns>The modified cluster.</returns>
116-
public async Task<Cluster> ModifyClusterAsync(string clusterIdentifier, string preferredMaintenanceWindow)
128+
/// <returns>True if successful.</returns>
129+
public async Task<bool> ModifyClusterAsync(string clusterIdentifier, string preferredMaintenanceWindow)
117130
{
118131
try
119132
{
@@ -124,29 +137,29 @@ public async Task<Cluster> ModifyClusterAsync(string clusterIdentifier, string p
124137
};
125138

126139
var response = await _redshiftClient.ModifyClusterAsync(request);
127-
Console.WriteLine($"The modified cluster was successfully modified and has {preferredMaintenanceWindow} as the maintenance window");
128-
return response.Cluster;
140+
Console.WriteLine($"The modified cluster was successfully modified and has {response.Cluster.PreferredMaintenanceWindow} as the maintenance window");
141+
return true;
129142
}
130143
catch (ClusterNotFoundException ex)
131144
{
132-
Console.WriteLine($"Cluster not found: {ex.Message}");
133-
throw;
145+
Console.WriteLine($"Cluster {clusterIdentifier} not found: {ex.Message}");
146+
return false;
134147
}
135148
catch (Exception ex)
136149
{
137150
Console.WriteLine($"Couldn't modify cluster. Here's why: {ex.Message}");
138-
throw;
151+
return false;
139152
}
140153
}
141154
// snippet-end:[Redshift.dotnetv4.ModifyCluster]
142155

143156
// snippet-start:[Redshift.dotnetv4.DeleteCluster]
144157
/// <summary>
145-
/// Delete an Amazon Redshift cluster.
158+
/// Delete an Amazon Redshift cluster without a final snapshot.
146159
/// </summary>
147160
/// <param name="clusterIdentifier">The identifier for the cluster.</param>
148-
/// <returns>The deleted cluster.</returns>
149-
public async Task<Cluster> DeleteClusterAsync(string clusterIdentifier)
161+
/// <returns>True if successful.</returns>
162+
public async Task<bool> DeleteClusterWithoutSnapshotAsync(string clusterIdentifier)
150163
{
151164
try
152165
{
@@ -158,17 +171,17 @@ public async Task<Cluster> DeleteClusterAsync(string clusterIdentifier)
158171

159172
var response = await _redshiftClient.DeleteClusterAsync(request);
160173
Console.WriteLine($"The {clusterIdentifier} was deleted");
161-
return response.Cluster;
174+
return true;
162175
}
163176
catch (ClusterNotFoundException ex)
164177
{
165178
Console.WriteLine($"Cluster not found: {ex.Message}");
166-
throw;
179+
return false;
167180
}
168181
catch (Exception ex)
169182
{
170183
Console.WriteLine($"Couldn't delete cluster. Here's why: {ex.Message}");
171-
throw;
184+
return false;
172185
}
173186
}
174187
// snippet-end:[Redshift.dotnetv4.DeleteCluster]
@@ -468,18 +481,10 @@ private async Task WaitForStatementToCompleteAsync(string statementId)
468481
/// Wait for a cluster to become available.
469482
/// </summary>
470483
/// <param name="clusterIdentifier">The cluster identifier.</param>
471-
/// <param name="isInteractive">Whether to prompt for user input.</param>
472484
/// <returns>A task representing the asynchronous operation.</returns>
473-
public async Task WaitForClusterAvailableAsync(string clusterIdentifier, bool isInteractive = true)
485+
public async Task WaitForClusterAvailableAsync(string clusterIdentifier)
474486
{
475-
Console.WriteLine($"Wait until {clusterIdentifier} is available.");
476-
if (isInteractive)
477-
{
478-
Console.WriteLine("Press Enter to continue...");
479-
Console.ReadLine();
480-
}
481-
482-
Console.WriteLine("Waiting for cluster to become available. This may take a few minutes.");
487+
Console.WriteLine($"Wait until {clusterIdentifier} is available. This may take a few minutes.");
483488

484489
var startTime = DateTime.Now;
485490
var clusters = await DescribeClustersAsync(clusterIdentifier);

dotnetv4/Redshift/Scenarios/RedshiftBasics.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public static async Task Main(string[] args)
5353
/// </summary>
5454
public static async Task RunScenarioAsync()
5555
{
56+
// Set all variables to default values
57+
string userName = "awsuser";
58+
string userPassword = "AwsUser1000";
59+
string clusterIdentifier = "redshift-cluster-movies";
60+
var databaseName = "dev";
61+
int recordCount = 50;
62+
int year = 2013;
5663
try
5764
{
5865
Console.WriteLine(
@@ -64,24 +71,16 @@ public static async Task RunScenarioAsync()
6471
Console.WriteLine(
6572
"================================================================================");
6673

67-
// Set all variables to default values
68-
string userName = "awsuser";
69-
string userPassword = "AwsUser1000";
70-
string clusterIdentifier = "redshift-cluster-movies";
71-
var databaseName = "dev";
72-
int recordCount = 50;
73-
int year = 2013;
74-
7574
// Step 1: Get user credentials (if interactive)
7675
if (IsInteractive)
7776
{
78-
Console.WriteLine("Please enter your user name (default is awsuser):");
77+
Console.WriteLine("Please enter a user name for the cluster (default is awsuser):");
7978
var userInput = Console.ReadLine();
8079
if (!string.IsNullOrEmpty(userInput))
8180
userName = userInput;
8281

8382
Console.WriteLine("================================================================================");
84-
Console.WriteLine("Please enter your user password (default is AwsUser1000):");
83+
Console.WriteLine("Please enter a user password for the cluster (default is AwsUser1000):");
8584
var passwordInput = Console.ReadLine();
8685
if (!string.IsNullOrEmpty(passwordInput))
8786
userPassword = passwordInput;
@@ -105,7 +104,7 @@ public static async Task RunScenarioAsync()
105104

106105
// Step 4: Wait for cluster to become available
107106
Console.WriteLine("================================================================================");
108-
await Wrapper.WaitForClusterAvailableAsync(clusterIdentifier, IsInteractive);
107+
await Wrapper.WaitForClusterAvailableAsync(clusterIdentifier);
109108
Console.WriteLine("================================================================================");
110109

111110
// Step 5: List databases
@@ -152,7 +151,7 @@ public static async Task RunScenarioAsync()
152151
Console.Write("Enter a value: ");
153152

154153
var recordCountInput = Console.ReadLine();
155-
if (int.TryParse(recordCountInput, out var inputCount) && inputCount >= 50 && inputCount <= 200)
154+
if (int.TryParse(recordCountInput, out var inputCount) && inputCount is >= 50 and <= 200)
156155
{
157156
recordCount = inputCount;
158157
}
@@ -178,7 +177,7 @@ public static async Task RunScenarioAsync()
178177
{
179178
Console.Write("Enter a year: ");
180179
var yearInput = Console.ReadLine();
181-
if (int.TryParse(yearInput, out var inputYear) && inputYear >= 2012 && inputYear <= 2014)
180+
if (int.TryParse(yearInput, out var inputYear) && inputYear is >= 2012 and <= 2014)
182181
{
183182
year = inputYear;
184183
}
@@ -212,15 +211,15 @@ public static async Task RunScenarioAsync()
212211
{
213212
Console.WriteLine("Would you like to delete the Amazon Redshift cluster? (y/n)");
214213
var deleteResponse = Console.ReadLine();
215-
if (deleteResponse?.ToLower() == "y" || deleteResponse?.ToLower() == "yes")
214+
if (deleteResponse?.ToLower() == "y")
216215
{
217-
await Wrapper.DeleteClusterAsync(clusterIdentifier);
216+
await Wrapper.DeleteClusterWithoutSnapshotAsync(clusterIdentifier);
218217
}
219218
}
220219
else
221220
{
222-
Console.WriteLine("Deleting the Amazon Redshift cluster (non-interactive mode)...");
223-
await Wrapper.DeleteClusterAsync(clusterIdentifier);
221+
Console.WriteLine("Deleting the Amazon Redshift cluster...");
222+
await Wrapper.DeleteClusterWithoutSnapshotAsync(clusterIdentifier);
224223
}
225224
Console.WriteLine("================================================================================");
226225

@@ -231,6 +230,8 @@ public static async Task RunScenarioAsync()
231230
catch (Exception ex)
232231
{
233232
Console.WriteLine($"An error occurred during the scenario: {ex.Message}");
233+
Console.WriteLine("Deleting the Amazon Redshift cluster...");
234+
await Wrapper!.DeleteClusterWithoutSnapshotAsync(clusterIdentifier);
234235
throw;
235236
}
236237
}

0 commit comments

Comments
 (0)