From 8d978de81863718a1c016a7f09159e2c8485d117 Mon Sep 17 00:00:00 2001 From: Aamir Tiwari Date: Wed, 16 Oct 2024 15:34:46 +0530 Subject: [PATCH] Add support for getExperimentRun and podLog graphql queries Signed-off-by: Aamir Tiwari --- pkg/apis/experiment/experiment.go | 100 +++++++++++++++++++++++++++++- pkg/apis/experiment/query.go | 30 +++++++++ pkg/apis/experiment/types.go | 57 ++++++++++++++++- 3 files changed, 185 insertions(+), 2 deletions(-) diff --git a/pkg/apis/experiment/experiment.go b/pkg/apis/experiment/experiment.go index 57352408..3343f165 100644 --- a/pkg/apis/experiment/experiment.go +++ b/pkg/apis/experiment/experiment.go @@ -248,7 +248,7 @@ func GetExperimentList(pid string, in model.ListExperimentRequest, cred types.Cr // GetExperimentRunsList sends GraphQL API request for fetching a list of experiment runs. func GetExperimentRunsList(pid string, in model.ListExperimentRunRequest, cred types.Credentials) (ExperimentRunListData, error) { - var gqlReq GetChaosExperimentRunGraphQLRequest + var gqlReq GetChaosExperimentRunsGraphQLRequest var err error gqlReq.Query = ListExperimentRunsQuery @@ -295,6 +295,56 @@ func GetExperimentRunsList(pid string, in model.ListExperimentRunRequest, cred t } } +// GetExperimentRun sends GraphQL API request for fetching an experiment run. +func GetExperimentRun(pid string, nid string, cred types.Credentials) (ExperimentRunData, error) { + + var gqlReq GetChaosExperimentRunGraphQLRequest + var err error + + gqlReq.Query = ExperimentRunsQuery + gqlReq.Variables.ProjectID = pid + gqlReq.Variables.NotifyID = nid + + query, err := json.Marshal(gqlReq) + if err != nil { + return ExperimentRunData{}, err + } + + resp, err := apis.SendRequest( + apis.SendRequestParams{ + Endpoint: cred.ServerEndpoint + utils.GQLAPIPath, + Token: cred.Token, + }, + query, + string(types.Post), + ) + if err != nil { + return ExperimentRunData{}, err + } + + bodyBytes, err := io.ReadAll(resp.Body) + defer resp.Body.Close() + if err != nil { + return ExperimentRunData{}, err + } + + if resp.StatusCode == http.StatusOK { + var experimentRun ExperimentRunData + err = json.Unmarshal(bodyBytes, &experimentRun) + if err != nil { + return ExperimentRunData{}, err + } + + if len(experimentRun.Errors) > 0 { + return ExperimentRunData{}, errors.New(experimentRun.Errors[0].Message) + } + + return experimentRun, nil + } else { + return ExperimentRunData{}, errors.New("error while fetching the Chaos Experiment run") + } +} + // DeleteChaosExperiment sends GraphQL API request for deleting a given Chaos Experiment. func DeleteChaosExperiment(projectID string, experimentID *string, cred types.Credentials) (DeleteChaosExperimentData, error) { @@ -346,3 +396,51 @@ func DeleteChaosExperiment(projectID string, experimentID *string, cred types.Cr return DeleteChaosExperimentData{}, errors.New("Error while deleting the Chaos Experiment") } } + +// GetPodLogs sends GraphQL API request for fetching logs for a given Chaos Experiment. +func GetPodLogs(podLogReq PodLogRequest, cred types.Credentials) (PodLogData, error) { + + var gqlReq GetPodLogsGraphQLRequest + var err error + + gqlReq.Query = GetPodLogsQuery + gqlReq.Variables.Request = podLogReq + + query, err := json.Marshal(gqlReq) + if err != nil { + return PodLogData{}, err + } + + resp, err := apis.SendRequest( + apis.SendRequestParams{ + Endpoint: cred.ServerEndpoint + utils.GQLAPIPath, + Token: cred.Token, + }, + query, + string(types.Post), + ) + if err != nil { + return PodLogData{}, err + } + bodyBytes, err := io.ReadAll(resp.Body) + defer resp.Body.Close() + if err != nil { + return PodLogData{}, err + } + + if resp.StatusCode == http.StatusOK { + var podLogData PodLogData + err = json.Unmarshal(bodyBytes, &podLogData) + if err != nil { + return PodLogData{}, err + } + + if len(podLogData.Errors) > 0 { + return PodLogData{}, errors.New(podLogData.Errors[0].Message) + } + + return podLogData, nil + } else { + return PodLogData{}, errors.New("error while fetching logs") + } +} diff --git a/pkg/apis/experiment/query.go b/pkg/apis/experiment/query.go index e629e74a..e8bc5c89 100644 --- a/pkg/apis/experiment/query.go +++ b/pkg/apis/experiment/query.go @@ -44,6 +44,30 @@ const ( } } }` + + ExperimentRunsQuery = `query getExperimentRun($projectID: ID!, $experimentRunID: ID, $notifyID: ID) { + getExperimentRun( + projectID: $projectID + experimentRunID: $experimentRunID + notifyID: $notifyID + ) + { + experimentRunID + experimentID + experimentName + infra { + name + infraID + } + updatedAt + updatedBy{ + username + } + phase + resiliencyScore + executionData + } + }` DeleteExperimentQuery = `mutation deleteChaosExperiment($projectID: ID!, $experimentID: String!, $experimentRunID: String) { deleteChaosExperiment( projectID: $projectID @@ -51,4 +75,10 @@ const ( experimentRunID: $experimentRunID ) }` + GetPodLogsQuery = `subscription podLog($request: PodLogRequest!) { + getPodLog(request: $request) { + log + __typename + } + }` ) diff --git a/pkg/apis/experiment/types.go b/pkg/apis/experiment/types.go index 39a6ddfb..f88c82cf 100644 --- a/pkg/apis/experiment/types.go +++ b/pkg/apis/experiment/types.go @@ -54,6 +54,18 @@ type GetChaosExperimentsGraphQLRequest struct { } `json:"variables"` } +type ExperimentRunData struct { + Errors []struct { + Message string `json:"message"` + Path []string `json:"path"` + } `json:"errors"` + Data ExperimentRun `json:"data"` +} + +type ExperimentRun struct { + ExperimentRunDetails model.ExperimentRun `json:"getExperimentRun"` +} + type ExperimentRunListData struct { Errors []struct { Message string `json:"message"` @@ -66,7 +78,7 @@ type ExperimentRunsList struct { ListExperimentRunDetails model.ListExperimentRunResponse `json:"listExperimentRun"` } -type GetChaosExperimentRunGraphQLRequest struct { +type GetChaosExperimentRunsGraphQLRequest struct { Query string `json:"query"` Variables struct { ProjectID string `json:"projectID"` @@ -74,6 +86,14 @@ type GetChaosExperimentRunGraphQLRequest struct { } `json:"variables"` } +type GetChaosExperimentRunGraphQLRequest struct { + Query string `json:"query"` + Variables struct { + ProjectID string `json:"projectID"` + NotifyID string `json:"notifyID"` + } `json:"variables"` +} + type DeleteChaosExperimentData struct { Errors []struct { Message string `json:"message"` @@ -94,3 +114,38 @@ type DeleteChaosExperimentGraphQLRequest struct { ExperimentRunID *string `json:"experimentRunID"` } `json:"variables"` } + +type PodLogResponse struct { + Log string `json:"log"` + Typename string `json:"__typename"` +} + +type PodLogDetails struct { + GetPodLog PodLogResponse `json:"getPodLog"` +} + +type PodLogData struct { + Errors []struct { + Message string `json:"message"` + Path []string `json:"path"` + } `json:"errors"` + Data PodLogDetails `json:"data"` +} + +// Define the PodLogRequest structure +type PodLogRequest struct { + InfraID string `json:"infraID"` + ExperimentRunID string `json:"experimentRunID"` + PodName string `json:"podName"` + PodNamespace string `json:"podNamespace"` + PodType string `json:"podType"` + RunnerPod string `json:"runnerPod"` + ChaosNamespace string `json:"chaosNamespace"` +} + +type GetPodLogsGraphQLRequest struct { + Query string `json:"query"` + Variables struct { + Request PodLogRequest `json:"request"` + } `json:"variables"` +}