Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Commit

Permalink
Fixes crc-org#100 Use pull-secret API
Browse files Browse the repository at this point in the history
  • Loading branch information
gbraad committed Jul 20, 2021
1 parent ad97e0a commit 3ce8711
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
16 changes: 5 additions & 11 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,19 +229,12 @@ 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)
{
// 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)
if (!pullsecret)
{
var pullSecretForm = new PullSecretPickerForm();
var pullSecretPath = pullSecretForm.ShowFilePicker();
Expand All @@ -254,13 +248,13 @@ async private void StartMenu_Click(object sender, EventArgs e)
["pull-secret-file"] = pullSecretPath
};

await TaskHelpers.TryTaskAndNotify(TaskHandlers.SetConfig, pullSecretConfig,
string data = File.ReadAllText(pullSecretPath);
await TaskHelpers.TryTaskAndNotify(TaskHandlers.SetPullSecret, data,
"Pull Secret stored",
"Pull Secret not stored",
String.Empty);
}


TrayIcon.NotifyInfo(@"Starting Cluster");

var startResult = await TaskHelpers.TryTaskAndNotify(TaskHandlers.Start,
Expand Down
46 changes: 42 additions & 4 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 NotFoundException) // 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,9 +135,12 @@ 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 NotFoundException(body);
}
}
catch (TimeoutException e)
Expand Down Expand Up @@ -134,9 +169,12 @@ 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 NotFoundException(body);
}
}
catch (TimeoutException 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";
}
}
6 changes: 6 additions & 0 deletions Communication/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace CRCTray.Communication
{
internal class NotFoundException : Exception
{
public NotFoundException(string message) : base(message) { }

}

internal class APIException : Exception
{
public APIException(string message) : base(message) { }
Expand Down
10 changes: 10 additions & 0 deletions Helpers/Handlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ public static SetUnsetConfig UnsetConfig(List<string> cfg)
return getTypedResults(DaemonCommander.UnsetConfig, config);
}

public static bool GetPullSecret()
{
return getTypedResults(DaemonCommander.GetPullSecret);
}

public static bool SetPullSecret(string data)
{
return getTypedResults(DaemonCommander.SetPullSecret, data);
}

public static ConsoleResult LoginForDeveloper()
{
return WebConsole();
Expand Down

0 comments on commit 3ce8711

Please sign in to comment.