Skip to content

Commit

Permalink
Deletes multiple Element Instances, given a list of IDs or --all; acc…
Browse files Browse the repository at this point in the history
…omodates for instances being used elsewhere, fixes #55 (#56)

Deletes multiple Element Instances, given a list of IDs or --all; accomodates for instances being used elsewhere, fixes #55
  • Loading branch information
ghchinoy authored Apr 20, 2018
1 parent 6b56d0b commit b7b60f6
Showing 1 changed file with 79 additions and 29 deletions.
108 changes: 79 additions & 29 deletions cmd/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,29 @@ var instanceDocsCmd = &cobra.Command{
},
}

var allElementInstances bool

func getAllElementInstances(base, auth string) ([]int, error) {
var instanceIDlist []int
bodybytes, statuscode, _, err := ce.GetAllInstances(base, auth)
if err != nil {
if statuscode == -1 {
fmt.Println("Unable to reach CE API. Please check your configuration / profile.")
}
return instanceIDlist, err
}
var instances []ce.ElementInstance
err = json.Unmarshal(bodybytes, &instances)
if err != nil {
return instanceIDlist, err
}
for _, v := range instances {
instanceIDlist = append(instanceIDlist, v.ID)
}
return instanceIDlist, nil

}

var deleteElementInstanceCmd = &cobra.Command{
Use: "delete",
Short: "Delete an Element Instance",
Expand All @@ -320,38 +343,64 @@ var deleteElementInstanceCmd = &cobra.Command{
fmt.Println(err)
os.Exit(1)
}
// check for Instance ID & Operation name
if len(args) < 1 {
fmt.Println("Please provide an Instance ID ")
return
}
if _, err := strconv.ParseInt(args[0], 10, 64); err != nil {
fmt.Println("Please provide an Instance ID that is an integer")
return
}
bodybytes, statuscode, curlcmd, err := ce.DeleteElementInstance(profilemap["base"], profilemap["auth"], args[0])
if err != nil {
if statuscode == -1 {
fmt.Println("Unable to reach CE API. Please check your configuration / profile.")

var instanceList []int

if allElementInstances {
instanceList, err = getAllElementInstances(profilemap["base"], profilemap["auth"])
} else {
// check for Instance ID & Operation name
if len(args) < 1 {
fmt.Println("Please provide at least one Instance ID ")
return
}
// must all be ints
for _, v := range args {
//log.Printf("%v\n", v)
id, err := strconv.ParseInt(v, 10, 64)
if err != nil {
fmt.Println("Please provide an Instance ID that is an integer")
return
}
instanceList = append(instanceList, int(id))
}
fmt.Println(err)
os.Exit(1)
}
// handle global options, curl
if showCurl {
log.Println(curlcmd)
}
// handle non 200
if statuscode != 200 {
log.Printf("HTTP Error: %v\n", statuscode)
// handle this nicely, show error description
}
// handle global options, json
if outputJSON {
fmt.Printf("%s\n", bodybytes)
return

for _, id := range instanceList {
bodybytes, statuscode, curlcmd, err := ce.DeleteElementInstance(profilemap["base"], profilemap["auth"], strconv.Itoa(id))
if err != nil {
if statuscode == -1 {
fmt.Println("Unable to reach CE API. Please check your configuration / profile.")
}
fmt.Println(err)
os.Exit(1)
}
// handle global options, curl
if showCurl {
log.Println(curlcmd)
}
// handle non 200
if statuscode != 200 {
if statuscode == 409 {
message := make(map[string]interface{})
_ = json.Unmarshal(bodybytes, &message)
fmt.Printf("Cannot delete Element Instance %v: %s\n", id, message["message"])
} else if statuscode == 404 {
fmt.Printf("Cannot delete Element Instance %v: Not found\n", id)
} else {
log.Printf("HTTP Error: %v\n", statuscode)
}
// handle this nicely, show error description
}
// handle global options, json
if outputJSON {
fmt.Printf("%s\n", bodybytes)
return
}
if statuscode == 200 {
fmt.Printf("Deleted Element Instance %v\n", id)
}
}
fmt.Printf("Deleted Element Instance %s", args[0])
},
}

Expand Down Expand Up @@ -706,6 +755,7 @@ func init() {
instancesCmd.AddCommand(testInstancesCmd)
testInstancesCmd.PersistentFlags().BoolVarP(&removeBadInstances, "remove", "", false, "remove bad instances")
instancesCmd.AddCommand(deleteElementInstanceCmd)
deleteElementInstanceCmd.PersistentFlags().BoolVarP(&allElementInstances, "all", "", false, "delete all instances")
instancesCmd.AddCommand(instanceEnableCmd)
instancesCmd.AddCommand(instanceDisableCmd)
instancesCmd.AddCommand(instanceEventsEnableCmd)
Expand Down

0 comments on commit b7b60f6

Please sign in to comment.