77 "log"
88)
99
10- // DeleteApplication deletes an Application and its dependent resources
10+ // DeleteApplication deletes an Application and disassociates its Components
11+ // (it does NOT delete VMs; VMs are owned by the Application lifecycle)
1112func DeleteApplication (name string ) {
1213 var app v1.Application
1314 if db .DB == nil {
@@ -20,7 +21,9 @@ func DeleteApplication(name string) {
2021 }
2122
2223 for _ , compRef := range app .Spec .Components {
23- DeleteComponent (compRef .Name )
24+ // Remove the component record but do NOT delete VMs here; VMs belong to the application lifecycle.
25+ db .DB .Where ("name = ?" , compRef .Name ).Delete (& v1.Component {})
26+ log .Printf ("Component %s disassociated from application %s" , compRef .Name , name )
2427 }
2528
2629 if err := db .DB .Delete (& app ).Error ; err != nil {
@@ -30,7 +33,8 @@ func DeleteApplication(name string) {
3033 log .Printf ("Application %s deleted successfully" , name )
3134}
3235
33- // DeleteComponent deletes a Component and its VMs
36+ // DeleteComponent deletes a Component record if it's not referenced by any
37+ // Application. It does NOT delete VMs (VMs belong to applications).
3438func DeleteComponent (name string ) {
3539 var comp v1.Component
3640 if db .DB == nil {
@@ -42,13 +46,16 @@ func DeleteComponent(name string) {
4246 log .Fatalf ("Component %s not found: %v" , name , err )
4347 }
4448
45- var vms []v1.VirtualMachine
46- if err := db .DB .Where ("metadata_labels @> ?" , map [string ]string {"component" : name }).Find (& vms ).Error ; err != nil {
47- log .Fatalf ("Failed to find VMs for component %s: %v" , name , err )
48- }
49-
50- for _ , vm := range vms {
51- DeleteVM (vm .Metadata .Name )
49+ // Do not delete VMs here. Prevent deletion if the component is still referenced by any Application.
50+ var apps []v1.Application
51+ if err := db .DB .Find (& apps ).Error ; err == nil {
52+ for _ , a := range apps {
53+ for _ , cref := range a .Spec .Components {
54+ if cref .Name == name {
55+ log .Fatalf ("Component %s is still referenced by Application %s; cannot delete" , name , a .Metadata .Name )
56+ }
57+ }
58+ }
5259 }
5360
5461 if err := db .DB .Delete (& comp ).Error ; err != nil {
0 commit comments