@@ -53,24 +53,43 @@ type Spec struct {
53
53
OSDisk infrav1.OSDisk
54
54
CustomData string
55
55
VMType compute.VMType
56
+ HostType infrav1.HostType
56
57
}
57
58
58
59
// Get provides information about a virtual machine.
59
60
func (s * Service ) Get (ctx context.Context , spec interface {}) (interface {}, error ) {
61
+ var err error
60
62
vmSpec , ok := spec .(* Spec )
61
63
if ! ok {
62
- return compute. VirtualMachine {} , errors .New ("invalid vm specification" )
64
+ return nil , errors .New ("invalid vm specification" )
63
65
}
64
66
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 ])
72
81
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
+ }
74
93
}
75
94
76
95
// Reconcile gets/creates/updates a virtual machine.
@@ -173,36 +192,91 @@ func (s *Service) Reconcile(ctx context.Context, spec interface{}) error {
173
192
}
174
193
}
175
194
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
+ }
184
203
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
+ }
185
214
klog .V (2 ).Infof ("successfully created vm %s " , vmSpec .Name )
186
215
return err
187
216
}
188
217
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
+
189
254
// Delete deletes the virtual machine with the provided name.
190
255
func (s * Service ) Delete (ctx context.Context , spec interface {}) error {
191
256
vmSpec , ok := spec .(* Spec )
192
257
if ! ok {
193
258
return errors .New ("invalid vm Specification" )
194
259
}
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
+
197
271
if err != nil && azurestackhci .ResourceNotFound (err ) {
198
272
// already deleted
199
273
return nil
200
274
}
201
275
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 ())
203
277
}
204
278
205
- klog .V (2 ).Infof ("successfully deleted vm %s " , vmSpec .Name )
279
+ klog .V (2 ).Infof ("successfully deleted %s %s " , vmSpec . HostType , vmSpec .Name )
206
280
return err
207
281
}
208
282
0 commit comments