MCPcopy Create free account
hub / github.com/cloudbase/garm / DeleteTemplate

Method DeleteTemplate

database/sql/templates.go:246–282  ·  view source on GitHub ↗
(ctx context.Context, id uint)

Source from the content-addressed store, hash-verified

244}
245
246func (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}

Callers

nothing calls this directly

Calls 5

sendNotifyMethod · 0.95
getTemplateMethod · 0.95
sqlToParamTemplateMethod · 0.95
IsAdminFunction · 0.92
DeleteMethod · 0.65

Tested by

no test coverage detected