Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Use pull-secret API and ask for copy-paste #102

Merged
merged 4 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions CRCTray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO;
using CRCTray.Communication;
using CRCTray.Helpers;

Expand Down Expand Up @@ -228,39 +229,28 @@ await TaskHelpers.TryTaskAndNotify(TaskHandlers.Stop,
async private void StartMenu_Click(object sender, EventArgs e)
{
// Check using get-config if pullSecret is configured
var configs = await TaskHelpers.TryTaskAndNotify(TaskHandlers.ConfigView,
var pullsecret = await TaskHelpers.TryTaskAndNotify(TaskHandlers.GetPullSecret,
String.Empty,
String.Empty,
String.Empty);

if(configs == null)
if (!pullsecret)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is not working as expected, the pull-secret prompt always appears when you click on start even when its already saved in the credential store

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the pull secret is not stored the api returns a 404 http status not found

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to check this, but this always returns false? The idea is that an exception returns a default typed result. I see that the 200, does not return any value, so this means the bool type will therefore have no value.

return getResultsForBasicCommand<bool>(BasicCommands.PullSecret);

{
// no config was returned, does this mean a communication error?
TrayIcon.NotifyError("Unable to read configuration. Is the CRC daemon running?");
return;
}

if (configs != null && configs.Configs.PullSecretFile == String.Empty)
{
var pullSecretForm = new PullSecretPickerForm();
var pullSecretPath = pullSecretForm.ShowFilePicker();
if (pullSecretPath == String.Empty)
var pullSecretForm = new PullSecretForm();
pullSecretForm.ShowDialog();
var pullSecretContent = pullSecretForm.PullSecret;
if (pullSecretContent == String.Empty)
{
TrayIcon.NotifyWarn(@"No Pull Secret was provided, Cannot start cluster without pull secret.");
TrayIcon.NotifyWarn(@"No Pull Secret was provided. Cannot start cluster without pull secret.");
return;
}
Dictionary<String, dynamic> pullSecretConfig = new Dictionary<String, dynamic>
{
["pull-secret-file"] = pullSecretPath
};

await TaskHelpers.TryTaskAndNotify(TaskHandlers.SetConfig, pullSecretConfig,
await TaskHelpers.TryTaskAndNotify(TaskHandlers.SetPullSecret, pullSecretContent,
"Pull Secret stored",
"Pull Secret not stored",
String.Empty);
}


TrayIcon.NotifyInfo(@"Starting Cluster");

var startResult = await TaskHelpers.TryTaskAndNotify(TaskHandlers.Start,
Expand Down
50 changes: 44 additions & 6 deletions Communication/DaemonCommander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,38 @@ public static LogsResult GetLogs()
return getResultsForBasicCommand<LogsResult>(BasicCommands.Logs);
}

public static bool GetPullSecret()
{
try
{
// On success the API returns 200 but no value.
getResultsForBasicCommand<bool>(BasicCommands.PullSecret);
return true;
}

catch (AggregateException ae)
{
ae.Handle((x) =>
{
if (x is APINotFoundException) // This we know how to handle.
{
// Marking as handled
return true;
}

return false;
});

return false;
}
}

public static bool SetPullSecret(string data)
{
var result = postResponse(BasicCommands.PullSecret, data, 30);
return true;
}

private static string SendBasicCommand(string command, int timeout)
{
return getResponse(command, timeout).Result;
Expand All @@ -103,14 +135,17 @@ private static async Task<string> postResponse(string cmd, string content, int t

body = await response.Content.ReadAsStringAsync();

if (response.StatusCode == HttpStatusCode.InternalServerError)
switch (response.StatusCode)
{
throw new APIException(body);
case HttpStatusCode.InternalServerError:
throw new APIException(body);
case HttpStatusCode.NotFound:
throw new APINotFoundException(body);
}
}
catch (TimeoutException e)
{
throw new CommunicationException(e.Message);
throw new APICommunicationException(e.Message);
}
catch (Exception e)
{
Expand All @@ -134,14 +169,17 @@ private static async Task<string> getResponse(string cmd, int timeout)
HttpResponseMessage response = await httpClient.GetAsync(command);
body = await response.Content.ReadAsStringAsync();

if (response.StatusCode == HttpStatusCode.InternalServerError)
switch (response.StatusCode)
{
throw new APIException(body);
case HttpStatusCode.InternalServerError:
throw new APIException(body);
case HttpStatusCode.NotFound:
throw new APINotFoundException(body);
}
}
catch (TimeoutException e)
{
throw new CommunicationException(e.Message);
throw new APICommunicationException(e.Message);
}
catch (Exception e)
{
Expand Down
2 changes: 2 additions & 0 deletions Communication/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ static class BasicCommands
internal const string ConfigSet = "config/set";
internal const string ConfigUnset = "config/unset";
internal const string Logs = "logs";

internal const string PullSecret = "pull-secret";
}
}
10 changes: 8 additions & 2 deletions Communication/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ public APIException(string message) : base(message) { }

}

internal class CommunicationException : Exception
internal class APINotFoundException : Exception
{
public CommunicationException(string message) : base(message) { }
public APINotFoundException(string message) : base(message) { }

}

internal class APICommunicationException : Exception
{
public APICommunicationException(string message) : base(message) { }

}

Expand Down
Loading