Skip to content

Commit

Permalink
Merge get/list env command (#206)
Browse files Browse the repository at this point in the history
* Merge get/list env command

Signed-off-by: Shivam Purohit <[email protected]>

* add environment details flag

Signed-off-by: Shivam Purohit <[email protected]>

* improve logic for list environment

Signed-off-by: Shivam Purohit <[email protected]>

---------

Signed-off-by: Shivam Purohit <[email protected]>
  • Loading branch information
shivam-Purohit authored Apr 15, 2024
1 parent 6b276da commit 47ba363
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 282 deletions.
44 changes: 43 additions & 1 deletion pkg/apis/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func CreateEnvironment(pid string, request models.CreateEnvironmentRequest, cred
}
}

func GetEnvironmentList(pid string, cred types.Credentials) (ListEnvironmentData, error) {
func ListChaosEnvironments(pid string, cred types.Credentials) (ListEnvironmentData, error) {
var err error
var gqlReq CreateEnvironmentListGQLRequest
gqlReq.Query = ListEnvironmentQuery
Expand Down Expand Up @@ -93,6 +93,48 @@ func GetEnvironmentList(pid string, cred types.Credentials) (ListEnvironmentData
return ListEnvironmentData{}, err
}
}
func GetChaosEnvironment(pid string, envid string, cred types.Credentials) (GetEnvironmentData, error) {
var err error
var gqlReq CreateEnvironmentGetGQLRequest
gqlReq.Query = GetEnvironmentQuery

gqlReq.Variables.ProjectID = pid
gqlReq.Variables.EnvironmentID = envid
query, err := json.Marshal(gqlReq)
if err != nil {
return GetEnvironmentData{}, err
}
resp, err := apis.SendRequest(
apis.SendRequestParams{
Endpoint: cred.ServerEndpoint + utils.GQLAPIPath,
Token: cred.Token,
},
query,
string(types.Post),
)
if err != nil {
return GetEnvironmentData{}, errors.New("Error in Getting Chaos Environment: " + err.Error())
}
bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return GetEnvironmentData{}, errors.New("Error in Getting Chaos Environment: " + err.Error())
}

if resp.StatusCode == http.StatusOK {
var getEnvironment GetEnvironmentData
err = json.Unmarshal(bodyBytes, &getEnvironment)
if err != nil {
return GetEnvironmentData{}, errors.New("Error in Getting Chaos Environment: " + err.Error())
}
if len(getEnvironment.Errors) > 0 {
return GetEnvironmentData{}, errors.New(getEnvironment.Errors[0].Message)
}
return getEnvironment, nil
} else {
return GetEnvironmentData{}, err
}
}

func DeleteEnvironment(pid string, envid string, cred types.Credentials) (DeleteChaosEnvironmentData, error) {
var err error
Expand Down
18 changes: 18 additions & 0 deletions pkg/apis/environment/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ const (
}
}
`
GetEnvironmentQuery = `query getEnvironment($projectID: ID!, $environmentID : ID!) {
getEnvironment(projectID: $projectID,environmentID: $environmentID){
environmentID
name
createdAt
updatedAt
createdBy{
username
}
updatedBy{
username
}
infraIDs
type
tags
}
}`

ListEnvironmentQuery = `query listEnvironments($projectID: ID!, $request: ListEnvironmentRequest) {
listEnvironments(projectID: $projectID,request: $request){
environments {
Expand Down
20 changes: 20 additions & 0 deletions pkg/apis/environment/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ type CreateEnvironmentData struct {
EnvironmentDetails model.Environment `json:"createEnvironment"`
}

type GetEnvironmentData struct {
Errors []struct {
Message string `json:"message"`
Path []string `json:"path"`
} `json:"errors"`
Data GetEnvironment `json:"data"`
}

type GetEnvironment struct {
EnvironmentDetails model.Environment `json:"getEnvironment"`
}

type CreateEnvironmentGetGQLRequest struct {
Query string `json:"query"`
Variables struct {
ProjectID string `json:"projectID"`
EnvironmentID string `json:"environmentID"`
}
}

type ListEnvironmentData struct {
Errors []struct {
Message string `json:"message"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connect/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ var infraCmd = &cobra.Command{
infra_ops.PrintExistingInfra(infraList)
os.Exit(1)
}
envIDs, err := environment.GetEnvironmentList(newInfra.ProjectId, credentials)
envIDs, err := environment.ListChaosEnvironments(newInfra.ProjectId, credentials)
utils.PrintError(err)

// Check if Environment exists
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/create/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var environmentCmd = &cobra.Command{
}
newEnvironment.Type = models.EnvironmentType(envType)

envs, err := environment.GetEnvironmentList(pid, credentials)
envs, err := environment.ListChaosEnvironments(pid, credentials)
if err != nil {
utils.PrintError(err)
}
Expand Down
16 changes: 6 additions & 10 deletions pkg/cmd/delete/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Note: The default location of the config file is $HOME/.litmusconfig, and can be
os.Exit(1)
}

environmentList, err := environment.GetEnvironmentList(projectID, credentials)
environmentGet, err := environment.GetChaosEnvironment(projectID, environmentID, credentials)
if err != nil {
if strings.Contains(err.Error(), "permission_denied") {
utils.Red.Println("❌ You don't have enough permissions to delete an environment.")
Expand All @@ -112,15 +112,11 @@ Note: The default location of the config file is $HOME/.litmusconfig, and can be
os.Exit(1)
}
}
environmentListData := environmentList.Data.ListEnvironmentDetails.Environments
for i := 0; i < len(environmentListData); i++ {
if environmentListData[i].EnvironmentID == environmentID {
if len(environmentListData[i].InfraIDs) > 0 {
utils.Red.Println("Chaos Infras present in the Chaos Environment" +
"delete the Chaos Infras first to delete the Environment")
os.Exit(1)
}
}
environmentGetData := environmentGet.Data.EnvironmentDetails
if len(environmentGetData.InfraIDs) > 0 {
utils.Red.Println("Chaos Infras present in the Chaos Environment" +
", delete the Chaos Infras first to delete the Environment")
os.Exit(1)
}

// confirm before deletion
Expand Down
114 changes: 0 additions & 114 deletions pkg/cmd/get/environment.go

This file was deleted.

Loading

0 comments on commit 47ba363

Please sign in to comment.