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

Method UpdateTemplate

database/sql/templates.go:190–244  ·  view source on GitHub ↗
(ctx context.Context, id uint, param params.UpdateTemplateParams)

Source from the content-addressed store, hash-verified

188}
189
190func (s *sqlDatabase) UpdateTemplate(ctx context.Context, id uint, param params.UpdateTemplateParams) (template params.Template, err error) {
191 var hasChange bool
192 defer func() {
193 if err == nil && hasChange {
194 s.sendNotify(common.TemplateEntityType, common.UpdateOperation, template)
195 }
196 }()
197 var tpl Template
198 err = s.conn.Transaction(func(tx *gorm.DB) error {
199 tpl, err = s.getTemplate(ctx, tx, id)
200 if err != nil {
201 return fmt.Errorf("failed to get template: %w", err)
202 }
203 if !auth.IsAdmin(ctx) {
204 if tpl.UserID == nil {
205 return runnerErrors.NewBadRequestError("cannot edit system templates")
206 }
207 }
208 if param.Description != nil {
209 hasChange = true
210 tpl.Description = *param.Description
211 }
212
213 if param.Name != nil {
214 hasChange = true
215 tpl.Name = *param.Name
216 }
217 if len(param.Data) > 0 {
218 hasChange = true
219 data, err := s.marshalAndSeal(param.Data)
220 if err != nil {
221 return fmt.Errorf("failed to seal data: %w", err)
222 }
223 tpl.Data = data
224 }
225
226 if !hasChange {
227 return nil
228 }
229
230 if q := tx.Save(&tpl); q.Error != nil {
231 return fmt.Errorf("failed to save template: %w", q.Error)
232 }
233
234 template, err = s.sqlToParamTemplate(tpl)
235 if err != nil {
236 return fmt.Errorf("failed to convert template: %w", err)
237 }
238 return nil
239 })
240 if err != nil {
241 return params.Template{}, fmt.Errorf("failed to update template: %w", err)
242 }
243 return s.GetTemplate(ctx, tpl.ID)
244}
245
246func (s *sqlDatabase) DeleteTemplate(ctx context.Context, id uint) (err error) {
247 var template params.Template

Callers

nothing calls this directly

Calls 6

sendNotifyMethod · 0.95
getTemplateMethod · 0.95
marshalAndSealMethod · 0.95
sqlToParamTemplateMethod · 0.95
GetTemplateMethod · 0.95
IsAdminFunction · 0.92

Tested by

no test coverage detected