(ctx context.Context, entity params.ForgeEntity, scaleSetID uint, param params.UpdateScaleSetParams, callback func(old, newSet params.ScaleSet) error)
| 211 | } |
| 212 | |
| 213 | func (s *sqlDatabase) UpdateEntityScaleSet(ctx context.Context, entity params.ForgeEntity, scaleSetID uint, param params.UpdateScaleSetParams, callback func(old, newSet params.ScaleSet) error) (updatedScaleSet params.ScaleSet, err error) { |
| 214 | var rowsAffected int64 |
| 215 | defer func() { |
| 216 | if err == nil && rowsAffected > 0 { |
| 217 | s.sendNotify(common.ScaleSetEntityType, common.UpdateOperation, updatedScaleSet) |
| 218 | } |
| 219 | }() |
| 220 | err = s.conn.Transaction(func(tx *gorm.DB) error { |
| 221 | scaleSet, err := s.getEntityScaleSet(tx, entity.EntityType, entity.ID, scaleSetID, "Instances", "Tags") |
| 222 | if err != nil { |
| 223 | return fmt.Errorf("error fetching scale set: %w", err) |
| 224 | } |
| 225 | |
| 226 | old, err := s.sqlToCommonScaleSet(scaleSet) |
| 227 | if err != nil { |
| 228 | return fmt.Errorf("error converting scale set: %w", err) |
| 229 | } |
| 230 | |
| 231 | updatedScaleSet, rowsAffected, err = s.updateScaleSet(tx, scaleSet, param) |
| 232 | if err != nil { |
| 233 | return fmt.Errorf("error updating scale set: %w", err) |
| 234 | } |
| 235 | |
| 236 | if callback != nil { |
| 237 | if err := callback(old, updatedScaleSet); err != nil { |
| 238 | return fmt.Errorf("error executing update callback: %w", err) |
| 239 | } |
| 240 | } |
| 241 | return nil |
| 242 | }) |
| 243 | if err != nil { |
| 244 | return params.ScaleSet{}, err |
| 245 | } |
| 246 | |
| 247 | updatedScaleSet, err = s.GetScaleSetByID(ctx, scaleSetID) |
| 248 | if err != nil { |
| 249 | return params.ScaleSet{}, err |
| 250 | } |
| 251 | return updatedScaleSet, nil |
| 252 | } |
| 253 | |
| 254 | func (s *sqlDatabase) getEntityScaleSet(tx *gorm.DB, entityType params.ForgeEntityType, entityID string, scaleSetID uint, preload ...string) (ScaleSet, error) { |
| 255 | if entityID == "" { |
no test coverage detected