- Notifications
You must be signed in to change notification settings - Fork3
chore: add Vpn.Service app for Manager#9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
26 changes: 25 additions & 1 deletionCoder.Desktop.sln
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletionsCoder.Desktop.sln.DotSettings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletionsCoderSdk/CoderApiClient.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
namespace CoderSdk; | ||
/// <summary> | ||
/// Changes names from PascalCase to snake_case. | ||
/// </summary> | ||
internal class SnakeCaseNamingPolicy : JsonNamingPolicy | ||
{ | ||
public override string ConvertName(string name) | ||
{ | ||
return string.Concat( | ||
name.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + char.ToLower(x) : char.ToLower(x).ToString()) | ||
); | ||
} | ||
} | ||
/// <summary> | ||
/// Provides a limited selection of API methods for a Coder instance. | ||
/// </summary> | ||
public partial class CoderApiClient | ||
{ | ||
// TODO: allow adding headers | ||
private readonly HttpClient _httpClient = new(); | ||
private readonly JsonSerializerOptions _jsonOptions; | ||
public CoderApiClient(string baseUrl) | ||
{ | ||
var url = new Uri(baseUrl, UriKind.Absolute); | ||
if (url.PathAndQuery != "/") | ||
throw new ArgumentException($"Base URL '{baseUrl}' must not contain a path", nameof(baseUrl)); | ||
_httpClient.BaseAddress = url; | ||
_jsonOptions = new JsonSerializerOptions | ||
{ | ||
PropertyNameCaseInsensitive = true, | ||
PropertyNamingPolicy = new SnakeCaseNamingPolicy(), | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, | ||
}; | ||
} | ||
public CoderApiClient(string baseUrl, string token) : this(baseUrl) | ||
{ | ||
SetSessionToken(token); | ||
} | ||
public void SetSessionToken(string token) | ||
{ | ||
_httpClient.DefaultRequestHeaders.Remove("Coder-Session-Token"); | ||
_httpClient.DefaultRequestHeaders.Add("Coder-Session-Token", token); | ||
} | ||
private async Task<TResponse> SendRequestAsync<TResponse>(HttpMethod method, string path, | ||
object? payload, CancellationToken ct = default) | ||
{ | ||
try | ||
{ | ||
var request = new HttpRequestMessage(method, path); | ||
if (payload is not null) | ||
{ | ||
var json = JsonSerializer.Serialize(payload, _jsonOptions); | ||
request.Content = new StringContent(json, Encoding.UTF8, "application/json"); | ||
} | ||
var res = await _httpClient.SendAsync(request, ct); | ||
// TODO: this should be improved to try and parse a codersdk.Error response | ||
res.EnsureSuccessStatusCode(); | ||
var content = await res.Content.ReadAsStringAsync(ct); | ||
var data = JsonSerializer.Deserialize<TResponse>(content, _jsonOptions); | ||
if (data is null) throw new JsonException("Deserialized response is null"); | ||
return data; | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new Exception($"API Request: {method} {path} (req body: {payload is not null})", e); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletionsCoderSdk/CoderSdk.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
</Project> |
22 changes: 22 additions & 0 deletionsCoderSdk/Deployment.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace CoderSdk; | ||
public class BuildInfo | ||
{ | ||
public string ExternalUrl { get; set; } = ""; | ||
public string Version { get; set; } = ""; | ||
public string DashboardUrl { get; set; } = ""; | ||
public bool Telemetry { get; set; } = false; | ||
public bool WorkspaceProxy { get; set; } = false; | ||
public string AgentApiVersion { get; set; } = ""; | ||
public string ProvisionerApiVersion { get; set; } = ""; | ||
public string UpgradeMessage { get; set; } = ""; | ||
public string DeploymentId { get; set; } = ""; | ||
} | ||
public partial class CoderApiClient | ||
{ | ||
public Task<BuildInfo> GetBuildInfo(CancellationToken ct = default) | ||
{ | ||
return SendRequestAsync<BuildInfo>(HttpMethod.Get, "/api/v2/buildinfo", null, ct); | ||
} | ||
} |
17 changes: 17 additions & 0 deletionsCoderSdk/Users.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace CoderSdk; | ||
public class User | ||
{ | ||
public const string Me = "me"; | ||
// TODO: fill out more fields | ||
public string Username { get; set; } = ""; | ||
} | ||
public partial class CoderApiClient | ||
{ | ||
public Task<User> GetUser(string user, CancellationToken ct = default) | ||
{ | ||
return SendRequestAsync<User>(HttpMethod.Get, $"/api/v2/users/{user}", null, ct); | ||
} | ||
} |
9 changes: 5 additions & 4 deletionsTests/Vpn.Proto/RpcHeaderTest.cs → Tests.Vpn.Proto/RpcHeaderTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletionsTests.Vpn.Proto/RpcMessageTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Coder.Desktop.Vpn.Proto; | ||
namespace Coder.Desktop.Tests.Vpn.Proto; | ||
[TestFixture] | ||
public class RpcRoleAttributeTest | ||
{ | ||
[Test] | ||
public void Ok() | ||
{ | ||
var role = new RpcRoleAttribute("manager"); | ||
Assert.That(role.Role, Is.EqualTo("manager")); | ||
role = new RpcRoleAttribute("tunnel"); | ||
Assert.That(role.Role, Is.EqualTo("tunnel")); | ||
role = new RpcRoleAttribute("service"); | ||
Assert.That(role.Role, Is.EqualTo("service")); | ||
role = new RpcRoleAttribute("client"); | ||
Assert.That(role.Role, Is.EqualTo("client")); | ||
} | ||
} | ||
[TestFixture] | ||
public class RpcMessageTest | ||
{ | ||
[Test] | ||
public void GetRole() | ||
{ | ||
// RpcMessage<RPC> is not a supported message type and doesn't have an | ||
// RpcRoleAttribute | ||
var ex = Assert.Throws<ArgumentException>(() => _ = RpcMessage<RPC>.GetRole()); | ||
Assert.That(ex.Message, | ||
Does.Contain("Message type 'Coder.Desktop.Vpn.Proto.RPC' does not have a RpcRoleAttribute")); | ||
Assert.That(ManagerMessage.GetRole(), Is.EqualTo("manager")); | ||
Assert.That(TunnelMessage.GetRole(), Is.EqualTo("tunnel")); | ||
Assert.That(ServiceMessage.GetRole(), Is.EqualTo("service")); | ||
Assert.That(ClientMessage.GetRole(), Is.EqualTo("client")); | ||
} | ||
} |
File renamed without changes.
35 changes: 35 additions & 0 deletionsTests.Vpn.Proto/Tests.Vpn.Proto.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<RootNamespace>Coder.Desktop.Tests.Vpn.Proto</RootNamespace> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/> | ||
<PackageReference Include="NUnit" Version="4.2.2"/> | ||
<PackageReference Include="NUnit.Analyzers" Version="4.4.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Using Include="NUnit.Framework"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Vpn.Proto\Vpn.Proto.csproj"/> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.