Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add admin partition create API support (#379) #380

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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/AdminPartitionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// -----------------------------------------------------------------------
// <copyright file="AdminPartitionTest.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.Linq;
using System.Text;
using System.Threading.Tasks;
using NuGet.Versioning;
using Xunit;

namespace Consul.Test
{
public class AdminPartitionTest : BaseFixture
{
[EnterpriseOnlyFact]
public async Task AdminPartition_CreateAdminPartition()
{
var cutOffVersion = SemanticVersion.Parse("1.11.0");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, as for now we will not be able to run enterprise tests for versions 1.10+, so these test is going to be skipped, e.g.:

[xUnit.net 00:00:20.03]     Consul.Test.AdminPartitionTest.AdminPartition_CreateAdminPartition [SKIP]
[xUnit.net 00:00:20.03]       Skipped; this test requires a consul enterprise server to run.

We run enterprise tests only for versions before 1.10 -> https://github.com/G-Research/consuldotnet/blob/master/.github/workflows/ci.yml#L82-L88

I'm afraid we will not be able accept this PR because we are unable to test it at the moment.

Copy link
Contributor Author

@larrytamnjong larrytamnjong Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcin-krystianc makes sense, thanks. Please take a look at this other PR #382 when you have time.

Skip.If(AgentVersion < cutOffVersion, $"Current version is {AgentVersion}, but `Admin Partition` is only supported from Consul {cutOffVersion}");

var check = new Partition { Name = "na-west", Description = "Partition for North America West" };

var request = await _client.AdminPartition.Create(check);

Assert.Equal(check.Name, request.Response.Name);
}
}
}
88 changes: 88 additions & 0 deletions Consul/AdminPartition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// -----------------------------------------------------------------------
// <copyright file="Partition.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;
using Consul.Interfaces;

namespace Consul
{
/// <summary>
/// Partition is the configuration of a single admin partition. Admin Partitions are a Consul Enterprise feature.
/// </summary>
public class Partition
{
/// <summary>
/// // Name is the name of the Partition.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Description is where the user puts any information they want
/// about the admin partition. It is not used internally.
/// </summary>
public string Description { get; set; }
}

public class PartitionResponse : Partition
{
/// <summary>
/// DeletedAt is the time when the Partition was marked for deletion
/// This is nullable so that we can omit if empty when encoding in JSON
/// </summary>
public DateTime DeletedAt { get; set; }
/// <summary>
/// CreateIndex is the Raft index at which the Partition was created
/// </summary>
public ulong CreateIndex { get; set; }
/// <summary>
/// ModifyIndex is the latest Raft index at which the Partition was modified.
/// </summary>
public ulong ModifyIndex { get; set; }
}
public class AdminPartition : IAdminPartitionEndpoint
{
private readonly ConsulClient _client;
internal AdminPartition(ConsulClient c)
{
_client = c;
}
public async Task<WriteResult<PartitionResponse>> Create(Partition p, CancellationToken ct = default)
{
return await Create(p, WriteOptions.Default, ct).ConfigureAwait(false);
}
public async Task<WriteResult<PartitionResponse>> Create(Partition p, WriteOptions q, CancellationToken ct = default)
{
var res = await _client.Put<Partition, PartitionResponse>("/v1/partition", p, q).Execute(ct).ConfigureAwait(false);

return new WriteResult<PartitionResponse>(res, res.Response);
}
}

public partial class ConsulClient : IConsulClient
{
private Lazy<AdminPartition> _adminPartition;

/// <summary>
/// Partition returns a handle to the Partition endpoints
/// </summary>
public IAdminPartitionEndpoint AdminPartition => _adminPartition.Value;
}
}
1 change: 1 addition & 0 deletions Consul/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ private void InitializeEndpoints()
_authMethod = new Lazy<AuthMethod>(() => new AuthMethod(this));
_namespaces = new Lazy<Namespaces>(() => new Namespaces(this));
_discoveryChain = new Lazy<DiscoveryChain>(() => new DiscoveryChain(this));
_adminPartition = new Lazy<AdminPartition>(() => new AdminPartition(this));
}

#region IDisposable Support
Expand Down
35 changes: 35 additions & 0 deletions Consul/Interfaces/IAdminPartitionEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// -----------------------------------------------------------------------
// <copyright file="IPartition.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 Admin Partition API Endpoints
/// </summary>
public interface IAdminPartitionEndpoint
{
Task<WriteResult<PartitionResponse>> Create(Partition p, WriteOptions q, CancellationToken ct = default);
Task<WriteResult<PartitionResponse>> Create(Partition p, CancellationToken ct = default);
}
}
1 change: 1 addition & 0 deletions Consul/Interfaces/IConsulClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ public interface IConsulClient : IDisposable
ICoordinateEndpoint Coordinate { get; }
ISnapshotEndpoint Snapshot { get; }
IDiscoveryChainEndpoint DiscoveryChain { get; }
IAdminPartitionEndpoint AdminPartition { get; }
}
}
Loading