Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

[Feat]: Add `GET` for `/api/connections` endpoint in Ingester (#109)

Patcher99828829acc646
diff --git a/docs/0.1/api-reference/endpoint/connections/get.mdx b/docs/0.1/api-reference/endpoint/connections/get.mdx
new file mode 100644
index 00000000..f3490035
--- /dev/null
+++ b/docs/0.1/api-reference/endpoint/connections/get.mdx
@@ -0,0 +1,6 @@
+---
+title: 'Get Connections'
+openapi: 'GET /api/connections'
+---
+
+This functionality was introduced to the Doku Ingester in version `0.1.2`
\ No newline at end of file
diff --git a/docs/0.1/api-reference/openiapi.yml b/docs/0.1/api-reference/openiapi.yml
index 3247711f..6b743658 100644
--- a/docs/0.1/api-reference/openiapi.yml
+++ b/docs/0.1/api-reference/openiapi.yml
@@ -43,6 +43,26 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/json201Response'
+ get:
+ summary: Get Connections details
+ operationId: getConnection
+ tags:
+ - Connection
+ security:
+ - ApiKeyAuth: []
+ responses:
+ '200':
+ description: Connection details retrieved successfully
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/ConnectionDetailsResponse'
+ '404':
+ description: Unauthorized request
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/json401Response'
/api/keys:
get:
summary: Get a list of API keys
@@ -143,7 +163,32 @@ components:
ApiKey:
type: string
description: API Key for the Observability Platform. Example - `glc*******`
-
+ ConnectionDetailsResponse:
+ type: object
+ properties:
+ connections:
+ type: object
+ properties:
+ apiKey:
+ type: string
+ created_at:
+ type: string
+ format: date-time
+ id:
+ type: string
+ logsUrl:
+ type: string
+ logsUsername:
+ type: string
+ metricsUrl:
+ type: string
+ metricsUsername:
+ type: string
+ platform:
+ type: string
+ status:
+ type: integer
+ example: 200
json201Response:
type: object
required:
diff --git a/docs/latest/api-reference/endpoint/connections/get.mdx b/docs/latest/api-reference/endpoint/connections/get.mdx
new file mode 100644
index 00000000..6e50f6db
--- /dev/null
+++ b/docs/latest/api-reference/endpoint/connections/get.mdx
@@ -0,0 +1,6 @@
+---
+title: 'Get Connection'
+openapi: 'GET /api/connections'
+---
+
+This functionality was introduced to the Doku Ingester in version `0.1.2`
\ No newline at end of file
diff --git a/docs/latest/api-reference/openiapi.yml b/docs/latest/api-reference/openiapi.yml
index 3247711f..6b743658 100644
--- a/docs/latest/api-reference/openiapi.yml
+++ b/docs/latest/api-reference/openiapi.yml
@@ -43,6 +43,26 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/json201Response'
+ get:
+ summary: Get Connections details
+ operationId: getConnection
+ tags:
+ - Connection
+ security:
+ - ApiKeyAuth: []
+ responses:
+ '200':
+ description: Connection details retrieved successfully
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/ConnectionDetailsResponse'
+ '404':
+ description: Unauthorized request
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/json401Response'
/api/keys:
get:
summary: Get a list of API keys
@@ -143,7 +163,32 @@ components:
ApiKey:
type: string
description: API Key for the Observability Platform. Example - `glc*******`
-
+ ConnectionDetailsResponse:
+ type: object
+ properties:
+ connections:
+ type: object
+ properties:
+ apiKey:
+ type: string
+ created_at:
+ type: string
+ format: date-time
+ id:
+ type: string
+ logsUrl:
+ type: string
+ logsUsername:
+ type: string
+ metricsUrl:
+ type: string
+ metricsUsername:
+ type: string
+ platform:
+ type: string
+ status:
+ type: integer
+ example: 200
json201Response:
type: object
required:
diff --git a/docs/mint.json b/docs/mint.json
index 149b9d52..bf51a31d 100644
--- a/docs/mint.json
+++ b/docs/mint.json
@@ -172,7 +172,8 @@
"icon": "link",
"pages": [
"latest/api-reference/endpoint/connections/create",
- "latest/api-reference/endpoint/connections/delete"
+ "latest/api-reference/endpoint/connections/delete",
+ "latest/api-reference/endpoint/connections/get"
],
"version": "latest"
}
@@ -310,7 +311,8 @@
"icon": "link",
"pages": [
"0.1/api-reference/endpoint/connections/create",
- "0.1/api-reference/endpoint/connections/delete"
+ "0.1/api-reference/endpoint/connections/delete",
+ "0.1/api-reference/endpoint/connections/get"
],
"version": "0.1"
}
diff --git a/src/ingester/api/handlers.go b/src/ingester/api/handlers.go
index 682cae1a..1127e8e8 100644
--- a/src/ingester/api/handlers.go
+++ b/src/ingester/api/handlers.go
@@ -163,6 +163,36 @@ func deleteConnectionsHandler(w http.ResponseWriter, r *http.Request) {
sendJSONResponse(w, http.StatusOK, "Connection deleted successfully")
}
+// getConnectionsHandler handles the retrieval of a new Connection
+func getConnectionsHandler(w http.ResponseWriter, r *http.Request) {
+
+ observability, err := db.GetConnection(getAuthKey(r))
+ if err != nil {
+ if err.Error() == "NOTFOUND" {
+ sendJSONResponse(w, http.StatusNotFound, "No existing Connection found")
+ return
+ }
+ if err.Error() == "AUTHFAILED" {
+ sendJSONResponse(w, http.StatusUnauthorized, errMsgAuthFailed)
+ return
+ }
+ sendJSONResponse(w, http.StatusBadRequest, "Error getting connection: "+err.Error())
+ return
+ }
+
+ response := map[string]interface{}{
+ "status": http.StatusOK,
+ "connections": observability,
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK) // Setting the status code here is actually superfluous since `Encode` will write it as well.
+
+ if err := json.NewEncoder(w).Encode(response); err != nil {
+ http.Error(w, "Error encoding JSON", http.StatusInternalServerError)
+ }
+}
+
// generateAPIKeyHandler handles the creation of a new API Key
func generateAPIKeyHandler(w http.ResponseWriter, r *http.Request) {
var request APIKeyRequest
@@ -276,6 +306,8 @@ func APIKeyHandler(w http.ResponseWriter, r *http.Request) {
// ConnectionsHandler handles all 'Connections' tasks recieved on `/api/connections` endpoint.
func ConnectionsHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
+ case "GET":
+ getConnectionsHandler(w, r)
case "POST":
generateConnectionsHandler(w, r)
case "DELETE":
diff --git a/src/ingester/db/db.go b/src/ingester/db/db.go
index 626ed6af..1fd523d9 100644
--- a/src/ingester/db/db.go
+++ b/src/ingester/db/db.go
@@ -553,3 +553,47 @@ func DeleteConnection(existingAPIKey string) error {
return nil
}
+
+// / GetConnection gets the existing connection configuration from the database.
+func GetConnection(existingAPIKey string) (map[string]interface{}, error) {
+ // Authenticate the provided API key before proceeding
+ _, err := CheckAPIKey(existingAPIKey)
+ if err != nil {
+ log.Warn().Msg("Authorization Failed for an API Key")
+ return nil, fmt.Errorf("AUTHFAILED")
+ }
+
+ connectionDetails := map[string]interface{}{}
+
+ // Prepare the ClickHouse SQL query.
+ query := fmt.Sprintf(`SELECT id, platform, metricsUrl, logsUrl, apiKey, metricsUsername, logsUsername, created_at FROM %s ORDER BY id LIMIT 1`, doku_connections_table)
+
+ // QueryRow executes the query and returns at most one row.
+ row := db.QueryRow(ctx, query)
+
+ var id, platform, metricsUrl, logsUrl, apiKey, metricsUsername, logsUsername string
+ var createdAt time.Time
+
+ // Scan the results into variables.
+ err = row.Scan(&id, &platform, &metricsUrl, &logsUrl, &apiKey, &metricsUsername, &logsUsername, &createdAt)
+ if err != nil {
+ if err == sql.ErrNoRows {
+ log.Info().Msg("No configuration found in the database.")
+ return nil, fmt.Errorf("NOTFOUND")
+ }
+ log.Error().Err(err).Msg("Failed to get the connection configuration")
+ return nil, err
+ }
+
+ // Populate the results map.
+ connectionDetails["id"] = id
+ connectionDetails["platform"] = platform
+ connectionDetails["metricsUrl"] = metricsUrl
+ connectionDetails["logsUrl"] = logsUrl
+ connectionDetails["apiKey"] = apiKey
+ connectionDetails["metricsUsername"] = metricsUsername
+ connectionDetails["logsUsername"] = logsUsername
+ connectionDetails["created_at"] = createdAt
+
+ return connectionDetails, nil
+}
diff --git a/src/ingester/main.go b/src/ingester/main.go
index 0f359354..5977deba 100644
--- a/src/ingester/main.go
+++ b/src/ingester/main.go
@@ -84,7 +84,7 @@ func main() {
r.HandleFunc("/api/push", api.DataHandler).Methods("POST")
r.HandleFunc("/api/keys", api.APIKeyHandler).Methods("GET", "POST", "DELETE")
r.HandleFunc("/", api.BaseEndpoint).Methods("GET")
- r.HandleFunc("/api/connections", api.ConnectionsHandler).Methods("POST", "DELETE")
+ r.HandleFunc("/api/connections", api.ConnectionsHandler).Methods("POST", "DELETE", "GET")
// Define and start the HTTP server
server := &http.Server{