Skip to content

Commit

Permalink
run sql
Browse files Browse the repository at this point in the history
  • Loading branch information
strokyl committed Sep 16, 2024
1 parent ec3b0fa commit af51449
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 1 deletion.
20 changes: 20 additions & 0 deletions client/console_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,26 @@ func (client *Client) CreateApplicationInstanceToken(applicationInstanceName, na
}
}

type SqlResult struct {
Header []string `json:"header"`
Row [][]interface{} `json:"row"`
}

func (client *Client) ExecuteSql(maxLine int, sql string) (SqlResult, error) {
var result SqlResult
client.setApiKeyFromEnvIfNeeded()
url := client.baseUrl + "/public/sql/v1/execute"
resp, err := client.client.R().SetBody(sql).SetQueryParam("maxLine", fmt.Sprintf("%d", maxLine)).Post(url)
if err != nil {
return result, err
} else if resp.IsError() {
return result, fmt.Errorf(extractApiError(resp))
} else {
err = json.Unmarshal(resp.Body(), &result)
return result, err
}
}

func (client *Client) DeleteToken(uuid string) error {
client.setApiKeyFromEnvIfNeeded()
url := client.baseUrl + "/token/v1/" + uuid
Expand Down
61 changes: 61 additions & 0 deletions cmd/execute_sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmd

import (
"fmt"
"os"

"text/tabwriter"

"github.com/conduktor/ctl/schema"
"github.com/spf13/cobra"
)

func initSql(kinds schema.KindCatalog) {
_, ok := kinds["IndexedTopic"]
if ok {
numLine := 1
var sqlCmd = &cobra.Command{
Use: "sql",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
sqlResult, err := consoleApiClient().ExecuteSql(numLine, args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "Could not execute SQL: %s\n", err)
os.Exit(1)
}

minwidth := 0
tabwidth := 2
padding := 2
padchar := byte(' ')
flags := uint(0)
writer := tabwriter.NewWriter(os.Stdout, minwidth, tabwidth, padding, padchar, flags)
headerStr := ""
for _, column := range sqlResult.Header {
if headerStr != "" {
headerStr += "\t" + column
} else {
headerStr += column
}
}
fmt.Fprintln(writer, headerStr)
for _, line := range sqlResult.Row {
lineStr := ""
for _, data := range line {
dataStr := fmt.Sprintf("%v", data)
if lineStr != "" {
lineStr += "\t" + dataStr
} else {
lineStr += dataStr
}
}
fmt.Fprintln(writer, lineStr)
}
writer.Flush()
},
}

sqlCmd.Flags().IntVarP(&numLine, "num-line", "n", 100, "Number of line to display")
rootCmd.AddCommand(sqlCmd)
}
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ func init() {
initConsoleMkKind()
initGatewayMkKind()
initPrintKind(kinds)
initSql(kinds)
}
23 changes: 23 additions & 0 deletions docker/initializer.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,28 @@
]
}
}
},
{
"httpRequest": {
"method": "POST",
"path": "/api/public/sql/v1/execute",
"queryStringParameters": {
"maxLine": ["100"]
},
"headers": {
"Authorization": "Bearer yo"
},
"body": "select * from \"julien-cloud_sql_test\""
},
"httpResponse": {
"statusCode": 200,
"body": {"header": [], "row": []},
"headers": {
"Content-Type": [
"application/json"
]
}
}
}

]
7 changes: 7 additions & 0 deletions indexed_topic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: IndexedTopic
metadata:
cluster: julien-cloud
name: sql_test
spec:
retentionTimeInSecond: 360000
2 changes: 1 addition & 1 deletion schema/console-default-schema.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"Application":{"Versions":{"1":{"ListPath":"/public/self-serve/v1/application","Name":"Application","ParentPathParam":[],"ListQueryParamter":{},"Order":8}}},"ApplicationGroup":{"Versions":{"1":{"ListPath":"/public/self-serve/v1/application-group","Name":"ApplicationGroup","ParentPathParam":[],"ListQueryParamter":{},"Order":11}}},"ApplicationInstance":{"Versions":{"1":{"ListPath":"/public/self-serve/v1/application-instance","Name":"ApplicationInstance","ParentPathParam":[],"ListQueryParamter":{"application":{"FlagName":"application","Required":false,"Type":"string"}},"Order":9}}},"ApplicationInstancePermission":{"Versions":{"1":{"ListPath":"/public/self-serve/v1/application-instance-permission","Name":"ApplicationInstancePermission","ParentPathParam":[],"ListQueryParamter":{"filterByApplication":{"FlagName":"application","Required":false,"Type":"string"},"filterByApplicationInstance":{"FlagName":"application-instance","Required":false,"Type":"string"},"filterByGrantedTo":{"FlagName":"granted-to","Required":false,"Type":"string"}},"Order":10}}},"Group":{"Versions":{"2":{"ListPath":"/public/iam/v2/group","Name":"Group","ParentPathParam":[],"ListQueryParamter":{},"Order":1}}},"KafkaCluster":{"Versions":{"2":{"ListPath":"/public/console/v2/kafka-cluster","Name":"KafkaCluster","ParentPathParam":[],"ListQueryParamter":{},"Order":2}}},"KafkaConnectCluster":{"Versions":{"2":{"ListPath":"/public/console/v2/cluster/{cluster}/kafka-connect","Name":"KafkaConnectCluster","ParentPathParam":["cluster"],"ListQueryParamter":{},"Order":3}}},"KsqlDBCluster":{"Versions":{"2":{"ListPath":"/public/console/v2/cluster/{cluster}/ksqldb","Name":"KsqlDBCluster","ParentPathParam":["cluster"],"ListQueryParamter":{},"Order":4}}},"Subject":{"Versions":{"2":{"ListPath":"/public/kafka/v2/cluster/{cluster}/subject","Name":"Subject","ParentPathParam":["cluster"],"ListQueryParamter":{},"Order":6}}},"Topic":{"Versions":{"2":{"ListPath":"/public/kafka/v2/cluster/{cluster}/topic","Name":"Topic","ParentPathParam":["cluster"],"ListQueryParamter":{},"Order":5}}},"TopicPolicy":{"Versions":{"1":{"ListPath":"/public/self-serve/v1/topic-policy","Name":"TopicPolicy","ParentPathParam":[],"ListQueryParamter":{"app-instance":{"FlagName":"application-instance","Required":false,"Type":"string"}},"Order":7}}},"User":{"Versions":{"2":{"ListPath":"/public/iam/v2/user","Name":"User","ParentPathParam":[],"ListQueryParamter":{},"Order":0}}}}
{"Alert":{"Versions":{"2":{"ListPath":"/public/monitoring/v2/cluster/{cluster}/alert","Name":"Alert","ParentPathParam":["cluster"],"ListQueryParamter":{"connect":{"FlagName":"connect","Required":false,"Type":"string"},"connector":{"FlagName":"connector","Required":false,"Type":"string"}},"Order":14}}},"Application":{"Versions":{"1":{"ListPath":"/public/self-serve/v1/application","Name":"Application","ParentPathParam":[],"ListQueryParamter":{},"Order":10}}},"ApplicationGroup":{"Versions":{"1":{"ListPath":"/public/self-serve/v1/application-group","Name":"ApplicationGroup","ParentPathParam":[],"ListQueryParamter":{},"Order":13}}},"ApplicationInstance":{"Versions":{"1":{"ListPath":"/public/self-serve/v1/application-instance","Name":"ApplicationInstance","ParentPathParam":[],"ListQueryParamter":{"application":{"FlagName":"application","Required":false,"Type":"string"}},"Order":11}}},"ApplicationInstancePermission":{"Versions":{"1":{"ListPath":"/public/self-serve/v1/application-instance-permission","Name":"ApplicationInstancePermission","ParentPathParam":[],"ListQueryParamter":{"filterByApplication":{"FlagName":"application","Required":false,"Type":"string"},"filterByApplicationInstance":{"FlagName":"application-instance","Required":false,"Type":"string"},"filterByGrantedTo":{"FlagName":"granted-to","Required":false,"Type":"string"}},"Order":12}}},"Connector":{"Versions":{"2":{"ListPath":"/public/kafka/v2/cluster/{cluster}/connect/{connectCluster}/connector","Name":"Connector","ParentPathParam":["cluster","connectCluster"],"ListQueryParamter":{},"Order":7}}},"Group":{"Versions":{"2":{"ListPath":"/public/iam/v2/group","Name":"Group","ParentPathParam":[],"ListQueryParamter":{},"Order":1}}},"IndexedTopic":{"Versions":{"1":{"ListPath":"/public/sql/v1/cluster/{cluster}/indexed_topic","Name":"IndexedTopic","ParentPathParam":["cluster"],"ListQueryParamter":{},"Order":8}}},"KafkaCluster":{"Versions":{"2":{"ListPath":"/public/console/v2/kafka-cluster","Name":"KafkaCluster","ParentPathParam":[],"ListQueryParamter":{},"Order":2}}},"KafkaConnectCluster":{"Versions":{"2":{"ListPath":"/public/console/v2/cluster/{cluster}/kafka-connect","Name":"KafkaConnectCluster","ParentPathParam":["cluster"],"ListQueryParamter":{},"Order":3}}},"KsqlDBCluster":{"Versions":{"2":{"ListPath":"/public/console/v2/cluster/{cluster}/ksqldb","Name":"KsqlDBCluster","ParentPathParam":["cluster"],"ListQueryParamter":{},"Order":4}}},"Subject":{"Versions":{"2":{"ListPath":"/public/kafka/v2/cluster/{cluster}/subject","Name":"Subject","ParentPathParam":["cluster"],"ListQueryParamter":{},"Order":6}}},"Topic":{"Versions":{"2":{"ListPath":"/public/kafka/v2/cluster/{cluster}/topic","Name":"Topic","ParentPathParam":["cluster"],"ListQueryParamter":{},"Order":5}}},"TopicPolicy":{"Versions":{"1":{"ListPath":"/public/self-serve/v1/topic-policy","Name":"TopicPolicy","ParentPathParam":[],"ListQueryParamter":{"app-instance":{"FlagName":"application-instance","Required":false,"Type":"string"}},"Order":9}}},"User":{"Versions":{"2":{"ListPath":"/public/iam/v2/user","Name":"User","ParentPathParam":[],"ListQueryParamter":{},"Order":0}}}}
1 change: 1 addition & 0 deletions test_final_exec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ main() {
run conduktor token create admin a_admin_token
run conduktor token create application-instance -i=my_app_instance a_admin_token
run conduktor token delete 0-0-0-0-0
run conduktor sql 'select * from "julien-cloud_sql_test"' -n 100

# Gateway
run conduktor apply -f /test_resource_gw.yml
Expand Down

0 comments on commit af51449

Please sign in to comment.