(ctx context.Context, id uint)
| 244 | } |
| 245 | |
| 246 | func (s *sqlDatabase) DeleteTemplate(ctx context.Context, id uint) (err error) { |
| 247 | var template params.Template |
| 248 | |
| 249 | defer func() { |
| 250 | if err == nil { |
| 251 | s.sendNotify(common.TemplateEntityType, common.DeleteOperation, template) |
| 252 | } |
| 253 | }() |
| 254 | err = s.conn.Transaction(func(tx *gorm.DB) error { |
| 255 | tpl, err := s.getTemplate(ctx, tx, id, "Pools", "ScaleSets") |
| 256 | if err != nil { |
| 257 | return fmt.Errorf("failed to get template: %w", err) |
| 258 | } |
| 259 | if !auth.IsAdmin(ctx) { |
| 260 | if tpl.UserID == nil { |
| 261 | return runnerErrors.NewBadRequestError("cannot delete system templates") |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | if len(tpl.Pools) > 0 || len(tpl.ScaleSets) > 0 { |
| 266 | return runnerErrors.NewBadRequestError("cannot delete template while in use by pools or scale sets") |
| 267 | } |
| 268 | template, err = s.sqlToParamTemplate(tpl) |
| 269 | if err != nil { |
| 270 | return fmt.Errorf("failed to convert template: %w", err) |
| 271 | } |
| 272 | |
| 273 | if q := tx.Unscoped().Delete(&tpl); q.Error != nil { |
| 274 | return fmt.Errorf("failed to delete template: %w", err) |
| 275 | } |
| 276 | return nil |
| 277 | }) |
| 278 | if err != nil { |
| 279 | return fmt.Errorf("failed to delete template: %w", err) |
| 280 | } |
| 281 | return nil |
| 282 | } |
nothing calls this directly
no test coverage detected