@@ -888,6 +888,13 @@ func (cih CreateInstanceHandler) Handle(c echo.Context) error {
888888 }
889889 }
890890
891+ // Verify that the Machine's capabilities match the Instance Type's capabilities
892+ if machine .InstanceTypeID != nil && machine .ID != "" {
893+ if apiErr := common .VerifyInstanceTypeMachineCapabilitiesMatch (ctx , logger , cih .dbSession , * machine .InstanceTypeID , machine .ID ); apiErr != nil {
894+ return cutil .NewAPIErrorResponse (c , apiErr .Code , apiErr .Message , apiErr .Data )
895+ }
896+ }
897+
891898 // Acquire a lock on the MachineID
892899 err = tx .TryAcquireAdvisoryLock (ctx , cdb .GetAdvisoryLockIDFromString (machine .ID ), nil )
893900 if err != nil {
@@ -1021,7 +1028,8 @@ func (cih CreateInstanceHandler) Handle(c echo.Context) error {
10211028 }
10221029
10231030 // Select unallocated Machine for the requested instance type
1024- machine , err = common .GetUnallocatedMachineForInstanceType (ctx , tx , cih .dbSession , instanceType )
1031+ var apiErr * cutil.APIError
1032+ machine , apiErr , err = common .GetUnallocatedMachineForInstanceType (ctx , tx , cih .dbSession , instanceType , logger )
10251033 if err != nil {
10261034 if err == common .ErrInstanceTypeMachineNotFound {
10271035 return cutil .NewAPIErrorResponse (c , http .StatusBadRequest ,
@@ -1030,16 +1038,14 @@ func (cih CreateInstanceHandler) Handle(c echo.Context) error {
10301038 logger .Error ().Err (err ).Msg ("error retrieving Machine from DB for Instance Type" )
10311039 return cutil .NewAPIErrorResponse (c , http .StatusInternalServerError , "Failed to retrieve available baremetal Machines for specified Instance Type" , nil )
10321040 }
1033- } // if apiRequest.InstanceTypeID != nil
10341041
1035- // NOTE: At this stage, we have a Machine ID whether it was provided in request or selected through Instance Type
1036-
1037- // Verify here if Instance Type and Machine capabilities match
1038- if instanceTypeID != nil && machine != nil {
1039- if apiErr := common .ResolveInstanceTypeMachineCapabilitiesMatch (ctx , logger , cih .dbSession , * instanceTypeID , machine .ID ); apiErr != nil {
1042+ // No machine found with matching capabilities
1043+ if apiErr != nil {
10401044 return cutil .NewAPIErrorResponse (c , apiErr .Code , apiErr .Message , apiErr .Data )
10411045 }
1042- }
1046+ } // if apiRequest.InstanceTypeID != nil
1047+
1048+ // NOTE: At this stage, we have a Machine ID whether it was provided in request or selected through Instance Type
10431049
10441050 mcDAO := cdbm .NewMachineCapabilityDAO (cih .dbSession )
10451051
@@ -2067,23 +2073,6 @@ func (uih UpdateInstanceHandler) buildInstanceUpdateRequestOsConfig(c echo.Conte
20672073 }, osID , nil
20682074}
20692075
2070- // instanceUpdateNeedsInstanceTypeMachineCapabilityValidation returns true when
2071- // the update touches interface or secondary-VPC networking so Instance Type vs
2072- // Machine capabilities must be validated. Pure metadata/OS/NSG/SSH updates
2073- // return false so existing instances without machine capability rows still work.
2074- func instanceUpdateNeedsInstanceTypeMachineCapabilityValidation (apiRequest * model.APIInstanceUpdateRequest ) bool {
2075- if apiRequest == nil {
2076- return false
2077- }
2078- if apiRequest .IsInterfaceUpdateRequest () {
2079- return true
2080- }
2081- if apiRequest .SecondaryVpcIDs != nil {
2082- return true
2083- }
2084- return false
2085- }
2086-
20872076// Handle godoc
20882077// @Summary Update an existing Instance
20892078// @Description Update an existing Instance for the org
@@ -2495,9 +2484,9 @@ func (uih UpdateInstanceHandler) Handle(c echo.Context) error {
24952484 // Verify Instance Type and Machine capabilities match when the request
24962485 // changes networking (after interface / VPC validation so more specific
24972486 // errors are returned first). Skip for metadata-only updates.
2498- if instanceUpdateNeedsInstanceTypeMachineCapabilityValidation ( & apiRequest ) &&
2487+ if apiRequest . NeedsCapabilityValidation ( ) &&
24992488 instance .InstanceTypeID != nil && machine != nil {
2500- if apiErr := common .ResolveInstanceTypeMachineCapabilitiesMatch (ctx , logger , uih .dbSession , * instance .InstanceTypeID , machine .ID ); apiErr != nil {
2489+ if apiErr := common .VerifyInstanceTypeMachineCapabilitiesMatch (ctx , logger , uih .dbSession , * instance .InstanceTypeID , machine .ID ); apiErr != nil {
25012490 return cutil .NewAPIErrorResponse (c , apiErr .Code , apiErr .Message , apiErr .Data )
25022491 }
25032492 }
0 commit comments