@@ -53,24 +53,43 @@ type Spec struct {
5353 OSDisk infrav1.OSDisk
5454 CustomData string
5555 VMType compute.VMType
56+ HostType infrav1.HostType
5657}
5758
5859// Get provides information about a virtual machine.
5960func (s * Service ) Get (ctx context.Context , spec interface {}) (interface {}, error ) {
61+ var err error
6062 vmSpec , ok := spec .(* Spec )
6163 if ! ok {
62- return compute. VirtualMachine {} , errors .New ("invalid vm specification" )
64+ return nil , errors .New ("invalid vm specification" )
6365 }
6466
65- vm , err := s .Client .Get (ctx , s .Scope .GetResourceGroup (), vmSpec .Name )
66- if err != nil {
67- return nil , err
68- }
69- if vm == nil || len (* vm ) == 0 {
70- return nil , errors .Wrapf (err , "vm %s not found" , vmSpec .Name )
71- }
67+ switch vmSpec .HostType {
68+ case infrav1 .HostTypeBareMetal :
69+ var baremetalmachine * []compute.BareMetalMachine
70+
71+ baremetalmachine , err = s .BareMetalClient .Get (ctx , s .Scope .GetResourceGroup (), vmSpec .Name )
72+ if err != nil {
73+ return nil , err
74+ }
75+
76+ if baremetalmachine == nil || len (* baremetalmachine ) == 0 {
77+ return nil , errors .Errorf ("bare-metal machine %s not found" , vmSpec .Name )
78+ }
79+
80+ return converters .BareMetalMachineConvertToCAPH ((* baremetalmachine )[0 ])
7281
73- return converters .SDKToVM ((* vm )[0 ])
82+ default :
83+ vm , err := s .VMClient .Get (ctx , s .Scope .GetResourceGroup (), vmSpec .Name )
84+ if err != nil {
85+ return nil , err
86+ }
87+ if vm == nil || len (* vm ) == 0 {
88+ return nil , errors .Errorf ("vm %s not found" , vmSpec .Name )
89+ }
90+
91+ return converters .VMConvertToCAPH ((* vm )[0 ])
92+ }
7493}
7594
7695// Reconcile gets/creates/updates a virtual machine.
@@ -173,36 +192,91 @@ func (s *Service) Reconcile(ctx context.Context, spec interface{}) error {
173192 }
174193 }
175194
176- _ , err = s . Client . CreateOrUpdate (
177- ctx ,
178- s . Scope . GetResourceGroup (),
179- vmSpec . Name ,
180- & virtualMachine )
181- if err != nil {
182- return errors .Wrapf (err , "cannot create vm " )
183- }
195+ switch vmSpec . HostType {
196+ case infrav1 . HostTypeBareMetal :
197+ _ , err := s . createOrUpdateBareMetal (
198+ ctx ,
199+ & virtualMachine )
200+ if err != nil {
201+ return errors .Wrapf (err , "cannot create bare-metal machine " )
202+ }
184203
204+ default :
205+ _ , err = s .VMClient .CreateOrUpdate (
206+ ctx ,
207+ s .Scope .GetResourceGroup (),
208+ vmSpec .Name ,
209+ & virtualMachine )
210+ if err != nil {
211+ return errors .Wrapf (err , "cannot create vm" )
212+ }
213+ }
185214 klog .V (2 ).Infof ("successfully created vm %s " , vmSpec .Name )
186215 return err
187216}
188217
218+ func (s * Service ) createOrUpdateBareMetal (ctx context.Context , virtualMachine * compute.VirtualMachine ) (* compute.BareMetalMachine , error ) {
219+ // Create a new baremetal machine
220+ bareMetalMachine := & compute.BareMetalMachine {
221+ Name : virtualMachine .Name ,
222+ }
223+
224+ bareMetalMachine .BareMetalMachineProperties = & compute.BareMetalMachineProperties {
225+ StorageProfile : & compute.BareMetalMachineStorageProfile {
226+ ImageReference : & compute.BareMetalMachineImageReference {
227+ ID : virtualMachine .VirtualMachineProperties .StorageProfile .ImageReference .ID ,
228+ Name : virtualMachine .VirtualMachineProperties .StorageProfile .ImageReference .Name ,
229+ },
230+ },
231+ OsProfile : & compute.BareMetalMachineOSProfile {
232+ ComputerName : virtualMachine .VirtualMachineProperties .OsProfile .ComputerName ,
233+ AdminUsername : virtualMachine .VirtualMachineProperties .OsProfile .AdminUsername ,
234+ AdminPassword : virtualMachine .VirtualMachineProperties .OsProfile .AdminPassword ,
235+ CustomData : virtualMachine .VirtualMachineProperties .OsProfile .CustomData ,
236+ LinuxConfiguration : virtualMachine .VirtualMachineProperties .OsProfile .LinuxConfiguration ,
237+ },
238+ SecurityProfile : virtualMachine .VirtualMachineProperties .SecurityProfile ,
239+ ProvisioningState : virtualMachine .VirtualMachineProperties .ProvisioningState ,
240+ Statuses : virtualMachine .VirtualMachineProperties .Statuses ,
241+ }
242+
243+ // Try to apply the update.
244+ _ , err := s .BareMetalClient .CreateOrUpdate (ctx , s .Scope .GetResourceGroup (), * bareMetalMachine .Name , bareMetalMachine )
245+
246+ if err != nil {
247+ return nil , errors .Wrap (err , "Failed to create baremetal machine." )
248+ }
249+
250+ klog .V (2 ).Infof ("Successfully created baremetal machine %s " , bareMetalMachine .Name )
251+ return bareMetalMachine , nil
252+ }
253+
189254// Delete deletes the virtual machine with the provided name.
190255func (s * Service ) Delete (ctx context.Context , spec interface {}) error {
191256 vmSpec , ok := spec .(* Spec )
192257 if ! ok {
193258 return errors .New ("invalid vm Specification" )
194259 }
195- klog .V (2 ).Infof ("deleting vm %s " , vmSpec .Name )
196- err := s .Client .Delete (ctx , s .Scope .GetResourceGroup (), vmSpec .Name )
260+
261+ var err error
262+ switch vmSpec .HostType {
263+ case infrav1 .HostTypeBareMetal :
264+ klog .V (2 ).Infof ("deleting bare-metal machine %s " , vmSpec .Name )
265+ err = s .BareMetalClient .Delete (ctx , s .Scope .GetResourceGroup (), vmSpec .Name )
266+ default :
267+ klog .V (2 ).Infof ("deleting vm %s " , vmSpec .Name )
268+ err = s .VMClient .Delete (ctx , s .Scope .GetResourceGroup (), vmSpec .Name )
269+ }
270+
197271 if err != nil && azurestackhci .ResourceNotFound (err ) {
198272 // already deleted
199273 return nil
200274 }
201275 if err != nil {
202- return errors .Wrapf (err , "failed to delete vm %s in resource group %s" , vmSpec .Name , s .Scope .GetResourceGroup ())
276+ return errors .Wrapf (err , "failed to delete %s %s in resource group %s" , vmSpec . HostType , vmSpec .Name , s .Scope .GetResourceGroup ())
203277 }
204278
205- klog .V (2 ).Infof ("successfully deleted vm %s " , vmSpec .Name )
279+ klog .V (2 ).Infof ("successfully deleted %s %s " , vmSpec . HostType , vmSpec .Name )
206280 return err
207281}
208282
0 commit comments