diff --git a/Consul.Test/AgentTest.cs b/Consul.Test/AgentTest.cs index ff1cd40f8..2409a26f5 100644 --- a/Consul.Test/AgentTest.cs +++ b/Consul.Test/AgentTest.cs @@ -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); + } } } diff --git a/Consul/Agent.cs b/Consul/Agent.cs index f38f856c4..610ac2fad 100644 --- a/Consul/Agent.cs +++ b/Consul/Agent.cs @@ -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 Config { get; set; } + public List 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; } + } + /// /// AgentService represents a service known to the agent /// @@ -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; } } /// @@ -1042,6 +1105,7 @@ public async Task> GetAgentHostInfo(CancellationToken { return await _client.Get($"v1/agent/host").Execute(ct).ConfigureAwait(false); } + /// /// GetAgentVersion returns the version of the agent /// @@ -1051,6 +1115,30 @@ public async Task> GetAgentVersion(CancellationToken c { return await _client.Get("/v1/agent/version").Execute(ct).ConfigureAwait(false); } + + /// + /// GetServiceConfiguration returns the service definition of a service registered on the local agent + /// + /// Id of service to fetch + /// Cancellation Token + /// Service Configuration + public async Task> GetServiceConfiguration(string serviceId, CancellationToken ct = default) + { + return await GetServiceConfiguration(serviceId, QueryOptions.Default, ct).ConfigureAwait(false); + } + + /// + /// GetServiceConfiguration returns the service definition of a service registered on the local agent + /// + /// Id of service to fetch + /// Query Options + /// Cancellation Token + /// Service Configuration + public async Task> GetServiceConfiguration(string serviceId, QueryOptions q, CancellationToken ct = default) + { + return await _client.Get($"/v1/agent/service/{serviceId}", q).Execute(ct).ConfigureAwait(false); + } + /// /// Log streamer /// diff --git a/Consul/Interfaces/IAgentEndpoint.cs b/Consul/Interfaces/IAgentEndpoint.cs index 6b41faf42..38da97bbf 100644 --- a/Consul/Interfaces/IAgentEndpoint.cs +++ b/Consul/Interfaces/IAgentEndpoint.cs @@ -69,5 +69,7 @@ public interface IAgentEndpoint Task Reload(string node, CancellationToken ct = default); Task> GetAgentHostInfo(CancellationToken ct = default); Task Leave(string node, CancellationToken ct = default); + Task> GetServiceConfiguration(string serviceID, QueryOptions q, CancellationToken ct = default); + Task> GetServiceConfiguration(string serviceID, CancellationToken ct = default); } }