Skip to content

Commit

Permalink
Add support for getExperimentRun and podLog graphql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
aamir-tiwari-sumo committed Oct 16, 2024
1 parent 32f3232 commit 0015c81
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 2 deletions.
100 changes: 99 additions & 1 deletion pkg/apis/experiment/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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")
}
}
30 changes: 30 additions & 0 deletions pkg/apis/experiment/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,41 @@ 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
experimentID: $experimentID
experimentRunID: $experimentRunID
)
}`
GetPodLogsQuery = `subscription podLog($request: PodLogRequest!) {
getPodLog(request: $request) {
log
__typename
}
}`
)
57 changes: 56 additions & 1 deletion pkg/apis/experiment/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -66,14 +78,22 @@ type ExperimentRunsList struct {
ListExperimentRunDetails model.ListExperimentRunResponse `json:"listExperimentRun"`
}

type GetChaosExperimentRunGraphQLRequest struct {
type GetChaosExperimentRunsGraphQLRequest struct {
Query string `json:"query"`
Variables struct {
ProjectID string `json:"projectID"`
GetChaosExperimentRunRequest model.ListExperimentRunRequest `json:"request"`
} `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"`
Expand All @@ -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"`
}

0 comments on commit 0015c81

Please sign in to comment.