-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathinfra.go
More file actions
121 lines (101 loc) · 3.57 KB
/
Copy pathinfra.go
File metadata and controls
121 lines (101 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Copyright © 2021 The LitmusChaos Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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 disconnect
import (
"fmt"
"os"
"strings"
"github.com/litmuschaos/litmusctl/pkg/apis/infrastructure"
"github.com/litmuschaos/litmusctl/pkg/completion"
"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/utils"
"github.com/spf13/cobra"
)
// infraCmd represents the infra command
var infraCmd = &cobra.Command{
Use: "chaos-infra",
Short: `Disconnect a Chaos Infrastructure
Example:
#disconnect a Chaos Infrastructure
litmusctl disconnect chaos-infra 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
`,
Run: func(cmd *cobra.Command, args []string) {
// Fetch user credentials
credentials, err := utils.GetCredentials(cmd)
utils.PrintError(err)
projectID, err := cmd.Flags().GetString("project-id")
utils.PrintError(err)
// Handle blank input for project ID
if projectID == "" {
utils.White_B.Print("\nEnter the Project ID: ")
fmt.Scanln(&projectID)
if projectID == "" {
utils.Red.Println("⛔ Project ID can't be empty!!")
os.Exit(1)
}
}
var infraID string
if len(args) == 0 {
utils.White_B.Print("\nEnter the Infra ID: ")
fmt.Scanln(&infraID)
} else {
infraID = args[0]
}
// Handle blank input for Infra ID
if infraID == "" {
utils.Red.Println("⛔ Chaos Infra ID can't be empty!!")
os.Exit(1)
}
// Perform authorization
userDetails, err := apis.GetProjectDetails(credentials)
utils.PrintError(err)
var editAccess = false
var project apis.Project
for _, p := range userDetails.Data.Projects {
if p.ID == projectID {
project = p
}
}
for _, member := range project.Members {
if (member.UserID == userDetails.Data.ID) && (member.Role == "Owner" || member.Role == "Editor") {
editAccess = true
}
}
if !editAccess {
utils.Red.Println("⛔ User doesn't have edit access to the project!!")
os.Exit(1)
}
// Make API call
disconnectedInfra, err := infrastructure.DisconnectInfra(projectID, infraID, credentials)
if err != nil {
if strings.Contains(err.Error(), "no documents in result") {
utils.Red.Println("❌ The specified Project ID or Chaos Infrastructure ID doesn't exist.")
os.Exit(1)
} else {
utils.Red.Println("\n❌ Error in disconnecting Chaos Infrastructure: ", err.Error())
os.Exit(1)
}
}
if strings.Contains(disconnectedInfra.Data.Message, "infra deleted successfully") {
utils.White_B.Println("\n🚀 Chaos Infrastructure successfully disconnected.")
} else {
utils.White_B.Println("\n❌ Failed to disconnect Chaos Infrastructure. Please check if the ID is correct or not.")
}
},
}
func init() {
DisconnectCmd.AddCommand(infraCmd)
infraCmd.Flags().String("project-id", "", "Set the project-id to disconnect Chaos Infrastructure for the particular project. To see the projects, apply litmusctl get projects")
infraCmd.RegisterFlagCompletionFunc("project-id", completion.ProjectIDFlagCompletion)
}