(ctx context.Context, name string)
| 110 | } |
| 111 | |
| 112 | func (s *sqlDatabase) GetTemplateByName(ctx context.Context, name string) (params.Template, error) { |
| 113 | userID, err := getUIDFromContext(ctx) |
| 114 | if err != nil { |
| 115 | return params.Template{}, fmt.Errorf("failed to get template: %w", err) |
| 116 | } |
| 117 | var templates []Template |
| 118 | q := s.conn.Model(&Template{}). |
| 119 | Where("name = ?", name). |
| 120 | Where("user_id = ? or user_id IS NULL", userID). |
| 121 | Preload("ScaleSets"). |
| 122 | Preload("Pools"). |
| 123 | Preload("User") |
| 124 | |
| 125 | q = q.Find(&templates) |
| 126 | if q.Error != nil { |
| 127 | if errors.Is(q.Error, gorm.ErrRecordNotFound) { |
| 128 | return params.Template{}, runnerErrors.ErrNotFound |
| 129 | } |
| 130 | return params.Template{}, fmt.Errorf("failed to get template: %w", q.Error) |
| 131 | } |
| 132 | |
| 133 | if len(templates) == 0 { |
| 134 | return params.Template{}, runnerErrors.ErrNotFound |
| 135 | } |
| 136 | if len(templates) > 1 { |
| 137 | return params.Template{}, runnerErrors.NewConflictError("multiple templates match the specified name %q. Please get template by ID.", name) |
| 138 | } |
| 139 | ret, err := s.sqlToParamTemplate(templates[0]) |
| 140 | if err != nil { |
| 141 | return params.Template{}, fmt.Errorf("failed to convert template: %w", err) |
| 142 | } |
| 143 | return ret, nil |
| 144 | } |
| 145 | |
| 146 | func (s *sqlDatabase) CreateTemplate(ctx context.Context, param params.CreateTemplateParams) (template params.Template, err error) { |
| 147 | if param.IsSystem && !auth.IsAdmin(ctx) { |
nothing calls this directly
no test coverage detected