Skip to content

Commit 5596052

Browse files
committed
cmd/controller: support -a/--application
1 parent 7836176 commit 5596052

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

cmd/cli/get.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ var getCmd = &cobra.Command{
7474
if name != "" {
7575
q.Set("name", name)
7676
}
77+
if getApplication != "" && (resourceType == "Application" || resourceType == "Component" || resourceType == "VirtualMachine") {
78+
q.Set("application", getApplication)
79+
}
7780
path := endpoint + "?" + q.Encode()
7881
body, err := ControllerRequest("GET", path, nil)
7982
if err != nil {
@@ -134,9 +137,11 @@ func init() {
134137
}
135138

136139
var getAll bool
140+
var getApplication string
137141

138142
func init() {
139143
getCmd.Flags().BoolVarP(&getAll, "all", "A", false, "Show all VMs from CloudStack (include unmanaged) — controller mode only")
144+
getCmd.Flags().StringVarP(&getApplication, "application", "a", "", "Filter Application/Component/VirtualMachine results by application name")
140145
}
141146

142147
// tryDecodeAndPrint attempts to decode controller JSON into known typed

controller/controller.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,19 @@ func (c *Controller) handleList(w http.ResponseWriter, r *http.Request) {
465465
}
466466
kind := r.URL.Query().Get("kind")
467467
name := r.URL.Query().Get("name")
468+
appFilter := r.URL.Query().Get("application")
468469
switch kind {
469470
case "Application":
470471
var apps []v1.Application
471472
if db.DB == nil {
472473
http.Error(w, "database unavailable", http.StatusServiceUnavailable)
473474
return
474475
}
475-
if err := db.DB.Find(&apps).Error; err != nil {
476+
q := db.DB
477+
if appFilter != "" {
478+
q = q.Where("name = ?", appFilter)
479+
}
480+
if err := q.Find(&apps).Error; err != nil {
476481
http.Error(w, "failed to list applications", http.StatusInternalServerError)
477482
return
478483
}
@@ -487,7 +492,11 @@ func (c *Controller) handleList(w http.ResponseWriter, r *http.Request) {
487492
http.Error(w, "database unavailable", http.StatusServiceUnavailable)
488493
return
489494
}
490-
if err := db.DB.Find(&comps).Error; err != nil {
495+
q := db.DB
496+
if appFilter != "" {
497+
q = q.Where("application = ?", appFilter)
498+
}
499+
if err := q.Find(&comps).Error; err != nil {
491500
http.Error(w, "failed to list components", http.StatusInternalServerError)
492501
return
493502
}
@@ -520,7 +529,11 @@ func (c *Controller) handleList(w http.ResponseWriter, r *http.Request) {
520529
http.Error(w, "database unavailable", http.StatusServiceUnavailable)
521530
return
522531
}
523-
if err := db.DB.Find(&vms).Error; err != nil {
532+
q := db.DB
533+
if appFilter != "" {
534+
q = q.Where("application = ?", appFilter)
535+
}
536+
if err := q.Find(&vms).Error; err != nil {
524537
http.Error(w, "failed to list virtualmachines", http.StatusInternalServerError)
525538
return
526539
}

0 commit comments

Comments
 (0)