Skip to content

feat: add PingAPI to check status of OSS and Cloud instance #39

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

Merged
merged 6 commits into from
Oct 15, 2021
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
7 changes: 7 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ jobs:
directory: QueryCpu
- check-example:
directory: DeleteData
- check-example:
directory: InfluxDBStatus
- save_cache:
name: Saving Example Cache
key: *cache-example-key
Expand All @@ -260,6 +262,8 @@ jobs:
- Examples/QueryCpu/.build/repositories
- Examples/DeleteData/Package.resolved
- Examples/DeleteData/.build/repositories
- Examples/InfluxDBStatus/Package.resolved
- Examples/InfluxDBStatus/.build/repositories

workflows:
version: 2
Expand All @@ -272,6 +276,9 @@ workflows:
- tests-linux:
name: tests-linux-5.4
swift-image: "swift:5.4"
- tests-linux:
name: tests-linux-5.5
swift-image: "swift:5.5"
- tests-macOS
nightly:
triggers:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 0.8.0 [unreleased]

### Features
1. [#39](https://github.com/influxdata/influxdb-client-swift/pull/39): Add a `PingAPI to check status of OSS and Cloud instance.

### CI
1. [#39](https://github.com/influxdata/influxdb-client-swift/pull/39): Add Swift 5.5 to CI

## 0.7.0 [2021-09-17]

### Features
Expand Down
36 changes: 36 additions & 0 deletions Examples/InfluxDBStatus/.swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
opt_in_rules:
- array_init
- collection_alignment
- contains_over_first_not_nil
- closure_end_indentation
- closure_spacing
- conditional_returns_on_newline
- empty_count
- empty_string
- explicit_init
- explicit_self
- fatal_error_message
- first_where
- implicit_return
- missing_docs
- modifier_order
- multiline_arguments
- multiline_literal_brackets
- multiline_parameters
- operator_usage_whitespace
- pattern_matching_keywords
- redundant_nil_coalescing
- redundant_type_annotation
- sorted_first_last
- sorted_imports
- trailing_closure
- unneeded_parentheses_in_closure_argument
- unused_import
- unused_declaration
- vertical_parameter_alignment_on_call
- vertical_whitespace_closing_braces
- vertical_whitespace_opening_braces

excluded:
- Package.swift
- .build/
20 changes: 20 additions & 0 deletions Examples/InfluxDBStatus/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// swift-tools-version:5.3

import PackageDescription

let package = Package(
name: "InfluxDBStatus",
products: [
.executable(name: "influxdb-status", targets: ["InfluxDBStatus"])
],
dependencies: [
.package(name: "influxdb-client-swift", path: "../../"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.3.0")
],
targets: [
.target(name: "InfluxDBStatus", dependencies: [
.product(name: "InfluxDBSwiftApis", package: "influxdb-client-swift"),
.product(name: "ArgumentParser", package: "swift-argument-parser")
])
]
)
54 changes: 54 additions & 0 deletions Examples/InfluxDBStatus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# InfluxDBStatus

This is an example how to check status of InfluxDB.

## Prerequisites:
- Docker
- Cloned examples:
```bash
git clone git@github.com:influxdata/influxdb-client-swift.git
cd Examples/InfluxDBStatus
```

## Sources:
- [Package.swift](/Examples/InfluxDBStatus/Package.swift)
- [main.swift](/Examples/InfluxDBStatus/Sources/InfluxDBStatus/main.swift)


## How to test:
1. Start InfluxDB:
```bash
docker run --rm \
--name influxdb_v2 \
--detach \
--publish 8086:8086 \
influxdb:latest
```
1. Configure your username, password, organization, bucket and token:
```bash
docker run --rm \
--link influxdb_v2 \
curlimages/curl -s -i -X POST http://influxdb_v2:8086/api/v2/setup \
-H 'accept: application/json' \
-d '{"username": "my-user", "password": "my-password", "org": "my-org", "bucket": "my-bucket", "token": "my-token"}'
```
1. Start SwiftCLI by:
```bash
docker run --rm \
--link influxdb_v2 \
--privileged \
--interactive \
--tty \
--volume $PWD/../..:/client \
--workdir /client/Examples/InfluxDBStatus \
swift:5.3 /bin/bash
```
1. Check status of InfluxDB by:
```bash
swift run influxdb-status --token my-token --url http://influxdb_v2:8086
```
## Expected output

```bash
InfluxDB status: UP, version: Optional("2.0.9"), build: Optional("OSS")
```
47 changes: 47 additions & 0 deletions Examples/InfluxDBStatus/Sources/InfluxDBStatus/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Created by Jakub Bednář on 10/13/2021.
//

import ArgumentParser
import Foundation
import InfluxDBSwift
import InfluxDBSwiftApis

struct InfluxDBStatus: ParsableCommand {
@Option(name: .shortAndLong, help: "Authentication token.")
private var token: String

@Option(name: .shortAndLong, help: "HTTP address of InfluxDB.")
private var url: String

public func run() {
// Initialize Client and API
let client = InfluxDBClient(url: url, token: token)
let api = InfluxDB2API(client: client)

api.pingAPI.getPing { headers, error in
if let error = error {
print("InfluxDB status: DOWN: \(error)")
self.atExit(client: client, error: error)
}
if let headers = headers {
let version = headers["X-Influxdb-Version"]
let build = headers["X-Influxdb-Build"]
print("InfluxDB status: UP, version: \(version), build: \(build)")
atExit(client: client)
}
}

// Wait to end of script
RunLoop.current.run()
}

private func atExit(client: InfluxDBClient, error: InfluxDBClient.InfluxDBError? = nil) {
// Dispose the Client
client.close()
// Exit script
Self.exit(withError: error)
}
}

InfluxDBStatus.main()
1 change: 1 addition & 0 deletions Examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

## Others
- [DeleteData](DeleteData#deletedata) - How to delete data with specified predicate
- [InfluxDBStatus](InfluxDBStatus#influxdbstatus) - This is an example how to check status of InfluxDB

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ The client supports following management API:
| [**BucketsAPI**](https://influxdata.github.io/influxdb-client-swift/Classes/InfluxDB2API/BucketsAPI.html) | https://docs.influxdata.com/influxdb/v2.0/api/#tag/Buckets |
| [**DBRPsAPI**](https://influxdata.github.io/influxdb-client-swift/Classes/InfluxDB2API/DBRPsAPI.html) | https://docs.influxdata.com/influxdb/v2.0/api/#tag/DBRPs |
| [**HealthAPI**](https://influxdata.github.io/influxdb-client-swift/Classes/InfluxDB2API/HealthAPI.html) | https://docs.influxdata.com/influxdb/v2.0/api/#tag/Health |
| [**PingAPI**](https://influxdata.github.io/influxdb-client-swift/Classes/InfluxDB2API/PingAPI.html) | https://docs.influxdata.com/influxdb/v2.0/api/#tag/Ping |
| [**LabelsAPI**](https://influxdata.github.io/influxdb-client-swift/Classes/InfluxDB2API/LabelsAPI.html) | https://docs.influxdata.com/influxdb/v2.0/api/#tag/Labels |
| [**OrganizationsAPI**](https://influxdata.github.io/influxdb-client-swift/Classes/InfluxDB2API/OrganizationsAPI.html) | https://docs.influxdata.com/influxdb/v2.0/api/#tag/Organizations |
| [**ReadyAPI**](https://influxdata.github.io/influxdb-client-swift/Classes/InfluxDB2API/ReadyAPI.html) | https://docs.influxdata.com/influxdb/v2.0/api/#tag/Ready |
Expand Down
4 changes: 4 additions & 0 deletions Scripts/generate-sources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cp -r "${SCRIPT_PATH}"/generated/InfluxDB2/Classes/OpenAPIs/APIs/AuthorizationsA
cp -r "${SCRIPT_PATH}"/generated/InfluxDB2/Classes/OpenAPIs/APIs/BucketsAPI.swift "${SCRIPT_PATH}"/../Sources/InfluxDBSwiftApis/Generated/APIs/
cp -r "${SCRIPT_PATH}"/generated/InfluxDB2/Classes/OpenAPIs/APIs/DBRPsAPI.swift "${SCRIPT_PATH}"/../Sources/InfluxDBSwiftApis/Generated/APIs/
cp -r "${SCRIPT_PATH}"/generated/InfluxDB2/Classes/OpenAPIs/APIs/HealthAPI.swift "${SCRIPT_PATH}"/../Sources/InfluxDBSwiftApis/Generated/APIs/
cp -r "${SCRIPT_PATH}"/generated/InfluxDB2/Classes/OpenAPIs/APIs/PingAPI.swift "${SCRIPT_PATH}"/../Sources/InfluxDBSwiftApis/Generated/APIs/
cp -r "${SCRIPT_PATH}"/generated/InfluxDB2/Classes/OpenAPIs/APIs/LabelsAPI.swift "${SCRIPT_PATH}"/../Sources/InfluxDBSwiftApis/Generated/APIs/
cp -r "${SCRIPT_PATH}"/generated/InfluxDB2/Classes/OpenAPIs/APIs/OrganizationsAPI.swift "${SCRIPT_PATH}"/../Sources/InfluxDBSwiftApis/Generated/APIs/
cp -r "${SCRIPT_PATH}"/generated/InfluxDB2/Classes/OpenAPIs/APIs/ReadyAPI.swift "${SCRIPT_PATH}"/../Sources/InfluxDBSwiftApis/Generated/APIs/
Expand Down Expand Up @@ -138,6 +139,9 @@ cp -r "${SCRIPT_PATH}"/generated/InfluxDB2/Classes/OpenAPIs/SynchronizedDictiona

# post process sources
sed -i 's/Any]>/String]>/' "${SCRIPT_PATH}"/../Sources/InfluxDBSwiftApis/Generated/APIs/UsersAPI.swift
sed -i 's/Void?/[String: String]?/' "${SCRIPT_PATH}"/../Sources/InfluxDBSwiftApis/Generated/APIs/PingAPI.swift
sed -i 's/.success/let .success(response)/' "${SCRIPT_PATH}"/../Sources/InfluxDBSwiftApis/Generated/APIs/PingAPI.swift
sed -i 's/completion(()/completion(response.header/' "${SCRIPT_PATH}"/../Sources/InfluxDBSwiftApis/Generated/APIs/PingAPI.swift
sed -i 's/extern: File? = nil, //' "${SCRIPT_PATH}"/../Sources/InfluxDBSwift/Generated/Models/Query.swift
sed -i '/self.extern = extern/d' "${SCRIPT_PATH}"/../Sources/InfluxDBSwift/Generated/Models/Query.swift
sed -i 's/Any/String/' "${SCRIPT_PATH}"/../Sources/InfluxDBSwift/Generated/Models/Query.swift
Expand Down
110 changes: 110 additions & 0 deletions Sources/InfluxDBSwiftApis/Generated/APIs/PingAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// PingAPI.swift
//
// Generated by openapi-generator
// https://openapi-generator.tech
//

import Foundation
import InfluxDBSwift

extension InfluxDB2API {


public class PingAPI {
private let influxDB2API: InfluxDB2API

public init(influxDB2API: InfluxDB2API) {
self.influxDB2API = influxDB2API
}

/**
Checks the status of InfluxDB instance and version of InfluxDB.

- parameter apiResponseQueue: The queue on which api response is dispatched.
- parameter completion: completion handler to receive the data and the error objects
*/
public func getPing(apiResponseQueue: DispatchQueue? = nil, completion: @escaping (_ data: [String: String]?,_ error: InfluxDBClient.InfluxDBError?) -> Void) {
getPingWithRequestBuilder().execute(apiResponseQueue ?? self.influxDB2API.apiResponseQueue) { result -> Void in
switch result {
case let .success(response):
completion(response.header, nil)
case let .failure(error):
completion(nil, error)
}
}
}

/**
Checks the status of InfluxDB instance and version of InfluxDB.
- GET /ping
- BASIC:
- type: http
- name: BasicAuth
- API Key:
- type: apiKey u=&p= (QUERY)
- name: QuerystringAuth
- BASIC:
- type: http
- name: TokenAuth
- responseHeaders: [X-Influxdb-Build(String), X-Influxdb-Version(Int)]
- returns: RequestBuilder<Void>
*/
internal func getPingWithRequestBuilder() -> RequestBuilder<Void> {
let path = "/ping"
let URLString = influxDB2API.basePath + path
let parameters: [String:Any]? = nil

let url = URLComponents(string: URLString)

let requestBuilder: RequestBuilder<Void> = influxDB2API.requestBuilderFactory.getRequestNonDecodableBuilder(method: "GET", URLString: (url?.string ?? URLString), parameters: parameters, isBody: false, headers: [:], influxDB2API: influxDB2API)

return requestBuilder
}

/**
Checks the status of InfluxDB instance and version of InfluxDB.

- parameter apiResponseQueue: The queue on which api response is dispatched.
- parameter completion: completion handler to receive the data and the error objects
*/
public func headPing(apiResponseQueue: DispatchQueue? = nil, completion: @escaping (_ data: [String: String]?,_ error: InfluxDBClient.InfluxDBError?) -> Void) {
headPingWithRequestBuilder().execute(apiResponseQueue ?? self.influxDB2API.apiResponseQueue) { result -> Void in
switch result {
case let .success(response):
completion(response.header, nil)
case let .failure(error):
completion(nil, error)
}
}
}

/**
Checks the status of InfluxDB instance and version of InfluxDB.
- HEAD /ping
- BASIC:
- type: http
- name: BasicAuth
- API Key:
- type: apiKey u&#x3D;&amp;p&#x3D; (QUERY)
- name: QuerystringAuth
- BASIC:
- type: http
- name: TokenAuth
- responseHeaders: [X-Influxdb-Build(String), X-Influxdb-Version(Int)]
- returns: RequestBuilder<Void>
*/
internal func headPingWithRequestBuilder() -> RequestBuilder<Void> {
let path = "/ping"
let URLString = influxDB2API.basePath + path
let parameters: [String:Any]? = nil

let url = URLComponents(string: URLString)

let requestBuilder: RequestBuilder<Void> = influxDB2API.requestBuilderFactory.getRequestNonDecodableBuilder(method: "HEAD", URLString: (url?.string ?? URLString), parameters: parameters, isBody: false, headers: [:], influxDB2API: influxDB2API)

return requestBuilder
}

}
}
3 changes: 3 additions & 0 deletions Sources/InfluxDBSwiftApis/InfluxDB2API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class InfluxDB2API {
/// Lazy initialized `HealthAPI`.
public lazy var healthAPI: HealthAPI = { HealthAPI(influxDB2API: self) }()

/// Lazy initialized `PingAPI`.
public lazy var pingAPI: PingAPI = { PingAPI(influxDB2API: self) }()

/// Lazy initialized `LabelsAPI`.
public lazy var labelsAPI: LabelsAPI = { LabelsAPI(influxDB2API: self) }()

Expand Down
28 changes: 28 additions & 0 deletions Tests/InfluxDBSwiftApisTests/PingAPITests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Created by Jakub Bednář on 13.10.2021.
//

import InfluxDBSwift
@testable import InfluxDBSwiftApis
import XCTest

final class PingAPITests: APIXCTestCase {
func testPing() {
let expectation = self.expectation(description: "Success response from API doesn't arrive")

api.pingAPI.getPing { headers, error -> Void in
if let error = error {
XCTFail(error.description)
}

if let headers = headers {
XCTAssertTrue(headers["X-Influxdb-Build"] != nil)
XCTAssertTrue(headers["X-Influxdb-Version"] != nil)
}

expectation.fulfill()
}

waitForExpectations(timeout: 5, handler: nil)
}
}