diff --git a/Consul.Test/ClusterPeeringTest.cs b/Consul.Test/ClusterPeeringTest.cs new file mode 100644 index 000000000..c2d7b9934 --- /dev/null +++ b/Consul.Test/ClusterPeeringTest.cs @@ -0,0 +1,44 @@ +// ----------------------------------------------------------------------- +// +// 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. +// +// ----------------------------------------------------------------------- + +using System.Collections.Generic; +using System.Threading.Tasks; +using NuGet.Versioning; +using Xunit; + +namespace Consul.Test +{ + public class ClusterPeeringTest : BaseFixture + { + [SkippableFact] + public async Task ClusterPeeringTest_Create() + { + var cutOffVersion = SemanticVersion.Parse("1.14.0"); + Skip.If(AgentVersion < cutOffVersion, $"Current version is {AgentVersion}, but this test is only supported from Consul {cutOffVersion}"); + + var clusterPeeringEntry = new ClusterPeeringTokenEntry + { + PeerName = "cluster-02", + Meta = new Dictionary { ["env"] = "production" } + }; + var clusterPeeringCreateResponse = await _client.ClusterPeering.GenerateToken(clusterPeeringEntry); + Assert.NotNull(clusterPeeringCreateResponse); + Assert.NotNull(clusterPeeringCreateResponse.Response.PeeringToken); + } + } +} diff --git a/Consul/Client.cs b/Consul/Client.cs index 89fa487d8..59db09313 100644 --- a/Consul/Client.cs +++ b/Consul/Client.cs @@ -457,6 +457,7 @@ private void InitializeEndpoints() #pragma warning restore CS0618 // Type or member is obsolete _agent = new Lazy(() => new Agent(this)); _catalog = new Lazy(() => new Catalog(this)); + _clusterPeering = new Lazy(() => new ClusterPeering(this)); _coordinate = new Lazy(() => new Coordinate(this)); _configuration = new Lazy(() => new Configuration(this)); _event = new Lazy(() => new Event(this)); diff --git a/Consul/ClusterPeering.cs b/Consul/ClusterPeering.cs new file mode 100644 index 000000000..c6bd9c2a2 --- /dev/null +++ b/Consul/ClusterPeering.cs @@ -0,0 +1,88 @@ +// ----------------------------------------------------------------------- +// +// 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. +// +// ----------------------------------------------------------------------- + +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Consul.Interfaces; + +namespace Consul +{ + public class ClusterPeeringTokenEntry + { + public string PeerName { get; set; } + public Dictionary Meta { get; set; } + } + + public class ClusterPeeringTokenResponse + { + public string PeeringToken { get; set; } + } + + /// + /// ClusterPeering is used to interact with Cluster Peering in Consul through the API + /// + public class ClusterPeering : IClusterPeeringEndpoint + { + private readonly ConsulClient _client; + + internal ClusterPeering(ConsulClient client) + { + _client = client; + } + + /// + /// Generates a Peering Token in Consul + /// + /// The new Cluster Peering Entry + /// Cancellation token for long poll request. If set, OperationCanceledException will be thrown if the request is cancelled before completing + /// A write result containing the created ACL AuthMethod + public Task> GenerateToken(ClusterPeeringTokenEntry tokenEntry, + CancellationToken ct = default) + { + return GenerateToken(tokenEntry, WriteOptions.Default, ct); + } + + /// + /// Generates a Peering Token in Consul + /// + /// A new Cluster Peering Entry + /// + /// Cancellation token for long poll request. If set, OperationCanceledException will be thrown if the request is cancelled before completing + /// A new Binding Rule + public async Task> GenerateToken(ClusterPeeringTokenEntry tokenEntry, WriteOptions options, + CancellationToken ct = default) + { + var res = await _client + .Post("/v1/peering/token", tokenEntry, options).Execute(ct) + .ConfigureAwait(false); + return new WriteResult(res, res.Response); + } + } + + public partial class ConsulClient : IConsulClient + { + private Lazy _clusterPeering; + + /// + /// Cluster Peering returns a handle to the Cluster Peering endpoints + /// + public IClusterPeeringEndpoint ClusterPeering => _clusterPeering.Value; + } +} diff --git a/Consul/Interfaces/IClusterPeeringEndpoint.cs b/Consul/Interfaces/IClusterPeeringEndpoint.cs new file mode 100644 index 000000000..427c9a491 --- /dev/null +++ b/Consul/Interfaces/IClusterPeeringEndpoint.cs @@ -0,0 +1,34 @@ +// ----------------------------------------------------------------------- +// +// 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. +// +// ----------------------------------------------------------------------- + +using System.Threading; +using System.Threading.Tasks; + +namespace Consul.Interfaces +{ + /// + /// The interface for Cluster Peering API Endpoints + /// + public interface IClusterPeeringEndpoint + { + Task> GenerateToken(ClusterPeeringTokenEntry tokenEntry, CancellationToken ct = default); + + Task> GenerateToken(ClusterPeeringTokenEntry tokenEntry, WriteOptions options, + CancellationToken ct = default); + } +}