Skip to content

Commit

Permalink
fix(minor): Add CAS parameter to KV endpoint update/delete. (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
ser-0xff authored Oct 1, 2024
1 parent ca18e44 commit d7c56b5
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Sources/ConsulServiceDiscovery/Consul.swift
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,16 @@ public final class Consul: Sendable {
/// - value: value to store
/// - key: specifies the path of the key
/// - datacenter: Specifies the datacenter to query. This will default to the datacenter of the agent being queried.
/// - cas: Specifies to use a Check-And-Set operation.
/// - lockOp: Supply a session ID to use in a lock acquisition or releasing operation.
/// - Returns: EventLoopFuture<Bool> to deliver result
/// [apidoc]: https://developer.hashicorp.com/consul/api-docs/kv#create-update-key
///
public func updateValue(
_ value: String,
forKey key: String,
inDatacenter datacenter: String? = nil,
cas: Int? = nil,
lockOp: LockOp? = nil
) -> EventLoopFuture<Bool> {
var components = URLComponents()
Expand All @@ -323,6 +326,10 @@ public final class Consul: Sendable {
queryItems.append(URLQueryItem(name: "dc", value: datacenter))
}

if let cas {
queryItems.append(URLQueryItem(name: "cas", value: "\(cas)"))
}

if let lockOp {
let queryItem = switch lockOp {
case .acquire(let session): URLQueryItem(name: "acquire", value: session)
Expand Down Expand Up @@ -435,13 +442,15 @@ public final class Consul: Sendable {
/// - Parameters
/// - key: specifies the path of the key
/// - datacenter: Specifies the datacenter to query. This will default to the datacenter of the agent being queried.
/// - cas: Specifies to use a Check-And-Set operation.
/// - recurse: Specifies to delete all keys which have the specified prefix. Without this, only a key with an exact match will be deleted.
/// - Returns: EventLoopFuture<Bool> to deliver result
/// [apidoc]: https://developer.hashicorp.com/consul/api-docs/kv#delete-key
///
public func removeValue(
forKey key: String,
inDatacenter datacenter: String? = nil,
cas: Int? = nil,
recurse: Bool? = nil
) -> EventLoopFuture<Bool> {
var components = URLComponents()
Expand All @@ -452,6 +461,10 @@ public final class Consul: Sendable {
queryItems.append(URLQueryItem(name: "dc", value: datacenter))
}

if let cas {
queryItems.append(URLQueryItem(name: "cas", value: "\(cas)"))
}

if let recurse, recurse {
queryItems.append(URLQueryItem(name: "recurse", value: "true"))
}
Expand Down

0 comments on commit d7c56b5

Please sign in to comment.