Skip to content

Commit d85ba62

Browse files
committed
Moved check to common library
1 parent 46880bd commit d85ba62

3 files changed

Lines changed: 174 additions & 66 deletions

File tree

api/pkg/api/handler/instance.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,14 +1036,9 @@ func (cih CreateInstanceHandler) Handle(c echo.Context) error {
10361036

10371037
// Verify here if Instance Type and Machine capabilities match
10381038
if instanceTypeID != nil && machine != nil {
1039-
isMatch, _, apiErr := common.MatchInstanceTypeCapabilitiesForMachines(ctx, logger, cih.dbSession, *instanceTypeID, []string{machine.ID})
1040-
if apiErr != nil {
1039+
if apiErr := common.ResolveInstanceTypeMachineCapabilitiesMatch(ctx, logger, cih.dbSession, *instanceTypeID, machine.ID); apiErr != nil {
10411040
return cutil.NewAPIErrorResponse(c, apiErr.Code, apiErr.Message, apiErr.Data)
10421041
}
1043-
1044-
if !isMatch {
1045-
return cutil.NewAPIErrorResponse(c, http.StatusBadRequest, fmt.Sprintf("Capabilities for Machine: %v do not match Instance Type's Capabilities", machine.ID), nil)
1046-
}
10471042
}
10481043

10491044
mcDAO := cdbm.NewMachineCapabilityDAO(cih.dbSession)
@@ -2072,6 +2067,23 @@ func (uih UpdateInstanceHandler) buildInstanceUpdateRequestOsConfig(c echo.Conte
20722067
}, osID, nil
20732068
}
20742069

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+
20752087
// Handle godoc
20762088
// @Summary Update an existing Instance
20772089
// @Description Update an existing Instance for the org
@@ -2480,20 +2492,18 @@ func (uih UpdateInstanceHandler) Handle(c echo.Context) error {
24802492
}
24812493
}
24822494

2483-
mcDAO := cdbm.NewMachineCapabilityDAO(uih.dbSession)
2484-
2485-
// Verify here if Instance Type and Machine capabilities match
2486-
if instance.InstanceTypeID != nil && machine != nil {
2487-
isMatch, _, apiErr := common.MatchInstanceTypeCapabilitiesForMachines(ctx, logger, uih.dbSession, *instance.InstanceTypeID, []string{machine.ID})
2488-
if apiErr != nil {
2495+
// Verify Instance Type and Machine capabilities match when the request
2496+
// changes networking (after interface / VPC validation so more specific
2497+
// errors are returned first). Skip for metadata-only updates.
2498+
if instanceUpdateNeedsInstanceTypeMachineCapabilityValidation(&apiRequest) &&
2499+
instance.InstanceTypeID != nil && machine != nil {
2500+
if apiErr := common.ResolveInstanceTypeMachineCapabilitiesMatch(ctx, logger, uih.dbSession, *instance.InstanceTypeID, machine.ID); apiErr != nil {
24892501
return cutil.NewAPIErrorResponse(c, apiErr.Code, apiErr.Message, apiErr.Data)
24902502
}
2491-
2492-
if !isMatch {
2493-
return cutil.NewAPIErrorResponse(c, http.StatusBadRequest, fmt.Sprintf("Capabilities for Machine: %v do not match Instance Type's Capabilities", machine.ID), nil)
2494-
}
24952503
}
24962504

2505+
mcDAO := cdbm.NewMachineCapabilityDAO(uih.dbSession)
2506+
24972507
// Validate DPU Interfaces if Instance Type has Network Capability with DPU device type
24982508
if isDeviceInfoPresent {
24992509
// Get Network Capabilities with DPU device type

0 commit comments

Comments
 (0)