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/BindingRuleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using NuGet.Versioning;
using Xunit;
Expand Down Expand Up @@ -157,5 +158,48 @@ public async Task BindingRule_Update()
Assert.NotEqual(newBindingRuleResult.Response.Description, updatedBindingRule.Response.Description);
Assert.NotEqual(newBindingRuleResult.Response.BindType, updatedBindingRule.Response.BindType);
}

[Fact]
public async Task BindingRule_Delete()
{
var authMethodEntry = new AuthMethodEntry
{
Name = "AuthMethodApiTest",
Type = "kubernetes",
Description = "Auth Method for API Unit Testing",
Config = new Dictionary<string, string>
{
["Host"] = _host,
["CACert"] = _caCert,
["ServiceAccountJWT"] = _serviceAccountJWT
}
};

await _client.AuthMethod.Create(authMethodEntry);

var authMethodsResponse = await _client.AuthMethod.List();
var existingAuthMethod = authMethodsResponse.Response?.FirstOrDefault();
var bindingRuleEntry = new ACLBindingRule
{
Description = "ACL Binding Rule for API Unit Testing",
AuthMethod = existingAuthMethod.Name,
Selector = "serviceaccount.namespace==default",
BindType = "service",
BindName = "${serviceaccount.name}"
};

var newBindingRuleResult = await _client.BindingRule.Create(bindingRuleEntry);
Assert.NotNull(newBindingRuleResult.Response);
Assert.Equal(bindingRuleEntry.Description, newBindingRuleResult.Response.Description);
Assert.Equal(bindingRuleEntry.AuthMethod, newBindingRuleResult.Response.AuthMethod);
Assert.False(string.IsNullOrEmpty(newBindingRuleResult.Response.ID));

var deleteResult = await _client.BindingRule.Delete(newBindingRuleResult.Response.ID);
Assert.NotNull(deleteResult);
Assert.Equal(HttpStatusCode.OK, deleteResult.StatusCode);

var readBindingRule = await _client.BindingRule.Read(newBindingRuleResult.Response.ID);
Assert.Equal(HttpStatusCode.NotFound, readBindingRule.StatusCode);
}
}
}
24 changes: 24 additions & 0 deletions Consul/BindingRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,30 @@ public async Task<WriteResult<ACLBindingRuleResponse>> Update(ACLBindingRule ent
var res = await _client.Put<ACLBindingRule, ACLBindingRuleResponse>($"/v1/acl/binding-rule/{entry.ID}", entry, options).Execute(ct).ConfigureAwait(false);
return new WriteResult<ACLBindingRuleResponse>(res, res.Response);
}

/// <summary>
/// Deletes an ACL binding rule.
/// </summary>
/// <param name="id">Specifies the UUID of the binding rule you delete</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></returns>
public Task<WriteResult> Delete(string id, CancellationToken ct = default)
{
return Delete(id, WriteOptions.Default, ct);
}

/// <summary>
/// Deletes an ACL binding rule.
/// </summary>
/// <param name="id">Specifies the UUID of the binding rule you delete</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></returns>
public async Task<WriteResult> Delete(string id, WriteOptions options, CancellationToken ct = default)
{
var res = await _client.Delete($"/v1/acl/binding-rule/{id}", options).Execute(ct).ConfigureAwait(false);
return res;
}
}

public partial class ConsulClient : IConsulClient
Expand Down
2 changes: 2 additions & 0 deletions Consul/Interfaces/IBindingRuleEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ Task<WriteResult<ACLBindingRuleResponse>> Create(ACLBindingRule entry, WriteOpti
Task<QueryResult<ACLBindingRuleResponse>> Read(string id, QueryOptions options, CancellationToken ct = default);
Task<WriteResult<ACLBindingRuleResponse>> Update(ACLBindingRule entry, CancellationToken ct = default);
Task<WriteResult<ACLBindingRuleResponse>> Update(ACLBindingRule entry, WriteOptions options, CancellationToken ct = default);
Task<WriteResult> Delete(string id, CancellationToken ct = default);
Task<WriteResult> Delete(string id, WriteOptions options, CancellationToken ct = default);
}
}
Loading