Skip to content

Commit

Permalink
Merge pull request #68 from NoxOrg/refactor/cli_cache_remote
Browse files Browse the repository at this point in the history
Refactor/cli cache remote
  • Loading branch information
jan-schutte authored Aug 8, 2023
2 parents a6ac054 + b4d334f commit 4822a32
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 132 deletions.
2 changes: 1 addition & 1 deletion src/Nox.Cli.Abstractions/Caching/INoxCliCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface INoxCliCache: IChangeTracking
string UserPrincipalName { get; set; }
string TenantId { get; set; }

string RemoteUrl { get; set; }
Uri? RemoteUri { get; set; }

DateTimeOffset Expires { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/Nox.Cli.Caching/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Nox.Cli.Caching.Tests")]
[assembly: InternalsVisibleTo("CachingTests")]
namespace Nox.Cli.Caching;
24 changes: 7 additions & 17 deletions src/Nox.Cli.Caching/NoxCliCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class NoxCliCache : INoxCliCache

private string _tid = string.Empty;

private string _remoteUrl = string.Empty;
private Uri? _remoteUri;

public string Username
{
Expand Down Expand Up @@ -40,29 +40,19 @@ public string UserPrincipalName
}
}

public string RemoteUrl
public Uri? RemoteUri
{
get => _remoteUrl;
get => _remoteUri;
set
{
if (_remoteUrl != value)
if (_remoteUri != value)
{
_remoteUrl = value;
_remoteUri = value;
IsChanged = true;
}
}
}

public string TemplateUrl
{
get => _remoteUrl + "/templates/" + _tid;
}

public string WorkflowUrl
{
get => _remoteUrl + "/workflows/" + _tid;
}

public string TenantId
{
get => _tid;
Expand Down Expand Up @@ -138,9 +128,9 @@ public NoxCliCache()

}

public NoxCliCache(string remoteUrl, string cachePath, string cacheFile)
public NoxCliCache(Uri remoteUri, string cachePath, string cacheFile)
{
_remoteUrl = remoteUrl;
_remoteUri = remoteUri;
Expires = new DateTimeOffset(DateTime.Now.AddDays(7));
}

Expand Down
92 changes: 47 additions & 45 deletions src/Nox.Cli.Caching/NoxCliCacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ public class NoxCliCacheManager: INoxCliCacheManager
private string _workflowCachePath;
private string _templateCachePath;
private string _localWorkflowPath;
private string _tenantId;
private string _workflowUrl;
private string _templateUrl;
private bool _isServer;
private readonly string _remoteUrl;
private readonly Uri _remoteUri;
private Uri? _workflowUri;
private List<string> _buildLog;
private IManifestConfiguration? _manifest;
private List<IWorkflowConfiguration>? _workflows;
private IPersistedTokenCache? _tokenCache;
private IDeserializer _deserializer;

internal void ForServer()
{
Expand All @@ -62,7 +61,8 @@ internal void UseLocalWorkflowPath(string localWorkflowPath)

internal void UseTenantId(string tenantId)
{
_tenantId = tenantId;
if (string.IsNullOrEmpty(tenantId)) throw new NoxCliException("Tenant has not been configured!");
SetTenantId(tenantId);
}

internal void AddBuildEventHandler(EventHandler<ICacheManagerBuildEventArgs> handler)
Expand All @@ -73,13 +73,12 @@ internal void AddBuildEventHandler(EventHandler<ICacheManagerBuildEventArgs> han
public bool IsOnline {
get
{
if (string.IsNullOrEmpty(_remoteUrl)) return false;
if (_remoteUri == null) return false;
var ping = new Ping();
try
{
var uri = new Uri(_remoteUrl);
if (uri.Host == "localhost") return true;
var reply = ping.Send(uri.Host, 3000);
if (_remoteUri.Host == "localhost") return true;
var reply = ping.Send(_remoteUri.Host, 3000);
if (reply.Status == IPStatus.Success)
{
return true;
Expand Down Expand Up @@ -131,10 +130,9 @@ public void RefreshTemplate(string name)
if (templateInfo == null) throw new NoxCliException($"Unable to find template {name} in the template cache.");
if (!_isServer && IsOnline)
{
var client = new RestClient(_templateUrl);
var infoRequest = new RestRequest("Info") { Method = Method.Post };
var client = new RestClient(GetRemoteUri($"/templateInfo/{name}"));
var infoRequest = new RestRequest() { Method = Method.Get };
infoRequest.AddHeader("Accept", "application/json");
infoRequest.AddJsonBody($"{{\"FilePath\": \"{name}\"}}");
var fileInfo = JsonConvert.DeserializeObject<RemoteFileInfo>(client.Execute(infoRequest).Content!);
if (fileInfo!.Size != templateInfo.Size || fileInfo.ShaChecksum != templateInfo.ShaChecksum)
{
Expand All @@ -144,28 +142,26 @@ public void RefreshTemplate(string name)
}
}

public NoxCliCacheManager(string remoteUrl, IPersistedTokenCache? tokenCache = null)
public NoxCliCacheManager(string? remoteUrl, IPersistedTokenCache? tokenCache = null)
{
_buildLog = new List<string>();
if (string.IsNullOrEmpty(remoteUrl))
{
_remoteUrl = remoteUrl;
_remoteUri = new Uri("https://noxorg.dev");
}
else
{
_remoteUrl = "https://noxorg.dev";
_remoteUri = new Uri(remoteUrl);
}
_remoteUrl = remoteUrl;

_cachePath = WellKnownPaths.CachePath;
_workflowCachePath = WellKnownPaths.WorkflowsCachePath;
_templateCachePath = WellKnownPaths.TemplatesCachePath;
_tenantId = "";
_workflowUrl = "";
_templateUrl = "";
Directory.CreateDirectory(_cachePath);
_cacheFile = WellKnownPaths.CacheFile;
_localWorkflowPath = ".";
_tokenCache = tokenCache;
_deserializer = BuildDeserializer();
}

internal INoxCliCacheManager Build()
Expand All @@ -175,8 +171,8 @@ internal INoxCliCacheManager Build()
Dictionary<string,string> yamlFiles = new(StringComparer.OrdinalIgnoreCase);
if (_isServer)
{
_cache!.TenantId = _tenantId;
SetRemoteUrls();
if (string.IsNullOrEmpty(_cache!.TenantId)) throw new NoxCliException("Tenant has not been configured!");

GetOnlineWorkflowsAndManifest(yamlFiles);

GetOnlineTemplates();
Expand All @@ -193,16 +189,17 @@ internal INoxCliCacheManager Build()
}

var deserializer = BuildDeserializer();
ResolveManifest(deserializer, yamlFiles);
ResolveWorkflows(deserializer, yamlFiles);
ResolveManifest(yamlFiles);
ResolveWorkflows(yamlFiles);

Save();
return this;
}

private void GetOrCreateCache()
internal void GetOrCreateCache()
{
_cache = new NoxCliCache(_remoteUrl, _cachePath, _cacheFile);
if (_remoteUri == null) throw new NoxCliException("Remote Uri has not been set!");
_cache = new NoxCliCache(_remoteUri, _cachePath, _cacheFile);

if (!File.Exists(_cacheFile) || _isServer) return;
Load();
Expand All @@ -228,9 +225,8 @@ private void GetCredentialsFromAzureToken()

_cache.Username = GetUsernameFromUpn(auth.AuthenticationRecord.Username);
_cache.UserPrincipalName = auth.AuthenticationRecord.Username;
_tenantId = auth.AuthenticationRecord.TenantId;
_cache.TenantId = _tenantId;
SetRemoteUrls();
SetTenantId(auth.AuthenticationRecord.TenantId);

Save();
RaiseBuildEvent($"{Emoji.Known.GreenCircle} Logged in as {auth.AuthenticationRecord.Username} on tenant {auth.AuthenticationRecord.TenantId}");
}
Expand All @@ -251,11 +247,11 @@ private void Load()
_cache.ClearChanges();
}

private void GetOnlineWorkflowsAndManifest(IDictionary<string, string> yamlFiles)
internal void GetOnlineWorkflowsAndManifest(IDictionary<string, string> yamlFiles)
{
try
{
var client = new RestClient(_workflowUrl);
var client = new RestClient(GetRemoteUri("/scripts"));

var request = new RestRequest() { Method = Method.Get };

Expand Down Expand Up @@ -385,22 +381,22 @@ private IDeserializer BuildDeserializer()
.Build();
}

private void ResolveManifest(IDeserializer deserializer, Dictionary<string, string> yamlFiles)
internal void ResolveManifest(Dictionary<string, string> yamlFiles)
{
_manifest = yamlFiles
.Where(kv => kv.Key.EndsWith(".cli.nox.yaml"))
.Select(kv => deserializer.Deserialize<ManifestConfiguration>(kv.Value))
.Select(kv => _deserializer.Deserialize<ManifestConfiguration>(kv.Value))
.FirstOrDefault();
}

private void ResolveWorkflows(IDeserializer deserializer, Dictionary<string, string> yamlFiles)
internal void ResolveWorkflows(Dictionary<string, string> yamlFiles)
{
_workflows = new List<IWorkflowConfiguration>();
foreach (var yaml in yamlFiles.Where(kv => kv.Key.EndsWith(FileExtension.WorkflowDefinition.TrimStart('*'))))
{
try
{
_workflows.Add(deserializer.Deserialize<WorkflowConfiguration>(yaml.Value));
_workflows.Add(_deserializer.Deserialize<WorkflowConfiguration>(yaml.Value));
}
catch (Exception ex)
{
Expand Down Expand Up @@ -462,11 +458,11 @@ private FileInfo[] GetFilesWithSearchPatterns(DirectoryInfo path, string[] searc
return files.ToArray();
}

private void GetOnlineTemplates()
internal void GetOnlineTemplates()
{
try
{
var client = new RestClient(_templateUrl);
var client = new RestClient(GetRemoteUri("/templates"));

var fileListRequest = new RestRequest() { Method = Method.Get };

Expand Down Expand Up @@ -558,9 +554,9 @@ private void RaiseBuildEvent(string message)
BuildEvent?.Invoke(this, new CacheManagerBuildEventArgs(plainMessage, message));
}

private string GetOnlineTemplate(string name)
internal string GetOnlineTemplate(string name)
{
var client = new RestClient(_templateUrl);
var client = new RestClient(GetRemoteUri("/templates"));
var fileRequest = new RestRequest() { Method = Method.Post };
fileRequest.AddHeader("Accept", "application/json");
fileRequest.AddJsonBody($"{{\"FilePath\": \"{name}\"}}");
Expand All @@ -570,13 +566,6 @@ private string GetOnlineTemplate(string name)
return fileContent;
}

private void SetRemoteUrls()
{
if (string.IsNullOrEmpty(_tenantId)) throw new NoxCliException("Tenant Id has not been set!");
_workflowUrl = $"{_remoteUrl}/workflows/{_tenantId}";
_templateUrl = $"{_remoteUrl}/templates/{_tenantId}";
}

private string GetUsernameFromUpn(string upn)
{
var result = upn;
Expand All @@ -588,5 +577,18 @@ private string GetUsernameFromUpn(string upn)
result = result.Replace('.', ' ');
return result;
}

private void SetTenantId(string tenantId)
{
_cache!.TenantId = tenantId;
_workflowUri = new Uri(_remoteUri, $"workflows/{tenantId}");
}

private Uri GetRemoteUri(string path)
{
var uriBuilder = new UriBuilder(_workflowUri!);
uriBuilder.Path += path;
return uriBuilder.Uri;
}

}
7 changes: 7 additions & 0 deletions src/Nox.Cli.sln
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreTests", "CoreTests\Core
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nox.Cli.PersonalAccessToken", "Nox.Cli.PersonalAccessToken\Nox.Cli.PersonalAccessToken.csproj", "{4815F900-D0AB-4237-B401-77D449FBCAFE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CachingTests", "..\tests\CachingTests\CachingTests.csproj", "{A5BC67F2-0570-4F49-8F15-7DED77E12066}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -207,6 +209,10 @@ Global
{4815F900-D0AB-4237-B401-77D449FBCAFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4815F900-D0AB-4237-B401-77D449FBCAFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4815F900-D0AB-4237-B401-77D449FBCAFE}.Release|Any CPU.Build.0 = Release|Any CPU
{A5BC67F2-0570-4F49-8F15-7DED77E12066}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A5BC67F2-0570-4F49-8F15-7DED77E12066}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5BC67F2-0570-4F49-8F15-7DED77E12066}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5BC67F2-0570-4F49-8F15-7DED77E12066}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -231,5 +237,6 @@ Global
{E71FC419-2898-485D-8588-8E012F064F48} = {E3FA4748-9B96-42E7-BDE8-1F08AA8AD4D9}
{6117EE16-9A61-446F-B409-484DE41FC26E} = {E3FA4748-9B96-42E7-BDE8-1F08AA8AD4D9}
{4D25DF29-1045-4F6C-A5EF-C66C94A5B1E4} = {63454657-F25B-4BAB-8868-0766A619BFE6}
{A5BC67F2-0570-4F49-8F15-7DED77E12066} = {63454657-F25B-4BAB-8868-0766A619BFE6}
EndGlobalSection
EndGlobal
Loading

0 comments on commit 4822a32

Please sign in to comment.