Skip to content

Commit

Permalink
added changes to add server endpoint (#160)
Browse files Browse the repository at this point in the history
Signed-off-by: Sarthak Jain <[email protected]>
  • Loading branch information
SarthakJain26 committed Nov 10, 2023
1 parent 49fc5b0 commit 9b468e1
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 71 deletions.
9 changes: 5 additions & 4 deletions pkg/apis/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package environment
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"

models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/types"
"github.com/litmuschaos/litmusctl/pkg/utils"
"io/ioutil"
"net/http"
)

// CreateEnvironment connects the Infra with the given details
Expand All @@ -19,7 +20,7 @@ func CreateEnvironment(pid string, request models.CreateEnvironmentRequest, cred
gqlReq.Variables.Request = request

query, err := json.Marshal(gqlReq)
resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, query, string(types.Post))
resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: cred.ServerEndpoint + utils.GQLAPIPath, Token: cred.Token}, query, string(types.Post))
if err != nil {
return CreateEnvironmentResponse{}, errors.New("Error in Creating Chaos Infrastructure: " + err.Error())
}
Expand Down Expand Up @@ -60,7 +61,7 @@ func GetEnvironmentList(pid string, cred types.Credentials) (ListEnvironmentData

resp, err := apis.SendRequest(
apis.SendRequestParams{
Endpoint: cred.Endpoint + utils.GQLAPIPath,
Endpoint: cred.ServerEndpoint + utils.GQLAPIPath,
Token: cred.Token,
},
query,
Expand Down
19 changes: 10 additions & 9 deletions pkg/apis/experiment/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package experiment
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/types"
"github.com/litmuschaos/litmusctl/pkg/utils"
"io/ioutil"
"net/http"
)

// CreateExperiment sends GraphQL API request for creating a Experiment
Expand All @@ -43,7 +44,7 @@ func CreateExperiment(pid string, requestData model.SaveChaosExperimentRequest,

resp, err := apis.SendRequest(
apis.SendRequestParams{
Endpoint: cred.Endpoint + utils.GQLAPIPath,
Endpoint: cred.ServerEndpoint + utils.GQLAPIPath,
Token: cred.Token,
},
query,
Expand Down Expand Up @@ -80,7 +81,7 @@ func CreateExperiment(pid string, requestData model.SaveChaosExperimentRequest,

// Query to Run the Chaos Experiment
runQuery := `{"query":"mutation{ \n runChaosExperiment(experimentID: \"` + requestData.ID + `\", projectID: \"` + pid + `\"){\n notifyID \n}}"}`
resp, err = apis.SendRequest(apis.SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, []byte(runQuery), string(types.Post))
resp, err = apis.SendRequest(apis.SendRequestParams{Endpoint: cred.ServerEndpoint + utils.GQLAPIPath, Token: cred.Token}, []byte(runQuery), string(types.Post))

if err != nil {
return RunExperimentResponse{}, errors.New("Error in Running Chaos Experiment: " + err.Error())
Expand Down Expand Up @@ -124,7 +125,7 @@ func SaveExperiment(pid string, requestData model.SaveChaosExperimentRequest, cr

resp, err := apis.SendRequest(
apis.SendRequestParams{
Endpoint: cred.Endpoint + utils.GQLAPIPath,
Endpoint: cred.ServerEndpoint + utils.GQLAPIPath,
Token: cred.Token,
},
query,
Expand Down Expand Up @@ -166,7 +167,7 @@ func RunExperiment(pid string, eid string, cred types.Credentials) (RunExperimen
var err error
runQuery := `{"query":"mutation{ \n runChaosExperiment(experimentID: \"` + eid + `\", projectID: \"` + pid + `\"){\n notifyID \n}}"}`

resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, []byte(runQuery), string(types.Post))
resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: cred.ServerEndpoint + utils.GQLAPIPath, Token: cred.Token}, []byte(runQuery), string(types.Post))

if err != nil {
return RunExperimentResponse{}, errors.New("Error in Running Chaos Experiment: " + err.Error())
Expand Down Expand Up @@ -211,7 +212,7 @@ func GetExperimentList(pid string, in model.ListExperimentRequest, cred types.Cr

resp, err := apis.SendRequest(
apis.SendRequestParams{
Endpoint: cred.Endpoint + utils.GQLAPIPath,
Endpoint: cred.ServerEndpoint + utils.GQLAPIPath,
Token: cred.Token,
},
query,
Expand Down Expand Up @@ -261,7 +262,7 @@ func GetExperimentRunsList(pid string, in model.ListExperimentRunRequest, cred t

resp, err := apis.SendRequest(
apis.SendRequestParams{
Endpoint: cred.Endpoint + utils.GQLAPIPath,
Endpoint: cred.ServerEndpoint + utils.GQLAPIPath,
Token: cred.Token,
},
query,
Expand Down Expand Up @@ -313,7 +314,7 @@ func DeleteChaosExperiment(projectID string, experimentID *string, cred types.Cr

resp, err := apis.SendRequest(
apis.SendRequestParams{
Endpoint: cred.Endpoint + utils.GQLAPIPath,
Endpoint: cred.ServerEndpoint + utils.GQLAPIPath,
Token: cred.Token,
},
query,
Expand Down
15 changes: 9 additions & 6 deletions pkg/apis/infrastructure/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package infrastructure
import (
"encoding/json"
"errors"
"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/types"
"fmt"
"io/ioutil"
"net/http"

"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/types"

models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmusctl/pkg/utils"
)
Expand All @@ -38,14 +40,15 @@ func GetInfraList(c types.Credentials, pid string, request models.ListInfraReque
if err != nil {
return InfraData{}, err
}
resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: c.Endpoint + utils.GQLAPIPath, Token: c.Token}, query, string(types.Post))
resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: c.ServerEndpoint + utils.GQLAPIPath, Token: c.Token}, query, string(types.Post))
if err != nil {
return InfraData{}, err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {

return InfraData{}, err
}

Expand All @@ -62,7 +65,7 @@ func GetInfraList(c types.Credentials, pid string, request models.ListInfraReque

return Infra, nil
} else {
return InfraData{}, err
return InfraData{}, fmt.Errorf("error getting detais from server")
}
}

Expand Down Expand Up @@ -94,7 +97,7 @@ func ConnectInfra(infra types.Infra, cred types.Credentials) (InfraConnectionDat
}

query, err := json.Marshal(gqlReq)
resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, query, string(types.Post))
resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: cred.ServerEndpoint + utils.GQLAPIPath, Token: cred.Token}, query, string(types.Post))
if err != nil {
return InfraConnectionData{}, errors.New("Error in registering Chaos Infrastructure: " + err.Error())
}
Expand Down Expand Up @@ -160,7 +163,7 @@ func DisconnectInfra(projectID string, infraID string, cred types.Credentials) (

resp, err := apis.SendRequest(
apis.SendRequestParams{
Endpoint: cred.Endpoint + utils.GQLAPIPath,
Endpoint: cred.ServerEndpoint + utils.GQLAPIPath,
Token: cred.Token,
},
query,
Expand Down
10 changes: 5 additions & 5 deletions pkg/apis/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func CreateProjectRequest(projectName string, cred types.Credentials) (createPro

type listProjectResponse struct {
Data []struct {
ID string `json:"ID"`
ID string `json:"projectID"`
Name string `json:"Name"`
CreatedAt int64 `json:"CreatedAt"`
} `json:"data"`
Expand Down Expand Up @@ -139,14 +139,14 @@ type Data struct {

type Member struct {
Role string `json:"Role"`
UserID string `json:"UserID"`
UserName string `json:"UserName"`
UserID string `json:"userID"`
UserName string `json:"username"`
}

type Project struct {
ID string `json:"ID"`
ID string `json:"projectID"`
Name string `json:"Name"`
CreatedAt string `json:"created_at"`
CreatedAt int64 `json:"createdAt"`
Members []Member `json:"Members"`
}

Expand Down
59 changes: 30 additions & 29 deletions pkg/cmd/config/setAccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ package config

import (
"fmt"
"github.com/litmuschaos/litmusctl/pkg/apis/experiment"
"net/url"
"os"
"strings"
"time"

"github.com/litmuschaos/litmusctl/pkg/apis/experiment"

"github.com/golang-jwt/jwt"
"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/config"
Expand Down Expand Up @@ -61,7 +62,7 @@ var setAccountCmd = &cobra.Command{
utils.White_B.Print("\nHost endpoint where litmus is installed: ")
fmt.Scanln(&authInput.Endpoint)

for authInput.Endpoint == "" {
if authInput.Endpoint == "" {
utils.Red.Println("\n⛔ Host URL can't be empty!!")
os.Exit(1)
}
Expand Down Expand Up @@ -121,8 +122,9 @@ var setAccountCmd = &cobra.Command{
users = append(users, user)

var account = types.Account{
Endpoint: authInput.Endpoint,
Users: users,
Endpoint: authInput.Endpoint,
Users: users,
ServerEndpoint: authInput.Endpoint,
}

// If config file doesn't exist or length of the file is zero.
Expand All @@ -142,7 +144,6 @@ var setAccountCmd = &cobra.Command{
err := config.CreateNewLitmusCtlConfig(configFilePath, litmuCtlConfig)
utils.PrintError(err)

os.Exit(0)
} else {
// checking syntax
err = config.ConfigSyntaxCheck(configFilePath)
Expand All @@ -152,41 +153,41 @@ var setAccountCmd = &cobra.Command{
Account: account,
CurrentAccount: authInput.Endpoint,
CurrentUser: claims["username"].(string),
ServerEndpoint: authInput.Endpoint,
}

err = config.UpdateLitmusCtlConfig(updateLitmusCtlConfig, configFilePath)
utils.PrintError(err)
}
utils.White_B.Printf("\naccount.username/%s configured", claims["username"].(string))
} else {
utils.Red.Println("\nError: some flags are missing. Run 'litmusctl config set-account --help' for usage. ")
}

serverResp, err := experiment.GetServerVersion(authInput.Endpoint)
var isCompatible bool
if err != nil {
utils.Red.Println("\nError: ", err)
} else {
compatibilityArr := utils.CompatibilityMatrix[os.Getenv("CLIVersion")]
for _, v := range compatibilityArr {
if v == serverResp.Data.GetServerVersion.Value {
isCompatible = true
break
}
serverResp, err := experiment.GetServerVersion(authInput.Endpoint)
var isCompatible bool
if err != nil {
utils.Red.Println("\nError: ", err)
} else {
compatibilityArr := utils.CompatibilityMatrix[os.Getenv("CLIVersion")]
for _, v := range compatibilityArr {
if v == serverResp.Data.GetServerVersion.Value {
isCompatible = true
break
}
}

if !isCompatible {
utils.Red.Println("\n🚫 ChaosCenter version: " + serverResp.Data.GetServerVersion.Value + " is not compatible with the installed LitmusCTL version: " + os.Getenv("CLIVersion"))
utils.White_B.Println("Compatible ChaosCenter versions are: ")
utils.White_B.Print("[ ")
for _, v := range compatibilityArr {
utils.White_B.Print("'" + v + "' ")
}
utils.White_B.Print("]\n")
} else {
utils.White_B.Println("\n✅ Installed versions of ChaosCenter and LitmusCTL are compatible! ")
if !isCompatible {
utils.Red.Println("\n🚫 ChaosCenter version: " + serverResp.Data.GetServerVersion.Value + " is not compatible with the installed LitmusCTL version: " + os.Getenv("CLIVersion"))
utils.White_B.Println("Compatible ChaosCenter versions are: ")
utils.White_B.Print("[ ")
for _, v := range compatibilityArr {
utils.White_B.Print("'" + v + "' ")
}
utils.White_B.Print("]\n")
} else {
utils.White_B.Println("\n✅ Installed versions of ChaosCenter and LitmusCTL are compatible! ")
}

} else {
utils.Red.Println("\nError: some flags are missing. Run 'litmusctl config set-account --help' for usage. ")
}
},
}
Expand Down
15 changes: 11 additions & 4 deletions pkg/cmd/create/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ package create

import (
"fmt"
"os"

models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/apis/environment"
"github.com/litmuschaos/litmusctl/pkg/infra_ops"
"github.com/litmuschaos/litmusctl/pkg/utils"
"github.com/spf13/cobra"
"os"
)

// environmentCmd represents the Chaos infra command
Expand Down Expand Up @@ -77,7 +78,9 @@ var environmentCmd = &cobra.Command{
newEnvironment.Description = &description

envType, err := cmd.Flags().GetString("type")
utils.PrintError(err)
if err != nil {
utils.PrintError(err)
}

// Handle blank input for project ID
if envType == "" {
Expand All @@ -92,7 +95,9 @@ var environmentCmd = &cobra.Command{
newEnvironment.Type = models.EnvironmentType(envType)

envs, err := environment.GetEnvironmentList(pid, credentials)
utils.PrintError(err)
if err != nil {
utils.PrintError(err)
}

// Generate EnvironmentID from Environment Name
envID := utils.GenerateNameID(newEnvironment.Name)
Expand All @@ -115,7 +120,9 @@ var environmentCmd = &cobra.Command{

// Perform authorization
userDetails, err := apis.GetProjectDetails(credentials)
utils.PrintError(err)
if err != nil {
utils.PrintError(err)
}
var editAccess = false
var project apis.Project
for _, p := range userDetails.Data.Projects {
Expand Down
7 changes: 4 additions & 3 deletions pkg/cmd/get/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ package get

import (
"fmt"
models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmusctl/pkg/apis/infrastructure"
"os"
"strings"
"text/tabwriter"

models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmusctl/pkg/apis/infrastructure"

"github.com/litmuschaos/litmusctl/pkg/utils"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -52,7 +53,7 @@ var InfraCmd = &cobra.Command{
infras, err := infrastructure.GetInfraList(credentials, projectID, models.ListInfraRequest{})
if err != nil {
if strings.Contains(err.Error(), "permission_denied") {
utils.Red.Println("❌ The specified Project ID doesn't exist.")
utils.Red.Println("❌ you don't have enough permissions to access this project")
os.Exit(1)
} else {
utils.PrintError(err)
Expand Down
1 change: 1 addition & 0 deletions pkg/config/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func UpdateLitmusCtlConfig(litmusconfig types.UpdateLitmusCtlConfig, filename st
for i, act := range obj.Accounts {
if act.Endpoint == litmusconfig.Account.Endpoint {
var innerflag = false
obj.Accounts[i].ServerEndpoint = litmusconfig.ServerEndpoint
for j, user := range act.Users {
if user.Username == litmusconfig.Account.Users[0].Username {
obj.Accounts[i].Users[j].Username = litmusconfig.Account.Users[0].Username
Expand Down
Loading

0 comments on commit 9b468e1

Please sign in to comment.