Skip to content

Commit

Permalink
Added Support for Getting Service Configuration (G-Research#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
sammychinedu2ky committed Dec 1, 2023
1 parent 8b79843 commit fff5394
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Consul.Test/AgentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,5 +1054,36 @@ public async Task Agent_Reload()
System.IO.File.WriteAllText(configFile, initialConfig);
}
}

[SkippableFact]
public async Task Agent_GetServiceConfiguration()
{
var cutOffVersion = SemanticVersion.Parse("1.3.0");
Skip.If(AgentVersion < cutOffVersion, $"Current version is {AgentVersion}, but `Agent_GetServiceConfiguration` is only supported from Consul {cutOffVersion}");
var service = new AgentServiceRegistration
{
Name = "test",
Port = 8000,
Kind = ServiceKind.ConnectProxy,
Proxy = new AgentServiceProxy
{
DestinationServiceName = "test",
DestinationServiceID = "test",
LocalServiceAddress = "127.0.0.1",
LocalServicePort = 8000,
Upstreams = new AgentServiceProxyUpstream[] {
new AgentServiceProxyUpstream {
DestinationName = "db",
LocalBindPort = 8001
}
}
}
};
await _client.Agent.ServiceRegister(service);
var serviceConfiguration2 = await _client.Agent.GetServiceConfiguration("test");
Assert.Equal(serviceConfiguration2.Response.Proxy.DestinationServiceName, service.Proxy.DestinationServiceName);
Assert.Equal(serviceConfiguration2.Response.Proxy.DestinationServiceID, service.Proxy.DestinationServiceID);
Assert.Equal(serviceConfiguration2.Response.Proxy.LocalServiceAddress, service.Proxy.LocalServiceAddress);
}
}
}
88 changes: 88 additions & 0 deletions Consul/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,66 @@ public class AgentCheck
public string Type { get; set; }
}

public class TaggedAddresses
{
public AddressDetails Lan { get; set; }
public AddressDetails Wan { get; set; }
}

public class AddressDetails
{
public string Address { get; set; }
public int Port { get; set; }
}

public class Weights
{
public int Passing { get; set; }
public int Warning { get; set; }
}

public class TransparentProxy
{
public int OutboundListenerPort { get; set; }
}

public class Upstream
{
public string DestinationType { get; set; }
public string DestinationName { get; set; }
public int LocalBindPort { get; set; }
}

public class Proxy
{
public string DestinationServiceName { get; set; }
public string DestinationServiceID { get; set; }
public string LocalServiceAddress { get; set; }
public int LocalServicePort { get; set; }
public string Mode { get; set; }
public TransparentProxy TransparentProxy { get; set; }
public Dictionary<string, string> Config { get; set; }
public List<Upstream> Upstreams { get; set; }
}

public class ServiceConfiguration
{
public string Kind { get; set; }
public string ID { get; set; }
public string Service { get; set; }
public object Tags { get; set; }
public object Meta { get; set; }
public string Namespace { get; set; }
public int Port { get; set; }
public string Address { get; set; }
public TaggedAddresses TaggedAddresses { get; set; }
public Weights Weights { get; set; }
public bool EnableTagOverride { get; set; }
public string Datacenter { get; set; }
public string ContentHash { get; set; }
public Proxy Proxy { get; set; }
}

/// <summary>
/// AgentService represents a service known to the agent
/// </summary>
Expand Down Expand Up @@ -261,6 +321,9 @@ public class AgentServiceRegistration

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public AgentServiceProxy Proxy { get; set; }

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public ServiceKind Kind { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -1042,6 +1105,7 @@ public async Task<QueryResult<AgentHostInfo>> GetAgentHostInfo(CancellationToken
{
return await _client.Get<AgentHostInfo>($"v1/agent/host").Execute(ct).ConfigureAwait(false);
}

/// <summary>
/// GetAgentVersion returns the version of the agent
/// </summary>
Expand All @@ -1051,6 +1115,30 @@ public async Task<QueryResult<AgentVersion>> GetAgentVersion(CancellationToken c
{
return await _client.Get<AgentVersion>("/v1/agent/version").Execute(ct).ConfigureAwait(false);
}

/// <summary>
/// GetServiceConfiguration returns the service definition of a service registered on the local agent
/// </summary>
/// <param name="serviceId">Id of service to fetch</param>
/// <param name="ct">Cancellation Token</param>
/// <returns>Service Configuration</returns>
public async Task<QueryResult<ServiceConfiguration>> GetServiceConfiguration(string serviceId, CancellationToken ct = default)
{
return await GetServiceConfiguration(serviceId, QueryOptions.Default, ct).ConfigureAwait(false);
}

/// <summary>
/// GetServiceConfiguration returns the service definition of a service registered on the local agent
/// </summary>
/// <param name="serviceId">Id of service to fetch</param>
/// <param name="q">Query Options</param>
/// <param name="ct">Cancellation Token</param>
/// <returns>Service Configuration</returns>
public async Task<QueryResult<ServiceConfiguration>> GetServiceConfiguration(string serviceId, QueryOptions q, CancellationToken ct = default)
{
return await _client.Get<ServiceConfiguration>($"/v1/agent/service/{serviceId}", q).Execute(ct).ConfigureAwait(false);
}

/// <summary>
/// Log streamer
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Consul/Interfaces/IAgentEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,7 @@ public interface IAgentEndpoint
Task<WriteResult> Reload(string node, CancellationToken ct = default);
Task<QueryResult<AgentHostInfo>> GetAgentHostInfo(CancellationToken ct = default);
Task<WriteResult> Leave(string node, CancellationToken ct = default);
Task<QueryResult<ServiceConfiguration>> GetServiceConfiguration(string serviceID, QueryOptions q, CancellationToken ct = default);
Task<QueryResult<ServiceConfiguration>> GetServiceConfiguration(string serviceID, CancellationToken ct = default);
}
}

0 comments on commit fff5394

Please sign in to comment.