Skip to content

Commit

Permalink
add support for discovery chain get method (G-Research#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
larrytamnjong committed Oct 8, 2024
1 parent 169ee86 commit f23e782
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 0 deletions.
1 change: 1 addition & 0 deletions Consul/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ private void InitializeEndpoints()
_aclReplication = new Lazy<ACLReplication>(() => new ACLReplication(this));
_authMethod = new Lazy<AuthMethod>(() => new AuthMethod(this));
_namespaces = new Lazy<Namespaces>(() => new Namespaces(this));
_discoveryChain = new Lazy<DiscoveryChain>(() => new DiscoveryChain(this));
}

#region IDisposable Support
Expand Down
169 changes: 169 additions & 0 deletions Consul/DiscoveryChain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// -----------------------------------------------------------------------
// <copyright file="DiscoveryChain.cs" company="G-Research Limited">
// Copyright 2020 G-Research Limited
//
// Licensed under the Apache License, Version 2.0 (the "License"),
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Consul.Interfaces;
using Newtonsoft.Json;

namespace Consul
{
public class DiscoveryChainResponse
{
public CompiledDiscoveryChain Chain { get; set; }
}

public class CompiledDiscoveryChain
{
public string ServiceName { get; set; }
public string Namespace { get; set; }
public string Datacenter { get; set; }

// CustomizationHash is a unique hash of any data that affects the
// compilation of the discovery chain other than config entries or the
// name/namespace/datacenter evaluation criteria.
//
// If set, this value should be used to prefix/suffix any generated load
// balancer data plane objects to avoid sharing customized and
// non-customized versions.
public string CustomizationHash { get; set; }

// Protocol is the overall protocol shared by everything in the chain.
public string Protocol { get; set; }
// StartNode is the first key into the Nodes map that should be followed
// when walking the discovery chain.
public string StartNode { get; set; }

// Nodes contains all nodes available for traversal in the chain keyed by a
// unique name. You can walk this by starting with StartNode.
//
// NOTE: The names should be treated as opaque values and are only
// guaranteed to be consistent within a single compilation.
public Dictionary<string, DiscoveryGraphNode> Nodes { get; set; }

// Targets is a list of all targets used in this chain.
// NOTE: The names should be treated as opaque values and are only
// guaranteed to be consistent within a single compilation.
public Dictionary<string, DiscoveryTarget> Targets { get; set; }
}

public class DiscoveryGraphNode
{
public string Type { get; set; }
public string Name { get; set; } // This is NOT necessarily a service

// fields for Type==router
public List<DiscoveryRoute> Routes { get; set; }

// fields for Type==splitter
public List<DiscoverySplit> Splits { get; set; }

// fields for Type==resolver
public DiscoveryResolver Resolver { get; set; }

// shared by Type==resolver || Type==splitter
public LoadBalancerConfig LoadBalancer { get; set; }
}

public class DiscoverySplit
{
public float Weight { get; set; }
public string NextNode { get; set; }
}

public class DiscoveryRoute
{
public Routes Definition { get; set; }
public string NextNode { get; set; }
}

public class DiscoveryResolver
{
public bool Default { get; set; }

[JsonConverter(typeof(DurationTimespanConverter))]
public TimeSpan? ConnectTimeout { get; set; }

public string Target { get; set; }
public DiscoveryFailover Failover { get; set; }
}
public class DiscoveryFailover
{
public List<string> Targets { get; set; }
}

public class DiscoveryTarget
{
public string ID { get; set; }
public string Service { get; set; }
public string ServiceSubset { get; set; }
public string Namespace { get; set; }
public string Datacenter { get; set; }
public MeshGatewayConfig MeshGateway { get; set; }
public ServiceResolverEntry Subset { get; set; }
public bool External { get; set; }
public string SNI { get; set; }
public string Name { get; set; }
}

public class DiscoveryChain : IDiscoveryChainEndpoint
{
private readonly ConsulClient _client;

internal DiscoveryChain(ConsulClient c)
{
_client = c;
}

/// <summary>
/// Get is used to return the compiled discovery chain for a service.
/// </summary>
/// <param name="name">Specifies the service to query when compiling the discovery chain</param>
/// <param name="q">Query Options</param>
/// <param name="ct">Cancellation Token</param>
/// <returns>An empty write result</returns>
public Task<QueryResult<DiscoveryChainResponse>> Get(string name, QueryOptions q, CancellationToken ct = default)
{
return _client.Get<DiscoveryChainResponse>($"/v1/discovery-chain/{name}", q).Execute(ct);
}

/// <summary>
/// Get is used to return the compiled discovery chain for a service.
/// </summary>
/// <param name="name">Specifies the service to query when compiling the discovery chain</param>
/// <param name="ct">Cancellation Token</param>
/// <returns>An empty write result</returns>
public Task<QueryResult<DiscoveryChainResponse>> Get(string name, CancellationToken ct = default)
{
return Get(name, QueryOptions.Default, ct);
}
}

public partial class ConsulClient : IConsulClient
{
private Lazy<DiscoveryChain> _discoveryChain;

/// <summary>
/// DiscoveryChain returns a handle to the discovery chain endpoints
/// </summary>
public IDiscoveryChainEndpoint DiscoveryChain => _discoveryChain.Value;
}
}
1 change: 1 addition & 0 deletions Consul/Interfaces/IConsulClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ public interface IConsulClient : IDisposable
IPreparedQueryEndpoint PreparedQuery { get; }
ICoordinateEndpoint Coordinate { get; }
ISnapshotEndpoint Snapshot { get; }
IDiscoveryChainEndpoint DiscoveryChain { get; }
}
}
35 changes: 35 additions & 0 deletions Consul/Interfaces/IDiscoveryChainEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// -----------------------------------------------------------------------
// <copyright file="IDiscoveryChainEndpoint.cs" company="G-Research Limited">
// Copyright 2020 G-Research Limited
//
// Licensed under the Apache License, Version 2.0 (the "License"),
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Consul.Interfaces
{
/// <summary>
/// The interface for the Discovery Chain API Endpoints
/// </summary>
public interface IDiscoveryChainEndpoint
{
Task<QueryResult<DiscoveryChainResponse>> Get(string name, QueryOptions q, CancellationToken ct = default);
Task<QueryResult<DiscoveryChainResponse>> Get(string name, CancellationToken ct = default);
}
}

0 comments on commit f23e782

Please sign in to comment.