Skip to content

Commit

Permalink
refactor(agents): delete -> disconnect
Browse files Browse the repository at this point in the history
Signed-off-by: PrayagS <prayagsavsani@gmail.com>
  • Loading branch information
PrayagS committed May 23, 2022
1 parent 49c01c3 commit e090e79
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
28 changes: 14 additions & 14 deletions pkg/apis/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,30 +143,30 @@ func ConnectAgent(agent types.Agent, cred types.Credentials) (AgentConnectionDat
}
}

type DeleteAgentData struct {
type DisconnectAgentData struct {
Errors []struct {
Message string `json:"message"`
Path []string `json:"path"`
} `json:"errors"`
Data DeleteAgentDetails `json:"data"`
Data DisconnectAgentDetails `json:"data"`
}

type DeleteAgentDetails struct {
type DisconnectAgentDetails struct {
Message string `json:"deleteClusters"`
}

type DeleteAgentGraphQLRequest struct {
type DisconnectAgentGraphQLRequest struct {
Query string `json:"query"`
Variables struct {
ProjectID string `json:"projectID"`
ClusterIDs []*string `json:"clusterIDs"`
} `json:"variables"`
}

// DeleteAgent sends GraphQL API request for deleting ChaosAgent(s).
func DeleteAgent(projectID string, clusterIDs []*string, cred types.Credentials) (DeleteAgentData, error) {
// DisconnectAgent sends GraphQL API request for disconnecting ChaosAgent(s).
func DisconnectAgent(projectID string, clusterIDs []*string, cred types.Credentials) (DisconnectAgentData, error) {

var gqlReq DeleteAgentGraphQLRequest
var gqlReq DisconnectAgentGraphQLRequest
var err error

gqlReq.Query = `mutation deleteClusters($projectID: String!, $clusterIDs: [String]!) {
Expand All @@ -180,7 +180,7 @@ func DeleteAgent(projectID string, clusterIDs []*string, cred types.Credentials)

query, err := json.Marshal(gqlReq)
if err != nil {
return DeleteAgentData{}, err
return DisconnectAgentData{}, err
}

resp, err := SendRequest(
Expand All @@ -192,28 +192,28 @@ func DeleteAgent(projectID string, clusterIDs []*string, cred types.Credentials)
string(types.Post),
)
if err != nil {
return DeleteAgentData{}, err
return DisconnectAgentData{}, err
}

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

if resp.StatusCode == http.StatusOK {
var deleteAgentData DeleteAgentData
var deleteAgentData DisconnectAgentData
err = json.Unmarshal(bodyBytes, &deleteAgentData)
if err != nil {
return DeleteAgentData{}, err
return DisconnectAgentData{}, err
}

if len(deleteAgentData.Errors) > 0 {
return DeleteAgentData{}, errors.New(deleteAgentData.Errors[0].Message)
return DisconnectAgentData{}, errors.New(deleteAgentData.Errors[0].Message)
}

return deleteAgentData, nil
} else {
return DeleteAgentData{}, err
return DisconnectAgentData{}, err
}
}
20 changes: 10 additions & 10 deletions pkg/cmd/delete/agent.go → pkg/cmd/disconnect/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package delete
package disconnect

import (
"fmt"
Expand All @@ -29,10 +29,10 @@ import (
// agentCmd represents the agent command
var agentCmd = &cobra.Command{
Use: "agent",
Short: `Delete a ChaosAgent
Short: `Disconnect a ChaosAgent
Example:
#delete an agent
litmusctl delete agent c520650e-7cb6-474c-b0f0-4df07b2b025b --project-id=c520650e-7cb6-474c-b0f0-4df07b2b025b
#disconnect an agent
litmusctl disconnect agent c520650e-7cb6-474c-b0f0-4df07b2b025b --project-id=c520650e-7cb6-474c-b0f0-4df07b2b025b
Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,
Expand Down Expand Up @@ -93,22 +93,22 @@ var agentCmd = &cobra.Command{
// Make API call
var agentIDs []*string
agentIDs = append(agentIDs, &agentID)
deletedAgent, err := apis.DeleteAgent(projectID, agentIDs, credentials)
disconnectedAgent, err := apis.DisconnectAgent(projectID, agentIDs, credentials)
if err != nil {
utils.Red.Println("\n❌ Error in deleting ChaosAgent: ", err.Error())
utils.Red.Println("\n❌ Error in disconnecting ChaosAgent: ", err.Error())
os.Exit(1)
}

if strings.Contains(deletedAgent.Data.Message, "Successfully deleted clusters") {
utils.White_B.Println("\n🚀 ChaosAgent successfully deleted.")
if strings.Contains(disconnectedAgent.Data.Message, "Successfully deleted clusters") {
utils.White_B.Println("\n🚀 ChaosAgent successfully disconnected.")
} else {
utils.White_B.Println("\n❌ Failed to delete ChaosAgent. Please check if the ID is correct or not.")
utils.White_B.Println("\n❌ Failed to disconnect ChaosAgent. Please check if the ID is correct or not.")
}
},
}

func init() {
DeleteCmd.AddCommand(agentCmd)
DisconnectCmd.AddCommand(agentCmd)

agentCmd.Flags().String("project-id", "", "Set the project-id to create workflow for the particular project. To see the projects, apply litmusctl get projects")
}
14 changes: 7 additions & 7 deletions pkg/cmd/delete/delete.go → pkg/cmd/disconnect/disconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package delete
package disconnect

import (
"github.com/spf13/cobra"
)

// deleteCmd represents the delete command
var DeleteCmd = &cobra.Command{
Use: "delete",
Short: `Delete resources for LitmusChaos agent plane.
// disconnectCmd represents the disconnect command
var DisconnectCmd = &cobra.Command{
Use: "disconnect",
Short: `Disconnect resources for LitmusChaos agent plane.
Examples:
#delete an agent
litmusctl delete agent c520650e-7cb6-474c-b0f0-4df07b2b025b --project-id=c520650e-7cb6-474c-b0f0-4df07b2b025b
#disconnect an agent
litmusctl disconnect agent c520650e-7cb6-474c-b0f0-4df07b2b025b --project-id=c520650e-7cb6-474c-b0f0-4df07b2b025b
Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import (
"net/http"
"os"

"github.com/litmuschaos/litmusctl/pkg/cmd/disconnect"
"github.com/litmuschaos/litmusctl/pkg/cmd/upgrade"
"github.com/litmuschaos/litmusctl/pkg/cmd/version"
"github.com/litmuschaos/litmusctl/pkg/utils"

"github.com/litmuschaos/litmusctl/pkg/cmd/config"
"github.com/litmuschaos/litmusctl/pkg/cmd/create"
"github.com/litmuschaos/litmusctl/pkg/cmd/delete"
"github.com/litmuschaos/litmusctl/pkg/cmd/get"
config2 "github.com/litmuschaos/litmusctl/pkg/config"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -59,7 +59,7 @@ func init() {
rootCmd.AddCommand(config.ConfigCmd)
rootCmd.AddCommand(create.CreateCmd)
rootCmd.AddCommand(get.GetCmd)
rootCmd.AddCommand(delete.DeleteCmd)
rootCmd.AddCommand(disconnect.DisconnectCmd)
rootCmd.AddCommand(version.VersionCmd)
rootCmd.AddCommand(upgrade.UpgradeCmd)

Expand Down

0 comments on commit e090e79

Please sign in to comment.