Something went wrong. Try again.
My PaaS for hosting multiple elasticsearch clusters in a Kubernetes environment.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165package sdk_test
import ( "context" "fmt" "net/http" "net/http/httptest" "time"
"level3.git.onstackit.cloud/LEVEL3/gabriel/src/sdk" "level3.git.onstackit.cloud/LEVEL3/gabriel/src/sdk/model")
// Example demonstrates a full lifecycle against a running API:// registering, logging in, provisioning an Elasticsearch instance,// indexing and searching documents, then tearing everything down.func Example() { ctx := context.Background()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") switch r.Method + " " + r.URL.Path { case http.MethodPost + " /v1/auth/register", http.MethodPost + " /v1/auth/login": w.WriteHeader(map[string]int{ http.MethodPost + " /v1/auth/register": http.StatusCreated, http.MethodPost + " /v1/auth/login": http.StatusOK, }[r.Method+" "+r.URL.Path]) _, _ = w.Write([]byte(`{"role":"default","token":"jwt-token-abc"}`)) case http.MethodPost + " /v1/elasticsearch/instances": w.WriteHeader(http.StatusCreated) _, _ = w.Write([]byte(`{"id":"inst-1","name":"my-es-cluster","status":"creating","createdAt":"2024-01-01T00:00:00Z","updatedAt":"2024-01-01T00:00:00Z"}`)) case http.MethodGet + " /v1/elasticsearch/instances": w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte(`[{"id":"inst-1","name":"my-es-cluster","status":"creating","createdAt":"2024-01-01T00:00:00Z","updatedAt":"2024-01-01T00:00:00Z"}]`)) case http.MethodGet + " /v1/elasticsearch/instances/my-es-cluster": w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte(`{"id":"inst-1","name":"my-es-cluster","status":"creating","createdAt":"2024-01-01T00:00:00Z","updatedAt":"2024-01-01T00:00:00Z"}`)) case http.MethodGet + " /v1/elasticsearch/instances/my-es-cluster/logs": w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte(`[{"id":"log-1","instanceId":"inst-1","operation":"create_instance","status":"success","message":"instance created","timestamp":"2024-01-01T00:00:00Z","username":"johndoe"}]`)) case http.MethodPost + " /v1/search/my-es-cluster/docs": w.WriteHeader(http.StatusCreated) _, _ = w.Write([]byte(`{"documentId":"doc-1","index":"articles"}`)) case http.MethodGet + " /v1/search/my-es-cluster/docs": w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte(`[{"documentId":"doc-1","index":"articles","text":"The quick brown fox jumps over the lazy dog"}]`)) case http.MethodGet + " /v1/search/my-es-cluster/indices": w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte(`{"indices":["articles","default"]}`)) case http.MethodDelete + " /v1/search/my-es-cluster/docs/abc123": w.WriteHeader(http.StatusNoContent) case http.MethodDelete + " /v1/elasticsearch/instances/my-es-cluster": w.WriteHeader(http.StatusNoContent) default: w.WriteHeader(http.StatusNotFound) } })) defer srv.Close()
client, err := sdk.NewClient(srv.URL, "") if err != nil { panic(err) }
regRes, err := client.Register(ctx, "johndoe", "s3cureP@ssw0rd", "sunset-rocket-42") if err != nil { panic(err) } fmt.Printf("registered with role %s\n", regRes.Role)
loginRes, err := client.Login(ctx, "johndoe", "s3cureP@ssw0rd") if err != nil { panic(err) } fmt.Printf("logged in as %s\n", loginRes.Role)
authed, err := sdk.NewClient(srv.URL, loginRes.Token) if err != nil { panic(err) }
inst, err := authed.CreateInstance(ctx, "my-es-cluster") if err != nil { panic(err) } fmt.Printf("instance %s is %s\n", inst.Name, inst.Status)
instances, err := authed.ListInstances(ctx) if err != nil { panic(err) } for _, i := range instances { fmt.Printf("- %s (%s)\n", i.Name, i.Status) }
got, err := authed.GetInstance(ctx, "my-es-cluster") if err != nil { panic(err) } fmt.Printf("instance: %s, status: %s\n", got.Name, got.Status)
logs, err := authed.GetInstanceLogs(ctx, "my-es-cluster") if err != nil { panic(err) } for _, entry := range logs { fmt.Printf("[%s] %s by %s: %s\n", entry.Timestamp.Format(time.RFC3339), entry.Operation, entry.Username, entry.Message, ) }
idxRes, err := authed.StoreDocument(ctx, "my-es-cluster", model.IndexDocReq{ Text: "The quick brown fox jumps over the lazy dog", Index: "articles", }) if err != nil { panic(err) } fmt.Printf("indexed as %s in %s\n", idxRes.DocumentID, idxRes.Index)
docs, err := authed.SearchDocuments(ctx, "my-es-cluster", model.SearchQuery{ Query: "quick brown fox", Index: "articles", }) if err != nil { panic(err) } for _, doc := range docs { fmt.Printf("[%s] %s\n", doc.ID, doc.Text) }
indices, err := authed.ListIndices(ctx, "my-es-cluster") if err != nil { panic(err) } fmt.Println(indices.Indices)
err = authed.DeleteDocument(ctx, "my-es-cluster", "abc123", model.DeleteDocReq{Index: "articles"}) if err != nil { panic(err) } fmt.Println("document deleted")
err = authed.DeleteInstance(ctx, "my-es-cluster") if err != nil { panic(err) } fmt.Println("instance deleted") // Output: // registered with role default // logged in as default // instance my-es-cluster is creating // - my-es-cluster (creating) // instance: my-es-cluster, status: creating // [2024-01-01T00:00:00Z] create_instance by johndoe: instance created // indexed as doc-1 in articles // [doc-1] The quick brown fox jumps over the lazy dog // [articles default] // document deleted // instance deleted}