@@ -22,46 +22,44 @@ package cloud
2222import (
2323 "context"
2424
25+ "github.com/apache/cloudstack-go/v2/cloudstack"
2526 "k8s.io/klog/v2"
2627)
2728
2829func (c * client ) GetVMByID (ctx context.Context , vmID string ) (* VM , error ) {
2930 logger := klog .FromContext (ctx )
30- p := c .VirtualMachine .NewListVirtualMachinesParams ()
31- p .SetId (vmID )
32- if c .projectID != "" {
33- p .SetProjectid (c .projectID )
34- }
3531 logger .V (2 ).Info ("CloudStack API call" , "command" , "ListVirtualMachines" , "params" , map [string ]string {
3632 "id" : vmID ,
3733 "projectID" : c .projectID ,
3834 })
39- l , err := c .VirtualMachine .ListVirtualMachines (p )
40- if err != nil {
41- return nil , err
42- }
43- if l .Count == 0 {
44- return nil , ErrNotFound
45- }
46- if l .Count > 1 {
47- return nil , ErrTooManyResults
48- }
49- vm := l .VirtualMachines [0 ]
50- logger .V (2 ).Info ("Returning VM" , "vmID" , vm .Id , "zoneID" , vm .Zoneid )
5135
52- return & VM {
53- ID : vm .Id ,
54- ZoneID : vm .Zoneid ,
55- }, nil
36+ return c .getVMByParam (ctx , func (p * cloudstack.ListVirtualMachinesParams ) {
37+ p .SetId (vmID )
38+ })
5639}
5740
5841func (c * client ) getVMByName (ctx context.Context , name string ) (* VM , error ) {
5942 logger := klog .FromContext (ctx )
60- p := c .VirtualMachine .NewListVirtualMachinesParams ()
61- p .SetName (name )
6243 logger .V (2 ).Info ("CloudStack API call" , "command" , "ListVirtualMachines" , "params" , map [string ]string {
63- "name" : name ,
44+ "name" : name ,
45+ "projectID" : c .projectID ,
46+ })
47+
48+ return c .getVMByParam (ctx , func (p * cloudstack.ListVirtualMachinesParams ) {
49+ p .SetName (name )
6450 })
51+ }
52+
53+ func (c * client ) getVMByParam (ctx context.Context , setParams func (p * cloudstack.ListVirtualMachinesParams )) (* VM , error ) {
54+ p := c .VirtualMachine .NewListVirtualMachinesParams ()
55+
56+ if c .projectID != "" {
57+ p .SetProjectid (c .projectID )
58+ }
59+
60+ // set params for virtual machine list
61+ setParams (p )
62+
6563 l , err := c .VirtualMachine .ListVirtualMachines (p )
6664 if err != nil {
6765 return nil , err
@@ -73,6 +71,7 @@ func (c *client) getVMByName(ctx context.Context, name string) (*VM, error) {
7371 return nil , ErrTooManyResults
7472 }
7573 vm := l .VirtualMachines [0 ]
74+ klog .FromContext (ctx ).V (2 ).Info ("Returning VM" , "vmID" , vm .Id , "zoneID" , vm .Zoneid )
7675
7776 return & VM {
7877 ID : vm .Id ,
0 commit comments