Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Consul.Test/ClusterPeeringTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// -----------------------------------------------------------------------
// <copyright file="ClusterPeeringTest.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.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<string, string> { ["env"] = "production" }
};
var clusterPeeringCreateResponse = await _client.ClusterPeering.GenerateToken(clusterPeeringEntry);
Assert.NotNull(clusterPeeringCreateResponse);
Assert.NotNull(clusterPeeringCreateResponse.Response.PeeringToken);
}
}
}
1 change: 1 addition & 0 deletions Consul/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ private void InitializeEndpoints()
#pragma warning restore CS0618 // Type or member is obsolete
_agent = new Lazy<Agent>(() => new Agent(this));
_catalog = new Lazy<Catalog>(() => new Catalog(this));
_clusterPeering = new Lazy<ClusterPeering>(() => new ClusterPeering(this));
_coordinate = new Lazy<Coordinate>(() => new Coordinate(this));
_configuration = new Lazy<Configuration>(() => new Configuration(this));
_event = new Lazy<Event>(() => new Event(this));
Expand Down
88 changes: 88 additions & 0 deletions Consul/ClusterPeering.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// -----------------------------------------------------------------------
// <copyright file="ClusterPeering.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.Threading;
using System.Threading.Tasks;
using Consul.Interfaces;

namespace Consul
{
public class ClusterPeeringTokenEntry
{
public string PeerName { get; set; }
public Dictionary<string, string> Meta { get; set; }
}

public class ClusterPeeringTokenResponse
{
public string PeeringToken { get; set; }
}

/// <summary>
/// ClusterPeering is used to interact with Cluster Peering in Consul through the API
/// </summary>
public class ClusterPeering : IClusterPeeringEndpoint
{
private readonly ConsulClient _client;

internal ClusterPeering(ConsulClient client)
{
_client = client;
}

/// <summary>
/// Generates a Peering Token in Consul
/// </summary>
/// <param name="tokenEntry">The new Cluster Peering Entry</param>
/// <param name="ct">Cancellation token for long poll request. If set, OperationCanceledException will be thrown if the request is cancelled before completing</param>
/// <returns>A write result containing the created ACL AuthMethod</returns>
public Task<WriteResult<ClusterPeeringTokenResponse>> GenerateToken(ClusterPeeringTokenEntry tokenEntry,
CancellationToken ct = default)
{
return GenerateToken(tokenEntry, WriteOptions.Default, ct);
}

/// <summary>
/// Generates a Peering Token in Consul
/// </summary>
/// <param name="tokenEntry">A new Cluster Peering Entry</param>
/// <param name="options"></param>
/// <param name="ct">Cancellation token for long poll request. If set, OperationCanceledException will be thrown if the request is cancelled before completing</param>
/// <returns>A new Binding Rule</returns>
public async Task<WriteResult<ClusterPeeringTokenResponse>> GenerateToken(ClusterPeeringTokenEntry tokenEntry, WriteOptions options,
CancellationToken ct = default)
{
var res = await _client
.Post<ClusterPeeringTokenEntry, ClusterPeeringTokenResponse>("/v1/peering/token", tokenEntry, options).Execute(ct)
.ConfigureAwait(false);
return new WriteResult<ClusterPeeringTokenResponse>(res, res.Response);
}
}

public partial class ConsulClient : IConsulClient
{
private Lazy<ClusterPeering> _clusterPeering;

/// <summary>
/// Cluster Peering returns a handle to the Cluster Peering endpoints
/// </summary>
public IClusterPeeringEndpoint ClusterPeering => _clusterPeering.Value;
}
}
34 changes: 34 additions & 0 deletions Consul/Interfaces/IClusterPeeringEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// -----------------------------------------------------------------------
// <copyright file="IClusterPeering.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.Threading;
using System.Threading.Tasks;

namespace Consul.Interfaces
{
/// <summary>
/// The interface for Cluster Peering API Endpoints
/// </summary>
public interface IClusterPeeringEndpoint
{
Task<WriteResult<ClusterPeeringTokenResponse>> GenerateToken(ClusterPeeringTokenEntry tokenEntry, CancellationToken ct = default);

Task<WriteResult<ClusterPeeringTokenResponse>> GenerateToken(ClusterPeeringTokenEntry tokenEntry, WriteOptions options,
CancellationToken ct = default);
}
}