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
46 changes: 46 additions & 0 deletions Consul.Test/BindingRuleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,51 @@ public async Task BindingRule_Delete()
var readBindingRule = await _client.BindingRule.Read(newBindingRuleResult.Response.ID);
Assert.Equal(HttpStatusCode.NotFound, readBindingRule.StatusCode);
}

[Fact]
public async Task BindingRule_List()
{
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 1 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 listResult = await _client.BindingRule.List();
var existingRule = listResult.Response?.FirstOrDefault(r => r.ID == newBindingRuleResult.Response.ID);
Assert.NotEmpty(listResult.Response);
Assert.NotNull(existingRule);

await _client.BindingRule.Delete(existingRule.ID);
var listResult2 = await _client.BindingRule.List();
var existingRule2 = listResult2.Response?.FirstOrDefault(r => r.ID == existingRule.ID);
Assert.Null(existingRule2);
}
}
}
22 changes: 22 additions & 0 deletions Consul/BindingRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,28 @@ public async Task<WriteResult> Delete(string id, WriteOptions options, Cancellat
var res = await _client.Delete($"/v1/acl/binding-rule/{id}", options).Execute(ct).ConfigureAwait(false);
return res;
}

/// <summary>
/// Retrieves a listing of all binding rules
/// </summary>
/// <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 lists of all the ACL binding rules</returns>
public Task<QueryResult<ACLBindingRuleResponse[]>> List(CancellationToken ct = default)
{
return List(QueryOptions.Default, ct);
}

/// <summary>
/// Retrieves a listing of all binding rules
/// </summary>
/// <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 lists of all the ACL binding rules</returns>
public async Task<QueryResult<ACLBindingRuleResponse[]>> List(QueryOptions options, CancellationToken ct = default)
{
var res = await _client.Get<ACLBindingRuleResponse[]>("/v1/acl/binding-rules").Execute(ct).ConfigureAwait(false);
return new QueryResult<ACLBindingRuleResponse[]>(res, res.Response);
}
}

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 @@ -36,5 +36,7 @@ Task<WriteResult<ACLBindingRuleResponse>> Create(ACLBindingRule entry, WriteOpti
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);
Task<QueryResult<ACLBindingRuleResponse[]>> List(CancellationToken ct = default);
Task<QueryResult<ACLBindingRuleResponse[]>> List(QueryOptions options, CancellationToken ct = default);
}
}
Loading