Skip to content

Commit ab7ea84

Browse files
committed
controller: get virtualmachinespec
1 parent 2b8b6ce commit ab7ea84

7 files changed

Lines changed: 46 additions & 8 deletions

File tree

Development.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Modes and CLI behavior
9090
- Unsupported in standalone: `Application`, `Component`, and `VirtualMachineSpec`. These higher-level constructs require the controller and DB.
9191
- Supported in standalone: direct CloudStack resource kinds such as `VirtualMachine`, `Network`, `Volume`, `SSHKey`, `SecurityGroup`, `AffinityGroup`, and `UserData` (subject to SDK coverage).
9292

93-
- Default behavior (cluster mode): when `-s` is not specified the CLI assumes cluster mode and operations are intended to be persisted to the database and reconciled by the controller. Resources are managed via the database/controller.
93+
- Default behavior (controller mode): when `-s` is not specified the CLI assumes controller mode and operations are intended to be persisted to the database and reconciled by the controller. Resources are managed via the database/controller.
9494

9595
Examples:
9696

@@ -108,7 +108,7 @@ Examples:
108108
CLI flags: `--all / -A`
109109
------------------------
110110

111-
Two commands support an `--all` (short `-A`) flag in cluster mode to query CloudStack directly rather than the controller DB:
111+
Two commands support an `--all` (short `-A`) flag in controller mode to query CloudStack directly rather than the controller DB:
112112

113113
- `get VirtualMachine -A` — list all VMs from CloudStack (including unmanaged VMs); without `-A` the controller returns only VMs persisted in the DB (managed by cloudstackctl).
114114
- `describe <Kind> <name> -A` — describe the named resource by querying CloudStack directly rather than using controller-managed state.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ spec:
203203
Usage notes:
204204
205205
- To run the CLI in standalone mode (no DB/controller): `./cloudstackctl -s get VirtualMachine` or `./cloudstackctl -s apply -f vm.yaml`.
206-
- To run in cluster mode (default), ensure the controller and Postgres are running; `apply` will send resources to the controller which persists them to the DB and reconciles via CloudStack.
206+
- To run in controller mode (default), ensure the controller and Postgres are running; `apply` will send resources to the controller which persists them to the DB and reconciles via CloudStack.
207207

208208
---
209209

cmd/cli/apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var applyCmd = &cobra.Command{
5555
return
5656
}
5757

58-
// cluster mode: apply by POSTing to controller HTTP API
58+
// controller mode: apply by POSTing to controller HTTP API
5959
body, err := ControllerRequest("POST", "/apply", jsonData)
6060
if err != nil {
6161
log.Fatalf("Failed to POST to controller: %v", err)

cmd/cli/describe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ func init() {
8484
var describeAll bool
8585

8686
func init() {
87-
describeCmd.Flags().BoolVarP(&describeAll, "all", "A", false, "Describe a VM from CloudStack (cluster mode only)")
87+
describeCmd.Flags().BoolVarP(&describeAll, "all", "A", false, "Describe a VM from CloudStack (controller mode only)")
8888
}

cmd/cli/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func init() {
8383
var getAll bool
8484

8585
func init() {
86-
getCmd.Flags().BoolVarP(&getAll, "all", "A", false, "Show all VMs from CloudStack (include unmanaged) — cluster mode only")
86+
getCmd.Flags().BoolVarP(&getAll, "all", "A", false, "Show all VMs from CloudStack (include unmanaged) — controller mode only")
8787
}
8888

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

controller/cmd_delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func DeleteVM(name string) {
107107
log.Printf("VM %s deleted successfully", name)
108108
}
109109

110-
// DeleteNetwork deletes a Network resource (handles standalone and cluster modes)
110+
// DeleteNetwork deletes a Network resource (handles standalone and controller modes)
111111
func DeleteNetwork(name string) {
112112
if db.DB == nil {
113113
// standalone or DB not initialized: try CloudStack directly

controller/controller.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (c *Controller) Start() {
5353
w.Write([]byte("ok"))
5454
})
5555

56-
// Accept resource apply requests from the CLI in cluster mode
56+
// Accept resource apply requests from the CLI in controller mode
5757
http.HandleFunc("/apply", func(w http.ResponseWriter, r *http.Request) {
5858
if r.Method != http.MethodPost {
5959
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
@@ -256,6 +256,33 @@ func (c *Controller) Start() {
256256
w.WriteHeader(http.StatusOK)
257257
w.Write(b)
258258
return
259+
case "VirtualMachineSpec":
260+
var specs []v1.VirtualMachineSpecResource
261+
if db.DB == nil {
262+
http.Error(w, "database unavailable", http.StatusServiceUnavailable)
263+
return
264+
}
265+
if name != "" {
266+
var vs v1.VirtualMachineSpecResource
267+
if err := db.DB.Where("metadata_name = ?", name).First(&vs).Error; err != nil {
268+
http.Error(w, "VirtualMachineSpec not found", http.StatusNotFound)
269+
return
270+
}
271+
b, _ := json.Marshal(vs)
272+
w.Header().Set("Content-Type", "application/json")
273+
w.WriteHeader(http.StatusOK)
274+
w.Write(b)
275+
return
276+
}
277+
if err := db.DB.Find(&specs).Error; err != nil {
278+
http.Error(w, "failed to list VirtualMachineSpec", http.StatusInternalServerError)
279+
return
280+
}
281+
b, _ := json.Marshal(specs)
282+
w.Header().Set("Content-Type", "application/json")
283+
w.WriteHeader(http.StatusOK)
284+
w.Write(b)
285+
return
259286
case "Component":
260287
var comps []v1.Component
261288
if db.DB == nil {
@@ -380,6 +407,17 @@ func (c *Controller) Start() {
380407
w.WriteHeader(http.StatusOK)
381408
w.Write(b)
382409
return
410+
case "VirtualMachineSpec":
411+
var vs v1.VirtualMachineSpecResource
412+
if db.DB == nil || db.DB.Where("metadata_name = ?", name).First(&vs).Error != nil {
413+
http.Error(w, "VirtualMachineSpec not found", http.StatusNotFound)
414+
return
415+
}
416+
b, _ := json.Marshal(vs)
417+
w.Header().Set("Content-Type", "application/json")
418+
w.WriteHeader(http.StatusOK)
419+
w.Write(b)
420+
return
383421
case "VirtualMachine":
384422
// If client asked for all, delegate to handlers which query CloudStack
385423
if r.URL.Query().Get("all") == "true" {

0 commit comments

Comments
 (0)